-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeploy.sh
executable file
·401 lines (338 loc) · 13.7 KB
/
deploy.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
#!/bin/bash
# Deployment script for tax calculator
# Purpose: copies the correct docker-compose, requirements.txt, and .env file to
# the correct locations and then runs the docker commands and initialization
# scripts.
# Command: ./deploy.sh [environment]] -[destroy] -[restart] > deployment.log
# Options: environment = development, staging, or production
# -destroy = delete all containers and images before Building
# -restart = restart docker-machine before building Containers
# > deployment.log = write starttup messages to a file 'deployment.log'
#
echo "-----------------------------------------------------"
echo "Starting build: $(date)"
echo "-----------------------------------------------------"
SECONDS=0
while getopts "e:m:r:d:i:c:o:" option; do
case $option in
e ) env=$OPTARG
;;
m ) machine_name=$OPTARG
;;
r ) restart=$OPTARG
;;
d ) destroy=$OPTARG
;;
i ) import=$OPTARG
;;
c ) check=$OPTARG
;;
o ) options=$OPTARG
;;
esac
done
if [[ "$destroy" == "destroy" ]]
then
destroy=1
else
destroy=0
fi
if [[ "$restart" == "restart" ]]
then
restart=1
else
restart=0
fi
if [[ "$import" == "import" ]]
then
import=1
else
import=0
fi
if [[ "$check" == "check" ]]
then
check=1
else
check=0
fi
if [[ "$options" == "interactive" ]]
then
interactive=1
else
interactive=0
fi
if [[ "$env" == "prod" ]]
then
environment="production"
elif [[ "$env" == "stg" ]]
then
environment="staging"
elif [[ "$env" == "dev" ]]
then
environment="development"
else
environment=$env
fi
echo "-----------------------------------------------------"
echo "Inputs from command"
echo "-----------------------------------------------------"
echo "env: $env"
echo "environment: $environment"
echo "machine_name: $machine_name"
echo "restart: $restart"
echo "destroy: $destroy"
echo "import: $import"
echo "check: $check"
has_docker_machine=$(which docker-machine)
if [ -z "$has_docker_machine" ]
then
docker_machine=0
else
docker_machine=1
fi
echo "has docker machine: $has_docker_machine"
echo "docker machine flag: $docker_machine"
echo "docker machine condition: (($docker_machine != 0 ))"
# check arguments
if [ -z "$environment" ]
then
echo "No environment was passed. Specify development, staging, or production."
exit 1 # terminate and indicate error
fi
if [ -z "$machine_name" ]
then
echo "No machine_name was passed, using default as docker-machine."
machine_name=default
fi
# Is docker machine running
echo "Check if Docker-machine is running..."
if (($docker_machine == 1 ))
then
docker_running=$(docker-machine status $machine_name)
echo "-----------------------------------------------------"
echo "Docker-machine status for $machine_name: $docker_running"
echo "-----------------------------------------------------"
if [[ "$docker_running" == *"Running"* ]]
then
echo "-----------------------------------------------------"
echo "Docker-machine is running...set environment variables."
echo "-----------------------------------------------------"
eval "$(docker-machine env $machine_name)"
fi
if [[ "$docker_running" == *"Stopped"* ]]
then
echo "-----------------------------------------------------"
echo "Docker-machine is stopped...start machine."
echo "-----------------------------------------------------"
$(docker-machine start $machine_name)
echo "-----------------------------------------------------"
echo "Set environment variables."
echo "-----------------------------------------------------"
eval "$(docker-machine env $machine_name)"
fi
if [[ "$docker_running" == *"Saved"* ]]
then
echo "-----------------------------------------------------"
echo "Docker-machine is Saved...start machine."
echo "-----------------------------------------------------"
$(docker-machine start $machine_name)
echo "-----------------------------------------------------"
echo "Set environment variables."
echo "-----------------------------------------------------"
eval "$(docker-machine env $machine_name)"
fi
echo "-----------------------------------------------------"
echo "Checking if destroy command was passed"
echo "-----------------------------------------------------"
if (($destroy != 0 ))
then
echo "-------------------------------------------------------------------"
echo "Destroy was passed -- clearing out existing containers and images."
echo "-------------------------------------------------------------------"
eval $(docker stop $(docker ps -a -q) && docker rm $(docker ps -a -q) --force && docker rmi $(docker images -a -q) --force)
else
echo "-----------------------------------------------------"
echo "Destroy was not passed -- using containers."
echo "-----------------------------------------------------"
fi
echo "-----------------------------------------------------"
echo "Checking if restart command was passed"
echo "-----------------------------------------------------"
if (($restart != 0 ))
then
echo "-------------------------------------------------------------------"
echo "Restart was passed -- restart docker machine."
echo "-------------------------------------------------------------------"
eval $(docker-machine restart $machine_name)
else
echo "-----------------------------------------------------"
echo "Restart was not passed -- using existing docker machine."
echo "-----------------------------------------------------"
fi
else
echo "-----------------------------------------------------"
echo "Docker machine not installed - no restart is possible"
echo "-----------------------------------------------------"
fi
# Set ENV variables
# Copy assets from deployment directories to execute directory
if [ ! -d "./envs/$environment" ]; then
echo "./envs/$environment directory does not exist."
exit 1 # terminate and indicate error
fi
if [ ! -f "./envs/$environment/docker-compose.yml" ]; then
echo "$environment directory or docker-compose.yml do not exist."
exit 1 # terminate and indicate error
fi
echo "-----------------------------------------------------"
echo "copying nginx files for $environment "
echo "-----------------------------------------------------"
echo "Loading $environment scripts"
mkdir ./nginx
cp -fr ./envs/$environment/nginx/sites-enabled ./nginx/
cp -fr ./envs/$environment/nginx/Dockerfile ./nginx/Dockerfile
cp -fr ./envs/$environment/nginx/nginx.conf ./nginx/nginx.conf
cp -fr ./envs/$environment/docker-compose.yml ./docker-compose.yml
cp -fr ./envs/$environment/requirements.txt ./requirements.txt
cp -fr ./envs/$environment/nginx/ssl ./nginx/
echo "-----------------------------------------------------"
if [ ! -f "./docker-compose.yml" ]; then
echo "docker-compose.yml does not exist. Was it copied?"
exit 1 # terminate and indicate error
fi
# set .env vars
export $(grep -v '^#' .env | xargs)
if (($import != 0 ))
then
# Create DB Structure
echo "-----------------------------------------------------"
echo "Copy database dump to ./django-rest-app/scripts/mysql-dump"
echo "Data will be imported when container is created"
echo "-----------------------------------------------------"
cp -fr ./$APP_DIR/scripts/$MYSQL_DATABASE_CREATE_SQL ./django-rest-app/scripts/mysql-dump/$MYSQL_DATABASE_CREATE_SQL
else
echo "-----------------------------------------------------"
echo "Database dump was removed from ./django-rest-app/scripts/mysql-dump"
echo "No database created or updated because 'I' flag not passed"
echo "-----------------------------------------------------"
rm -rf ./$APP_DIR/scripts/mysql-dump/$MYSQL_DATABASE_CREATE_SQL
fi
echo "-----------------------------------------------------"
echo "Export mysql data"
echo "-----------------------------------------------------"
#docker exec web python manage.py dumpdata users authtoken v1 --output mydata.json
docker exec mysql bash ./scripts/export_mysql_backup.sh
#exit 1 # terminate and indicate error
if [ -f "./docker-compose.yml" ]; then
docker_ip=$(docker-machine ip $machine_name)
echo "-----------------------------------------------------"
echo "Starting containers on $docker_ip"
echo "-----------------------------------------------------"
# Build Containers
echo "-----------------------------------------------------"
echo "Building containers"
echo "-----------------------------------------------------"
docker-compose build
# Start Containers
echo "-----------------------------------------------------"
echo "Starting containers"
echo "-----------------------------------------------------"
if (($interactive != 0 ))
then
echo "-----------------------------------------------------"
echo "Running as interactive so create superuser and checks will not run"
echo "-----------------------------------------------------"
docker-compose up --remove-orphans
echo "-----------------------------------------------------"
echo "Running as interactive allows changes in code to compile on server"
echo "-----------------------------------------------------"
else
echo "-----------------------------------------------------"
echo "Running as detached so create superuser and checks will run"
echo "-----------------------------------------------------"
docker-compose up -d --remove-orphans
docker system prune --all --volumes --force
echo "-----------------------------------------------------"
echo "Running as detached means rebuilding containers to recompile code"
echo "-----------------------------------------------------"
echo "-----------------------------------------------------"
echo "Pause to allow things to come up"
echo "-----------------------------------------------------"
sleep 15
# Initialize Application
echo "-----------------------------------------------------"
echo "Create superuser"
echo "-----------------------------------------------------"
#docker exec -tt web python manage.py createsuperuser --noinput
echo "-----------------------------------------------------"
echo "Create static files"
echo "-----------------------------------------------------"
docker exec web python manage.py collectstatic --noinput
echo "-----------------------------------------------------"
echo "Import mysql data"
echo "-----------------------------------------------------"
docker exec mysql bash ./scripts/import_mysql_backup.sh
docker_ip=$(docker-machine ip $machine_name)
echo "-----------------------------------------------------"
echo "Containers are running on $docker_ip"
echo "-----------------------------------------------------"
# Run tests
echo "-----------------------------------------------------"
echo "Run tests"
echo "-----------------------------------------------------"
fi
minutes=$((SECONDS/60))
seconds=$((SECONDS%60))
echo "-----------------------------------------------------"
echo "Ending build: $(date)"
echo "Build took $minutes minutes and $seconds seconds."
echo "-----------------------------------------------------"
# These are dependency and security checks that should be run on each build.
# Any security issues should be mitagated or a description of why they are
# not relevant should be included below.
if (($check != 0 ))
then
echo "-----------------------------------------------------"
echo "PEP8 checks"
echo "-----------------------------------------------------"
exec -it web pep8 --show-source --show-pep8 testsuite/E40.py
exec -it web pep8 --statistics -qq Python-3.6/Lib
echo "-----------------------------------------------------"
echo "Dependency checks"
echo "Only works with versioned packages check"
echo "-----------------------------------------------------"
echo "Dependency Security check"
echo "-----------------------------------------------------"
docker exec -it web safety check --json -r requirements.txt
echo "-----------------------------------------------------"
echo "Version check"
echo "-----------------------------------------------------"
docker exec -it web pip-check -a -H
# Any security issues should be mitagated or a description of why they are
# not relevant should be inccluded below.
echo "-----------------------------------------------------"
echo "Django Security check"
echo "-----------------------------------------------------"
docker exec -it web python manage.py check --deploy
echo "-----------------------------------------------------"
echo "Bandit Security check"
echo "-----------------------------------------------------"
docker exec -it web bandit -r $APP_DIR/
echo "-----------------------------------------------------"
echo "License check"
echo "-----------------------------------------------------"
docker exec -it web pip-licenses --with-system --with-urls --order=license
else
echo "-----------------------------------------------------"
echo "No security or version checks were done"
echo "-----------------------------------------------------"
fi
minutes=$((SECONDS/60))
seconds=$((SECONDS%60))
echo "-----------------------------------------------------"
echo "Ending build: $(date)"
echo "Build took $minutes minutes and $seconds seconds."
echo "-----------------------------------------------------"
fi # end running detached
# unset .env vars
unset $(grep -v '^#' .env | sed -E 's/(.*)=.*/\1/' | xargs)