forked from forem/forem
-
Notifications
You must be signed in to change notification settings - Fork 0
/
docker-run.sh
executable file
·512 lines (461 loc) · 13.2 KB
/
docker-run.sh
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
#!/bin/bash
###########################################
#
# Interactive mode script handling
#
###########################################
if [ "$1" = "INTERACTIVE-DEMO" ]
then
# echo "#---"
# echo "# Starting up INTERACTIVE-DEMO mode"
# echo "#---"
# Configure RUN_MODE as DEMO
RUN_MODE="DEMO"
echo "|---"
echo "|"
echo "| Welcome to DEV.TO interactive docker demo setup guide."
echo "|"
echo "| For this container to work, we will need at minimum ALGOLIA API keys"
echo "| For logins to work, we will need either GITHUB or TWITTER API keys"
echo "|"
echo "| See ( https://docs.dev.to/get-api-keys-dev-env/ ) "
echo "| for instructions on how to get the various API keys "
echo "|"
echo "| Once you got your various API keys, please proceed to the next step"
echo "|"
echo "|---"
echo "|---"
echo "| Setting up ALGOLIASEARCH keys (required)"
echo "|---"
echo -n "| Please indicate your ALGOLIASEARCH_APPLICATION_ID : "
read INPUT_KEY
if [ ! -z "$INPUT_KEY" ]
then
export ALGOLIASEARCH_APPLICATION_ID="$INPUT_KEY"
fi
echo -n "| Please indicate your ALGOLIASEARCH_SEARCH_ONLY_KEY : "
read INPUT_KEY
if [ ! -z "$INPUT_KEY" ]
then
export ALGOLIASEARCH_SEARCH_ONLY_KEY="$INPUT_KEY"
fi
echo -n "| Please indicate your ALGOLIASEARCH_API_KEY (aka admin key) : "
read INPUT_KEY
if [ ! -z "$INPUT_KEY" ]
then
export ALGOLIASEARCH_API_KEY="$INPUT_KEY"
fi
echo "|---"
echo "| Setting up GITHUB keys"
echo "| (OPTIONAL, leave blank and press enter to skip)"
echo "|---"
echo -n "| Please indicate your GITHUB_KEY : "
read INPUT_KEY
if [ ! -z "$INPUT_KEY" ]
then
export GITHUB_KEY="$INPUT_KEY"
fi
echo -n "| Please indicate your GITHUB_SECRET : "
read INPUT_KEY
if [ ! -z "$INPUT_KEY" ]
then
export GITHUB_SECRET="$INPUT_KEY"
fi
echo -n "| Please indicate your GITHUB_TOKEN : "
read INPUT_KEY
if [ ! -z "$INPUT_KEY" ]
then
export GITHUB_TOKEN="$INPUT_KEY"
fi
echo "|---"
echo "| Setting up TWITTER keys"
echo "| (OPTIONAL, leave blank and press enter to skip)"
echo "|---"
echo -n "| Please indicate your TWITTER_ACCESS_TOKEN : "
read INPUT_KEY
if [ ! -z "$INPUT_KEY" ]
then
export TWITTER_ACCESS_TOKEN="$INPUT_KEY"
fi
echo -n "| Please indicate your TWITTER_ACCESS_TOKEN_SECRET : "
read INPUT_KEY
if [ ! -z "$INPUT_KEY" ]
then
export TWITTER_ACCESS_TOKEN_SECRET="$INPUT_KEY"
fi
echo -n "| Please indicate your TWITTER_KEY : "
read INPUT_KEY
if [ ! -z "$INPUT_KEY" ]
then
export TWITTER_KEY="$INPUT_KEY"
fi
echo -n "| Please indicate your TWITTER_SECRET : "
read INPUT_KEY
if [ ! -z "$INPUT_KEY" ]
then
export TWITTER_SECRET="$INPUT_KEY"
fi
fi
###########################################
#
# Script header guide
#
###########################################
echo "#---"
echo "#"
echo "# This script will perform the following steps ... "
echo "#"
echo "# 1) Stop and remove any docker container with the name 'dev-to-postgres' and 'dev-to'"
echo "# 2) Reset any storage directories if RUN_MODE starts with 'RESET-'"
echo "# 3) Build the dev.to docker image, with the name of 'dev-to:dev' or 'dev-to:demo'"
echo "# 4) Deploy the postgres container, mounting '_docker-storage/postgres' with the name 'dev-to-postgres'"
echo "# 5) Deploy the dev-to container, with the name of 'dev-to-app', and sets up its port to 3000"
echo "#"
echo "# To run this script properly, execute with the following (inside the dev.to repository folder)..."
echo "# './docker-run.sh [RUN_MODE] [Additional docker envrionment arguments]'"
echo "#"
echo "# Alternatively to run this script in 'interactive mode' simply run"
echo "# './docker-run.sh INTERACTIVE-DEMO'"
echo "#"
echo "#---"
echo "#---"
echo "#"
echo "# RUN_MODE can either be the following"
echo "#"
echo "# - 'DEV' : Start up the container into bash, with a quick start guide"
echo "# - 'DEMO' : Start up the container, and run dev.to (requries ALGOLIA environment variables)"
echo "# - 'RESET-DEV' : Resets postgresql and upload data directory for a clean deployment, before running as DEV mode"
echo "# - 'RESET-DEMO' : Resets postgresql and upload data directory for a clean deployment, before running as DEMO mode"
echo "# - 'INTERACTIVE-DEMO' : Runs this script in 'interactive' mode to setup the 'DEMO'"
echo "#"
echo "# So for example to run a development container in bash its simply"
echo "# './docker-run.sh DEV'"
echo "#"
echo "# To run a simple demo, with some dummy data (replace <?> with the actual keys)"
echo "# './docker-run.sh DEMO -e ALGOLIASEARCH_APPLICATION_ID=<?> -e ALGOLIASEARCH_SEARCH_ONLY_KEY=<?> -e ALGOLIASEARCH_API_KEY=<?>'"
echo "#"
echo "# Finally to run a working demo, you will need to provide either..."
echo "# './docker-run.sh .... -e GITHUB_KEY=<?> -e GITHUB_SECRET=<?> -e GITHUB_TOKEN=<?>"
echo "#"
echo "# And / Or ..."
echo "# './docker-run.sh .... -e TWITTER_ACCESS_TOKEN=<?> -e TWITTER_ACCESS_TOKEN_SECRET=<?> -e TWITTER_KEY=<?> -e TWITTER_SECRET=<?>"
echo "#"
echo "# Note that all of this can also be configured via ENVIRONMENT variables prior to running the script"
echo "#"
echo "#---"
###########################################
#
# Core script logic
#
###########################################
#
# Arguments / Environment handling
#
# Terminate without argument
if [ -z "$1" ]
then
# Invalid RUN_MODE
echo "#---"
echo "# [FATAL ERROR] Missing RUN_MODE argument (see example above)"
echo "#---"
exit 1
fi
# Initialize : docker-run.sh RUN_MODE
# also parses it from argument array
if [ -z "$RUN_MODE" ]
then
RUN_MODE="DEMO"
fi
# Argument processing
if [ "$1" = "DEMO" ] || [ "$1" = "DEV" ] || [ "$1" = "RESET-DEV" ] || [ "$1" = "RESET-DEMO" ]
then
# The mode is passed into the command line script, process it
RUN_MODE="$1"
# Process argument array without RUN_MODE
ARG_ARRAY_STR="${@:2}"
else
if [ "$1" = "INTERACTIVE-DEMO" ]
then
RUN_MODE="DEMO"
# Process argument array without RUN_MODE
ARG_ARRAY_STR="${@:2}"
else
# Process argument array with $0
ARG_ARRAY_STR="${@:1}"
fi
fi
if [ "$RUN_MODE" = "DEMO" ] || [ "$RUN_MODE" = "DEV" ] || [ "$RUN_MODE" = "RESET-DEV" ] || [ "$RUN_MODE" = "RESET-DEMO" ]
then
# OK we validated run mode
RUN_MODE=$RUN_MODE
else
# Invalid RUN_MODE
echo "#---"
echo "# [FATAL ERROR] Invalid RUN_MODE : $RUN_MODE (see example above)"
echo "#---"
exit 2
fi
# Initialize : the currrent repository directory (and navigate to it)
if [ -z "$REPO_DIR" ]
then
REPO_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
fi
cd "$REPO_DIR";
# Initialize : the general storage directory (used to magic bash the upload / postgres directory)
if [ -z "$STORAGE_DIR" ]
then
STORAGE_DIR="$REPO_DIR/_docker-storage"
fi
# Initialize : Postgresql data directory
if [ -z "$POSTGRES_DIR" ]
then
POSTGRES_DIR="$STORAGE_DIR/postgresql-data/"
fi
# Initialize : dev-to public upload data
if [ -z "$UPLOAD_DIR" ]
then
UPLOAD_DIR="$STORAGE_DIR/public-upload/"
fi
echo "#---"
echo "# Ok, to start with - lets assume the following settings (provided or auto default)..."
echo "#"
echo "# RUN_MODE = $RUN_MODE"
echo "# REPO_DIR = $REPO_DIR"
echo "# STORAGE_DIR = $STORAGE_DIR"
echo "# UPLOAD_DIR = $UPLOAD_DIR"
echo "# POSTGRES_DIR = $POSTGRES_DIR"
echo "#"
echo "# PS : These settings can also be overwritten by ENVIRONMENT variables, extremely useful in a CI setup =)"
echo "#---"
#
# ENV variables to support forwarding, and the compulsory list from bash script to docker (if detected)
#
ENV_FORWARDING_LIST=(
# ALGOLIASEARCH (required for deployment)
"ALGOLIASEARCH_APPLICATION_ID"
"ALGOLIASEARCH_SEARCH_ONLY_KEY"
"ALGOLIASEARCH_API_KEY"
# login via GITHUB
"GITHUB_KEY"
"GITHUB_SECRET"
"GITHUB_TOKEN"
# login via TWITTER
"TWITTER_ACCESS_TOKEN"
"TWITTER_ACCESS_TOKEN_SECRET"
"TWITTER_KEY"
"TWITTER_SECRET"
# PUSHER integration
"PUSHER_APP_ID"
"PUSHER_KEY"
"PUSHER_SECRET"
"PUSHER_CLUSTER"
# @TODO : anything else to pass forward? S3<?>
)
ENV_FORWARDING_DEMO_COMPULSORY_LIST=(
# ALGOLIASEARCH (required for deployment)
"ALGOLIASEARCH_APPLICATION_ID"
"ALGOLIASEARCH_SEARCH_ONLY_KEY"
"ALGOLIASEARCH_API_KEY"
)
#
# dev.to docker command flags to pass forward
#
DEVTO_DOCKER_FLAGS="$ARG_ARRAY_STR"
#
# Scan for ENV variabels to forward
#
echo "#---"
echo "# Lets scan for dev.to environment variables that will automatically be passed"
echo "# forward into the continaer if present (very useful for CI testing)"
echo "#---"
for i in "${ENV_FORWARDING_LIST[@]}"
do
if [[ $DEVTO_DOCKER_FLAGS == *"$i"* ]]
then
echo "[detected in arguments] - $i"
continue
fi
if [ ! -z "$(printenv $i)" ]
then
echo "[detected env variable] - $i"
DEVTO_DOCKER_FLAGS="$DEVTO_DOCKER_FLAGS -e $i=$(printenv $i)"
continue
fi
echo "[skipped env variable] - $i"
done
#
# Check for DEMO compulsory list
#
if [[ "$RUN_MODE" == *"DEMO"* ]]
then
# Iterate compulsory list
for i in "${ENV_FORWARDING_DEMO_COMPULSORY_LIST[@]}"
do
# Exit if not found
if [[ $DEVTO_DOCKER_FLAGS != *"$i"* ]]
then
echo "#---"
echo "# [FATAL ERROR] Missing required DEMO env setting / argument for $i (see example above)"
echo "#---"
exit 3
fi
done
fi
#
# Stop and remove existing containers
#
EXISTING_POSTGRES=$(docker ps -a | grep "dev-to-postgres" | awk '{print $1}')
EXISTING_DEVTO=$(docker ps -a | grep "dev-to-app" | awk '{print $1}')
if [[ ! -z "$EXISTING_POSTGRES" ]] || [[ ! -z "$EXISTING_DEVTO" ]]
then
echo "#---"
echo "# Removing dev-to-postgres / dev-to-app containers"
echo "# Found the following : $EXISTING_POSTGRES $EXISTING_DEVTO"
echo "#---"
# I dunno how docker does a race condition against the previous echo step
# this works around that
sleep 0.001
# Stopping and removing containers
docker stop $EXISTING_POSTGRES $EXISTING_DEVTO;
docker rm $EXISTING_POSTGRES $EXISTING_DEVTO;
fi
#
# Reset the postgresql / upload folder
#
if [ "$RUN_MODE" = "RESET-DEV" ] || [ "$RUN_MODE" = "RESET-DEMO" ]
then
echo "#---"
echo "# Detected RESET based run mode : $RUN_MODE"
echo "#"
echo "# Will delete the following directory X_X "
echo "# - $UPLOAD_DIR"
echo "# - $POSTGRES_DIR"
echo "#"
echo "# And change RUN_MODE to : ${RUN_MODE:6}"
echo "#---"
# Remove files
rm -R "$POSTGRES_DIR"
rm -R "$UPLOAD_DIR"
# Update RUN_MODE
RUN_MODE="${RUN_MODE:6}"
fi
#
# Build the dev-to container
# Exits on failure
#
echo "#---"
echo "# Building dev-to $RUN_MODE docker container (yay!) ... "
echo "# If this is your first time running this build step, go grab a coffee - it may take a long while"
echo "# Alternatively, some paper swords with chairs : https://xkcd.com/303/"
echo "#---"
if [ "$RUN_MODE" = "DEV" ]
then
# Build the DEV mode container
docker build --target alpine-ruby-node -t dev-to:dev . || exit $?
else
# Build the DEMO mode container
docker build -t dev-to:demo . || exit $?
fi
#
# Deploy postgresql container
#
echo "#---"
echo "# Deploying postgresql container"
echo "#"
echo "# POSTGRES_DIR : $POSTGRES_DIR"
echo "#---"
mkdir -p "$POSTGRES_DIR"
docker run -d --name dev-to-postgres -e POSTGRES_PASSWORD=devto -e POSTGRES_USER=devto -e POSTGRES_DB=PracticalDeveloper_development -v "$POSTGRES_DIR:/var/lib/postgresql/data" postgres:10.7-alpine
#
# Wait for postgresql server
# this waits up to ~6*10 seconds
#
echo "#---"
echo "# Waiting for postgres server (this commonly takes 10 ~ 60 seconds) ... "
echo -n "# ."
RETRIES=12
until docker exec dev-to-postgres psql -U devto -d PracticalDeveloper_development -c "select 1" > /dev/null 2>&1 || [ $RETRIES -eq 0 ]; do
echo -n "."
sleep 5
done
echo ""
echo "# Wait completed, moving on ... "
echo "#---"
#
# Deploy dev-to container
#
echo "#---"
echo "# Deploying dev-to container"
echo "#"
echo "# RUN_MODE : $RUN_MODE"
echo "# REPO_DIR : $REPO_DIR"
echo "# UPLOAD_DIR : $UPLOAD_DIR"
echo "#---"
mkdir -p "$UPLOAD_DIR"
#
# in DEV mode - lets run in interactive mode
#
if [ "$RUN_MODE" = "DEV" ]
then
docker run -it -p 3000:3000 \
--name dev-to-app \
--link dev-to-postgres:db \
-v "$REPO_DIR:/usr/src/app" \
-e RUN_MODE="DEV" \
-e DATABASE_URL=postgresql://devto:devto@db:5432/PracticalDeveloper_development \
--entrypoint "/usr/src/app/docker-entrypoint.sh" \
$DEVTO_DOCKER_FLAGS \
dev-to:dev
# End of dev mode
exit 0;
fi
#
# in DEMO mode - lets run it in the background
#
docker run -d -p 3000:3000 \
--name dev-to-app \
--link dev-to-postgres:db \
-v "$UPLOAD_DIR:/usr/src/app/public/uploads/" \
-e RUN_MODE="DEMO" \
-e DATABASE_URL=postgresql://devto:devto@db:5432/PracticalDeveloper_development \
$DEVTO_DOCKER_FLAGS \
dev-to:demo
#
# Wait for dev.to server
# this waits up to ~20*30 seconds
#
echo "#---"
echo "# Waiting for dev.to server... "
echo "#"
echo "# this commonly takes 2 ~ 10 minutes, basically, a very long time .... =[ "
# Side note, looped to give 4 set of distinct lines
# espeially if long wait times occur (to make it more managable)
for i in 1 2 3 4
do
RETRIES=30
echo -n "# ."
until docker exec dev-to-app curl -I --max-time 5 -f http://localhost:3000/ > /dev/null 2>&1 || [ $RETRIES -eq 0 ]; do
echo -n "."
sleep 5
done
echo ""
done
echo "# Wait completed, moving on ... "
echo "#---"
#
# Dumping out docker info
#
echo "#---"
echo "# Displaying relevant docker information"
echo "#---"
DOCKER_INFO=$(docker ps)
echo "$DOCKER_INFO" | head -1
echo "$DOCKER_INFO" | grep dev-to-
#
# Finishing message
#
echo "#---"
echo "# Container deployed on port ( http://localhost:3000 )"
echo "# "
echo "# Time to dev.to(gether)"
echo "#---"