-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeploy.sh
executable file
·306 lines (255 loc) · 10.2 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
#!/bin/bash
# Deployment script for sdt-validation-server-api
# 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
;;
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 "destroy: $destroy"
echo "import: $import"
echo "check: $check"
#else
# echo "-----------------------------------------------------"
# echo "Docker not installed - no restart is possible"
# echo "-----------------------------------------------------"
#fi
#Open Docker, only if is not running
if (! docker stats --no-stream ); then
sudo service docker start;
#Wait until Docker daemon is running and has completed initialisation
while (! docker stats --no-stream ); do
# Docker takes a few seconds to initialize
echo "Waiting for Docker to launch..."
sleep 1
done
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 "Set env vars."
echo "-----------------------------------------------------"
# set .env vars
if (($environment == "staging"))
then
export $(grep -v '^#' .env.stg | xargs)
else (($environment == "production"))
export $(grep -v '^#' .env.prod | xargs)
fi
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 app 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-deploy.yml" ]; then
# Build Containers
echo "-----------------------------------------------------"
echo "Building containers"
echo "-----------------------------------------------------"
docker-compose -f docker-compose-deploy.yml build
# Prune Containers, Images, and Networks
echo "-----------------------------------------------------"
echo "Pruning containers, images, and networks"
echo "-----------------------------------------------------"
docker container prune -f
docker image prune -f
docker network prune -f
# 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 -f docker-compose-deploy.yml up --remove-orphans
echo "-----------------------------------------------------"
echo "Running as interactive allows changes in code to compile on the server"
echo "-----------------------------------------------------"
else
echo "-----------------------------------------------------"
echo "Running as detached so create superuser and checks will run"
echo "-----------------------------------------------------"
docker-compose -f docker-compose-deploy.yml up -d --remove-orphans
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 app python manage.py createsuperuser --noinput
echo "-----------------------------------------------------"
echo "Create static files"
echo "-----------------------------------------------------"
docker exec app python manage.py collectstatic --noinput
echo "-----------------------------------------------------"
echo "Import mysql data"
echo "-----------------------------------------------------"
docker exec app bash ../scripts/import_mysql_backup.sh
# Run tests
echo "-----------------------------------------------------"
echo "Run tests"
echo "-----------------------------------------------------"
docker exec app python manage.py test
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 app pep8 --show-source --show-pep8 testsuite/E40.py
exec -it app 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 app safety check --json -r requirements.txt
echo "-----------------------------------------------------"
echo "Version check"
echo "-----------------------------------------------------"
docker exec -it app 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 app python manage.py check --deploy
echo "-----------------------------------------------------"
echo "Bandit Security check"
echo "-----------------------------------------------------"
docker exec -it app bandit -r $APP_DIR/
echo "-----------------------------------------------------"
echo "License check"
echo "-----------------------------------------------------"
docker exec -it app 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
if (($environment == "staging"))
then
unset $(grep -v '^#' .env.stg | sed -E 's/(.*)=.*/\1/' | xargs)
else (($environment == "production"))
unset $(grep -v '^#' .env.prod | sed -E 's/(.*)=.*/\1/' | xargs)
fi