forked from carloseberhardt/apiploy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpushapi
More file actions
executable file
·604 lines (529 loc) · 14.6 KB
/
pushapi
File metadata and controls
executable file
·604 lines (529 loc) · 14.6 KB
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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
#!/bin/bash
# -*- mode:shell-script; coding:utf-8; -*-
#
# pushapi
#
# A bash script that deploys of API bundles to the
# Apigee Gateway.
#
## defaults
environment=test
url=https://api.enterprise.apigee.com
verbosity=2
validate="&validate=true"
deploy=1
undeployonly=0
ziponly=0
TAB=$'\t'
# import .pushapi
# This is just a file with credentials=user:password in it, and other default settings.
#
# NB: you should add .pushapi to the .gitignore to avoid uploading
# apigee creds to github.
# credentials=username:password
# org=org-name
# environment=environment_name (e.g. test)
# url=api_management_url (as of coding, https://api.enterprise.apigee.com)
# rev=revision_number (e.g. 1)
PDIRNAME=`dirname $0`
. $PDIRNAME/.pushapi
# TODO: should we default to creating a new revision?
# TODO: this would require some conditional logic down below, I assume.
function echoerror() { echo "$@" 1>&2; }
function usage() {
local CMD=`basename $0`
echo "$CMD: Deploy an API proxy to the Apigee Gateway."
echo " Uses the curl utility, which must be available on the path."
echo "usage: "
echo " $CMD [options] application"
echo "options: "
echo " -o org the org to use."
echo " -e env the environment to deploy to."
echo " -r rev the revision number to use."
echo " -u url the base server url to use."
echo " -c creds authn credentials for the API calls."
echo " -x undeploy & delete all revs of the API; do not deploy a new rev."
echo " -z just zip the bundle; do not deploy a new rev."
echo " -q quiet; decrease verbosity by 1"
echo " -v verbose; increase verbosity by 1"
echo
echo "Current parameter values:"
echo " mgmt api url: $url"
echo " organization: $org"
echo " environment: $environment"
echo " revision: $rev"
echo " application: $application"
echo " verbosity: $verbosity"
echo
echo "You can specify default settings in the .pushapi file."
echo
exit 1
}
function timer() {
if [[ $# -eq 0 ]]; then
echo $(date '+%s')
else
local stime=$1
etime=$(date '+%s')
if [[ -z "$stime" ]]; then stime=$etime; fi
dt=$((etime - stime))
ds=$((dt % 60))
dm=$(((dt / 60) % 60))
dh=$((dt / 3600))
printf '%d:%02d:%02d' $dh $dm $ds
fi
}
## =======================================================
## json parsing
json_throw () {
echo "$*" >&2
exit 1
}
BRIEF=0
LEAFONLY=1
PRUNE=0
json_awk_egrep () {
local pattern_string=$1
gawk '{
while ($0) {
start=match($0, pattern);
token=substr($0, start, RLENGTH);
print token;
$0=substr($0, start+RLENGTH);
}
}' pattern=$pattern_string
}
json_tokenize () {
local GREP
local ESCAPE
local CHAR
if echo "test string" | egrep -ao --color=never "test" &>/dev/null
then
GREP='egrep -ao --color=never'
else
GREP='egrep -ao'
fi
if echo "test string" | egrep -o "test" &>/dev/null
then
ESCAPE='(\\[^u[:cntrl:]]|\\u[0-9a-fA-F]{4})'
CHAR='[^[:cntrl:]"\\]'
else
GREP=json_awk_egrep
ESCAPE='(\\\\[^u[:cntrl:]]|\\u[0-9a-fA-F]{4})'
CHAR='[^[:cntrl:]"\\\\]'
fi
local STRING="\"$CHAR*($ESCAPE$CHAR*)*\""
local NUMBER='-?(0|[1-9][0-9]*)([.][0-9]*)?([eE][+-]?[0-9]*)?'
local KEYWORD='null|false|true'
local SPACE='[[:space:]]+'
$GREP "$STRING|$NUMBER|$KEYWORD|$SPACE|." | egrep -v "^$SPACE$"
}
json_parse_array () {
local index=0
local ary=''
read -r token
case "$token" in
']') ;;
*)
while :
do
json_parse_value "$1" "$index"
index=$((index+1))
ary="$ary""$value"
read -r token
case "$token" in
']') break ;;
',') ary="$ary," ;;
*) json_throw "EXPECTED , or ] GOT ${token:-EOF}" ;;
esac
read -r token
done
;;
esac
[ "$BRIEF" -eq 0 ] && value=`printf '[%s]' "$ary"` || value=
:
}
json_parse_object () {
local key
local obj=''
read -r token
case "$token" in
'}') ;;
*)
while :
do
case "$token" in
'"'*'"') key=$token ;;
*) json_throw "EXPECTED string GOT ${token:-EOF}" ;;
esac
read -r token
case "$token" in
':') ;;
*) json_throw "EXPECTED : GOT ${token:-EOF}" ;;
esac
read -r token
json_parse_value "$1" "$key"
obj="$obj$key:$value"
read -r token
case "$token" in
'}') break ;;
',') obj="$obj," ;;
*) json_throw "EXPECTED , or } GOT ${token:-EOF}" ;;
esac
read -r token
done
;;
esac
[ "$BRIEF" -eq 0 ] && value=`printf '{%s}' "$obj"` || value=
:
}
json_parse_value () {
local jpath="${1:+$1,}$2" isleaf=0 isempty=0 print=0
case "$token" in
'{') json_parse_object "$jpath" ;;
'[') json_parse_array "$jpath" ;;
# At this point, the only valid single-character tokens are digits.
''|[!0-9]) json_throw "EXPECTED value GOT ${token:-EOF}" ;;
*) value=$token
isleaf=1
[ "$value" = '""' ] && isempty=1
;;
esac
[ "$value" = '' ] && return
[ "$LEAFONLY" -eq 0 ] && [ "$PRUNE" -eq 0 ] && print=1
[ "$LEAFONLY" -eq 1 ] && [ "$isleaf" -eq 1 ] && [ $PRUNE -eq 0 ] && print=1
[ "$LEAFONLY" -eq 0 ] && [ "$PRUNE" -eq 1 ] && [ "$isempty" -eq 0 ] && print=1
[ "$LEAFONLY" -eq 1 ] && [ "$isleaf" -eq 1 ] && \
[ $PRUNE -eq 1 ] && [ $isempty -eq 0 ] && print=1
[ "$print" -eq 1 ] && printf "[%s]\t%s\n" "$jpath" "$value"
:
}
json_parse () {
read -r token
json_parse_value
read -r token
case "$token" in
'') ;;
*) json_throw "EXPECTED EOF GOT $token" ;;
esac
}
#usage:
# cat example.json | json_tokenize | json_parse
## =======================================================
starttime=$(timer)
while getopts "vqhe:o:c:r:u:xz" opt; do
case $opt in
h) usage ;;
q) verbosity=$(($verbosity-1)) ;;
v) verbosity=$(($verbosity+1)) ;;
e) environment=$OPTARG ;;
o) org=$OPTARG ;;
c) credentials=$OPTARG ;;
r) rev=$OPTARG ;;
u) url=$OPTARG ;;
x) undeployonly=1 && deploy=0 ;;
z) ziponly=1 && deploy=0 ;;
*) echo "unknown arg" && usage ;;
esac
done
shift $(($OPTIND - 1))
## allow for an application directory that is not local
if [ $# -gt 0 ]; then
appdir=$1
application=`basename $1`
fi
if [ -z "$application" ]; then
echoerror "You must specify an application."
echoerror
usage
exit 1
fi
if [ $deploy -gt 0 ]; then
if [ ! -d "$1" ]; then
echoerror "The directory does not exist."
echoerror "The application name you specify must refer to a directory."
echoerror
usage
exit 1
fi
fi
if [ $ziponly -gt 0 -a $undeployonly -gt 0 ] ; then
echoerror "Mutually exclusive. Choose -x or -z ."
echoerror
usage
exit 1
fi
if [ $ziponly -eq 0 -a $undeployonly -eq 0 ] ; then
if ! [[ "$rev" =~ ^[0-9]+$ ]] ; then
echoerror "Bad revision: $rev"
echoerror "The revision must be a number."
echoerror
usage
exit 1
fi
if ! [[ "$url" =~ ^https?://.+$ ]] ; then
echoerror "Bad or missing mgmt api url: $url"
echoerror
usage
exit 1
fi
if [[ -z "$org" ]]; then
echoerror "missing org"
echoerror
usage
exit 1
fi
if [[ -z "$environment" ]]; then
echoerror "missing environment"
echoerror
usage
exit 1
fi
fi
## function MYCURL_Q
## Print the curl command, omitting sensitive parameters, then run it.
## There are side effects:
## 1. puts curl output into file named ${CURL_OUT}. If the CURL_OUT
## env var is not set prior to calling this function, it is created
## and the name of a tmp file in /tmp is placed there.
## 2. puts curl http_status into variable CURL_RC
function MYCURL_Q() {
local outargs
local allargs
local ix
local ix2
local re
re="^(-[du]|--user)$" # the curl options to not echo
# grab the curl args, but skip the basic auth and the payload, if any.
while [ "$1" ]; do
allargs[$ix2]=$1
let "ix2+=1"
if [[ $1 =~ $re ]]; then
shift
allargs[$ix2]=$1
let "ix2+=1"
else
outargs[$ix]=$1
let "ix+=1"
fi
shift
done
[ -z "${CURL_OUT}" ] && CURL_OUT=`mktemp /tmp/apigee-pushapi.curl.out.XXXXXX`
if [ $verbosity -gt 1 ]; then
# emit the curl command, without the auth + payload
echo
echo "curl ${outargs[@]}"
fi
# run the curl command
CURL_RC=`curl -s -w "%{http_code}" -o "${CURL_OUT}" "${allargs[@]}"`
if [ $verbosity -gt 1 ]; then
# emit the http status code
echo "==> ${CURL_RC}"
echo
fi
}
function CleanupAndFinish() {
if [ -f ${CURL_OUT} ]; then
rm -rf ${CURL_OUT}
fi
if [ $verbosity -gt 1 ]; then
echo
echo "done"
date +"%Y-%m-%d %H:%M:%S"
fi
if [ $verbosity -gt 0 ]; then
printf 'Elapsed time: %s\n' $(timer $starttime)
echo
fi
}
function DeleteApiproxyArchive() {
if [ -f $application.zip ]; then
if [ $verbosity -gt 0 ]; then
echo "Deleting $application.zip..."
fi
rm -rf $application.zip
fi
}
function ProduceAndMaybeShowZip() {
local curdir
local zipout
if [ $verbosity -gt 0 ]; then
echo "Creating the zip..."
fi
if [ -f $application.zip ]; then
rm -rf $application.zip
fi
curdir=`pwd`
cd $appdir
zipout=`zip -r $curdir/$application.zip apiproxy -x "*/*.*~" -x "*/#*.*#"`
cd $curdir
if [ $verbosity -gt 1 ]; then
#echo $zipout
unzip -l $application.zip
echo
fi
}
function UndeployAndDelete() {
local rev1
rev1=$1
MYCURL_Q -u ${credentials} -X GET "${url}/v1/o/$org/apis/$application/revisions/${rev1}/deployments"
if [ ${CURL_RC} -eq 200 ]; then
if [ $verbosity -gt 1 ]; then
cat ${CURL_OUT}
echo
fi
## extract the environment names in the list of deployments.
## Before updating, must undeploy the apiproxy from all environments where it is deployed.
output_parsed=`cat ${CURL_OUT} | json_tokenize | json_parse`
echo "${output_parsed}" | grep \"environment\" | grep --silent \"name\"
if [ $? -eq 0 ]; then
# must undeploy from all environments before updating the revision.
deployed_envs=`echo "${output_parsed}" | grep \"environment\" | grep \"name\" | sed -E 's/\"//g'| sed -E 's/.+environment,[0-9]+,name.'"${TAB}"'//g'`
#echo "${deployed_envs}" | grep --silent $environment
declare -a env_array=(${deployed_envs})
for env in ${env_array[@]}; do
if [ $verbosity -gt 0 ]; then
echo Undeploying from [${env}]...
fi
MYCURL_Q -u ${credentials} -X POST "${url}/v1/o/$org/apis/$application/revisions/${rev1}/deployments?action=undeploy&env=${env}"
if [ $verbosity -gt 1 ]; then
cat ${CURL_OUT}
echo
fi
## TODO: check return status, exit on fail.
done
else
if [ $verbosity -gt 0 ]; then
echo No un-deployment necessary.
echo That revision of the proxy does not appear to be currently deployed.
fi
fi
if [ $verbosity -gt 0 ]; then
echo "Deleting the existing revision..."
fi
MYCURL_Q -u ${credentials} -X DELETE "${url}/v1/o/$org/apis/$application/revisions/${rev1}"
if [ $verbosity -gt 1 ]; then
cat ${CURL_OUT}
echo
fi
if [ ${CURL_RC} -ne 200 ]; then
echo The delete failed.
exit
fi
elif [ ${CURL_RC} -ne 404 ]; then
cat ${CURL_OUT}
echo
if [ ${CURL_RC} -eq 403 -o ${CURL_RC} -eq 401 ]; then
echo You do not have permissions to do that.
else
echo An unknown error occurred.
fi
echo
exit 1
else
echo No un-deployment necessary: That revision does not exist.
echo
fi
}
if [ $verbosity -gt 1 ]; then
echo verbosity = $verbosity
fi
if [ $ziponly -gt 0 ]; then
ProduceAndMaybeShowZip
echo "the zip is available as $application.zip"
else
if [ $verbosity -gt 0 ]; then
echo
echo "Getting deployments..."
fi
if [ $deploy -eq 0 ]; then
MYCURL_Q -u ${credentials} -X GET "${url}/v1/o/$org/apis/$application"
if [ $verbosity -gt 1 ]; then
cat ${CURL_OUT}
echo
fi
if [ ${CURL_RC} -eq 200 ]; then
output_parsed=`cat ${CURL_OUT} | json_tokenize | json_parse`
##echo "${output_parsed}"
echo "${output_parsed}" | grep --silent \"revision\"
if [ $? -eq 0 ]; then
# There are existing deployed revisions
echo "Removing all revisions of that API Proxy..."
deployed_revs=`echo "${output_parsed}" | grep \"revision\" | sed -E 's/\"//g' | sed -E 's/.+revision,[0-9]+.'"${TAB}"'//g'`
#echo "${deployed_revs}"
declare -a rev_array=(${deployed_revs})
for r1 in ${rev_array[@]}; do
UndeployAndDelete ${r1}
done
else
echo "There are no existing revisions of that API Proxy."
fi
if [ $verbosity -gt 0 ]; then
echo "Deleting the API Proxy..."
fi
MYCURL_Q -u ${credentials} -X DELETE "${url}/v1/o/$org/apis/$application"
if [ $verbosity -gt 1 ]; then
cat ${CURL_OUT}
echo
fi
if [ ${CURL_RC} -eq 200 ]; then
if [ $verbosity -gt 0 ]; then
echo "deleted."
fi
else
if [ $verbosity -lt 0 ]; then
cat ${CURL_OUT}
echo
fi
echo "The DELETE failed."
fi
elif [ ${CURL_RC} -eq 401 ]; then
echo "Not authorized to do that."
elif [ ${CURL_RC} -eq 404 ]; then
echo "No deployments found for that API proxy."
else
echo "status =" ${CURL_RC}
echo "Failed querying that API Proxy."
fi
else
# as necessary, un-deploy and delete the existing specific revision $rev
UndeployAndDelete $rev
# create the bundle zip
ProduceAndMaybeShowZip
# import
if [ $verbosity -gt 0 ]; then
echo "Importing the bundle..."
fi
MYCURL_Q -u $credentials -X POST "$url/v1/o/$org/apis?action=import&name=$application${validate}" -T $application.zip -H "Content-Type: application/octet-stream"
if [ $verbosity -gt 1 ]; then
cat ${CURL_OUT}
echo
echo
fi
if [ ${CURL_RC} -ne 201 ]; then
echo
if [ $verbosity -le 1 ]; then
cat ${CURL_OUT}
echo
fi
echoerror "There was an error importing that API bundle..."
DeleteApiproxyArchive
echo
CleanupAndFinish
exit 1
fi
# deploy
if [ $verbosity -gt 0 ]; then
echo "Deploying the API revision..."
fi
MYCURL_Q -u $credentials -X POST "$url/v1/o/$org/apis/$application/revisions/$rev/deployments?action=deploy&env=$environment"
if [[ ! ${CURL_RC} =~ 200 ]]; then
echo
echoerror "There was an error deploying that revision of the API."
cat ${CURL_OUT} 1>&2;
echo
fi
DeleteApiproxyArchive
fi
fi
CleanupAndFinish