This repository was archived by the owner on Dec 26, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmanage
More file actions
executable file
·473 lines (397 loc) · 12.9 KB
/
manage
File metadata and controls
executable file
·473 lines (397 loc) · 12.9 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
#/usr/bin/env bash
# use experimental BuildKit to enable
# --mount at RUN in Dockerfiles, enabling
# caching of pip packages
# !! Requires Docker v18.09 or later
export DOCKER_BUILDKIT=1
export COMPOSE_DOCKER_CLI_BUILD=1
COMPOSE_FILE="docker-compose.yml"
PACKAGE_MANAGER="npm"
PACKAGE_MANGER_INSTALL_CMD="npm i"
# no trailing / !
REPO_REMOTE_BASE="https://github.com/shadowban-eu"
TESTING_REPO_PATH="../testing"
TESTING_REPO_GIT_DIR="$TESTING_REPO_PATH/.git"
TIMELINE_TERMINATION_REPO_PATH="../timeline-termination"
TIMELINE_TERMINATION_REPO_GIT_DIR="$TIMELINE_TERMINATION_REPO_PATH/.git"
PWA_REPO_PATH="../pwa"
PWA_REPO_GIT_DIR="$PWA_REPO_PATH/.git"
# branch to check out when cloning TESTING and PWA
GIT_BRANCH="master"
# Services to build images for
# Essentially all services that have
# a build: entry in the $COMPOSE_FILE
BUILD_IMAGE_FOR="testing timeline-termination pwa www"
# Services to run detached, after successful init
AUX_SERVICES="db www"
# Domain to set up (used for e.g. SSL certificate)
# REMEMBER to point this to 127.0.0.1 in your DNS or hosts file!
CERT_DOMAIN="shadowban.dev"
# other settings
COLOR_FG_CYAN="\e[1;49;36m"
COLOR_FG_RED="\e[1;49;31m"
COLOR_FG_YELLOW="\e[1;49;33m"
COLOR_FG_GREEN="\e[1;49;32m"
COLOR_RESET="\e[0m"
PRODUCTION=0
SKIP_INIT_STEPS=0
# prevent changes to .env files from being added to git
git update-index --skip-worktree env/*
function read_env {
while IFS='=' read -r name value; do
if [ -z "$value" -o -z "$name" ]; then
echo "Error - unparsable line!" >&2
exit 1
fi
declare -xrg "$name=$value"
done < $1
}
function print_help {
echo "Usage: ./manage [db|dev <service>|init|mkcert <domain>|*] [-b <branch>] [-g <uri>] [-h] [-p]"
echo -e "Setup and manage shadowban.eu development docker containers\n"
echo "Depends:"
echo -e " docker >=18.0.9, docker-compose >=1.25, [mkcert]\n"
if [ -n "$1" ]; then
echo -e "$COLOR_FG_YELLOW--- Invalid option: -$1 ---$COLOR_RESET\n"
fi
echo "Managing commands:"
echo " init Clones all services, builds images,"
echo " sets up SSL, etc."
echo " db Open authenticated shell in db container"
echo " dev <service> Run <service> in foreground and all other services detached."
echo -e " mkcert -c <domain> Creates a new SSL cert/key pair for <domain>\n"
echo "Managing options:"
echo " -b <branch> Branch name to check out; default: master"
echo -e " (commands: init; default: master)\n"
echo " -c <domain> Domain for SSL certificate"
echo -e " (commands: mkcert, init; default: shadowban.dev)\n"
echo " -g <uri> Base URI for repositories to clone from"
echo -e " (commands: init; default: https://github.com/shadowban-eu/)\n"
echo " -p Run in production mode"
echo " -y Skip init steps and confirmation"
echo "Other:"
echo " -h You're looking at it."
echo " * All other arguments are passed to docker-compose"
echo " e.g. './manage help' to see the docker-compose help"
exit
}
if [[ ${#} -eq 0 ]]; then
print_help
fi
if [[ "$1" =~ ^(db|dev|down|init|mkcert)$ ]]; then
COMMAND=$1
shift
echo "Command: $COMMAND"
fi
while getopts ":b:c:hg:py" opt; do
case $opt in
b)
[ "$OPTARG" == "" ] && print_help "b needs a <branch> parameter"
echo "Option: -b GIT_BRANCH=$OPTARG"
GIT_BRANCH="$OPTARG"
;;
c)
[ "$OPTARG" == "" ] && print_help "b needs a <branch> parameter"
echo "Option: -c CERT_DOMAIN=$OPTARG"
CERT_DOMAIN=$OPTARG
;;
g)
[ "$OPTARG" == "" ] && print_help "g needs a <uri> paramter"
echo "Option: -g REPO_REMOTE_BASE=$OPTARG"
REPO_REMOTE_BASE="$OPTARG"
;;
h)
print_help
;;
p)
echo "Option: -p --- PRODUCTION MODE ---"
PRODUCTION=1
COMPOSE_FILE="docker-compose.production.yml"
BUILD_IMAGE_FOR="www"
;;
y)
echo "Option: -y - Skipping init steps"
SKIP_INIT_STEPS=1
;;
?)
print_help $OPTARG
;;
esac
[ -z "$COMMAND" ] && shift
done
# keep after getops; could be changed by -g
TESTING_REPO_REMOTE="$REPO_REMOTE_BASE/testing.git"
TIMELINE_TERMINATION_REPO_REMOTE="$REPO_REMOTE_BASE/timeline-termination.git"
PWA_REPO_REMOTE="$REPO_REMOTE_BASE/pwa.git"
function print_init_steps {
echo -e "\n--- shadowban.eu docker setup ---"
echo "This script will"
echo " 1. Clone the TESTING service ($GIT_BRANCH branch) to $TESTING_REPO_PATH"
[ $PRODUCTION -ne 1 ] && \
echo " 2. Install the TIMELINE_TERMINATION service ($GIT_BRANCH branch) to $TIMELINE_TERMINATION_REPO_PATH"
echo " 3. Install the PWA ($GIT_BRANCH branch) to $PWA_REPO_PATH"
echo " 4. Build images ($BUILD_IMAGE_FOR)"
echo " 5. Create the certificate for $CERT_DOMAIN (requires mkcert)"
echo -e "----------------------------------------"
read -p "Key to continue..."
}
function print_banner_border {
printf '#%.0s' `seq $1`; printf '\n'
}
function print_banner {
local TITLE="$1 $COLOR_FG_CYAN$2$COLOR_RESET"
local TITLE_LENGTH=${#TITLE}
local DIVIDER_LENGTH=$(($TITLE_LENGTH + 4))
echo ""
print_banner_border $DIVIDER_LENGTH
echo -e "# $TITLE"
print_banner_border $DIVIDER_LENGTH
}
function print_error {
echo -e "$COLOR_FG_RED!! $1$COLOR_RESET"
}
function print_warning {
echo -e "${COLOR_FG_YELLOW}$1$COLOR_RESET"
}
function print_success {
echo -e "${COLOR_FG_GREEN}$1$COLOR_RESET"
}
function bail_on_fail {
if [ $1 -ne 0 ]; then
print_error "Can't proceed without $2. Please check above output for errors!"
exit $1
fi
}
###
# 'mkcert' <DOMAIN>
###
function make_certificate() {
which mkcert &> /dev/null
if [ $? -ne 0 ]; then
echo "You need mkcert for certificate management!"
echo "Download from: https://github.com/FiloSottile/mkcert"
[ "$2" != "init" ] && exit 1
return
fi
local DOMAIN=$1
local OUT_PATH=./www/ssl/$DOMAIN
mkdir -p $OUT_PATH
bail_on_fail $? "SSL certificates"
mkcert \
-key-file $OUT_PATH/key.pem \
-cert-file $OUT_PATH/cert.pem \
$DOMAIN *.$DOMAIN
bail_on_fail $? "SSL certificates"
}
###
# pre-run checks
###
function check_docker_daemon {
docker ps &>/dev/null
if [ $? -ne 0 ]; then
print_error "No usable Docker service. You might have to add yourself to the docker group: https://docs.docker.com/engine/install/linux-postinstall/"
exit 1
fi
}
function check_package_manager {
# preferring yarn
yarn -v &> /dev/null
local YARN_RESULT=$?
if [ $YARN_RESULT -eq 0 ]; then
PACKAGE_MANAGER="yarn"
PACKAGE_MANGER_INSTALL_CMD="yarn"
else
# at least npm is a must
npm -v &> /dev/null
local NPM_RESULT=$?
[ $NPM_RESULT -ne 0 ] && \
print_error "Neither 'yarn' nor 'npm' found. Is one of them in your PATH?"
bail_on_fail $NPM_RESULT "yarn or npm"
fi
echo -e "${COLOR_FG_GREEN}using $PACKAGE_MANAGER$COLOR_RESET"
}
function check_empty_parent {
local HAS_MISC_DIRS=0
local ABSOLUTE_SELF=`pwd`
for DIR_NAME in ".."/*; do
if [ `realpath $DIR_NAME` != $ABSOLUTE_SELF ]; then
HAS_MISC_DIRS=1
break
fi
done
if [ $HAS_MISC_DIRS -eq 1 ]; then
print_warning "The parent directory is not empty."
read -p "Proceed with non-empty parent? [y/N] " USE_NON_EMPTY_PARENT
if [ "$USE_NON_EMPTY_PARENT" != "y" ]; then exit 0; fi
else
echo -e "${COLOR_FG_GREEN}OK$COLOR_RESET"
fi
}
function check_version {
local TESTED_BINARY=$1
local VERSION=$2
local VERSION_MAJOR=$((`echo $VERSION | cut -d '.' -f1`))
local VERSION_MINOR=$((`echo $VERSION | cut -d '.' -f2`))
local MIN_VERSION_MAJOR=$3
local MIN_VERSION_MINOR=$4
local MIN_VERSION="$MIN_VERSION_MAJOR.$MIN_VERSION_MINOR"
local CHECK_FAILED=0
echo $TESTED_BINARY
echo " Required: $MIN_VERSION"
echo -ne " Installed: "
[ $VERSION_MAJOR -lt $MIN_VERSION_MAJOR ] && \
CHECK_FAILED=1
[ $VERSION_MAJOR -eq $MIN_VERSION_MAJOR ] && \
[ $VERSION_MINOR -lt $MIN_VERSION_MINOR ] && \
CHECK_FAILED=1
if [ $CHECK_FAILED -eq 1 ]; then
echo -e "$COLOR_FG_RED$VERSION$COLOR_RESET"
print_error "Please upgrade to or install $TESTED_BINARY v$MIN_VERSION or later!"
exit 1
fi
echo -e "$COLOR_FG_GREEN$VERSION$COLOR_RESET"
}
function check_docker_compose_version {
local MIN_VERSION_MAJOR=1
local MIN_VERSION_MINOR=25
local VERSION=`docker-compose version --short`
check_version \
'docker-compose' \
$VERSION \
$MIN_VERSION_MAJOR \
$MIN_VERSION_MINOR
}
function check_docker_version {
local MIN_VERSION_MAJOR=18
local MIN_VERSION_MINOR=09
local VERSION=`docker version --format '{{.Server.Version}}'`
check_version \
'docker-compose' \
$VERSION \
$MIN_VERSION_MAJOR \
$MIN_VERSION_MINOR
}
###
# Clone the testing service
###
function init_repo {
local REPO_DIR=$1
local REPO_REMOTE=$2
local GIT_DIR_PATH_ABSOLUTE=`realpath $REPO_DIR 2> /dev/null`
git --git-dir=$GIT_DIR_PATH_ABSOLUTE/.git --work-tree=$GIT_DIR_PATH_ABSOLUTE branch &> /dev/null
if [[ $? == 0 && $GIT_DIR_PATH_ABSOLUTE != "" ]]; then
print_success "TESTING repo already present at $GIT_DIR_PATH_ABSOLUTE"
git --git-dir=$GIT_DIR_PATH_ABSOLUTE/.git --work-tree=$GIT_DIR_PATH_ABSOLUTE checkout $GIT_BRANCH
else
git clone --single-branch -b $GIT_BRANCH $REPO_REMOTE $GIT_DIR_PATH_ABSOLUTE
bail_on_fail $? "$GIT_DIR_PATH_ABSOLUTE"
fi
}
###
# Install Node dependencies
###
function install_node_packages {
(cd $1; $PACKAGE_MANGER_INSTALL_CMD)
bail_on_fail $? "dependencies for $1"
}
###
# Build the images for $BUILD_IMAGE_FOR
###
function build_images {
docker-compose -f $COMPOSE_FILE build $BUILD_IMAGE_FOR
bail_on_fail $? "images"
}
###
# Bring up services detached
###
function up_d_services {
docker-compose -f $COMPOSE_FILE up -d $1
bail_on_fail $? "auxiliary services"
}
###
# shadowban-eu ./manage
###
check_docker_daemon
case "$COMMAND" in
"mkcert")
[ "$1" == "" ] && echo "Please provide a domain name for the certificate!" && exit 1
make_certificate $1
exit 0
;;
"init")
[ $SKIP_INIT_STEPS -ne 1 ] && print_init_steps
print_banner "?" "Checking parent directory"
check_empty_parent
print_banner "?" "Checking Docker versions"
check_docker_version
check_docker_compose_version
print_banner "?" "Checking for YARN or NPM"
check_package_manager
print_banner "1" "Cloning TESTING"
init_repo $TESTING_REPO_PATH $TESTING_REPO_REMOTE
if [ $PRODUCTION -ne 1 ]; then
print_banner "2" "Installing TIMELINE_TERMINATION"
init_repo $TIMELINE_TERMINATION_REPO_PATH $TIMELINE_TERMINATION_REPO_REMOTE
install_node_packages $TIMELINE_TERMINATION_REPO_PATH
fi
if [ $PRODUCTION -ne 1 ]; then
print_banner "3" "Installing PWA"
init_repo $PWA_REPO_PATH $PWA_REPO_REMOTE
install_node_packages $PWA_REPO_PATH
fi
print_banner "4" "Building images"
build_images
if [ $PRODUCTION -ne 1 ]; then
print_banner "5" "Creating SSL certificate for $CERT_DOMAIN"
make_certificate $CERT_DOMAIN "init"
fi
print_banner "\\\0/" "${COLOR_FG_GREEN}Done"
echo -e "\nRemember to point $CERT_DOMAIN and test.$CERT_DOMAIN to 127.0.0.1 in your DNS or hosts file!\n"
echo -e "Run './manage up testing' to start the testing dev server."
echo -e "Then './manage up pwa' to start the React dev server; https://shadowban.dev/\n"
echo "Dev servers reload on file changes in their respective repository:"
echo " pwa: $PWA_REPO_PATH"
echo " timeline-termination: $TIMELINE_TERMINATION_REPO_PATH"
echo " testing: $TESTING_REPO_PATH"
exit 0
;;
"down")
echo "The 'down' command stops and destroys ALL containers. THIS WILL WIPE YOUR DATABASE."
echo "If you just want to stop services, abort here and use ./manage stop <services>!"
read -p "Proceed with destroying all containers? [y/N] " CONTINUE_DOWN
if [ "$CONTINUE_DOWN" == "y" ]; then
docker-compose -f $COMPOSE_FILE down
exit 0
else
exit 130 # "terminated by C-c"
fi
;;
"db")
read_env ./env/mongod.env
docker-compose -f $COMPOSE_FILE exec \
db mongo $MONGO_DB \
-u $MONGO_INITDB_ROOT_USERNAME \
-p $MONGO_INITDB_ROOT_PASSWORD \
--authenticationDatabase admin
exit 0
;;
"dev")
FOREGROUND_SERVICE="$2"
if [ "$FOREGROUND_SERVICE" == "" ]; then
print_warning "No foreground service specified.\nPlease choose 'testing' or 'pwa' to run in foreground (no -d)"
read -p "[testing|pwa]: " FOREGROUND_SERVICE
fi
if [ "$FOREGROUND_SERVICE" == "pwa" ]; then
up_d_services "$AUX_SERVICES testing"
elif [ "$FOREGROUND_SERVICE" == "testing" ]; then
up_d_services "$AUX_SERVICES pwa"
else
bail_on_fail 1 "foreground service"
fi
docker-compose -f $COMPOSE_FILE up $FOREGROUND_SERVICE
docker-compose -f $COMPOSE_FILE stop $AUX_SERVICES
exit 0
;;
esac
echo "Running docker-compose with '$@'"
docker-compose -f $COMPOSE_FILE $@