forked from canonical-web-and-design/vanillaframework.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun
executable file
·294 lines (242 loc) · 9.65 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
#! /usr/bin/env bash
# CAUTION:
# This file 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
# Define the versions of the two docker images
bundler_image="canonicalwebteam/bundler:v0.1.2"
node_image="canonicalwebteam/node:v0.1.0"
VERSION="0.3.2"
USAGE="Usage
===
$ ./run \\
[-m|--node-module PATH] # A path to a local node module to use instead of the installed dependencies \\
[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 \`yarn run serve\` (optionally running \`watch\` in the background)
- watch [-s|--watch-site]: Run \`yarn run watch\` (optionally watching for jekyll changes in the background)
- build: Run \`yarn run build\`
- test: Run \`yarn run test\`
- stop: Stop any running containers
- node <args>: Run a command in the node container
- 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)
"
##
# 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
# 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
# Settings
module_volumes=()
yarn_cache_volume="${CANONICAL_WEBTEAM_YARN_CACHE_VOLUME:-canonical-webteam-node-cache}"
[ -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?
run_serve_docker_opts="${CANONICAL_WEBTEAM_RUN_SERVE_DOCKER_OPTS:-}"
# Defaults environment settings
PORT=8000
# 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
-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 generator-canonical-webteam@$VERSION"; 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
}
run_as_user () {
# Run a container as the current user and in the current directory context
# Get options
container_name=${1}; shift # Get container_name as first argument
# Kill existing containers
kill_container "${container_name}"
# Start the new container
docker run \
--name ${container_name} `# Name the container` \
--rm `# Remove the container once it's finished` \
--user $(id -u):$(id -g) `# Use the current user inside container` \
${env_file} `# Pass environment variables into the container, if file exists` \
--volume `pwd`:`pwd` `# Mirror current directory inside container` \
--workdir `pwd` `# Set current directory to the image's work directory` \
${tty} `# Attach a pseudo-terminal, if relevant` \
$@ `# Extra arguments`
}
bundler_run () {
container_name="${1}"; shift
extra_options="${1:-}"; shift
# Kill existing containers
kill_container "${container_name}"
run_as_user "${container_name}" \
--volume ${project}-bundle:/bundler `# Persist ruby dependencies in a docker volume` \
${extra_options} `# Extra options` \
${bundler_image} $@ `# Run the jeklyll command`
}
node_run () {
# Standard options for running node commands
container_name="${1}"; shift
extra_options="${1:-}"; shift
# Run a command in the "node" image
run_as_user "${container_name}" \
--volume ${yarn_cache_volume}:/home/shared/.cache/yarn/ `# Bind cache to volume` \
${module_volumes[@]+"${module_volumes[@]}"} `# Add any override modules as volumes` \
${extra_options} `# Extra options` \
${node_image} ${@} `# Run command in node image`
}
node_install () {
# Install bower dependencies, if we need to
if [ -f "bower.json" ]; then
node_run "${project}-bower-install" "" bower install
fi
# Install yarn dependencies, without module overrides
run_as_user "${project}-yarn-install" \
--volume ${yarn_cache_volume}:/home/shared/.cache/yarn/ `# Bind yarn cache to volume` \
${node_image} yarn install `# Install yarn dependencies`
}
# Find current run command
run_command=${1:-}
if [[ -n "${run_command}" ]]; then shift; fi
# Do the real business
case $run_command in
""|"serve")
node_install
# 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
node_run "${project}-build" "" yarn run build
# Run watch command in the background
if ${run_watcher}; then
if [ -z "${detach}" ]; then trap "kill_container ${project}-watch" EXIT; fi
node_run "${project}-watch" "--detach" yarn run watch # Run watch in the background
fi
# Run the serve container, publishing the port, and detaching if required
bundler_run "${project}-serve" "--env PORT=${PORT} --publish ${PORT}:${PORT} ${detach} ${run_serve_docker_opts}" jekyll serve -P ${PORT} -H 0.0.0.0
;;
"stop")
echo "Stopping all running containers for ${project}"
running_containers="$(docker ps --quiet --filter name=${project})"
docker kill ${running_containers}
;;
"watch")
# Read optional arguments
watch_site=false
while [[ -n "${1:-}" ]] && [[ "${1:0:1}" == "-" ]]; do
key="$1"
case $key in
-s|--watch-site) watch_site=true ;;
*) invalid "Option '${key}' not recognised." ;;
esac
shift
done
if ${watch_site}; then
trap "kill_container ${project}-watch-site" EXIT
bundler_run "${project}-watch-site" "--detach" jekyll build --watch # Run site watcher in the background
fi
node_install
node_run "${project}-build" "" yarn run build
node_run "${project}-watch" "" yarn run watch
;;
"build")
node_install
node_run "${project}-build" "" yarn run build
bundler_run "${project}-build-site" "" jekyll build
;;
"test")
node_install
node_run "${project}-yarn-test" "" yarn run test
;;
"clean")
echo "Running 'clean' yarn script"
node_run "${project}-clean" "" yarn run clean || true # Run the clean script
echo "Removing all containers, volumes and networks for project: ${project}"
project_containers="$(docker ps --all --quiet --filter name=${project})"
project_volumes="$(docker volume ls --quiet --filter name=${project})"
project_networks="$(docker network ls --quiet --filter name=${project})"
if [ -n "${project_containers}" ]; then docker rm --force ${project_containers}; fi
if [ -n "${project_volumes}" ]; then docker volume rm --force ${project_volumes}; fi
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 ${yarn_cache_volume}"
containers_using_volume=$(docker ps --quiet --all --filter "volume=${yarn_cache_volume}")
if [ -n "${containers_using_volume}" ]; then docker rm --force ${containers_using_volume}; fi
docker volume rm --force ${yarn_cache_volume}
;;
"node") node_run "${project}-node-$(date +'%s')" "" $@ ;;
*) invalid "Command '${run_command}' not recognised." ;;
esac