forked from flexera-public/rightlink_scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrs_make_st
executable file
·431 lines (392 loc) · 14.7 KB
/
rs_make_st
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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
#! /bin/bash
# This script clones a "base" ServerTemplate and modifies it to use the scripts (cookbooks) in
# the current repo.
# In order to make RightScale API calls, the environment variable RS_KEY must be set to
# your RightScale API key, see the "API credentials" tab of the User Settings
# page of the RightScale dashboard.
# This script is still highly experimental and ill supported by the RightScale API, if you
# like it please vote for it :-)!
show_help() {
cat <<EOT
Usage: ${0##*/} [-options...]
Update or clone a base ServerTemplate and repoint the cookbooks it uses to the current repo & branch
-s st_name the name of the ServerTemplate to update or clone
-c clone the ST (instead of updating its HEAD revision)
-r rll_version update MCIs to the version of RLL (ex: -r 10.0.rc1)
-o repo_id id of repository to replace (ex: -o 241828003)
-h show this help
EOT
}
st_name=
clone=
rll=
repo_id=
while getopts "?hcs:r:o:" opt; do
case "$opt" in
h) show_help; exit 0 ;;
s) st_name=$OPTARG ;;
o) repo_id=$OPTARG ;;
c) clone=1 ;;
r) rll=$OPTARG ;;
'?') show_help >&2; exit 1 ;;
esac
done
# Shift off the options and optional --.
shift "$((OPTIND-1))"
if [[ -n "$rll" && -n "$clone" ]]; then
echo "-c (clone) and -r (RLL version) are not supported together"
echo "Please first clone without updating RLL and then re-run with"
echo "-s naming the new ST and -r naming the desired version of RLL"
exit 1
fi
if [[ -z "$RS_KEY" ]]; then
echo "Please set RS_KEY to your RightScale API key from the dashboard's Settings>API credentials"
exit 1
fi
if ! curl --version >/dev/null; then
echo "ERROR: cannot find curl: it is required for this script, sorry"
exit 1
fi
# hrefs to delete if something goes wrong
# Whoops, this doesn't work as expected 'cause we change thing in-place and so deleting
# stuff actually make things worse, better re-run the script
delete_hrefs=()
clean_up() {
exit 1
echo
echo "Ooops, something went wrong..."
echo "Cleaning up ${#delete_hrefs[@]} resources"
#for href in ${delete_hrefs[@]}; do
for (( idx=${#delete_hrefs[@]}-1 ; idx>=0 ; idx-- )) ; do
href="${delete_hrefs[$idx]}"
echo -n "Delete ${href} ... "
resp=`curl -sL -gi --retry 3 -H X-API-Version:1.5 -H "Authorization: Bearer $access_token" \
-X DELETE https://${RS_SERVER}${href}`
if [[ "$?" -ne 0 || ! "$resp" =~ 204\ No\ Content ]]; then
echo "OOOPS: $resp"
else
echo "Done"
fi
done
exit 1
}
branch_name=$(git symbolic-ref -q HEAD)
branch_name=${branch_name##refs/heads/}
branch_name=${branch_name:-HEAD}
echo "Current branch: ${branch_name}"
RS_SERVER=${RS_SERVER:-us-3.rightscale.com}
echo -n "Authenticating with RightScale at ${RS_SERVER} ... "
json=`curl -sL -gG --retry 3 -X POST -H X-API-Version:1.5 \
"https://${RS_SERVER}/api/oauth2?grant_type=refresh_token&refresh_token=${RS_KEY}"`
re='"access_token": *"([^"]*)"'
if [[ "$json" =~ $re ]]; then
echo "successful"
access_token="${BASH_REMATCH[1]}"
else
echo "failed:"
echo $json
exit 1
fi
#echo "curl -sL -gG --retry 3 -H X-API-Version:1.5 -H 'Authorization: Bearer $access_token'" \
# "https://${RS_SERVER}/api/"
# ===== Find Cookbook Repository
RS_REPO_NAME=${RS_REPO_NAME:-`basename $PWD`_${branch_name}}
if [[ -z "$RS_REPO_ID" ]]; then
# gotta figure out what the RightScale repo ID is for this baby
echo -n "Finding repositories called '${RS_REPO_NAME}' ... "
json=`curl -sL -gG --retry 3 -H X-API-Version:1.5 -H "Authorization: Bearer $access_token" \
"https://${RS_SERVER}/api/repositories?filter[]=name==${RS_REPO_NAME}"`
re='"id":"([0-9][0-9]*)"(.*)'
repo_ids=()
while [[ "$json" =~ $re ]]; do
repo_ids=("${repo_ids[@]}" "${BASH_REMATCH[1]}")
json="${BASH_REMATCH[2]}" # this is the rest of the json to find the next repo_id
done
if (("${#repo_ids[@]}" == 0)); then
echo "ERROR"
echo "No repo called '${RS_REPO_NAME}' found in the account,"
exit 1
elif (("${#repo_ids[@]}" > 1)); then
echo "ERROR"
echo "Found ${#repo_ids[@]} repos, please set RS_REPO_ID to the repo ID"
exit 1
fi
repo_href="/api/repositories/${repo_ids[0]}"
echo $repo_href
else
echo "Using RS_REPO_ID=${RS_REPO_ID}"
repo_href="/api/repositories/${RS_REPO_ID}"
fi
# ===== Find Server Template
# We locate the desired ServerTemplate based on the command line argument. The ST is required
# to create a server. It will provide boot scripts, operational scripts, decommissioning scripts,
# alert definitions to the existing instance. An empty ST with an MCI to launch the surrogate
# is the minimum required.
if [[ -z "$RS_BASE_ST_ID" ]]; then
echo -n "Finding ServerTemplate '$st_name' ... "
json=`curl -sL --retry 3 -H X-API-Version:1.5 -H "Authorization: Bearer $access_token" \
"https://${RS_SERVER}/api/server_templates" \
-gG --data-urlencode "filter[]=name==$st_name"`
re='"rel":"self","href":"(/api/server_templates/[0-9]*)"'
if [[ "$?" -ne 0 || ! "$json" =~ $re ]]; then
echo "ERROR: Failed to find ServerTemplate."
echo "JSON: $json"
exit 1
fi
matches=`echo $json | grep -o $re | wc -l`
if [[ "$matches" -gt 1 ]]; then
echo "ERROR: Multiple ServerTemplates found. Please set RS_BASE_ST_ID and re-run"
echo "$json"
exit 1
else
st_href="${BASH_REMATCH[1]}"
fi
echo "$st_href"
else
echo "Using RS_BASE_ST_ID=${RS_BASE_ST_ID}"
st_href="/api/server_templates/${RS_BASE_ST_ID}"
fi
if [[ -n "$clone" ]]; then
echo -n "Cloning the ST as '${st_name} (${branch_name})' ..."
resp=`curl -sL -gi --retry 3 -X POST -H X-API-Version:1.5 -H "Authorization: Bearer $access_token" \
"https://${RS_SERVER}${st_href}/clone" \
--data-urlencode "server_template[name]=${st_name} (${branch_name})"`
re='Location: *(/api/server_templates/[0-9]*)'
if [[ "$?" -ne 0 || ! "$resp" =~ 201\ Created || ! "$resp" =~ $re ]]; then
echo "ERROR: Failed to clone ServerTemplate."
echo "Exit code: $?"
echo "$resp"
exit 1
else
st_href="${BASH_REMATCH[1]}"
echo $st_href
fi
delete_hrefs=("${delete_hrefs[@]}" "${st_href}")
fi
if [[ -z "${RS_ACCT}" ]]; then
echo -n "Determining RS account number ... "
json=`curl -sL -gG --retry 3 -H X-API-Version:1.5 -H "Authorization: Bearer $access_token" \
"https://${RS_SERVER}/api/permissions"`
re='/api/accounts/([0-9]*)'
if [[ "$json" =~ $re ]]; then
RS_ACCT="${BASH_REMATCH[1]}"
echo "${RS_ACCT}"
else
echo "ERROR"
echo "Please set RS_ACCT"
clean_up
fi
fi
if [[ -z "$repo_id" ]]; then
echo "Finding repositories used by the ServerTemplate ... "
n=0
json=`curl -sL -g --retry 3 -X POST -H X-API-Version:1.5 -H "Authorization: Bearer $access_token" \
"https://${RS_SERVER}${st_href}/resolve"`
re='"(/api/repositories/[0-9]*)"(.*)'
while [[ $json =~ $re ]]; do
href="${BASH_REMATCH[1]}"
json="${BASH_REMATCH[2]}"
# get info about the repo
json2=`curl -sL -g --retry 3 -H X-API-Version:1.5 -H "Authorization: Bearer $access_token" \
"https://${RS_SERVER}${href}"`
# name
name=
re2='"name" *: *"([^"]*)"'
[[ $json2 =~ $re2 ]] && name="${BASH_REMATCH[1]}"
# source
source="unknown?"
re2='"source" *: *"([^"]*)"'
[[ $json2 =~ $re2 ]] && source="${BASH_REMATCH[1]}"
# print info
if [[ -n "$name" && -n "$source" ]]; then
echo " Found $name from $source"
else
echo " Found $href"
fi
((n++))
done
if (( n == 0 )); then
echo "No repository found. This may be due to a bug in the RightScale API (CM-637)"
echo "Please either add recipes from your repo manually at"
echo "https://${RS_SERVER}/acct/${RS_ACCT}/server_templates/`basename $st_href`#scripts"
echo "Or specify the id of the repo to replace using the -o flag"
#echo "$json"
clean_up
elif (( n > 1 )); then
echo "Cannot handle STs with multiple repositories, please specify the -o flag"
clean_up
fi
else
href="/api/repositories/$repo_id"
echo "Using repository href $href"
fi
echo -n "Replacing repository ... "
echo curl -sL -gi --retry 3 -X POST -H X-API-Version:1.5 -H "Authorization: Bearer $access_token" \
"https://${RS_SERVER}${st_href}/swap_repository" \
--data-urlencode "source_repository_href=$href" \
--data-urlencode "target_repository_href=$repo_href"
resp=`curl -sL -gi --retry 3 -X POST -H X-API-Version:1.5 -H "Authorization: Bearer $access_token" \
"https://${RS_SERVER}${st_href}/swap_repository" \
--data-urlencode "source_repository_href=$href" \
--data-urlencode "target_repository_href=$repo_href"`
if [[ "$?" -ne 0 || ! "$resp" =~ 200\ OK ]]; then
echo "ERROR: Failed to swap repositories"
echo "Exit code: $?"
echo "$resp"
clean_up
fi
echo "done"
echo "$resp"
if [[ -n "$rll" ]]; then
echo "Updating MCIs with an rs_agent:mime_include_url tag for RLL ${rll}"
echo -n "Finding MCIs attached to the ServerTemplate ... "
json=`curl -sL -gG --retry 3 -H X-API-Version:1.5 -H "Authorization: Bearer $access_token" \
"https://${RS_SERVER}/api/server_template_multi_cloud_images" \
--data-urlencode "filter[]=server_template_href==${st_href}"`
re='"(/api/multi_cloud_images/[0-9]*)"(.*)'
mcis=()
while [[ "$json" =~ $re ]]; do
mcis=("${mcis[@]}" "${BASH_REMATCH[1]}")
json="${BASH_REMATCH[2]}" # this is the rest of the json to find the next mci
done
echo "${#mcis[@]} found"
if (( ${#mcis[@]} == 0 )); then
echo "Please attach MCIs manually"
clean_up
fi
for mci in ${mcis[@]}; do
echo -n "Checking tag for MCI $mci ... "
json=`curl -sL -g --retry 3 -X POST -H X-API-Version:1.5 -H "Authorization: Bearer $access_token" \
"https://${RS_SERVER}/api/tags/by_resource" \
--data-urlencode "resource_hrefs[]=$mci"`
re='rs_agent:mime_include_url=.*/rll/([^/]*)/'
if [[ "$json" =~ $re ]]; then
cur_tag="${BASH_REMATCH[1]}"
if [[ "$cur_tag" == "$rll" ]]; then
echo "OK, already $cur_tag"
continue
fi
echo "$cur_tag"
fi
# echo -n " Updating its tags ... "
# url="https://rightlinklite.rightscale.com/rll/${rll}/rightlink.boot.sh"
# resp=`curl -sL -gi --retry 3 -X POST -H X-API-Version:1.5 -H "Authorization: Bearer $access_token" \
# "https://${RS_SERVER}/api/tags/multi_add" \
# --data-urlencode "resource_hrefs[]=${mci}" \
# --data-urlencode "tags[]=rs_agent:mime_include_url=${url}" \
# --data-urlencode "tags[]=rs_agent:type=right_link_lite"`
# if [[ "$?" -ne 0 || ! "$resp" =~ 204\ No\ Content ]]; then
# echo "ERROR: Failed to update tags"
# echo "Exit code: $?"
# echo "$resp"
# clean_up
# else
# echo Done
# fi
# continue
echo -n " Find its name ... "
json=`curl -sL -gG --retry 3 -H X-API-Version:1.5 -H "Authorization: Bearer $access_token" \
"https://${RS_SERVER}${mci}"`
re='"name" *: *"([^"]*)"'
if [[ "$json" =~ $re ]]; then
name="${BASH_REMATCH[1]}"
echo "$name"
else
echo "Ooops"
echo "$json"
clean_up
fi
# concoct new name for MCI, try to replace old RLL version in name
re="(.*)$cur_tag(.*)"
if [[ "$name" =~ $re ]]; then
new_name="${BASH_REMATCH[1]}${rll}${BASH_REMATCH[2]}"
else
new_name="${name} (${rll})"
fi
echo -n " Cloning it as ${new_name} ..."
resp=`curl -sL -gi --retry 3 -X POST -H X-API-Version:1.5 -H "Authorization: Bearer $access_token" \
"https://${RS_SERVER}${mci}/clone" \
--data-urlencode "multi_cloud_image[name]=${new_name}"`
re='Location: *(/api/multi_cloud_images/[0-9]*)'
if [[ "$?" -ne 0 || ! "$resp" =~ 201\ Created || ! "$resp" =~ $re ]]; then
echo "ERROR: Failed to clone MCI"
echo "Exit code: $?"
echo "$resp"
clean_up
else
mci_href="${BASH_REMATCH[1]}"
echo $mci_href
delete_hrefs=("${delete_hrefs[@]}" "${mci_href}")
fi
echo -n " Updating its tags ... "
url="https://rightlinklite.rightscale.com/rll/${rll}/rightlink.boot.sh"
resp=`curl -sL -gi --retry 3 -X POST -H X-API-Version:1.5 -H "Authorization: Bearer $access_token" \
"https://${RS_SERVER}/api/tags/multi_add" \
--data-urlencode "resource_hrefs[]=${mci_href}" \
--data-urlencode "tags[]=rs_agent:mime_include_url=${url}" \
--data-urlencode "tags[]=rs_agent:type=right_link_lite"`
if [[ "$?" -ne 0 || ! "$resp" =~ 204\ No\ Content ]]; then
echo "ERROR: Failed to update tags"
echo "Exit code: $?"
echo "$resp"
clean_up
else
echo Done
fi
echo " Swap out MCI in ServerTemplate"
echo -n " Finding association ... "
json=`curl -sL -gG --retry 3 -H X-API-Version:1.5 -H "Authorization: Bearer $access_token" \
"https://${RS_SERVER}/api/server_template_multi_cloud_images" \
--data-urlencode "filter[]=multi_cloud_image_href==${mci}" \
--data-urlencode "filter[]=server_template_href==${st_href}"`
ri='"(/api/server_template_multi_cloud_images/[0-9]*)"'
if [[ ! "$json" =~ $ri ]]; then
echo "ERROR: Cannot find association !?"
echo "$json"
clean_up
fi
stmci_href="${BASH_REMATCH[1]}"
is_default=
re='"is_default": *"*true'
[[ "$json" =~ $ri ]] && is_default=true
echo "done"
echo -n " Adding new association ... "
yuck=server_template_multi_cloud_image
resp=`curl -sL -gi --retry 3 -H X-API-Version:1.5 -H "Authorization: Bearer $access_token" \
-X POST "https://${RS_SERVER}/api/server_template_multi_cloud_images" \
--data-urlencode "${yuck}[multi_cloud_image_href]=${mci_href}" \
--data-urlencode "${yuck}[server_template_href]=${st_href}"`
re='Location: *(/api/[a-z_]*/[0-9]*)'
if [[ "$?" -ne 0 || ! "$resp" =~ 201\ Created || ! "$resp" =~ $re ]]; then
echo "ERROR: Failed to add MCI"
echo "Exit code: $?"
echo "$resp"
clean_up
fi
new_stmci="${BASH_REMATCH[1]}"
echo $new_stmci
if [[ -n "$is_default" ]]; then
echo -n " Making it the default MCI ... "
resp=`curl -sL -gi --retry 3 -H X-API-Version:1.5 -H "Authorization: Bearer $access_token" \
-X POST "https://${RS_SERVER}${new_stmci}/make_default"`
if [[ "$?" -ne 0 || ! "$resp" =~ 204\ No\ Content ]]; then
echo "ERROR: Failed to make MCI the default, please fix manually"
else
echo Done
fi
fi
echo -n " Deleting old association ... "
resp=`curl -sL -gi --retry 3 -H X-API-Version:1.5 -H "Authorization: Bearer $access_token" \
-X DELETE https://${RS_SERVER}${stmci_href}`
if [[ "$?" -ne 0 || ! "$resp" =~ 204\ No\ Content ]]; then
echo "OOOPS: $resp"
clean_up
else
echo "Done"
fi
done
fi
echo ""
echo "Your server template is now ready to launch, see"
echo "https://${RS_SERVER}/acct/${RS_ACCT}/server_templates/`basename $st_href`"
echo "Remember to commit it once you're happy with it"