-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun-app
572 lines (481 loc) · 14.2 KB
/
run-app
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
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
#!/bin/sh -u
# -u: Treat unset variables as an error when performing parameter expansion.
# If expansion is attempted on an unset interactive, exits with a non-zero status.
#
# Manage the dns-black-hole app
#
# Author: Loan Lassalle <https://github.com/lassalleloan>
# .conf file based on .conf.example file and filled with your own values
if [ ! -f .conf ]; then
printf ".conf: No such file\n"
exit 1
fi
# Start Docker application
DOCKER_WAS_RUNNING=false
# Image tag and container name
IMAGE_TAG="dns-black-hole"
CONTAINER_NAME="$IMAGE_TAG"
# Working directory
WORKING_DIRECTORY="$(sed -En 's/^WORKING_DIRECTORY=(\/[^ #]+)(( |#)?.*)$/\1/p' .conf)"
WORKING_DIRECTORY="${WORKING_DIRECTORY%/}"
# Source and destination directories for the Docker container's volume
SRC_VOLUME_1="$WORKING_DIRECTORY/src/etc/"
DST_VOLUME_1="/dns-black-hole/etc/"
SRC_VOLUME_2="$WORKING_DIRECTORY/src/blacklist"
DST_VOLUME_2="/dns-black-hole/hosts-master/blacklist"
SRC_VOLUME_3="$WORKING_DIRECTORY/src/whitelist"
DST_VOLUME_3="/dns-black-hole/hosts-master/whitelist"
SRC_VOLUME_4="$WORKING_DIRECTORY/src/myhosts"
DST_VOLUME_4="/dns-black-hole/hosts-master/myhosts"
# Processing mode (default: detach)
INTERACTIVE=false
DETACH=false
# Enable all additional category-specific
SORTED_REFERENCE_EXTENSIONS_STR="$(sed -En 's/^EXTENSION(_[0-9]+)*=([^ #]+)(( |#).*)?$/\2/p' .conf | \
sort -u)"
SORTED_REFERENCE_EXTENSIONS=(${SORTED_REFERENCE_EXTENSIONS_STR})
ENVS=(${SORTED_REFERENCE_EXTENSIONS[@]})
# Bypass any prior checking
FORCE=false
# Remove all unused Docker data
PRUNE=false
# Remove all Docker data
WIPE=false
# Default verbosity level
VERBOSITY="$(sed -En 's/^VERBOSITY=([0-9]{1})(( |#).*)?$/\1/p' .conf)"
# Current and latest version of StevenBlack/hosts source files
CURRENT_VERSION="$(sed -En 's/^CURRENT_VERSION=([0-9]+([.][0-9]+)*)(( |#).*)?$/\1/p' .conf)"
LATEST_VERSION="$(sed -En 's/^LATEST_VERSION=([0-9]+([.][0-9]+)*)(( |#).*)?$/\1/p' .conf)"
# Display a banner
banner() {
printf " _ _ _ _ _ _ \n"
printf " __| |_ _ ______| |__| |__ _ __| |_____| |_ ___| |___ __ _ _ __ _ __ \n"
printf " / _\` | ' \(_-<___| '_ \ / _\` / _| / /___| ' \/ _ \ / -_) / _\` | '_ \ '_ \ \n"
printf " \__,_|_||_/__/ |_.__/_\__,_\__|_\_\ |_||_\___/_\___| \__,_| .__/ .__/\n"
printf " |_| |_| \n"
}
# Usage of the script
usage() {
printf "Usage: run-app [[--interactive | --detach [--force]] [--extension <extension_name> ...] | --prune | --wipe] [--verbosity (0 | 1 | 2)]\n"
printf "\n"
printf "Manage the dns-black-hole app\n"
printf "\n"
printf "Version: 1.0.0, build deadbeef\n"
printf "\n"
printf "Author:\n"
printf " Loan Lassalle - <https://github.com/lassalleloan>\n"
printf "\n"
printf "Options:\n"
printf " -i, --interactive Keep stdin open even if not attached and allocate a pseudo-tty\n"
printf " -d, --detach Leave the container running in the background (default processing mode)\n"
printf " -e, --extension Enable additional category-specific\n"
printf " -f, --force Force the application to run, bypass any prior checking\n"
printf " -p, --prune Remove all unused Docker data\n"
printf " -w, --wipe Remove all Docker data\n"
printf " -v, --verbosity (0 | 1 | 2) Level of verbosity: no ouput, step information (default), all information\n"
printf " -h, --help Help on how to use this script\n"
}
# Start Docker application
start_docker() {
local verbosity="$1"
if [ "$verbosity" -ne 0 ]; then
printf "Starting Docker application\n"
fi
if docker info >/dev/null 2>&1; then
DOCKER_WAS_RUNNING=true
else
open --background -a Docker
while ! docker system info > /dev/null 2>&1; do
sleep 30
done
fi
}
# Stop Docker application
stop_docker() {
local verbosity="$1"
if [ "$verbosity" -ne 0 ]; then
printf "Stopping Docker application\n"
fi
if [ "$DOCKER_WAS_RUNNING" = false ]; then
killall Docker > /dev/null 2>&1
fi
}
# Build Docker container
build_container() {
local image_tag="$1"
local container_name="$2"
local verbosity="$3"
if [ "$verbosity" -ne 0 ]; then
printf "Building %s Docker container\n" "$container_name"
fi
if [ "$verbosity" -eq 2 ]; then
docker build --force-rm --tag "$image_tag" .
else
docker build --force-rm --quiet --tag "$image_tag" . >/dev/null 2>&1
fi
}
# Start Docker container
start_container() {
local image_tag="$1"
local container_name="$2"
local envs=("${!3}")
local src_directory_1="$4"
local dst_directory_1="$5"
local src_directory_2="$6"
local dst_directory_2="$7"
local src_directory_3="$8"
local dst_directory_3="$9"
local src_directory_4="${10}"
local dst_directory_4="${11}"
local interactive="${12}"
local verbosity="${13}"
if [ "$verbosity" -ne 0 ]; then
printf "Starting %s Docker container\n" "$container_name"
fi
if [ "$interactive" = true ]; then
docker run \
--interactive \
--entrypoint "sh" \
${envs[@]} \
--name "$container_name" \
--rm \
--tty \
--mount type=bind,source="$src_directory_1",target="$dst_directory_1" \
--mount type=bind,source="$src_directory_2",target="$dst_directory_2",readonly \
--mount type=bind,source="$src_directory_3",target="$dst_directory_3",readonly \
--mount type=bind,source="$src_directory_4",target="$dst_directory_4",readonly \
"$image_tag"
else
if [ "$verbosity" -eq 2 ]; then
docker run \
${envs[@]} \
--name "$container_name" \
--rm \
--mount type=bind,source="$src_directory_1",target="$dst_directory_1" \
--mount type=bind,source="$src_directory_2",target="$dst_directory_2",readonly \
--mount type=bind,source="$src_directory_3",target="$dst_directory_3",readonly \
--mount type=bind,source="$src_directory_4",target="$dst_directory_4",readonly \
"$image_tag"
else
docker run \
${envs[@]} \
--name "$container_name" \
--rm \
--mount type=bind,source="$src_directory_1",target="$dst_directory_1" \
--mount type=bind,source="$src_directory_2",target="$dst_directory_2",readonly \
--mount type=bind,source="$src_directory_3",target="$dst_directory_3",readonly \
--mount type=bind,source="$src_directory_4",target="$dst_directory_4",readonly \
"$image_tag" >/dev/null 2>&1
fi
fi
}
# Stop Docker container
stop_container() {
local container_name="$1"
local verbosity="$2"
if [ "$verbosity" -ne 0 ]; then
printf "Stopping %s Docker container\n" "$container_name"
fi
if [ "$verbosity" -eq 2 ]; then
docker stop "$container_name"
else
docker stop "$container_name" >/dev/null 2>&1
fi
}
# Kill Docker container
kill_container() {
local container_name="$1"
local verbosity="$2"
if [ "$verbosity" -ne 0 ]; then
printf "Killing %s Docker container\n" "$container_name"
fi
if [ "$verbosity" -eq 2 ]; then
docker kill "$container_name"
else
docker kill "$container_name" >/dev/null 2>&1
fi
}
# Remove Docker container
remove_container() {
local container_name="$1"
local verbosity="$2"
if [ "$verbosity" -ne 0 ]; then
printf "Removing %s Docker container\n" "$container_name"
fi
if [ "$verbosity" -eq 2 ]; then
docker rm --volumes "$container_name"
else
docker rm --force --volumes "$container_name" >/dev/null 2>&1
fi
}
# Remove Docker volume
remove_volume() {
local volume_name="$1"
local verbosity="$2"
if [ "$verbosity" -ne 0 ]; then
printf "Removing %s Docker volume\n" "$volume_name"
fi
if [ "$verbosity" -eq 2 ]; then
docker volume rm "$volume_name"
else
docker volume rm --force "$volume_name" >/dev/null 2>&1
fi
}
# Remove unused Docker volumes
remove_volumes_unused() {
local verbosity="$1"
if [ "$verbosity" -ne 0 ]; then
printf "Removing unused Docker volumes\n"
fi
if [ "$verbosity" -eq 2 ]; then
docker volume prune
else
docker volume prune --force >/dev/null 2>&1
fi
}
# Remove Docker image
remove_image() {
local image_name="$1"
local verbosity="$2"
if [ "$verbosity" -ne 0 ]; then
printf "Removing %s Docker image\n" "$image_name"
fi
if [ "$verbosity" -eq 2 ]; then
docker image rm "$image_name"
else
docker image rm --force "$image_name" >/dev/null 2>&1
fi
}
# Remove unused Docker images
remove_images_unused() {
local verbosity="$1"
if [ "$verbosity" -ne 0 ]; then
printf "Removing unused Docker images\n"
fi
if [ "$verbosity" -eq 2 ]; then
docker image prune --all
else
docker image prune --all --force >/dev/null 2>&1
fi
}
# Remove all unused Docker data
remove_all_unused() {
local verbosity="$1"
if [ "$verbosity" -ne 0 ]; then
printf "Removing all unused Docker data\n"
fi
if [ "$verbosity" -eq 2 ]; then
docker system prune --all --volumes
else
docker system prune --all --force --volumes >/dev/null 2>&1
fi
}
# Remove all Docker data
remove_all() {
local verbosity="$1"
if [ "$verbosity" -ne 0 ]; then
printf "Removing all Docker data\n"
fi
# Stop all containers
stop_container "$(docker ps --all --quiet)" "$verbosity"
# Kill all containers
kill_container "$(docker ps --all --quiet)" "$verbosity"
# Remove all containers
remove_container "$(docker ps --all --quiet)" "$verbosity"
# Remove all volumes
remove_volume "$(docker volume ls --quiet)" "$verbosity"
# Remove all images
remove_image "$(docker image ls --all --quiet)" "$verbosity"
}
# Check if an item is in an array
exit_if_not_contains_item() {
local array=("${!1}")
local match="$2"
for item in "${array[@]}"; do
if [ "$item" = "$match" ]; then
return 0
fi
done
printf "%s: No such item\n" "$OPTARG"
usage
exit 1
}
# Check if it is an integer
exit_if_not_integer() {
local variable="$1"
if ! echo "$variable" | grep -Eq "^[0-9]{1}$"; then
printf "%s: not a integer\n" "$variable"
usage
exit 1
fi
}
# Main
for arg in "$@"; do
shift
case "$arg" in
"--interactive")
set -- "$@" "-i"
;;
"--detach")
set -- "$@" "-d"
;;
"--extension")
set -- "$@" "-e"
;;
"--force")
set -- "$@" "-f"
;;
"--prune")
set -- "$@" "-p"
;;
"--wipe")
set -- "$@" "-w"
;;
"--verbosity")
set -- "$@" "-v"
;;
"--help")
set -- "$@" "-h"
;;
"--"*)
set -- "$@" "-?"
;;
*)
set -- "$@" "$arg"
esac
done
while getopts ":ide:fpwv:h" option; do
case $option in
i)
INTERACTIVE=true
;;
d)
DETACH=true
;;
e)
exit_if_not_contains_item SORTED_REFERENCE_EXTENSIONS[@] "$OPTARG"
NON_SORTED_SELECTED_EXTENSIONS+=("$OPTARG")
;;
f)
FORCE=true
;;
p)
PRUNE=true
;;
w)
WIPE=true
;;
v)
VERBOSITY=$OPTARG
exit_if_not_integer "$VERBOSITY"
;;
h)
usage
exit
;;
:)
printf "%s: required argument\n" "$OPTARG"
exit 1
;;
\?)
printf "%s: invalid option\n" "$OPTARG"
usage
exit 1
;;
esac
done
if { [ "$INTERACTIVE" = true ] || [ "$DETACH" = true ] && \
{ [ "$PRUNE" = true ] || [ "$WIPE" = true ]; } } || \
{ [ "$INTERACTIVE" = true ] || [ "$PRUNE" = true ] || [ "$WIPE" = true ] && \
[ "$DETACH" = true ]; }; then
printf -- "--interactive --detach --prune --wipe: options must be used separately\n"
usage
exit 1
elif [ "$PRUNE" = true ] || [ "$WIPE" = true ] && [ -n "${NON_SORTED_SELECTED_EXTENSIONS:-}" ]; then
printf -- "--extension: option must be used with --interactive or --detach option\n"
usage
exit 1
elif [ "$INTERACTIVE" = true ] || [ "$PRUNE" = true ] || [ "$WIPE" = true ] && \
[ "$FORCE" = true ]; then
printf -- "--force: option must be used with --detach option\n"
usage
exit 1
elif [ "$PRUNE" = true ]; then
# Start Docker application
start_docker "$VERBOSITY"
remove_all_unused "$VERBOSITY"
# Stop Docker application
stop_docker "$VERBOSITY"
exit 0
elif [ "$WIPE" = true ]; then
# Start Docker application
start_docker "$VERBOSITY"
remove_all "$VERBOSITY"
# Stop Docker application
stop_docker "$VERBOSITY"
exit 0
else
if [ "$INTERACTIVE" = true ]; then
VERBOSITY=2
fi
if [ -n "${NON_SORTED_SELECTED_EXTENSIONS:-}" ]; then
SELECTED_EXTENSIONS_STR="$(printf "%s\n" "${NON_SORTED_SELECTED_EXTENSIONS[@]}" | sort -u)"
ENVS=(${SELECTED_EXTENSIONS_STR})
# Bypass any prior checking
FORCE=true
fi
# Transform extensions into Docker container's environment variables
for index in "${!ENVS[@]}"; do
ENVS[index]="--env EXTENSION_$((index+1))=${ENVS[index]}"
done
if [ "$VERBOSITY" -ne 0 ]; then
banner
fi
# Pre-run script
if [ "$INTERACTIVE" = false ] && [ "$FORCE" = false ]; then
if [ "$VERBOSITY" -ne 0 ]; then
printf "\n"
fi
if ! sh -u pre-run -v "$VERBOSITY"; then
exit 0
fi
fi
# Run script
if [ "$VERBOSITY" -ne 0 ]; then
printf "\nRun script\n"
fi
# Start Docker application
start_docker "$VERBOSITY"
# Build Docker container
if ! build_container "$IMAGE_TAG" "$CONTAINER_NAME" "$VERBOSITY"; then
exit 1
fi
# Audit system
if [ "$VERBOSITY" -eq 0 ]; then
printf "%s\tinfo\tupgrade\t%s\t%s\t\"Upgrade in progress\"\n" \
"$(date -u +%FT%TZ)" "$CURRENT_VERSION" "$LATEST_VERSION" >> .log
fi
# Start Docker container
if ! start_container "$IMAGE_TAG" "$CONTAINER_NAME" \
ENVS[@] \
"$SRC_VOLUME_1" "$DST_VOLUME_1" \
"$SRC_VOLUME_2" "$DST_VOLUME_2" \
"$SRC_VOLUME_3" "$DST_VOLUME_3" \
"$SRC_VOLUME_4" "$DST_VOLUME_4" \
"$INTERACTIVE" "$VERBOSITY"; then
exit 1
fi
# Remove all related data to this container
stop_container "$CONTAINER_NAME" "$VERBOSITY"
kill_container "$CONTAINER_NAME" "$VERBOSITY"
remove_container "$CONTAINER_NAME" "$VERBOSITY"
remove_volumes_unused "$VERBOSITY"
remove_images_unused "$VERBOSITY"
# Stop Docker application
stop_docker "$VERBOSITY"
# Post-run script
if [ "$VERBOSITY" -ne 0 ]; then
printf "\n"
fi
sh -u post-run -v "$VERBOSITY"
fi