-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeploy.sh
More file actions
379 lines (339 loc) · 12.6 KB
/
deploy.sh
File metadata and controls
379 lines (339 loc) · 12.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
#!/bin/bash
# ─────────────────────────────────────────────
# Discord Bot – Deploy / Management Menu
# Supports: Docker mode | Python direct mode
# ─────────────────────────────────────────────
APP_NAME="discord-bot"
COMPOSE_FILE="docker-compose.yml"
PID_FILE="/tmp/${APP_NAME}.pid"
LOG_FILE="./bot.log"
PYTHON_CMD="python3"
MAIN_SCRIPT="main.py"
# ── Colours ────────────────────────────────────
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
CYAN='\033[0;36m'
BOLD='\033[1m'
NC='\033[0m' # No Colour
# ── Helpers ────────────────────────────────────
print_banner() {
clear
echo -e "${CYAN}${BOLD}"
echo " ██████╗ ██╗███████╗ ██████╗ ██████╗ ██████╗ ██████╗ ██████╗ ████████╗"
echo " ██╔══██╗██║██╔════╝██╔════╝██╔═══██╗██╔══██╗██╔══██╗██╔═══██╗╚══██╔══╝"
echo " ██║ ██║██║███████╗██║ ██║ ██║██████╔╝██║ ██║██║ ██║ ██║ "
echo " ██║ ██║██║╚════██║██║ ██║ ██║██╔══██╗██║ ██║██║ ██║ ██║ "
echo " ██████╔╝██║███████║╚██████╗╚██████╔╝██║ ██║██████╔╝╚██████╔╝ ██║ "
echo " ╚═════╝ ╚═╝╚══════╝ ╚═════╝ ╚═════╝ ╚═╝ ╚═╝╚═════╝ ╚═════╝ ╚═╝ "
echo -e "${NC}"
echo -e " ${BOLD}Discord Music Bot – Management Menu${NC}"
echo -e " Mode: ${YELLOW}${DEPLOY_MODE^^}${NC}"
echo " ─────────────────────────────────────────"
}
print_status() {
local status
status=$(get_status)
if [[ "$status" == "running" ]]; then
echo -e " Bot Status : ${GREEN}${BOLD}RUNNING${NC}"
else
echo -e " Bot Status : ${RED}${BOLD}STOPPED${NC}"
fi
echo " ─────────────────────────────────────────"
}
info() { echo -e " ${CYAN}[INFO]${NC} $*"; }
success() { echo -e " ${GREEN}[OK]${NC} $*"; }
warn() { echo -e " ${YELLOW}[WARN]${NC} $*"; }
error() { echo -e " ${RED}[ERROR]${NC} $*"; }
pause() {
echo ""
read -rp " Press [Enter] to return to menu..."
}
# ── Status detection ───────────────────────────
get_status() {
if [[ "$DEPLOY_MODE" == "docker" ]]; then
if docker ps --format '{{.Names}}' 2>/dev/null | grep -q "^${APP_NAME}$"; then
echo "running"
else
echo "stopped"
fi
else
if [[ -f "$PID_FILE" ]]; then
local pid
pid=$(cat "$PID_FILE")
if kill -0 "$pid" 2>/dev/null; then
echo "running"
else
rm -f "$PID_FILE"
echo "stopped"
fi
else
echo "stopped"
fi
fi
}
# ══════════════════════════════════════════════
# DOCKER FUNCTIONS
# ══════════════════════════════════════════════
docker_start() {
info "Building Docker image..."
if [[ -f "$COMPOSE_FILE" ]]; then
docker compose up -d --build
else
docker build -t "$APP_NAME" . && \
docker run -d \
--name "$APP_NAME" \
--restart unless-stopped \
--env-file .env \
"$APP_NAME"
fi
if [[ $? -eq 0 ]]; then
success "Bot started successfully (Docker)."
else
error "Failed to start bot."
fi
}
docker_stop() {
info "Stopping Docker container..."
if [[ -f "$COMPOSE_FILE" ]]; then
docker compose down
else
docker stop "$APP_NAME" && docker rm "$APP_NAME"
fi
if [[ $? -eq 0 ]]; then
success "Bot stopped."
else
error "Failed to stop bot."
fi
}
docker_restart() {
info "Restarting Docker container..."
if [[ -f "$COMPOSE_FILE" ]]; then
docker compose restart
else
docker restart "$APP_NAME"
fi
if [[ $? -eq 0 ]]; then
success "Bot restarted."
else
error "Failed to restart bot."
fi
}
docker_logs() {
info "Showing logs (Ctrl+C to exit)..."
echo ""
if [[ -f "$COMPOSE_FILE" ]]; then
docker compose logs -f --tail=50
else
docker logs -f --tail=50 "$APP_NAME"
fi
}
# ══════════════════════════════════════════════
# PYTHON DIRECT FUNCTIONS
# ══════════════════════════════════════════════
python_start() {
if [[ ! -f ".env" ]]; then
error ".env file not found. Create it from .env.example first."
return 1
fi
info "Activating virtual environment (if present)..."
[[ -f "venv/bin/activate" ]] && source venv/bin/activate
info "Installing / updating dependencies..."
pip install -q -r requirements.txt
info "Starting bot in background..."
nohup $PYTHON_CMD "$MAIN_SCRIPT" >> "$LOG_FILE" 2>&1 &
echo $! > "$PID_FILE"
success "Bot started (PID: $(cat $PID_FILE)). Logs → ${LOG_FILE}"
}
python_stop() {
if [[ ! -f "$PID_FILE" ]]; then
warn "No PID file found. Bot may not be running."
return 1
fi
local pid
pid=$(cat "$PID_FILE")
info "Stopping bot (PID: ${pid})..."
kill "$pid" 2>/dev/null && rm -f "$PID_FILE"
if [[ $? -eq 0 ]]; then
success "Bot stopped."
else
error "Could not stop process ${pid}."
fi
}
python_restart() {
python_stop
sleep 1
python_start
}
python_logs() {
if [[ ! -f "$LOG_FILE" ]]; then
warn "Log file not found: ${LOG_FILE}"
return 1
fi
info "Showing logs (Ctrl+C to exit)..."
echo ""
tail -f "$LOG_FILE"
}
# ══════════════════════════════════════════════
# MENU ACTIONS (dispatch by mode)
# ══════════════════════════════════════════════
action_start() {
local status
status=$(get_status)
if [[ "$status" == "running" ]]; then
warn "Bot is already running."
else
if [[ "$DEPLOY_MODE" == "docker" ]]; then docker_start; else python_start; fi
fi
pause
}
action_stop() {
local status
status=$(get_status)
if [[ "$status" == "stopped" ]]; then
warn "Bot is already stopped."
else
if [[ "$DEPLOY_MODE" == "docker" ]]; then docker_stop; else python_stop; fi
fi
pause
}
action_restart() {
if [[ "$DEPLOY_MODE" == "docker" ]]; then docker_restart; else python_restart; fi
pause
}
action_logs() {
if [[ "$DEPLOY_MODE" == "docker" ]]; then docker_logs; else python_logs; fi
pause
}
action_status() {
echo ""
local status
status=$(get_status)
if [[ "$status" == "running" ]]; then
success "Bot is ${GREEN}${BOLD}RUNNING${NC}."
if [[ "$DEPLOY_MODE" == "docker" ]]; then
echo ""
docker ps --filter "name=${APP_NAME}" --format " Container: {{.Names}}\n Image: {{.Image}}\n Uptime: {{.Status}}"
else
echo -e " PID: $(cat $PID_FILE 2>/dev/null || echo 'N/A')"
fi
else
warn "Bot is ${RED}${BOLD}STOPPED${NC}."
fi
pause
}
action_install_deps() {
info "Installing system dependencies (FFmpeg, Python3, pip)..."
sudo apt-get update -qq
sudo apt-get install -y ffmpeg python3 python3-pip python3-venv
if [[ ! -d "venv" ]]; then
info "Creating virtual environment..."
python3 -m venv venv
fi
source venv/bin/activate
info "Installing Python packages..."
pip install -q -r requirements.txt
success "All dependencies installed."
pause
}
action_switch_mode() {
echo ""
echo -e " Current mode: ${YELLOW}${DEPLOY_MODE^^}${NC}"
echo ""
echo " [1] Docker"
echo " [2] Python (direct)"
echo ""
read -rp " Select mode: " mode_choice
case "$mode_choice" in
1) DEPLOY_MODE="docker" && success "Switched to Docker mode." ;;
2) DEPLOY_MODE="python" && success "Switched to Python mode." ;;
*) warn "Invalid choice." ;;
esac
pause
}
# ══════════════════════════════════════════════
# MODE SELECTION (first run)
# ══════════════════════════════════════════════
select_mode() {
clear
echo -e "${CYAN}${BOLD}"
echo " ╔══════════════════════════════════════╗"
echo " ║ Discord Bot – Deploy Setup ║"
echo " ╚══════════════════════════════════════╝"
echo -e "${NC}"
echo " How do you want to run the bot?"
echo ""
echo " [1] Docker (recommended – isolated container)"
echo " [2] Python (direct – runs in current environment)"
echo ""
read -rp " Choose [1/2]: " choice
case "$choice" in
1)
if ! command -v docker &>/dev/null; then
error "Docker is not installed. Install it first: https://docs.docker.com/engine/install/ubuntu/"
exit 1
fi
DEPLOY_MODE="docker"
;;
2)
DEPLOY_MODE="python"
;;
*)
error "Invalid choice. Exiting."
exit 1
;;
esac
}
# ══════════════════════════════════════════════
# MAIN MENU LOOP
# ══════════════════════════════════════════════
main_menu() {
while true; do
print_banner
print_status
echo ""
echo -e " ${BOLD}[1]${NC} Start"
echo -e " ${BOLD}[2]${NC} Stop"
echo -e " ${BOLD}[3]${NC} Restart"
echo -e " ${BOLD}[4]${NC} View Logs"
echo -e " ${BOLD}[5]${NC} Status"
echo -e " ${BOLD}[6]${NC} Install Dependencies"
echo -e " ${BOLD}[7]${NC} Switch Mode (current: ${YELLOW}${DEPLOY_MODE^^}${NC})"
echo -e " ${BOLD}[0]${NC} Exit"
echo ""
read -rp " Choose an option: " opt
case "$opt" in
1) action_start ;;
2) action_stop ;;
3) action_restart ;;
4) action_logs ;;
5) action_status ;;
6) action_install_deps ;;
7) action_switch_mode ;;
0) echo -e "\n ${CYAN}Goodbye!${NC}\n"; exit 0 ;;
*) warn "Invalid option."; pause ;;
esac
done
}
# ══════════════════════════════════════════════
# ENTRY POINT
# ══════════════════════════════════════════════
# Allow non-interactive mode via CLI args: ./deploy.sh start|stop|restart
if [[ $# -gt 0 ]]; then
DEPLOY_MODE="${DEPLOY_MODE:-python}"
case "$1" in
start) action_start ;;
stop) action_stop ;;
restart) action_restart ;;
status) action_status ;;
logs) action_logs ;;
*)
echo "Usage: $0 [start|stop|restart|status|logs]"
echo " $0 (interactive menu)"
exit 1
;;
esac
exit 0
fi
select_mode
main_menu