-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathctrl.sh
More file actions
executable file
·479 lines (403 loc) · 12.1 KB
/
ctrl.sh
File metadata and controls
executable file
·479 lines (403 loc) · 12.1 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
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
#!/usr/bin/env bash
set -e
set -o pipefail
mkdir -p data
function txt {
case "$1" in
bold) tput bold 2>/dev/null ;;
reset) tput sgr0 2>/dev/null ;;
red) tput setaf 1 2>/dev/null ;;
green) tput setaf 2 2>/dev/null ;;
yellow) tput setaf 3 2>/dev/null ;;
esac
}
function txt_bold {
txt bold
}
function txt_reset {
txt reset
}
function txt_red {
txt red
}
function txt_green {
txt green
}
function txt_yellow {
txt yellow
}
function info { echo "$(txt bold)$(txt green)[+] $*$(txt reset)"; }
function error { echo "$(txt bold)$(txt red)[!] $*$(txt reset)"; }
function warning { echo "$(txt bold)$(txt yellow)[!] $*$(txt reset)"; }
function execute_cmd {
info "* $*"
"$@"
}
function usage {
cat <<EOF
Usage:
$0 <command> [OPTIONS...]
Commands:
build
Build and pull all images, including the docker base image.
update
Update the repository and all submodules.
up [OPTIONS...]
Start all services.
Options:
--debug Enable debug mode (prints exceptions on web interface).
--debug-toolbar Enable the debug toolbar (never use in production).
--maintenance Only allow admin users to login.
--disable-telegram Disable error reporting via telegram.
--hot-reloading Enable hot reloading of the server (except .html, .js, .sh files).
down
Stop and delete all services and networks. Disconnects all users and orphans running instances.
stop
Stop all services without removing the associated containers.
restart
Restart all services (disconnects currently connected users).
restart-web
Restart only the web interface (users stay connected via SSH).
ps
List all running containers.
top
List all processes running inside the containers.
logs [-f]
Print the logs of all services.
-f Follow log output and print incoming messages.
flask-cmd <args>
Run a Flask CLI command, e.g.:
flask-cmd db init
flask-cmd db migrate
flask-cmd db upgrade
db-migrate
Run Flask database migration.
db-init
Initialize the Flask database.
db-upgrade
Upgrade the Flask database.
--help
Show this help message and exit.
EOF
}
function docker_subnet_warning {
txt_yellow
txt_bold
cat <<EOF
You should consider adding more subnets to Docker, thus
we are able to create enough instances of each exercise.
cat /etc/docker/daemon.json
{
"default-address-pools":[
{"base":"172.16.0.0/16","size":24},
{"base":"172.17.0.0/16","size":24},
{"base":"172.18.0.0/16","size":24},
{"base":"172.19.0.0/16","size":24},
{"base":"172.20.0.0/16","size":24}
]
}
EOF
txt_reset
echo ""
}
# Check if the environment has all features we expect and that the framework has been cloned properly.
function has_binary {
command -v $1 >/dev/null 2>&1
return $?
}
function has_python_module {
pip3 show "$1" >/dev/null 2>&1
return $?
}
if [[ $# -lt 1 ]]; then
error "Not enough arguments"
usage
exit 1
fi
submodules=(
"ssh-wrapper/openssh-portable/README.md"
"ref-docker-base/ref-utils/README.md"
"ref-linux/README"
"webapp/ref/static/ace-builds/README.md"
)
for m in "${submodules[@]}"; do
if [[ ! -f "$m" ]]; then
error "Failed to find all required submodules!"
error "Please run: git submodule update --init --recursive"
error "For further notice, consult the README.md."
exit 1
fi
done
# TODO: Check if custom kernel is used.
if ! has_binary "docker"; then
error "Please install docker!"
exit 1
fi
# Check if cgroup freezer is used.
container_id=$(docker run -dt --rm alpine:latest sh -c "sleep 60")
if ! docker pause "$container_id" > /dev/null ; then
error "It looks like your current kernel does not support the cgroup freezer."
error "The feature is required, please update your kernel!"
docker rm -f "$container_id" > /dev/null
exit 1
fi
docker rm -f "$container_id" > /dev/null
cgroup_version="$(docker system info | grep "Cgroup Version" | cut -d ':' -f 2 | tr -d ' ')"
if [[ "$cgroup_version" != 2 ]]; then
error "docker system info report that you are using an unsupported cgroup version ($cgroup_version)"
error "We require cgroup v2 which should be the default on more recent distributions."
error "In order to force the kernel to use v2, you may append systemd.unified_cgroup_hierarchy=1"
error "to GRUB_CMDLINE_LINUX in /etc/default/grub."
error "However, it is perferable to update your distribution since it likely missen additional features."
exit 1
fi
if has_binary docker-compose; then
DOCKER_COMPOSE="docker-compose"
elif docker compose version > /dev/null; then
DOCKER_COMPOSE="docker compose"
else
error "Please install Docker Compose!"
exit 1
fi
if ! has_binary "jq"; then
error "Please install jq (apt install -y jq)"
exit 1
fi
if ! has_binary "pip3"; then
error "Please install pip3 (apt install -y python3-pip)"
exit 1
fi
if ! has_python_module "jinja2"; then
error "Please install jinja2 (apt install -y python3-jinja2)"
exit 1
fi
#Check the .env files used to parametrize the docker-compose file.
ENV_SETTINGS_FILE="settings.env"
if [[ ! -f $ENV_SETTINGS_FILE ]]; then
error "Please copy template.env to $ENV_SETTINGS_FILE and adapt the values"
exit 1
fi
# shellcheck disable=SC1090
source "$ENV_SETTINGS_FILE"
if [[ -z "$DOCKER_GROUP_ID" ]]; then
error "Please set DOCKER_GROUP_ID in $ENV_SETTINGS_FILE to your docker group ID (getent group docker)"
exit 1
fi
if [[ "$(getent group docker | cut -d ':' -f 3)" != "$DOCKER_GROUP_ID" ]]; then
error "DOCKER_GROUP_ID in $ENV_SETTINGS_FILE does not match the local docker group ID."
error "Use the id command to get the correct group ID."
exit 1
fi
if [[ -z "$SSH_HOST_PORT" ]]; then
error "Please set SSH_HOST_PORT in $ENV_SETTINGS_FILE to the port the entry ssh server should be expose on the host"
exit 1
fi
if [[ -z "$HTTP_HOST_PORT" ]]; then
error "Please set HTTP_HOST_PORT in $ENV_SETTINGS_FILE to the port the entry ssh server should be expose on the host"
exit 1
fi
if [[ -z "$SECRET_KEY" ]]; then
error "Please set SECRET_KEY in $ENV_SETTINGS_FILE to a random string"
exit 1
fi
if [[ -z "$SSH_TO_WEB_KEY" ]]; then
error "Please set SSH_TO_WEB_KEY in $ENV_SETTINGS_FILE to a random string"
exit 1
fi
if [[ -z "$POSTGRES_PASSWORD" ]]; then
error "Please set POSTGRES_PASSWORD in $ENV_SETTINGS_FILE to a random string"
exit 1
fi
if [[ -z "$ADMIN_PASSWORD" ]]; then
error "Please set ADMIN_PASSWORD in $ENV_SETTINGS_FILE to a random string"
exit 1
fi
#Check whether we have enough docker subnets configured.
if [[ ! -f "/etc/docker/daemon.json" ]]; then
docker_subnet_warning
else
if ! grep -q "default-address-pools" /etc/docker/daemon.json; then
warning "It looks like that there are no default-address-pools defined in your /etc/docker/daemon.json."
warning "This may cause you to run out of addresses depending on the number of deployed instances."
docker_subnet_warning
else
#TODO: We are just counting the number of configured nets, not their size
net_cnt="$(cat /etc/docker/daemon.json | jq '."default-address-pools"' | grep base | wc -l)"
if [[ $net_cnt -lt 4 ]]; then
docker_subnet_warning
fi
fi
fi
# Generate docker-compose files and generate keys.
if ! ./prepare.py; then
error "Failed to run prepare.py"
exit 1
fi
function update {
(
# Check for uncommitted changes, ignoring untracked files
# -n tests if string length is non-zero
# git status --porcelain -uno returns modified files (excluding untracked)
if [[ -n "$(git status --porcelain -uno)" ]]; then
error "There are uncommitted changes in the main repository. Please commit or stash them first."
exit 1
fi
# Check submodules for uncommitted changes, suppressing "Entering..." messages
if [[ -n "$(git submodule foreach --quiet git status --porcelain -uno)" ]]; then
error "There are uncommitted changes in one or more submodules. Please commit or stash them first."
exit 1
fi
info "=> Updating repository"
git pull
info "=> Updating submodules"
git submodule update --recursive
)
}
function build {
#Build the base image for all exercises
(
info "=> Building docker base image"
cd 'ref-docker-base'
./build.sh "$@"
)
(
info "=> Building release container"
execute_cmd $DOCKER_COMPOSE -p ref --env-file $ENV_SETTINGS_FILE build "$@"
execute_cmd $DOCKER_COMPOSE -p ref --env-file $ENV_SETTINGS_FILE pull
)
}
function up {
export REAL_HOSTNAME="$(hostname)"
export DEBUG=false
export DISABLE_RESPONSE_CACHING=false
export MAINTENANCE_ENABLED=false
export DISABLE_TELEGRAM=false
export DEBUG_TOOLBAR=false
export HOT_RELOADING=false
while [[ $# -gt 0 ]]; do
case $1 in
'--debug')
export DEBUG=true
shift
;;
'--disable-response-caching')
export DISABLE_RESPONSE_CACHING=true
shift
;;
'--maintenance')
export MAINTENANCE_ENABLED=true
shift
;;
'--disable-telegram')
export DISABLE_TELEGRAM=true
shift
;;
'--debug-toolbar')
export DEBUG_TOOLBAR=true
shift
;;
'--hot-reloading')
export HOT_RELOADING=true
shift
;;
*)
#Pass unknown flags to docker-compose
break
;;
esac
done
execute_cmd $DOCKER_COMPOSE -p ref --env-file $ENV_SETTINGS_FILE up "$@"
}
function down {
execute_cmd $DOCKER_COMPOSE -p ref --env-file $ENV_SETTINGS_FILE down
}
function log {
execute_cmd $DOCKER_COMPOSE --env-file $ENV_SETTINGS_FILE -p ref logs "$@"
}
function stop {
execute_cmd $DOCKER_COMPOSE --env-file $ENV_SETTINGS_FILE -p ref stop
}
function restart {
execute_cmd $DOCKER_COMPOSE --env-file $ENV_SETTINGS_FILE -p ref restart "$@"
}
function ps {
execute_cmd $DOCKER_COMPOSE --env-file $ENV_SETTINGS_FILE -p ref ps "$@"
}
function top {
execute_cmd $DOCKER_COMPOSE --env-file $ENV_SETTINGS_FILE -p ref top "$@"
}
function db_migrate {
execute_cmd $DOCKER_COMPOSE --env-file $ENV_SETTINGS_FILE -p ref run --rm web bash -c "DB_MIGRATE=1 FLASK_APP=ref python3 -m flask db migrate"
}
function db_init {
execute_cmd $DOCKER_COMPOSE --env-file $ENV_SETTINGS_FILE -p ref run --rm web bash -c "DB_MIGRATE=1 FLASK_APP=ref python3 -m flask db init"
}
function db_upgrade {
execute_cmd $DOCKER_COMPOSE --env-file $ENV_SETTINGS_FILE -p ref run --rm web bash -c "DB_MIGRATE=1 FLASK_APP=ref python3 -m flask db upgrade"
}
function flask-cmd {
execute_cmd $DOCKER_COMPOSE --env-file $ENV_SETTINGS_FILE -p ref run --rm web bash -c "FLASK_APP=ref python3 -m flask $*"
}
function are_you_sure {
read -r -p "$(txt bold)$(txt green)Are you sure? [y/N] $(txt reset)" yes_no
[[ "$yes_no" =~ ^[Yy]$ ]]
}
cmd="$1"
shift
case "$cmd" in
build)
build "$@"
;;
update)
update "$@"
;;
up)
up "$@"
;;
down)
are_you_sure || exit 0
down "$@"
;;
logs)
log "$@"
;;
stop)
are_you_sure || exit 0
stop "$@"
;;
restart)
are_you_sure || exit 0
restart "$@"
;;
restart-web)
are_you_sure || exit 0
restart web "$@"
;;
ps)
ps "$@"
;;
top)
top "$@"
;;
flask-cmd)
flask-cmd "$@"
;;
db-migrate)
db_migrate "$@"
;;
db-init)
db_init "$@"
;;
db-upgrade)
db_upgrade "$@"
;;
--help)
usage
exit 0
;;
*)
error "$cmd is not a valid command"
usage
exit 1
;;
esac