forked from johnfonner/abaco-cli
-
Notifications
You must be signed in to change notification settings - Fork 3
/
abaco-deploy.sh
executable file
·413 lines (365 loc) · 10.3 KB
/
abaco-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
402
403
404
405
406
407
408
409
410
411
412
413
#!/bin/bash
THIS=$(basename $0)
THIS=${THIS%.sh}
THIS=${THIS//[-]/ }
HELP="
Usage: ${THIS} [OPTION]...
Build and deploy an Abaco actor from a local project directory.
Requires Docker version 17.03.0-ce or higher, push access to a
Docker registry, and a properly-configured source directory.
Options:
-h show help message
-z oauth access token
-F Docker file (Dockerfile)
-B build config file (reactor.rc)
-O override DOCKER_HUB_ORG and ENV
-c override file REACTOR_IMAGE_TAG
-t override file REACTOR_IMAGE_VERSION
-s override REACTOR_STATEFUL to deploy the actor as stateless
-S override REACTOR_STATEFUL to deploy the actor as stateful
-A override REACTOR_TOKENS to deploy actor without Tapis tokens
-H include a hint during actor creation
-p don't pull source image when building
-k bypass Docker cache when building
-K skip pushing to the container registry
-U update preexisting actor (provided or from .ACTOR_ID)
-D display only (do not cache actor ID on the host)
-R dry run - build container image and stop
"
function usage() {
echo "$HELP"
exit 0
}
DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
source "$DIR/abaco-common.sh"
function get_actorid() {
local actorid="$1"
# If not a valid actor ID, try to get from .ACTOR_ID
if [[ $actorid == -* ]] || [ -z "$actorid" ]; then
if [ -s ".ACTOR_ID" ]; then
actorid=$(cat .ACTOR_ID)
if [[ "$actorid" == "null" ]]; then
actorid=
fi
else
actorid=
fi
fi
echo "$actorid"
}
dockerfile="Dockerfile"
config_rc="reactor.rc"
entrypoint="reactor.py"
default_env="secrets.json"
# allow inheritance from environment
passed_docker_org="${DOCKER_HUB_ORG}"
passed_image_name=
passed_image_tag=
tok=
dry_run=
no_push=0
no_build=${no_push}
dopull=1
nocache=0
displayonly=0
stateful=0
varstateful=
optstateful=
opt_no_token=0
no_tokens=0
verbose=0
current_actor=
while getopts ":hz:F:B:RH:pkUkKsvSDAO:c:t:" o; do
case "${o}" in
z) # API token
tok=${OPTARG}
;;
F) # Dockerfile
dockerfile=${OPTARG}
;;
B) # reactor build config
config_rc=${OPTARG}
;;
O) # docker hub username or org
passed_docker_org=${OPTARG}
;;
c) # docker repo name
passed_image_name=${OPTARG}
;;
H) # hint (string)
hint=${OPTARG}
;;
t) # docker repo tag
passed_image_tag=${OPTARG}
;;
z) # API token
tok=${OPTARG}
;;
K) # don't push to registry
no_push=1
;;
R) # dry run
dry_run=1
;;
p) # no pull
dopull=0
;;
k) # no use Docker cache
nocache=1
;;
s) # stateless
optstateful=0
;;
S) # stateful
optstateful=1
;;
A) # disable tokens
opt_no_token=1
;;
D) # display only
displayonly=1
;;
U) # update
current_actor=$(get_actorid "${@:$OPTIND:1}")
if [ -z "$current_actor" ]; then
warn "Actor ID not found. Creating new actor."
else
info "Updating actor $current_actor"
fi
;;
v) # verbose
verbose=1
;;
h | *) # print help text
usage
;;
esac
done
shift $((OPTIND - 1))
# No point building container again if we are not pushing it
if ((no_push)); then
no_build=1
fi
if [ ! -z "$tok" ]; then TOKEN=$tok; fi
if [[ "$very_verbose" == "true" ]]; then
verbose="true"
fi
# Check for mandatory files
for mandfile in $dockerfile $config_rc $entrypoint; do
if [ ! -f "$mandfile" ]; then
die "Cannot proceed without file $mandfile"
fi
done
# Look for config.yml and regenerate if not there
if [ ! -f "config.yml" ]; then
info "File config.yml was not found. Creating an empty one."
# Template out the reactor.rc file
cat <<EOF >config.yml
# Reactors config file
---
EOF
fi
# Look for message.jsonschema and generate a generic one
if [ ! -f "message.jsonschema" ]; then
info "File message.jsonschema was not found. Creating one just validates JSON."
# Template out the schema file
cat <<EOF >{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "Abaco Message",
"description": "A generic Abaco JSON message",
"type": "object"
}
EOF
fi
# Look for optional files
for optfile in secrets.json; do
if [ ! -f "$optfile" ]; then
info "Optional file $optfile not present"
fi
done
# Check existence and min version of Docker
command -v docker >/dev/null 2>&1 || { die "Docker is not installed or accessible"; }
DOCKER_VERSION="$(docker --version)"
if [[ ! "$DOCKER_VERSION" =~ "Docker version 17" ]] && [[ ! "$DOCKER_VERSION" =~ "Docker version 18" ]] && [[ ! "$DOCKER_VERSION" =~ "Docker version 19" ]] && [[ ! "$DOCKER_VERSION" =~ "Docker version 20" ]]; then
die "${DOCKER_VERSION} is not recent enough."
fi
# Verify the user is logged into a Registry
# This isn't formal validation that they can
# push to one, but is a decent sanity check for
# users who don't yet know about docker login
DOCKER_AUTHS="$(jq -r .auths $HOME/.docker/config.json)"
if [[ "${DOCKER_AUTHS}" == "{}" ]]; then
warn "You don't appear to be logged into a Docker Registry. If the next steps fail, please run 'docker login' and then redeploy."
fi
# Allow default set in ENV
ENV_DOCKER_HUB_ORG="${DOCKER_HUB_ORG}"
# Read in config variables
REACTOR_NAME=
REACTOR_DESCRIPTION=
REACTOR_STATEFUL=
REACTOR_PRIVILEGED=
REACTOR_USE_UID=
REACTOR_TOKENS=
# Docker image
DOCKER_HUB_ORG=
DOCKER_IMAGE_TAG=
DOCKER_IMAGE_VERSION=
set -a
source "${config_rc}"
set +a
# Validate that the ones that are not supposed to be empty... aren't empty
# Automatically assign values where we can
if [ -z "${DOCKER_HUB_ORG}" ] || [ "${DOCKER_HUB_ORG}" == "your_docker_registory_uname" ]; then
if [ ! -z "${ENV_DOCKER_HUB_ORG}" ]; then
DOCKER_HUB_ORG="${ENV_DOCKER_HUB_ORG}"
export DOCKER_HUB_ORG
else
die "DOCKER_HUB_ORG is your DockerHub username or organization. Set in ENV or $config_rc or pass via -O"
fi
fi
if [ ! -z "${passed_image_name}" ]; then
DOCKER_IMAGE_TAG="${passed_image_name}"
fi
if [ -z "${DOCKER_IMAGE_TAG}" ]; then
die "DOCKER_IMAGE_TAG cannot be empty. Set it in $config_rc or pass via -c"
fi
# Reactor values
if [ -z "${REACTOR_NAME}" ]; then
warn "REACTOR_NAME is empty so we're naming it for you. Don't you love your Reactor?"
source "${DIR}/libs/petname.sh"
export REACTOR_NAME=$(petname 3)
echo "${REACTOR_NAME}"
fi
# Whether to automatically request API tokens
if [ "${REACTOR_TOKENS}" == "1" ]; then
# Read default from config file
# Do not disable in abaco create/update
no_tokens=0
fi
# If CLI option was passed, disable in abaco create/update
if ((opt_no_token)); then
no_tokens=1
fi
# Read STATEFUL from config
if [ "${REACTOR_STATEFUL}" == "1" ]; then
stateful=1
else
stateful=0
fi
# Override STATEFUL=1 set in config.rc when the -s option is actively passed
if ((stateful)) && [[ ! -z "$optstateful" ]] && ! ((optstateful)); then
stateful=0
# Override STATEFUL= or STATEFUL=0 when -S option is passed
elif ! ((stateful)) && [[ ! -z "$optstateful" ]] && ((optstateful)); then
stateful=1
fi
# Docker stuff
DOCKER_BUILD_TARGET="${DOCKER_HUB_ORG}/${DOCKER_IMAGE_TAG}"
if [ ! -z "${passed_image_tag}" ]; then
DOCKER_IMAGE_VERSION=${passed_image_tag}
fi
# Force image to have a :version
if [ -z "${DOCKER_IMAGE_VERSION}" ]; then
DOCKER_IMAGE_VERSION="latest"
echo "Defaulting to ${DOCKER_IMAGE_TAG}:latest"
fi
if ((git_cli_avail)); then
if [[ "${DOCKER_USE_COMMIT_HASH}" == "1" ]]; then
GIT_COMMIT_HASH=$(git rev-parse --short HEAD 2>/dev/null)
if [[ ! -z "{GIT_COMMIT_HASH}" ]]; then
DOCKER_IMAGE_VERSION=${GIT_COMMIT_HASH}
fi
fi
fi
if [ ! -z "${DOCKER_IMAGE_VERSION}" ]; then
DOCKER_BUILD_TARGET="${DOCKER_BUILD_TARGET}:${DOCKER_IMAGE_VERSION}"
else
warn "It is considered a best practice to specify a version for a Docker image"
warn "Do this by setting DOCKER_IMAGE_VERSION in $config_rc or passing via -t"
fi
export DOCKER_BUILD_TARGET
# Try Docker build
buildopts="--rm=true"
if ((dopull)); then
buildopts="${buildopts} --pull"
fi
if ((nocache)); then
buildopts="${buildopts} --no-cache"
fi
info " Build Options: ${buildopts}"
if ((verbose)); then
build_cmd="docker -l warn build ${buildopts} -f ${dockerfile} -t ${DOCKER_BUILD_TARGET}"
info "${build_cmd}"
fi
if !((no_build)); then
docker -l warn build ${buildopts} -f "${dockerfile}" -t "${DOCKER_BUILD_TARGET}" . || { die "Error building ${DOCKER_BUILD_TARGET}"; }
fi
if [ "$dry_run" == 1 ]; then
info "Stopping deployment as this was only a dry run!"
exit 0
fi
# Push to container registry
if ! ((no_push)); then
info "Pushing ${DOCKER_BUILD_TARGET} to container registry."
docker push "${DOCKER_BUILD_TARGET}" || { die "Error pushing image to container registry"; }
info "Finalizing the push action..."
sleep 2
info "Done"
else
info "Skipped pushing to container registry."
fi
# Build abaco create/update CLI, then call it
ABACO_CREATE_OPTS="-f"
if [ "${REACTOR_PRIVILEGED}" == "1" ]; then
ABACO_CREATE_OPTS="$ABACO_CREATE_OPTS -p"
fi
if [ "${REACTOR_USE_UID}" == "1" ]; then
ABACO_CREATE_OPTS="$ABACO_CREATE_OPTS -u"
fi
# Pass -A to create if token issuance is to be disabled
if ((no_tokens)); then
ABACO_CREATE_OPTS="$ABACO_CREATE_OPTS -A"
fi
# Add a hint if one was given
if [ -n "$hint" ]; then
ABACO_CREATE_OPTS="$ABACO_CREATE_OPTS -H '$hint'"
fi
# If updating, do not include name or stateless
if [ -z "$current_actor" ]; then
ABACO_CREATE_OPTS="$ABACO_CREATE_OPTS -n ${REACTOR_NAME}"
if ((stateful)); then
ABACO_CREATE_OPTS="$ABACO_CREATE_OPTS -S"
else
ABACO_CREATE_OPTS="$ABACO_CREATE_OPTS -s"
fi
fi
# Read default environment variables from secrets.json
# This file never committed to Git or Docker image
if [ -f "${default_env}" ]; then
info "Reading environment variables from ${default_env}"
ABACO_CREATE_OPTS="$ABACO_CREATE_OPTS -E ${default_env}"
fi
if [ -f .ACTOR_ID ]; then
mv .ACTOR_ID .ACTOR_ID.bak
fi
if [ -z "$current_actor" ]; then
cmd="abaco create -v ${ABACO_CREATE_OPTS} ${DOCKER_BUILD_TARGET}"
else
cmd="abaco update -v ${ABACO_CREATE_OPTS} ${current_actor} ${DOCKER_BUILD_TARGET}"
fi
if ((verbose)); then
info "${cmd}"
fi
# Prepend directory to allow for multiple concurrent CLI to coexist
RESP=$(eval ${DIR}/${cmd})
ACTOR_ID=$(echo ${RESP} | jq -r .result.id)
if [[ "$ACTOR_ID" == "null" ]]; then
die "Failed to deploy actor $REACTOR_NAME"
fi
# Write cache
if ! ((displayonly)); then
echo -n "${ACTOR_ID}" >.ACTOR_ID
fi
echo "Successfully deployed actor with ID: $ACTOR_ID"
exit 0
# TODO: Add/update the alias registry if provided