-
Notifications
You must be signed in to change notification settings - Fork 31
/
appctl
executable file
·483 lines (439 loc) · 14.3 KB
/
appctl
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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
#!/bin/bash
# appctl is an utility script for managing your Misago deployment.
# To find out what options are available, run it without any arguments.
# Text styles
RED='\033[0;31m'
BOLD=$(tput bold)
NORMAL=$(tput sgr0)
# Define env path
# This is a path to an env file created by wizard
misago_env_path="./config/misago.env"
# Find docker executable path
docker=$(which docker)
# Utility functions used by action commands
error() {
echo -e "${RED}Error:${NORMAL} $1"
}
require_setup() {
if [ ! -e $misago_env_path ]; then
error "You need to setup your site using \"./appctl setup\" before you will be able to use this option."
echo
exit 1
fi
}
# Check if user has docker and docker compose
if [ -z $docker ]; then
error "You need to have Docker installed to use this tool."
echo
echo "Docker release for your system can be downloaded for free from this page:"
echo "https://www.docker.com/get-started"
echo
echo "If you are on Linux, you will also have to install Docker Compose after installing Docker:"
echo "https://docs.docker.com/compose/install/"
echo
exit 1
fi
if ! $docker compose version >/dev/null 2>&1; then
error "You need to have Docker Compose plugin installed to use this tool."
echo
echo "Guide for installing Docker Compose plugin on your system can be found on this page:"
echo "https://docs.docker.com/compose/install/"
echo
exit 1
fi
# Commands
intro() {
echo "Usage: ./appctl [arg] ..."
echo "Arguments grouped by type:"
echo
echo "Setup and upgrade:"
echo
echo " ${BOLD}setup${NORMAL} Setup new Misago site."
echo " ${BOLD}upgrade${NORMAL} Backup, rebuild, migrate and collect static."
echo
echo "Docker management:"
echo
echo " ${BOLD}start${NORMAL} Start all containers."
echo " ${BOLD}stop${NORMAL} Stop all containers."
echo " ${BOLD}restart${NORMAL} Stop and start all docker containers."
echo " ${BOLD}rebuild${NORMAL} Rebuild and restart Misago and celery container containers."
echo " ${BOLD}rebuildall${NORMAL} Pull, rebuild and restart all containers."
echo " ${BOLD}stats${NORMAL} See list and stats of running docker containers."
echo
echo "Change configuration:"
echo
echo " ${BOLD}forumindex${NORMAL} Switch forum index between threads and categories."
echo " ${BOLD}email${NORMAL} Change email setup."
echo " ${BOLD}hostname${NORMAL} Change hostname setup."
echo " ${BOLD}locale${NORMAL} Change locale setup."
echo " ${BOLD}timezone${NORMAL} Change timezone setup."
echo " ${BOLD}avatargallery${NORMAL} Load avatar gallery."
echo " ${BOLD}sentry${NORMAL} Enable or disable Sentry (sentry.io) for logging."
echo " ${BOLD}debug${NORMAL} Change debug mode."
echo " ${BOLD}secret${NORMAL} Reset secret key."
echo
echo " Note: you need to rebuild Misago container for changes made with those utils to take effect."
echo
echo "Backup:"
echo
echo " ${BOLD}backup${NORMAL} Backup and archive database and media."
echo " ${BOLD}restore BACKUP${NORMAL} Restore database and media from BACKUP archive."
echo
echo "Utilities:"
echo
echo " ${BOLD}collectstatic${NORMAL} Collect static assets."
echo " ${BOLD}manage.py${NORMAL} Runs \"python manage.py\" inside docker."
echo " ${BOLD}bash${NORMAL} Starts bash session inside running Misago container."
echo " ${BOLD}run${NORMAL} Runs \"docker compose run --rm misago\"."
echo " ${BOLD}psql${NORMAL} Runs psql connected to database."
echo " ${BOLD}flushredis${NORMAL} Flush the redis cache."
echo " ${BOLD}resetcron${NORMAL} Replace host's crontab with default one."
echo
}
# Handle invalid argument
invalid_argument() {
echo -e "Invalid argument: ${RED}$1${NORMAL}"
echo "Please run this script without any arguments to see the list of available arguments."
echo
exit 1
}
# Run new site setup
setup() {
# Test if env files already exist
for env_path in "${env_paths[@]}"; do
if [ -e $env_path ]; then
error "Setup appears to already been completed."
echo
exit 1
fi
done
# Run wizard to let user create env files
python3 wizard/setup.py
# Recheck if user completed setup
for env_path in "${env_paths[@]}"; do
if [ ! -e $env_path ]; then
echo "Setup canceled."
echo
exit 1
fi
done
read -p "Initialize default database? [Y/n]: " initialize_default_database
# Run docker build
docker compose build --no-cache --force-rm --pull
collectstatic
if [ "$initialize_default_database" != "n" ]; then
docker compose run --rm misago ./.run initialize_default_database
fi
start_containers
set_crontab
echo "Setup completed."
if [ "$initialize_default_database" != "n" ]; then
echo "If you have already pointed a domain at this server, you can now visit it to access your Misago site."
echo "Please note that it may take up to few minutes for HTTPS to activate."
else
echo "Database was not initialized. Restore from backup or load custom db dump to psql and use \"./appctl start\" to start Misago server."
fi
}
# Run collectstatic (uses misago-static volume) so site has loaded assets
collectstatic() {
docker compose run --rm misago python manage.py collectstatic --no-input
}
# Setup crontab to run `cron` script within Misago container using docker compose
set_crontab() {
current_path=$(pwd)
rm -f cron_tmp
touch cron_tmp
echo "30 1 * * * cd $current_path && $docker compose run --rm misago ./cron" >> cron_tmp
echo "0 1 * * * cd $current_path && $docker compose run --rm misago ./.run backup \"auto-\$(date +\%Y\%m\%d\%H\%M\%S)\"" >> cron_tmp
echo "0 1 * * * find $current_path/backups/ -name auto-* -type f -mtime +10 -delete" >> cron_tmp
echo "" >> cron_tmp
crontab cron_tmp
rm -f cron_tmp
}
# Run upgrade process
upgrade() {
require_setup
git_commit=$(git rev-parse HEAD)
git_commit=${git_commit:0:8}
echo "You are going to upgrade your Misago site to the new version."
echo "All running docker containers will be stopped for the duration of the upgrade."
echo "Your site's data will be backed up to the \"backups\" directory."
echo "New version of misago-docker will be pulled from github.com"
echo
echo "In case of any issues, run this command to return to the current version:"
echo
echo "git reset --hard $git_commit && docker compose build --no-cache --force-rm --pull"
echo
echo "Note: remember to also restore your backup in case you are rolling back!"
echo
read -p "Start upgrade process? [Y/n]: " start_upgrade
if [ "$start_upgrade" = "n" ]; then
echo "Upgrade canceled."
exit
fi
echo "Creating backup..."
create_new_backup "upgrade"
echo "Stopping containers for upgrade..."
docker compose stop
git pull
docker compose build --no-cache --force-rm --pull
collectstatic
docker compose run --rm misago python manage.py migrate
docker compose run --rm misago python manage.py invalidateversionedcaches
echo "Upgrade has been completed, restarting containers..."
start_containers
set_crontab
}
# Run second step of major misago docker update
complete_major_upgrade() {
require_setup
if [ ! -e ./backups/postgresql10.sql ]; then
error "Could not find 'postgresql10.sql' file created by misago-docker v0. Major update was not started or was started long time ago."
echo
exit 1
fi
echo "Downing running containers..."
docker compose stop
docker compose down --remove-orphans
echo "Rebuilding new containers..."
docker compose build --force-rm --no-cache --pull
echo "Importing data from Misago Docker v0..."
docker compose run --rm misago ./.run complete_v1_upgrade
echo "Restarting containers..."
docker compose stop
docker compose up --detach
echo "Resetting crontab contents..."
set_crontab
echo "Finished!"
}
# Start docker containers
start_containers() {
require_setup
docker compose up --detach
}
# Stop docker containers
stop_containers() {
require_setup
docker compose stop
}
# Restart docker containers
restart_containers() {
require_setup
docker compose stop
docker compose up --detach
}
# Rebuild misago containers
rebuild_misago_containers() {
require_setup
docker compose stop misago celery-worker
docker compose build --force-rm misago celery-worker
docker compose up --detach
}
# Rebuild all containers
rebuild_all_containers() {
require_setup
docker compose stop
docker compose build --force-rm --no-cache --pull
docker compose up --detach
}
# Show stats for running docker containers
show_stats() {
require_setup
docker stats
}
# Flush the redis cache
flush_redis() {
require_setup
echo "You are about to flush all values from the Redis cache."
echo "This will:"
echo " - Remove all cached information"
echo " - Close all user sessions (log all users out)"
echo
read -p "Flush Redis cache? [Y/n]: " should_flush_cache
if [ "$should_flush_cache" = "n" ]; then
echo "Cache flush canceled."
exit
fi
docker compose exec redis-6 redis-cli FLUSHALL
}
# Forum index configuration
change_forumindex() {
require_setup
python3 wizard/forumindex.py
}
# E-mail configuration
change_email() {
require_setup
python3 wizard/email.py
}
# Hostname configuration
change_hostname() {
require_setup
python3 wizard/hostname.py
}
# Locale configuration
change_locale() {
require_setup
python3 wizard/locale.py
}
# Timezone configuration
change_timezone() {
require_setup
python3 wizard/timezone.py
}
# Load avatar gallery
load_avatargallery() {
require_setup
docker compose run --rm misago python manage.py loadavatargallery
}
# Sentry configuration
change_sentry() {
require_setup
python3 wizard/sentry.py
}
# Debug configuration
change_debug() {
require_setup
python3 wizard/debug.py
}
# Reset secret key
reset_secret_key() {
require_setup
python3 wizard/secretkey.py
}
# Create new backup
create_new_backup() {
require_setup
backup_name="manual-$(date +%Y%m%d%H%M%S)"
$docker compose run --rm misago ./.run backup $backup_name
}
# Restore from backup
restore_from_backup() {
require_setup
if [[ ! $1 ]]; then
error "You need to specify backup archive to restore from."
fi
# Check if backup archive exists
backups_dir="./backups"
if [ ! -e "$backups_dir/$1" ]; then
error "Path backups/$1 doesn't exist."
fi
# Unarchive specified file to tmp directory
echo "Restoring from $1..."
tmp_dir="$backups_dir/tmp-$(date +%Y%m%d%H%M%S)"
mkdir $tmp_dir
tar -xzf "$backups_dir/$1" -C $tmp_dir
# Find media and database backups in unarchived directory
database_sql=false
media_dir=false
for backup_dir in "$tmp_dir/*"; do
if [ -d $backup_dir ]; then
database_sql="$(echo $backup_dir/database.sql)"
media_dir="$(echo $backup_dir/media)"
fi
done
if [ ! -e $database_sql ]; then
rm -rf $tmp_dir
error "Specified archive is missing database backup."
fi
if [ ! -d $media_dir ]; then
rm -rf $tmp_dir
error "Specified archive is missing media backup."
fi
# Restore from archive
docker compose run --rm misago ./.run psql < $database_sql
rm -rf ./misago/media/*
mv "$media_dir"/* ./misago/media/
# Cleanup...
rm -rf $tmp_dir
echo "Restore completed! Remember to run \"./appctl rebuild\" to rebuild and restart your site!"
echo
}
# Collect static files
run_collectstatic() {
require_setup
collectstatic
}
# Shortcut for starting bash session in running container
run_bash() {
docker compose exec misago bash
}
# Shortcut for docker compose run --rm misago python manage.py
run_managepy() {
docker compose run --rm misago python manage.py "${@:2}"
}
# Shortcut for docker compose run --rm misago...
docker_run() {
docker compose run --rm misago "${@:2}"
}
# Shortcut for psql
run_psql() {
require_setup
docker compose run --rm misago ./.run psql
}
# Command dispatcher
if [[ $1 ]]; then
if [[ $1 = "setup" ]]; then
setup
elif [[ $1 = "upgrade" ]]; then
upgrade
elif [[ $1 = "completemajorupgrade" ]]; then
complete_major_upgrade
elif [[ $1 = "up" ]]; then
start_containers
elif [[ $1 = "start" ]]; then
start_containers
elif [[ $1 = "stop" ]]; then
stop_containers
elif [[ $1 = "restart" ]]; then
restart_containers
elif [[ $1 = "rebuild" ]]; then
rebuild_misago_containers
elif [[ $1 = "rebuildall" ]]; then
rebuild_all_containers
elif [[ $1 = "stats" ]]; then
show_stats
elif [[ $1 = "flushredis" ]]; then
flush_redis
elif [[ $1 = "forumindex" ]]; then
change_forumindex
elif [[ $1 = "email" ]]; then
change_email
elif [[ $1 = "hostname" ]]; then
change_hostname
elif [[ $1 = "locale" ]]; then
change_locale
elif [[ $1 = "timezone" ]]; then
change_timezone
elif [[ $1 = "avatargallery" ]]; then
load_avatargallery
elif [[ $1 = "sentry" ]]; then
change_sentry
elif [[ $1 = "debug" ]]; then
change_debug
elif [[ $1 = "secret" ]]; then
reset_secret_key
elif [[ $1 = "backup" ]]; then
create_new_backup
elif [[ $1 = "restore" ]]; then
restore_from_backup $2
elif [[ $1 = "daily_backup" ]]; then
create_new_backup "auto"
elif [[ $1 = "collectstatic" ]]; then
run_collectstatic
elif [[ $1 = "manage.py" ]]; then
run_managepy $@
elif [[ $1 = "bash" ]]; then
run_bash
elif [[ $1 = "run" ]]; then
docker_run $@
elif [[ $1 = "psql" ]]; then
run_psql
elif [[ $1 = "resetcron" ]]; then
set_crontab
else
invalid_argument $1
fi
else
intro
fi