forked from rafalp/misago_docker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathappctl
executable file
·411 lines (370 loc) · 11.6 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
#!/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 paths
# Those are paths to env files created by wizard
misago_env_path="./config/misago.env"
env_paths=(
$misago_env_path
"./config/postgres.env"
)
# Utility functions used by action commands
error() {
echo -e "${RED}Error:${NORMAL} $1"
}
require_setup() {
for env_path in "${env_paths[@]}"; do
if [ ! -e $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
done
}
# Check if user has docker and docker-compose
if ! command -v docker >/dev/null 2>&1; 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
elif ! command -v docker-compose >/dev/null 2>&1; then
error "You need to have Docker Compose installed to use this tool."
echo
echo "Guide for installing Docker Compose 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 container."
echo " ${BOLD}stats${NORMAL} see list and stats of running docker containers."
echo
echo "Change configuration:"
echo
echo " ${BOLD}facebook${NORMAL} change sign-in with Facebook setup."
echo " ${BOLD}github${NORMAL} change sign-in with GitHub setup."
echo " ${BOLD}twitter${NORMAL} change sign-in with twitter setup."
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}dailybackup${NORMAL} enable or disable daily backup."
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 "Shortcuts:"
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
}
# 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
start_containers
if [ "$initialize_default_database" != "n" ]; then
docker-compose run --rm misago ./.run initialize_default_database
fi
collectstatic
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)
docker_compose=$(which docker-compose)
echo "30 1 * * * cd $current_path && $docker_compose run --rm misago ./cron" | crontab -
}
# 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 "Stopping containers for upgrade..."
docker-compose stop
create_new_backup
git pull
docker-compose build --no-cache --force-rm --pull
docker-compose run --rm misago python manage.py migrate
collectstatic
echo "Upgrade has been completed, restarting containers..."
start_containers
}
# 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 container
rebuild_misago_container() {
require_setup
docker-compose stop misago
docker-compose build --force-rm misago
docker-compose up --detach misago
}
# Show stats for running docker containers
show_stats() {
require_setup
docker stats
}
# Sign-in with facebook configuration
change_facebook_signin() {
require_setup
python3 wizard/facebook.py
}
# Sign-in with github configuration
change_github_signin() {
require_setup
python3 wizard/github.py
}
# Sign-in with twitter configuration
change_twitter_signin() {
require_setup
python3 wizard/twitter.py
}
# 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
}
# Daily backup configuration
change_daily_backup() {
require_setup
python3 wizard/dailybackup.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
docker-compose run --rm misago ./.run backup "manual"
}
# Restore from backup
restore_from_backup() {
require_setup
docker-compose run --rm misago ./.run restore "$1"
}
# 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 = "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_container
elif [[ $1 = "stats" ]]; then
show_stats
elif [[ $1 = "facebook" ]]; then
change_facebook_signin
elif [[ $1 = "github" ]]; then
change_github_signin
elif [[ $1 = "twitter" ]]; then
change_twitter_signin
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 = "dailybackup" ]]; then
change_daily_backup
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 = "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
else
invalid_argument $1
fi
else
intro
fi