forked from codeforboston/cornerwise
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstart.sh
executable file
·274 lines (234 loc) · 7.72 KB
/
start.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
#!/bin/bash
host_port=3000
project_dir=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
docker_opts=""
# Don't hardcode the image name. We want to be able to change our
# project name every week. Iterate!
# This should not contain any spaces.
image_name="bdsand/cornerwise"
# space-delimited list of environment variables, to be set in the container
docker_environment="APP_PORT=$host_port APP_NAME=$(basename $image_name)"
# Optionally specify a file containing environment settings
env_file=$project_dir/docker-support/env
function find_vm_name {
echo $(docker-machine ls | awk '/virtualbox/ { print $1 }' | head -n 1)
}
function open_browser {
if ( which xdg-open >/dev/null ); then
xdg-open $1
elif ( which open >/dev/null ); then
open $1
fi;
}
function download_docker_toolbox {
# Determine the latest version of Docker Toolbox and download it.
release_tag=$(curl -s https://api.github.com/repos/docker/toolbox/releases/latest | grep "tag_name" | perl -n -e '/"tag_name": "v([^"]+)"/ && print $1')
if [ -n "$release_tag" ]; then
echo "Downloading latest version of Docker Toolbox ($release_tag)."
open "https://github.com/docker/toolbox/releases/download/v$release_tag/DockerToolbox-$release_tag.pkg"
else
echo "Failed to determine the latest version of Docker Toolbox"
open_browser "https://www.docker.com/toolbox"
fi
}
function print_help {
echo "
Usage: ${BASH_SOURCE[0]} [options] [command]
Quickly build and launch the $image_name container. If a command is
specified, run that command in the container and exit.
Options:
-e <file> Specify a file containing environment variables to use
-F Suppress the script's default behavior of setting up the
VM to forward traffic on the host port to localhost.
-m <name> Specify a docker-machine machine to use
-p <port> Run on a port other than $host_port
-r Force Docker to run a new container, rather than
attach to one that is already running
-R Do NOT remove the container after it has exited
-x If a running container is found, stop it and start a new
one
"
}
if [ -z "$image_name" ]; then
image_name=$(basename $(git rev-parse --show-toplevel))
fi
# Indicates whether the image should be rebuild:
should_build=0
# If true, do not attach to a running container (if applicable):
force_run=0
vm_port_forwarding=1
# How long should the script wait for the application to launch?
open_timeout=30
# Do not rebuild the image if support files have changed
ignore_changes=0
autostart=0
# If a running container is found, should we stop it?
stop_running=0
skip_build_prompt=0
remove_after=1
while getopts ":bBFm:Op:rRSthx" opt; do
case $opt in
b)
should_build=1
skip_build_prompt=1
;;
B)
ignore_changes=1
skip_build_prompt=1
;;
e)
env_file="$OPTARG"
;;
r)
force_run=1
;;
R)
remove_after=0
;;
S)
autostart=0
;;
F)
vm_port_forwarding=0
;;
m)
vm_name="$OPTARG"
;;
p)
if ! [[ $OPTARG =~ ^[0-9]{4,}$ ]]; then
echo "Port must be an integer >=1000."
exit 1
fi
host_port=$OPTARG
;;
t)
if ! [[ $OPTARG =~ ^[0-9]+$ ]]; then
echo "Invalid timeout argument: $OPTARG"
exit 1
fi
open_timeout=$OPTARG
;;
x)
stop_running=1
;;
h)
print_help
exit 0
;;
\?)
echo "Unrecognized flag: -$OPTARG" >&2
exit 1
;;
esac
done
shift $((OPTIND-1))
if [ -z "$run_command" ]; then
if [ -n "$1" ]; then
run_command="$*"
else
run_command=/bin/bash
fi
fi
##################
if (! (which docker >/dev/null)); then
# Docker is not installed
echo "Docker is not installed."
if [ $(uname) == "Darwin" ]; then
download_docker_toolbox
echo "Please re-run this script after you've installed Docker Toolbox."
else
open_browser "http://docs.docker.com/installation/"
echo "Please re-run this script after you've installed Docker."
fi
exit 1
fi;
use_machine=0
# Using Docker-Machine?
if (which docker-machine >/dev/null); then
use_machine=1
if [ -z "$vm_name" ]; then
vm_name=$(find_vm_name)
fi
if [ -z "$vm_name" ]; then
vm_name=dev
# Initialize, if necessary:
docker-machine create -d virtualbox "$vm_name" >/dev/null
fi
machine_status=$(docker-machine status $vm_name)
if [ "$machine_status" = "Timeout" ] ; then
docker-machine restart $vm_name
elif [ "$machine_status" != "Running" ]; then
docker-machine start $vm_name
fi;
# Set up the environment variables so that the Docker client can
# connect to the VM.
eval $(docker-machine env $vm_name)
# Forward the VM port to localhost
if ((vm_port_forwarding)); then
VBoxManage controlvm $vm_name natpf1 delete django
# Only using VirtualBox... for now
VBoxManage controlvm $vm_name natpf1 "django,tcp,127.0.0.1,$host_port,,$host_port"
fi;
elif [ $(uname) == "Darwin" ]; then
# In case the user installed Docker separately
echo "You're running OS X, so you should install Docker Machine (part of Docker Toolbox)."
download_docker_toolbox
echo "Please re-run this script once you've installed Docker Toolbox."
exit 1
fi
if ((!force_run || stop_running)); then
# Determine if the container is already running:
image_name_esc=$(echo "$image_name" | sed -n 's/\([\/.?]\)/\\\1/pg')
container_id=$(docker ps | awk "/$image_name_esc/ { if (match(\$2, /^$image_name_esc/)) print \$1 }" | head -n 1)
else
container_id=""
fi;
if [ -n "$container_id" ]; then
if ((stop_running)); then
echo "Stopping container: $container_id"
docker stop $container_id
container_id=""
else
echo "There's a $image_name container already running with id $container_id."
echo "If you want to start a new container, re-run ${BASH_SOURCE[0]} -r."
fi
fi
if [ -n "$container_id" ]; then
# Found a container.
# If it's paused, unpause it:
is_paused=$(docker inspect --format='{{.State.Paused}}' $container_id)
if [ $is_paused = "true" ]; then
docker unpause $container_id
fi;
# Attach to it:
echo "Attaching to running container ($container_id)."
docker exec -it $container_id $run_command
else
echo "Starting container..."
env_opts=""
# Build the environment options:
for setting in $docker_environment; do
env_opts="$env_opts -e $setting"
done
if [ -f $env_file ]; then
env_opts="$env_opts --env-file=$env_file"
fi
if ((remove_after)); then
docker_opts="$docker_opts --rm"
fi
docker_opts="$docker_opts -it -v $project_dir/server:/app -v $project_dir/client:/client -v $project_dir/data:/data -v $project_dir/docker-support:/support -p $host_port:$host_port $env_opts $image_name"
if ((!autostart)); then
docker run $docker_opts $run_command
else
# Start the server:
container_id=$(docker run -d $docker_opts /app/start.sh)
echo "Container started with id $container_id."
# Attach to the container, and we're done
docker exec -it $container_id $run_command
fi
fi;
if [ $? -eq 1 ]; then
# TODO: Is it possible to distinguish between a failure to start the
# container and a timeout? Both apparently return exit code 1.
exit
fi;