forked from flexera-public/rightlink_scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrs_push
executable file
·289 lines (237 loc) · 9.36 KB
/
rs_push
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
#! /bin/bash
# This script pushes this repository to the upstream git repo (e.g. github) and
# makes an API call to RightScale to import and refetch the updated repo.
# As the RightScale refetch is happening, the script will check to ensure that the
# local commit SHA matches the latest succeeded commit SHA on RightScale.
# The cookbook will default be imported into the alternate namespace but can
# be imported into the primary namespace is desired.
RS_SERVER="${RS_SERVER:-my.rightscale.com}"
show_help() {
cat <<EOT
Usage: ${0##*/} [-options...]
Push local cookbook changes to both GitHub and to RightScale.
-k refresh_token RightScale API refresh token from the dash Settings>API Credentials (req.)
Can alternatively be set to environmental variable RS_KEY
-r rs_account RightScale account number. Can alternatively be set to environmental
variable RS_ACCT
-a api_hostname The hostname for the RightScale API, default: ${RS_SERVER}
-p Import cookbook into the primary instead of the alternate namespace
-v Debug mode
-h Show this help
EOT
}
# ===== Default input values
refresh_token=""
rs_account=""
api_hostname=""
import_namespace="alternate"
while getopts "hvpk:r:a:" opt; do
case "$opt" in
h) show_help; exit 0 ;;
v) set -x ;;
k) refresh_token="$OPTARG" ;;
r) rs_account="$OPTARG" ;;
a) api_hostname="$OPTARG" ;;
p) import_namespace="primary" ;;
'?') show_help >&2; exit 1 ;;
esac
done
if [[ -z "$RS_KEY" ]]; then
if [[ -n "$refresh_token" ]]; then
RS_KEY="$refresh_token"
else
echo "ERROR: Unable to find refresh token." >&2
echo "Please use -k refresh_token or set the environmental variable RS_KEY." >&2
show_help
exit 1
fi
fi
if ! which curl >/dev/null; then
echo "ERROR: Cannot find curl: it is required for this script." >&2
exit 1
fi
# ===== Determine local repository information and push changes to Github
branch_name="$(git symbolic-ref -q HEAD)"
branch_name="${branch_name##refs/heads/}"
branch_name="${branch_name:-HEAD}"
echo "Current branch: ${branch_name}"
commit_sha=$(git rev-parse HEAD)
echo "Latest commit: ${commit_sha}"
echo -n "Pushing local changes to Github ... "
git push || git push --set-upstream origin "${branch_name}" || exit 1
# ===== Authenticate with RightScale API
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 "Authentication successful."
access_token="${BASH_REMATCH[1]}"
else
echo "ERROR: ${json}" >&2
exit 1
fi
# ===== Determine RightScale Account Number
if [[ -n "$rs_account" ]]; then
RS_ACCT="$rs_account"
else
if [[ -z "$RS_ACCT" ]]; then
echo -n "Determining RS account number ... "
# Admin account privileges are required for this API call
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: Unable to determine RightScale account number." >&2
echo "Please use -r rs_account or set the environmental variable RS_ACCT." >&2
show_help
exit 1
fi
fi
fi
# ==== Determine the RightScale Repository ID
if [[ -z "$RS_REPO_ID" ]]; then
RS_REPO_NAME="${RS_REPO_NAME:-$(basename $PWD)_${branch_name}}"
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}")
json_save="${json}"
repo_ids=()
re='"id":"([0-9]+)"(.*)'
while [[ "$json" =~ $re ]]; do
repo_ids=("${repo_ids[@]}" "${BASH_REMATCH[1]}")
# Store the rest of the json to find the next repo_id
json="${BASH_REMATCH[2]}"
done
re2='"commit_reference":"([^"]*)"'
if [[ "${#repo_ids[@]}" -eq 0 ]]; then
origin=$(git config remote.origin.url)
origin=$(echo "$origin" | sed -e "s,[email protected]:,https://github.com/,")
echo "not found."
echo -n "Creating repository ... "
json=$(curl -isL -gG --retry 3 -H X-API-Version:1.5 -H "Authorization: Bearer $access_token" \
-X POST "https://${RS_SERVER}/api/repositories" \
--data-urlencode "repository[auto_import]=false" \
--data-urlencode "repository[name]=${RS_REPO_NAME}" \
--data-urlencode "repository[source]=${origin}" \
--data-urlencode "repository[commit_reference]=${branch_name}" \
--data-urlencode "repository[source_type]=git")
re="Location: /api/repositories/([0-9]*)"
if [[ "$json" =~ $re ]]; then
repo_ids=("${BASH_REMATCH[1]}")
else
echo "ERROR: Failed to create repository." >&2
exit 1
fi
echo "/api/repositories/${repo_ids[0]}"
elif [[ "${#repo_ids[@]}" -gt 1 ]]; then
echo "ERROR: Found ${#repo_ids[@]} repositories." >&2
echo "Please set the environmental variable RS_REPO_ID to the repository ID." >&2
exit 1
elif [[ ! "$json_save" =~ $re2 || "${BASH_REMATCH[1]}" != "$branch_name" ]]; then
echo "ERROR: Found repository ${repo_ids[@]} but branch is '${BASH_REMATCH[1]}' not '${branch_name}'." >&2
echo "Fix at https://${RS_SERVER}/acct/${RS_ACCT}/design#/repositories/${repo_ids[@]}/info" >&2
exit 1
else
echo "/api/repositories/${repo_ids[0]}"
fi
RS_REPO_ID="${repo_ids[0]}"
fi
# ===== Query RightScale to get the repository assets
echo -n "Retrieving repository assets ..."
json="[]"
# Loop waiting for repository assets to be populated after creating repository
while true; do
json=$(curl -sL -gG --retry 3 -H X-API-Version:1.5 -H "Authorization: Bearer ${access_token}" \
"https://${RS_SERVER}/api/repositories/${RS_REPO_ID}/repository_assets")
if [[ "$json" != "[]" ]]; then
break
fi
sleep 15
echo -n "."
done
asset_hrefs=()
re='(/api/repositories/[0-9]+/repository_assets/[0-9]+)(.*)'
while [[ "$json" =~ $re ]]; do
asset_hrefs=("${asset_hrefs[@]}" "${BASH_REMATCH[1]}")
json="${BASH_REMATCH[2]}" # this is the rest of the json to find the next asset HREF
done
if [[ "${#asset_hrefs[@]}" -gt 0 ]]; then
echo " ${#asset_hrefs[@]} assets found."
else
echo " ERROR: No repository assets found." >&2
exit 1
fi
# ===== Check to see which assets need importing
cb_import_preview="curl -sL -gG --retry 3 -X POST -H X-API-Version:1.5 -H \"Authorization: Bearer ${access_token}\" \
-X POST \"https://${RS_SERVER}/api/repositories/${RS_REPO_ID}/cookbook_import_preview\" \
--data-urlencode \"namespace=${import_namespace}\""
for asset_href in "${asset_hrefs[@]}"; do
cb_import_preview+=" --data-urlencode \"asset_hrefs[]=${asset_href}\""
done
json=$(eval ${cb_import_preview})
import_asset_hrefs=()
re='(.*)"outcome":"([^"]+)".*"id":([0-9]+)'
while [[ "$json" =~ $re ]]; do
if [[ "${BASH_REMATCH[2]}" != "unchanged" ]]; then
import_asset_hrefs=("${import_asset_hrefs[@]}" "/api/repositories/${RS_REPO_ID}/repository_assets/${BASH_REMATCH[3]}")
fi
json="${BASH_REMATCH[1]}"
done
# ===== Import cookbooks that have changed
if [[ "${#import_asset_hrefs[@]}" -gt 0 ]]; then
echo -n "Importing cookbooks ... "
cb_import="curl -isL -gG --retry 3 -X POST -H X-API-Version:1.5 -H \"Authorization: Bearer ${access_token}\" \
-X POST \"https://${RS_SERVER}/api/repositories/${RS_REPO_ID}/cookbook_import\" \
--data-urlencode \"follow=false\" --data-urlencode \"namespace=${import_namespace}\""
for asset_href in "${import_asset_hrefs[@]}"; do
cb_import+=" --data-urlencode \"asset_hrefs[]=${asset_href}\""
done
response=$(eval $cb_import)
re='204 No Content'
if [[ "$response" =~ $re ]]; then
echo "import successful."
else
echo "import failed." >&2
exit 1
fi
fi
# ===== Refetch respository after cookbooks have been imported
echo -n "Refetching repository with ID '${RS_REPO_ID}' ... "
json=$(curl -sL -gG --retry 3 -X POST -H X-API-Version:1.5 -H "Authorization: Bearer ${access_token}" \
"https://${RS_SERVER}/api/repositories/${RS_REPO_ID}/refetch?auto_import=false")
if [[ -z "$json" ]]; then
echo "refetch successful."
else
echo "ERROR: refetch failed." >&2
exit 1
fi
# ===== Check the local repositories current SHA against the last succeeded commit SHA on RightScale
start_time=$(date +%s)
while true; do
echo -n "Checking current SHA against latest succeeded commit ... "
json=$(curl -sL -gG --retry 3 -H X-API-Version:1.5 -H "Authorization: Bearer ${access_token}" \
"https://${RS_SERVER}/api/repositories/${RS_REPO_ID}")
succeeded_commit=""
re='"succeeded_commit":"([^"]+)"'
if [[ "$json" =~ $re && "$commit_sha" == "${BASH_REMATCH[1]}" ]]; then
echo "match found."
succeeded_commit="${BASH_REMATCH[1]}"
break
else
echo "match not found, sleeping 15 seconds and retrying."
sleep 15
fi
done
# ===== Final check for successful cookbook update
if [[ "$commit_sha" == "$succeeded_commit" ]]; then
echo "Cookbook with ID '${RS_REPO_ID}' update successful!"
final_time="$(($(date +%s) - $start_time))"
echo "Update took ${final_time} seconds"
else
echo "Cookbook update failed!" >&2
exit 1
fi