-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdeploy.sh
executable file
·218 lines (176 loc) · 6 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
#!/usr/bin/env bash
# //////////////////////////////////////////////////////////////////////////////
#
#
# //////////////////////////////////////////////////////////////////////////////
# Exit the script as soon as something fails (-e) or if a variable is not defined (-u)
set -e -u
function info () {
echo "##############################################"
echo "# #"
echo "# Project: ${PROJECT_NAME} ver. ${PROJECT_VERSION} #"
echo "# #"
echo "##############################################"
echo "Environment: ${ENV}"
echo "ECR Registry: ${AWS_REGISTRY}"
echo "BATCH JOB QUEUE: ${AWS_JOB_QUEUE}"
echo "DB: ${POSTGRES_DB}"
echo "S3 Bucket: ${S3_BUCKET}"
python --version
pyenv --version
pip --version
}
function sync_to_s3 () {
echo "##############################################"
echo "# #"
echo "# Uploading changes to s3://${S3_BUCKET} "
echo "# #"
echo "##############################################"
aws s3 sync experiments/ s3://${S3_BUCKET}/experiments
}
function sync_from_s3 () {
echo "##############################################"
echo "# #"
echo "# Getting changes from s3://${S3_BUCKET} "
echo "# #"
echo "##############################################"
aws s3 sync s3://${S3_BUCKET}experiments/ experiments
}
function update_jobs () {
tasks=triage
echo "Updating the job definition of the following tasks: ${tasks}"
for task in ${tasks}
do
echo "+----------------------------------------+"
echo "| |"
echo "| Updating ${task} job definition"
echo "| |"
echo "+----------------------------------------+"
aws batch register-job-definition --cli-input-json file://infrastructure/${task}-job-definition.json
done
}
function update_images () {
tasks=triage
echo "Updating the image of the following tasks: ${tasks}"
for task in ${tasks}
do
echo "+----------------------------------------+"
echo "| |"
echo "| Updating ${task} image"
echo "| |"
echo "+----------------------------------------+"
docker build --no-cache --tag dsapp/${PROJECT_NAME}/${task} infrastructure/
docker tag dsapp/${PROJECT_NAME}/${task} ${AWS_REGISTRY}/dsapp/${PROJECT_NAME}/${task}:${PROJECT_VERSION}
docker tag dsapp/${PROJECT_NAME}/${task} ${AWS_REGISTRY}/dsapp/${PROJECT_NAME}/${task}:latest
eval "$(aws ecr get-login --no-include-email --region us-west-2)"
docker push "${AWS_REGISTRY}"/dsapp/"${PROJECT_NAME}"/${task}:"${PROJECT_VERSION}"
docker push "${AWS_REGISTRY}"/dsapp/"${PROJECT_NAME}"/${task}:latest
done
}
function run_experiment () {
environment_overrides=$1
echo "Using environment_overrides: ${environment_overrides}"
parameters=$2
echo "Using parameters: ${parameters}"
command_overrides=${@:3}
# # Retrieve temporary session credentials for current user
session=$(aws sts get-session-token --duration-seconds 129600) # 36 h
# # Restructure these to mirror pipeline overrides
creds=$(<<<"$session" jq -f infrastructure/credentials.filter)
# # Merge these AWS session credentials into *all* pipeline overrides
overrides=$(
< ${environment_overrides} \
jq --arg creds "$creds" \
'.environment += ($creds|fromjson|.environment)'
)
if [ ! -z "$command_overrides" ]
then
echo "Adding ${command_overrides} to the command"
for cmd in ${command_overrides}
do
overrides=$(echo $overrides | jq --arg cmds "${cmd}" \
'.command |= .+ [$cmds]')
done
fi
echo "Final overrides: ${overrides}"
aws batch submit-job --job-queue ${AWS_JOB_QUEUE} \
--job-name ${PROJECT_NAME}-batch \
--job-definition ${PROJECT_NAME}-run-experiment \
--container-overrides "${overrides}" \
--parameters "${parameters}"
}
function run() {
run_experiment infrastructure/triage-overrides.json $@
}
function all () {
echo "Running everything!"
update-task-image
update-task-job
run $@
}
function help_menu () {
cat << EOF
Usage: ${0} (-h | -i | -u | -b | -r | -a | --sync_{to,from}_s3 )
OPTIONS:
-h|--help Show this message
-i|--info Show information about the environment
-b|--update-images Build the ${PROJECT_NAME}'s triage image and push it to the AWS ECR
-u|--update-jobs Update the ${PROJECT_NAME}'s triage job definition in AWS Batch
-r|--run-experiment Run experiments on ${PROJECT_NAME} data
-a|--all Creates images, pushes them the registry, updates the jobs and runs the pipeline
--sync-to-s3 Uploads the experiments and configuration files to s3://${S3_BUCKET}
--sync-from-s3 Gets the experiments and configuration files from s3://${S3_BUCKET}
EXAMPLES:
Build and push the images to your AWS ECR:
$ ./deploy.sh -b
Update the job's definitions:
$ ./deploy.sh -u
Run triage experiments:
$ ./deploy.sh -r --experiment_file=s3://${S3_BUCKET}/experiments/test.yaml,output_path=s3://${S3_BUCKET}/triage,replace=--replace
Everything!:
$ ./deploy.sh -a --experiment_file=s3://${S3_BUCKET}/experiments/test.yaml,output_path=s3://${S3_BUCKET}/triage,replace=--replace
EOF
}
if [[ $# -eq 0 ]] ; then
help_menu
exit 0
fi
# Deal with command line flags.
case "${1}" in
-b|--update-images)
update_images
shift
;;
-u|--update-jobs)
update_jobs
shift
;;
-r|--run-experiment)
run ${@:2}
shift
;;
-a|--all)
all
shift
;;
-i|--info)
info
shift
;;
--sync-from-s3)
sync_from_s3
shift
;;
--sync-to-s3)
sync_to_s3
shift
;;
-h|--help)
help_menu
shift
;;
*)
echo "${1} is not a valid flag, try running: ${0} --help"
;;
esac
shift