-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathselfservice
executable file
·235 lines (212 loc) · 5.22 KB
/
selfservice
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
#!/bin/bash
DEFAULT_AUTH_URL="https://auth.isv.ci"
if [ -z "${AUTH_URL}" ] ; then
AUTH_URL="${DEFAULT_AUTH_URL}"
fi
DEFAULT_SELF_SERVICE_URL="https://self-service.isv.ci"
if [ -z "${SELF_SERVICE_URL}" ] ; then
SELF_SERVICE_URL="${DEFAULT_SELF_SERVICE_URL}"
fi
function help {
cat <<EOF
USAGE: selfservice <command> [<args>]
A semi-official helper for running commands to the Self Service API
Commands:
help Prints this help message
auth <token> Retrieve an Auth token from the Auth service. Requires a API Token as an argument.
To get one, visit:
${AUTH_URL}
list List available environments
get <id> Get details for an environment. Requires an environment ID as an argument.
claim <type> Claim an environment
claimAndWait <type> Claim an environment and poll until it is ready
release <id> Release an environment
types List available environment types
version Prints the version for this tool
Variables:
AUTH_URL URL of the Auth service (Default: ${DEFAULT_AUTH_URL})
SELF_SERVICE_URL URL to Self Service instance (Default: ${DEFAULT_SELF_SERVICE_URL})
EOF
}
function getAuthToken {
api_token=$1
if [ -z "${api_token}" ] ; then
echo "Missing API token argument" >&2
help
exit 1
fi
if ! AUTH_TOKEN=$(
curl \
--fail \
--silent \
--show-error \
--request POST \
--header "Content-Type: application/json" \
--data "\"${api_token}\"" \
"${AUTH_URL}/token"
)
then
echo "Failed to get an auth token." >&2
echo "Check your API token." >&2
exit 1
fi
echo "export AUTH_TOKEN=${AUTH_TOKEN}"
}
function checkForAuthToken {
if [ -z "${AUTH_TOKEN}" ] ; then
echo "AUTH_TOKEN must be defined." >&2
echo "Try running \"selfservice auth <api_token>\" first" >&2
exit 1
fi
}
function listEnvironments {
curl \
--fail \
--silent \
--show-error \
--header "Authorization: Token ${AUTH_TOKEN}" \
--write-out "\n" \
"${SELF_SERVICE_URL}/environments.json"
}
function getEnvironment {
environment_id=$1
if [ -z "${environment_id}" ] ; then
echo "Missing environment ID argument"
help
exit 1
fi
if ! curl \
--fail \
--silent \
--show-error \
--header "Authorization: Token ${AUTH_TOKEN}" \
--write-out "\n" \
"${SELF_SERVICE_URL}/environments/${environment_id}.json"
then
echo "Failed to get environment" >&2
exit 1
fi
}
function listEnvironmentTypes {
curl \
--fail \
--silent \
--show-error \
--header "Authorization: Token ${AUTH_TOKEN}" \
--write-out "\n" \
"${SELF_SERVICE_URL}/environment_types.json"
}
function claimEnvironment {
environment_type=$1
if [ -z "${environment_type}" ] ; then
echo "Missing environment type argument" >&2
help
exit 1
fi
if ! curl \
--fail \
--silent \
--show-error \
--request POST \
--header "Authorization: Token ${AUTH_TOKEN}" \
--header "Content-Type: application/json" \
--data "{\"environment_type\": \"${environment_type}\"}" \
--write-out "\n" \
"${SELF_SERVICE_URL}/environments.json" ; then
echo "Failed to claim environment" >&2
exit 1
fi
}
function claimEnvironmentAndWait {
environment_type=$1
environment=$(claimEnvironment "${environment_type}")
id=$(echo "${environment}" | jq -r .id)
status=$(echo "${environment}" | jq -r .status)
sleepInterval=0
nextSleepInterval=1
maxSleepInterval=60
while [ "${status}" = "pending" ] ; do
sleep "${sleepInterval}"
echo -n "." >&2
nextSleepInterval=$((nextSleepInterval + sleepInterval))
sleepInterval=$((nextSleepInterval - sleepInterval))
sleepInterval=$((sleepInterval > maxSleepInterval ? maxSleepInterval : sleepInterval))
environment=$(getEnvironment "${id}")
status=$(echo "${environment}" | jq -r .status)
done
echo >&2
echo "${environment}"
if [ "${status}" != "success" ] ; then
echo "Failed to claim the environment" >&2
exit 1
fi
}
function releaseEnvironment {
environment_id=$1
if [ -z "${environment_id}" ] ; then
echo "Missing environment ID argument"
help
exit 1
fi
if ! curl \
--fail \
--silent \
--show-error \
--request DELETE \
--header "Authorization: Token ${AUTH_TOKEN}" \
--write-out "\n" \
"${SELF_SERVICE_URL}/environments/${environment_id}.json"
then
echo "Failed to release environment" >&2
exit 1
fi
}
function printVersion {
if [ -z "${SELFSERVICE_CLI_VERSION}" ] ; then
echo "dev"
else
echo "${SELFSERVICE_CLI_VERSION}"
fi
}
command=${1:-help}
case "${command}" in
auth)
getAuthToken "$2"
;;
help)
help
exit 0
;;
list)
checkForAuthToken
listEnvironments
;;
get)
checkForAuthToken
getEnvironment "$2"
;;
types)
checkForAuthToken
listEnvironmentTypes
;;
claim)
checkForAuthToken
claimEnvironment "$2"
;;
claimAndWait)
checkForAuthToken
claimEnvironmentAndWait "$2"
;;
release)
checkForAuthToken
releaseEnvironment "$2"
;;
version)
printVersion
;;
*)
echo "Unknown command: ${1}" >&2
help
exit 1
;;
esac