forked from istio/istio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgcb_build_lib.sh
159 lines (139 loc) · 4.48 KB
/
gcb_build_lib.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
152
153
154
155
156
157
158
159
#!/bin/bash
# Copyright 2017 Istio Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
################################################################################
set -o errexit
set -o nounset
set -o pipefail
set -x
SCRIPTPATH=$( cd "$(dirname "$0")" ; pwd -P )
source ${SCRIPTPATH}/json_parse_shared.sh
# parse_result_file(): parses the result from a build query.
# returns 1 if build successful, 0 if build still running, or 2 if build failed
# first input parameter is path to file to parse
#
# Typically this function just needs to parse json for something like:
# "status": "FAILURE"
# or
# "status": "SUCCESS"
#
# but if a request is too invalid to accept then there's a different response like:
#
# {
# "error": {
# "code": 400,
# "message": "Invalid JSON payload received. Unexpected end of string.",
# "status": "INVALID_ARGUMENT"
# }
# }
#
BUILD_FAILED=0
function parse_result_file {
local INPUT_FILE="$1"
[[ -z "${INPUT_FILE}" ]] && usage
local STATUS_VALUE=$(parse_json_for_first_string $INPUT_FILE "status")
local ERROR_VALUE=""
local ERROR_CODE=""
local ERROR_STATUS=""
local ERROR_LINE=$(parse_json_for_int $INPUT_FILE "code")
if [[ -n "$ERROR_LINE" ]]; then
ERROR_CODE=$(parse_json_for_int $INPUT_FILE "code")
ERROR_VALUE=$(parse_json_for_string $INPUT_FILE "message")
ERROR_STATUS=${STATUS_VALUE}
STATUS_VALUE="ERROR"
fi
case "${STATUS_VALUE}" in
ERROR)
echo "build has error code ${ERROR_CODE} with \"${ERROR_STATUS}\" and \"${ERROR_VALUE}\""
BUILD_FAILED=1
return 2
;;
FAILURE)
echo "build has failed"
BUILD_FAILED=1
return 2
;;
CANCELLED)
echo "build was cancelled"
BUILD_FAILED=1
return 2
;;
QUEUED)
echo "build is queued"
return 0
;;
WORKING)
echo "build is running at $(date)"
return 0
;;
SUCCESS)
echo "build is successful"
return 1
;;
*)
echo "unrecognized status: ${STATUS_VALUE}"
cat $INPUT_FILE
BUILD_FAILED=1
return 2
esac
}
function run_build() {
local MFEST_URL=$1
local MFEST_FILE=$2
local MFEST_VER=$3
local TEMPLATE_NAME=$4
local SUBS_FILE=$5
local PROJ_ID=$6
local SERVICE_ACCT=$7
local SERVICE_KEY_FILE=$8
local WAIT=$9
local REQUEST_FILE="$(mktemp /tmp/build.request.XXXX)"
local RESULT_FILE="$(mktemp /tmp/build.response.XXXX)"
# generate the json file, first strip off the closing } in the last line of the template
head --lines=-1 "${SCRIPTPATH}/${TEMPLATE_NAME}" > "${REQUEST_FILE}"
cat "${SUBS_FILE}" >> "${REQUEST_FILE}"
echo "}" >> "${REQUEST_FILE}"
# try to preserve the prior gcloud account that's in use
if [[ -n "${SERVICE_KEY_FILE}" ]]; then
local PRIOR_GCLOUD_ACCOUNT="$(gcloud config get-value account)"
gcloud auth activate-service-account "${SERVICE_ACCT}" --key-file="${SERVICE_KEY_FILE}"
if [[ -n "${PRIOR_GCLOUD_ACCOUNT}" ]]; then
gcloud config set account "${PRIOR_GCLOUD_ACCOUNT}"
fi
fi
curl -X POST -H "Authorization: Bearer $(gcloud auth --account ${SERVICE_ACCT} print-access-token)" \
-T "${REQUEST_FILE}" -s -o "${RESULT_FILE}" https://cloudbuild.googleapis.com/v1/projects/${PROJ_ID}/builds
# cleanup
rm -f "${REQUEST_FILE}"
# the following tries to find and parse a json line like:
# "id": "e1487f85-8585-44fe-a7dc-765502e5a8c0",
local BUILD_ID=$(parse_json_for_string $RESULT_FILE "id")
if [[ -z "$BUILD_ID" ]]; then
echo "failed to parse the following build result:"
cat $RESULT_FILE
exit 1
fi
echo "BUILD_ID is ${BUILD_ID}"
if [[ "${WAIT}" == "true" ]]; then
echo "waiting for build to complete"
while parse_result_file "${RESULT_FILE}"
do
sleep 60
curl -H "Authorization: Bearer $(gcloud auth --account ${SERVICE_ACCT} print-access-token)" -s --retry 3 \
-o "${RESULT_FILE}" "https://cloudbuild.googleapis.com/v1/projects/${PROJ_ID}/builds/{$BUILD_ID}"
done
fi
rm -f "${RESULT_FILE}"
}