This repository has been archived by the owner on May 10, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
/
run
executable file
·505 lines (429 loc) · 16.6 KB
/
run
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
#! /usr/bin/env bash
# CAUTION:
# This file was generated by [email protected]
# and should usually not be edited directly.
#
# This file was generated by the "canonical-webteam" Yeoman generator
# https://npmjs.org/package/generator-canonical-webteam
#
# Update it to the latest version with:
#
# $ sudo npm install -g yo generator-canonical-webteam
# $ yo canonical-webteam:run
set -euo pipefail
USAGE="How to use ./run v2.2.0
===
$ ./run \\
[-m|--node-module PATH] # A path to a local node module to use instead of the installed dependencies \\
[--no-debug] # Turn off the DEBUG setting \\
[COMMAND] # Optionally provide a command to run
If no COMMAND is provided, \`serve\` will be run.
Commands
---
- serve [-p|--port PORT] [-w|--watch] [-d|--detach]: Run a development server (optionally running \`watch\` in the background)
- watch [-s|--watch-site]: Run \`yarn run watch\` (for jekyll sites, watch for changes with \`--watch-site\`)
- build: Run \`yarn run build\`
- test: Run \`yarn run test\`
- stop: Stop any running containers
- exec [-p|--expose-port PORT] <args>: Run a command in the development container (optionally exposing a port to the host)
- clean: Remove all images and containers, any installed dependencies and the .docker-project file
- clean-cache: Empty cache files, which are saved between projects (eg, yarn)
"
##
# Variable definitions
##
# Define docker images versions
dev_image="canonicalwebteam/dev:v1.2.0"
if [ -n "${DOCKER_REGISTRY:-}" ]; then
dev_image="${DOCKER_REGISTRY}/${dev_image}"
fi
# Interactivity options
[ -t 1 ] && tty="--tty --interactive" || tty="" # Do we have a terminal?
[ -f .env ] && env_file="--env-file .env" || env_file="" # Do we have an env file?
# Defaults environment settings
PORT=8000
RUN_DEBUG=true
# Other variables
run_serve_docker_opts="${CANONICAL_WEBTEAM_RUN_SERVE_DOCKER_OPTS:-}"
module_volumes=()
##
# Check docker is installed correctly
##
if ! command -v docker >/dev/null 2>&1; then
echo "
Error: Docker not installed
==
Please install Docker before continuing:
https://www.docker.com/products/docker
"
exit 1
fi
if grep -q '^docker:' /etc/group && ! groups | grep -q '\bdocker\b'; then
echo "
Error: `whoami` not in docker group
===
Please add this user to the docker group, e.g. with:
\$ newgrp docker
"
exit 1
fi
# Grab HTTP_PROXY settings from the host
http_proxy=""
if [ -n "${HTTP_PROXY:-}" ]; then
http_proxy="--env HTTP_PROXY=${HTTP_PROXY} --env http_proxy=${HTTP_PROXY}"
fi
if [ -n "${HTTPS_PROXY:-}" ]; then
http_proxy="${http_proxy} --env HTTPS_PROXY=${HTTPS_PROXY} --env https_proxy=${HTTPS_PROXY}"
fi
# Generate the project name
if [[ -f ".docker-project" ]]; then
project=$(cat .docker-project)
else
directory=$(basename `pwd`)
hash=$((pwd | md5sum 2> /dev/null || md5 -q -s `pwd`) | cut -c1-8)
project=canonical-webteam-${directory}-${hash}
echo $project > .docker-project
fi
# Volume names
cache_volume="${CANONICAL_WEBTEAM_CACHE_VOLUME:-canonical-webteam-cache}"
etc_volume="${project}-etc"
usr_local_volume="${project}-usr-local"
db_volume="${project}-db"
# Container names
db_container="${project}-db"
pip_container="${project}-pip"
# Network name
network_name="${project}-net"
# Import environment settings
if [ -f .env ]; then
export $(cat .env | grep -v ^\# | xargs)
fi
invalid() {
message=${1}
echo "Error: ${message}"
echo ""
echo "$USAGE"
exit 1
}
# Read optional arguments
while [[ -n "${1:-}" ]] && [[ "${1:0:1}" == "-" ]]; do
key="$1"
case $key in
--no-debug) RUN_DEBUG=false ;;
-m|--node-module)
if [ -z "${2:-}" ]; then invalid "Missing module name. Usage: --node-module <path-to-module>."; fi
module_volumes+=("--volume" "${2}":"`pwd`/node_modules/$(basename ${2})")
shift
;;
-h|--help) echo "$USAGE"; exit ;;
-v|--version) echo "Generated from [email protected]"; exit ;;
*) invalid "Option '${key}' not recognised." ;;
esac
shift
done
kill_container () {
container_name="${1}"
# Kill any previous containers
previous_id=$(docker ps --all --quiet --filter "name=^/${container_name}$")
if [ -n "${previous_id}" ]; then
docker rm --force ${previous_id} > /dev/null;
fi
}
docker_run () {
# Get options
docker_run_options="${1}"; shift
# Generate container name from command
container_name="${project}-${@}"
container_name="${container_name// /_}" # Replace spaces with underscores
container_name=$(echo ${container_name} | tr -dc '[:alnum:]_.-') # Remove disallowed chars
# Kill existing containers
kill_container "${container_name}"
# Environment info
commit_id=$(git rev-parse HEAD || echo "unknown")
# Start the new container
docker run \
--name ${container_name} `# Name the container` \
--rm `# Remove the container once it's finished` \
--volume `pwd`:`pwd` `# Mirror current directory inside container` \
--workdir `pwd` `# Set current directory to the image's work directory` \
--volume ${etc_volume}:/etc `# Use etc with corresponding user added` \
--volume ${usr_local_volume}:/usr/local/ `# Bind local folder to volume` \
--volume ${cache_volume}:/home/shared/.cache/ `# Bind cache to volume` \
--env COMMIT_ID=${commit_id} `# Pass through the commit ID` \
${env_file} `# Pass environment variables into the container, if file exists` \
${http_proxy} `# Include HTTP proxy if needed` \
${tty} `# Attach a pseudo-terminal, if relevant` \
${docker_run_options} `# Extra options` \
${dev_image} $@ `# Run command in the image`
}
run_as_user () {
run_as_user_options="${1}"; shift
# Create local user and group in the dev image
uid=$(id -u)
gid=$(id -g)
if ! docker volume inspect -f " " ${etc_volume} 2> /dev/null; then
etc_run="docker run --rm --volume ${etc_volume}:/etc ${dev_image}"
if ! ${etc_run} grep -P "${gid}:$" /etc/group; then
${etc_run} groupadd -g ${gid} app-user
fi
if ! ${etc_run} grep -P "x:${uid}:" /etc/passwd; then
${etc_run} useradd -u ${uid} -g ${gid} app-user
fi
fi
docker_run "--user $(id -u):$(id -g) ${run_as_user_options}" $@
}
python_run () {
python_run_options="${1}"; shift
if grep -q django.db.backends.postgresql_psycopg2 */settings.py 2> /dev/null; then
# Create isolated network
if ! docker network inspect ${network_name} &> /dev/null; then
docker network create ${network_name}
fi
python_run_options="${python_run_options} --network ${network_name}"
# Start the database
if [[ "${python_run_options}" != *"--detach"* ]]; then trap "kill_container ${project}-db" EXIT; fi
if ! docker inspect -f {{.State.Running}} ${db_container} &>/dev/null; then
docker run \
--name ${db_container} `# Name the container` \
--rm `# Remove the container once it's finished` \
--volume "${db_volume}":/var/lib/postgresql/data `# Store dependencies in a docker volume` \
${http_proxy} `# Include HTTP proxy if needed` \
--network "${network_name}" `# Use an isolated network` \
--network-alias db `# Call this container "db" on the network so it can be found` \
--detach `# Run in the background` \
postgres `# Use the image for node version 7`
fi
# Provision database for django sites
if [ -f manage.py ]; then
docker_run "--network ${network_name}" python3 manage.py migrate
fi
fi
# Choose debug variable
debug="DEBUG=${RUN_DEBUG}"
if [ -f manage.py ]; then
debug="DJANGO_DEBUG=${RUN_DEBUG}"
elif [ -f app.py ]; then
debug="FLASK_DEBUG=${RUN_DEBUG}"
fi
# Run the command in the python docker image
run_as_user "--env ${debug} ${python_run_options}" $@
}
has_file_changed() {
file_to_check=$1
hash_file=".${file_to_check}.${project}.hash"
if [ -f ${file_to_check} ]; then
if [ -f "${hash_file}" ]; then
saved_hash="$(cat ${hash_file})"
else
saved_hash=""
fi
if type md5sum &> /dev/null; then
new_hash=$(md5sum ${file_to_check} | awk '{print $1;}')
else
new_hash=$(md5 ${file_to_check} | awk '{print $4;}')
fi
if [ "${saved_hash}" != "${new_hash}" ]; then
echo -n "${new_hash}" > ${hash_file}
return 0
fi
fi
return 1
}
update_dependencies() {
has_file_changed yarn.lock && yarnlock_changed=true || yarnlock_changed=false
has_file_changed package.json && packagejson_changed=true || packagejson_changed=false
has_file_changed bower.json && bowerjson_changed=true || bowerjson_changed=false
has_file_changed Gemfile && gemfile_changed=true || gemfile_changed=false
has_file_changed Gemfile.lock && gemfilelock_changed=true || gemfilelock_changed=false
has_file_changed requirements.txt && requirements_changed=true || requirements_changed=false
# Yarn
if ${packagejson_changed} || ${yarnlock_changed}; then
run_as_user "" yarn install
fi
# Bower
if ${bowerjson_changed}; then
run_as_user "" bower install
fi
# Ruby bundler
if ${gemfile_changed} || ${gemfilelock_changed}; then
run_as_user "" bundle install --path vendor/bundle
fi
# Python pip
if ${requirements_changed}; then
docker_run "" pip3 install --requirement requirements.txt
fi
}
# Find current run command
run_command=${1:-}
if [[ -n "${run_command}" ]]; then shift; fi
# Do the real business
case $run_command in
""|"serve")
update_dependencies
# Read optional arguments
detach=""
run_watcher=false
while [[ -n "${1:-}" ]] && [[ "${1:0:1}" == "-" ]]; do
key="$1"
case $key in
-d|--detach) detach="--detach" ;;
-p|--port)
if [ -z "${2:-}" ]; then invalid "Missing port number. Usage: --port XXXX"; fi
PORT=${2}
shift
;;
-w|--watch) run_watcher=true ;;
*) invalid "Option '${key}' not recognised." ;;
esac
shift
done
# Setup yarn dependencies
if [ -f package.json ]; then
run_as_user "" yarn run build
fi
# Run watch command in the background
if ${run_watcher}; then
if [ -z "${detach}" ]; then trap "kill_container ${project}-watch" EXIT; fi
run_as_user "--detach" yarn run watch # Run watch in the background
fi
# Select run function and run command
run_function="run_as_user"
run_command="yarn run serve"
run_options=""
if [ -f manage.py ]; then # django
run_function="python_run"
run_command="python3 manage.py runserver 0.0.0.0:${PORT}"
elif [ -f app.py ]; then # flask
run_function="python_run"
run_command="flask run --host 0.0.0.0 --port ${PORT}"
run_options="--env FLASK_APP=app.py"
elif [ -f _config.yml ]; then # jekyll
run_function="run_as_user"
run_command="bundle exec jekyll serve -P ${PORT} -H 0.0.0.0"
fi
# Run the serve container, publishing the port, and detaching if required
${run_function} "--env PORT=${PORT} --publish ${PORT}:${PORT} ${detach} ${run_serve_docker_opts} ${run_options}" ${run_command} $*
;;
"stop")
echo "Stopping all running containers for ${project}"
running_containers="$(docker ps --quiet --filter name=${project})"
docker kill ${running_containers}
;;
"watch")
update_dependencies
# Read optional arguments
watch_site=false
while [[ -n "${1:-}" ]] && [[ "${1:0:1}" == "-" ]]; do
key="$1"
case $key in
-s|--watch-site)
# Error if not a jekyll site
if [ ! -f _config.yml ]; then
echo "Error: Not a Jekyll site";
exit 1;
fi
watch_site=true
;;
*) invalid "Option '${key}' not recognised." ;;
esac
shift
done
if ${watch_site}; then
trap "kill_container ${project}-watch-site" EXIT
run_as_user "--detach" jekyll build --watch # Run site watcher in the background
fi
run_as_user "" yarn run build
run_as_user "" yarn run watch
;;
"build")
update_dependencies
run_as_user "" yarn run build
if [ -f _config.yml ]; then
# For jekyll sites
run_as_user "" bundle exec jekyll build
fi
;;
"test")
update_dependencies
test_error=false
# Run node tests
if [ -f package.json ]; then
echo "- Running yarn tests"
run_as_user "" yarn run test || test_error=true
fi
# Run django tests
if [ -f manage.py ]; then
echo "- Running Django tests"
python_run "" python3 manage.py test || test_error=true
fi
# Generic python tests
if [ -f tests.py ]; then
echo "- Running python tests from tests.py"
python_run "" python3 tests.py || test_error=true
fi
# Run flake8 (python syntax) checks
if [ -f manage.py ] || [ -f app.py ] || [ -f tests.py ]; then
echo "- Running flake8 tests"
python_run "" flake8 --exclude '*env*,node_modules' || test_error=true
fi
# Report success or failure
if ${test_error}; then
echo "==="
echo "Tests failed"
echo "==="
exit 1
else
echo "==="
echo "Tests succeeded"
echo "==="
fi
;;
"clean")
echo "Remove hash files"
rm -rf .*.hash
echo "Running 'clean' yarn script"
run_as_user "" yarn run clean || true # Run the clean script
echo "Removing docker objects for project: ${project}"
echo "- Removing containers using project volumes"
project_volumes="$(docker volume ls --quiet --filter name=${project})"
for volume in ${project_volumes}; do
echo " > Removing containers using volume ${volume}"
containers_using_volume="$(docker ps --all --quiet --filter volume=${volume})"
if [ -n "${containers_using_volume}" ]; then docker rm --force ${containers_using_volume}; fi
done
echo "- Removing project volumes"
if [ -n "${project_volumes}" ]; then docker volume rm ${project_volumes}; fi
echo "- Removing remaining project containers"
project_containers="$(docker ps --all --quiet --filter name=${project})"
if [ -n "${project_containers}" ]; then docker rm --force ${project_containers}; fi
echo "- Removing project networks"
project_networks="$(docker network ls --quiet --filter name=${project})"
if [ -n "${project_networks}" ]; then docker network rm ${project_networks}; fi
echo "Removing .docker-project file"
rm -rf .docker-project # Remove the project file
;;
"clean-cache")
# Clean node cache volume
echo "Removing cache volume ${cache_volume}"
containers_using_volume=$(docker ps --quiet --all --filter "volume=${cache_volume}")
if [ -n "${containers_using_volume}" ]; then docker rm --force ${containers_using_volume}; fi
docker volume rm ${cache_volume}
;;
"exec")
expose_port=""
while [[ -n "${1:-}" ]] && [[ "${1:0:1}" == "-" ]]; do
key="$1"
case $key in
-p|--expose-port)
if [ -z "${2:-}" ]; then invalid "Missing port number. Usage: --expose-port XXXX"; fi
expose_port="--publish ${2}:${2}"
shift
;;
*) invalid "Option '${key}' not recognised." ;;
esac
shift
done
run_as_user "${expose_port}" $@
;;
*) invalid "Command '${run_command}' not recognised." ;;
esac