forked from johnfonner/abaco-cli
-
Notifications
You must be signed in to change notification settings - Fork 3
/
abaco-update.sh
executable file
·151 lines (133 loc) · 3.86 KB
/
abaco-update.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
#!/bin/bash
THIS=$(basename $0)
THIS=${THIS%.sh}
THIS=${THIS//[-]/ }
HELP="
Usage: ${THIS} [OPTION]... [ACTORID | ALIAS] [IMAGE]
Updates an actor. State status and actor name
cannot be changed. Actor ID/alias and Docker image
required.
Options:
-h show help message
-z oauth access token
-e set environment variables (key=value)
-E read environment variables from json file
-H include a hint during actor creation
-p add privileged status
-A disable creation of Tapis tokens
-u use actor uid
-f force update
-v verbose output
-V very verbose output
"
# function usage() { echo "$0 usage:" && grep " .)\ #" $0; exit 0; }
function usage() {
echo "$HELP"
exit 0
}
DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
source "$DIR/abaco-common.sh"
privileged="false"
stateless="true"
force="false"
use_uid="false"
request_token="true"
tok=
# the s and S opts are here to swallow them when passed - we do not allow
# toggling between stateless and stateful via an update operation
while getopts ":he:E:H:pfsSAuvz:V" o; do
case "${o}" in
z) # custom token
tok=${OPTARG}
;;
e) # default environment (command line key=value)
env_args[${#env_args[@]}]=${OPTARG}
;;
E) # default environment (json file)
env_json=${OPTARG}
;;
H) # hint (string)
hint=${OPTARG}
;;
p) # privileged
privileged=true
;;
f) # force
force=true
;;
s) # stateless
stateless="true"
;;
S) # stateful!
stateless="false"
;;
A) # do not request tokens
request_token="false"
;;
u) # use uid
use_uid=true
;;
v) # verbose
verbose="true"
;;
V) # verbose
very_verbose="true"
;;
h | *) # print help text
usage
;;
esac
done
shift $((OPTIND - 1))
actorid="$1"
image="$2"
if [ ! -z "$tok" ]; then TOKEN=$tok; fi
if [[ "$very_verbose" == "true" ]]; then
verbose="true"
fi
# fail if no actorid or image
if [ -z "$actorid" ] || [ -z "$image" ]; then
echo "Please specify an actor ID and a Docker image"
usage
fi
# build the hint argument if needed
hintJSON=
if [ -n "$hint" ]; then
# put quotes around $hint if it is not a dictionary of terms.
if [ ! "${hint:0:1}" == "[" ]; then
hint="\"$hint\""
fi
hintJSON=", \"hints\": ${hint}"
fi
# default env
# check env vars json file (exists, have contents, be json)
file_default_env=
if [ ! -z "$env_json" ]; then
if [ ! -f "$env_json" ] || [ ! -s "$env_json" ] || ! $(is_json $(cat $env_json)); then
die "$env_json is not valid. Please ensure it exists and contains valid JSON."
fi
file_default_env=$(cat $env_json)
fi
# build command line env vars into json
args_default_env=$(build_json_from_array "${env_args[@]}")
#combine both
default_env=$(echo "$file_default_env $args_default_env" | jq -s add)
# WORKAROUND - fetch stateless from actor records.
# TODO - File Issue: Stateless should not be mandatory for an update operation
stateless=$(${DIR}/abaco ls -v ${actorid} | jq -r .result.stateless)
# curl command
# \"stateless\":\"${stateless}\",
data="{\"image\":\"${image}\", \"privileged\":${privileged}, \"stateless\":${stateless}, \"force\":${force}, \"useContainerUid\":${use_uid}, \"defaultEnvironment\":${default_env}, \"token\":${request_token}${hintJSON}}"
curlCommand="curl -X PUT -sk -H \"Authorization: Bearer $TOKEN\" -H \"Content-Type: application/json\" --data '$data' '$BASE_URL/actors/v2/${actorid}'"
function filter() {
# eval $@ | jq -r '.result | [.name, .id] | @tsv' | column -t
eval $@ | jq -r '.result | [.name, .id] | "\(.[0]) \(.[1])"' | column -t
}
if [[ "$very_verbose" == "true" ]]; then
echo "Calling $curlCommand"
fi
if [[ "$verbose" == "true" ]]; then
eval $curlCommand
else
filter $curlCommand
fi