This repository has been archived by the owner on Apr 15, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 24
/
run
executable file
·238 lines (195 loc) · 7.61 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
#! /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
node_image="canonicalwebteam/node:v0.1.0"
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)
- 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=()
node_cache_volume="${CANONICAL_WEBTEAM_NODE_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?
# 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 ;;
*) 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`
}
node_run () {
# Standard options for running node commands
container_name="${1}"
command="${2:-}"
extra_options="${3:-}"
# Run a command in the "node" image
run_as_user "${container_name}" \
--volume ${node_cache_volume}:/home/shared/.cache/ `# Bind cache to volume` \
${module_volumes[@]+"${module_volumes[@]}"} `# Add any override modules as volumes` \
${extra_options} `# Extra options` \
${node_image} ${command} `# 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 ${node_cache_volume}:/home/shared/.cache/ `# 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
# 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" "yarn run watch" "--detach" # Run watch in the background
fi
# Run the serve container, publishing the port, and detaching if required
node_run "${project}-serve" "yarn run serve" "--env PORT=${PORT} --publish ${PORT}:${PORT} ${detach}"
;;
"build")
node_install
node_run "${project}-build" "yarn run build"
;;
"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 ${node_cache_volume}"
containers_using_volume=$(docker ps --quiet --all --filter "volume=${node_cache_volume}")
if [ -n "${containers_using_volume}" ]; then docker rm --force ${containers_using_volume}; fi
docker volume rm --force ${node_cache_volume}
;;
"node") node_run "${project}-node-$(date +'%s')" "${@}" ;;
*) invalid "Command '${run_command}' not recognised." ;;
esac