-
Notifications
You must be signed in to change notification settings - Fork 74
/
Copy pathall-utilities
338 lines (296 loc) · 10.9 KB
/
all-utilities
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
#!/bin/bash
#
# Copyright (C) Extensible Service Proxy Authors
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
#
################################################################################
#
# Setting SUDO if not running as root.
if [[ $UID -ne 0 ]]; then
SUDO=sudo
fi
[[ -z "${PLATFORM}" ]] && PLATFORM='debian-9'
TOOLS_DIR='/tmp/esp-tools'
# Library of useful utilities.
function set_gcloud() {
export GCLOUD="$(which gcloud)" || export GCLOUD='/usr/lib/google-cloud-sdk/bin/gcloud'
export GSUTIL=$(which gsutil) || export GSUTIL='/usr/lib/google-cloud-sdk/bin/gsutil'
}
function set_bazel() {
export BAZEL="$(which bazel)" || export BAZEL='/usr/local/bin/bazel'
}
function set_wrk() {
export WRK="$(which wrk)" || export WRK='/usr/local/bin/wrk'
}
set_bazel
set_gcloud
set_wrk
function set_git() {
if [[ ! -e "${HOME}/.gitconfig" ]]; then
cat > "${HOME}/.gitconfig" << EOF
[user]
name = Jenkins
email = [email protected]
EOF
fi
# In order to have bazel fetch PRs
git config --global --add remote.origin.fetch \
"+refs/pull/*/head:refs/remotes/origin/pr/*" \
|| error_exit 'Cannot set git config.'
}
# Exit with a message and an exit code.
# Arguments:
# $1 - string with an error message
# $2 - exit code, defaults to 1
function error_exit() {
# ${BASH_SOURCE[1]} is the file name of the caller.
echo "${BASH_SOURCE[1]}: line ${BASH_LINENO[0]}: ${1:-Unknown Error.} (exit ${2:-1})" 1>&2
exit ${2:-1}
}
# Tag a source image with a target image which shouldn't exist.
# Arguments:
# $1 - the source image.
# $2 - the target image.
function docker_tag() {
${GCLOUD} docker -- pull "${1}" || error_exit "Cannot pull image: ${1}"
${GCLOUD} docker -- pull "${2}" && error_exit "Trying to override an existing image: ${2}"
docker tag "${1}" "${2}" || error_exit "Failed to tag ${1} with ${2}"
}
# Tag -f a source image with a target image which may exist already.
# Arguments:
# $1 - the source image.
# $2 - the target image.
function docker_tag_f() {
${GCLOUD} docker -- pull "${1}" || error_exit "Cannot pull image: ${1}"
docker tag -f "${1}" "${2}" || error_exit "Failed to tag ${1} with ${2}"
}
# Retries a command with an exponential back-off.
# The back-off base is a constant 3/2
# Options:
# -n Maximum total attempts (0 for infinite, default 10)
# -t Maximum time to sleep between retries (default 60)
# -s Initial time to sleep between retries. Subsequent retries
# subject to exponential back-off up-to the maximum time.
# (default 5)
function retry() {
local OPTIND OPTARG ARG
local COUNT=10
local SLEEP=5 MAX_SLEEP=60
local MUL=3 DIV=2 # Exponent base multiplier and divisor
# (Bash doesn't do floats)
while getopts ":n:s:t:" ARG; do
case ${ARG} in
n) COUNT=${OPTARG};;
s) SLEEP=${OPTARG};;
t) MAX_SLEEP=${OPTARG};;
*) echo "Unrecognized argument: -${OPTARG}";;
esac
done
shift $((OPTIND-1))
# If there is no command, abort early.
[[ ${#} -le 0 ]] && { echo "No command specified, aborting."; return 1; }
local N=1 S=${SLEEP} # S is the current length of sleep.
while : ; do
echo "${N}. Executing ${@}"
"${@}" && { echo "Command succeeded."; return 0; }
[[ (( COUNT -le 0 || N -lt COUNT )) ]] \
|| { echo "Command '${@}' failed ${N} times, aborting."; return 1; }
if [[ (( S -lt MAX_SLEEP )) ]] ; then
# Must always count full exponent due to integer rounding.
((S=SLEEP * (MUL ** (N-1)) / (DIV ** (N-1))))
fi
((S=(S < MAX_SLEEP) ? S : MAX_SLEEP))
echo "Command failed. Will retry in ${S} seconds."
sleep ${S}
((N++))
done
}
# Download api Keys from Cloud storage and source the file.
function set_api_keys() {
local api_key_directory="$(mktemp -d)"
$GSUTIL cp gs://esp-testing-secret-files/api_keys \
"${api_key_directory}/api_keys" \
|| error_exit "Failed to download API key file."
source "${api_key_directory}/api_keys"
}
# Download test-client keys from Cloud storage
function get_test_client_key() {
local key_path=$1
[[ -e $key_path ]] || $GSUTIL \
cp gs://esp-testing-secret-files/esp-test-client-b1289e36f3df.json $key_path
echo -n $key_path
return 0
}
# Creates a simple Json Status file
function create_status_file() {
local OPTIND OPTARG ARG
local file_path=''
local test_status=''
local test_id=''
local run_id=''
while getopts :f:s:t:r: ARG; do
case ${ARG} in
f) file_path="${OPTARG}";;
s) test_status=${OPTARG};;
t) test_id="${OPTARG}";;
r) run_id="${OPTARG}";;
*) echo "Unrecognized argument: -${OPTARG}";;
esac
done
[[ -n "${file_path}" ]] || { echo 'File path is not set.'; return 1; }
[[ -n "${test_status}" ]] || { echo 'Status is not set.'; return 1; }
[[ -n "${test_id}" ]] || { echo 'Test id is not set.'; return 1; }
[[ -n "${run_id}" ]] || { echo 'Run id is not set.'; return 1; }
mkdir -p "$(dirname "${file_path}")"
cat > "${file_path}" <<__EOF__
{
"scriptStatus": ${test_status},
"testId": "${test_id}",
"date": "$(date +%s)",
"runId": "${run_id}",
"headCommitHash": "$(git rev-parse --verify HEAD)"
}
__EOF__
return 0
}
# Uses 3 functions to detect memory leak for stress tests.
# 1) call detect_memory_leak_init() before your loop
# 2) call detect_memory_leak_check() for each iteration
# 3) call detect_memory_leak_final() at the end.
function detect_memory_leak_init() {
local host=${1}
# host format has to be: proto://host:port. port is optonal.
STATUS_SERVER="http:$(echo $host|awk -F : '{ print $2 }'):8090"
echo "STATUS_SERVER: ${STATUS_SERVER}"
START_MEMORY_USAGE=0
INCREASED_MEMORY_USAGE=0
}
function detect_memory_leak_check() {
local run_count=${1}
local local_json="$(mktemp /tmp/XXXXXX.json)"
curl "${STATUS_SERVER}/endpoints_status" > "${local_json}"
python -m json.tool "${local_json}"
local curr_usage=$(python -c "import json, sys; obj = json.load(open(\"${local_json}\")); \
print obj['processes'][0]['memoryUsage']")
rm "${local_json}"
[[ -n "${curr_usage}" ]] || { echo "Could not extract memory usage"; return 1; }
if [[ ${run_count} -eq 1 ]]; then
START_MEMORY_USAGE=${curr_usage}
echo "Start Memory Usage (Bytes): ${START_MEMORY_USAGE}."
else
INCREASED_MEMORY_USAGE=$((curr_usage - START_MEMORY_USAGE))
echo "Memory Increased in Test ${run_count} (Bytes): ${INCREASED_MEMORY_USAGE}"
fi
}
function detect_memory_leak_final() {
[[ ${INCREASED_MEMORY_USAGE} -gt 0 ]] \
|| { echo "Only run test once."; return 0; }
local memory_increased=$((INCREASED_MEMORY_USAGE / (1024*1024) ))
echo "Memory Increased (MB): ${memory_increased} ."
local threshold=40
echo "Memory Leak Threshold (MB): ${threshold}."
if [[ ${memory_increased} -gt ${threshold} ]]; then
echo "************ Memory leak is detected. *************"
return 1
else
return 0
fi
}
# Extract key from test env json created from
# script/create-test-env-json script.
# As an example, from this json
# {
# "test": "test-id",
# "run_id": "test-id-1902",
# "run_description": "Commit message",
# "owner": "John Doe"
# }
function extract_key_from_test_env_file() {
local key="${1}"
local json_path="${2}"
cat "${json_path}" \
| python -c "import json,sys;obj=json.load(sys.stdin);print obj['${key}']" \
|| { echo "Could not extract ${key} from ${json_path}"; return 1; }
}
# Current gcloud account may not have permission to deploy an GAE Flex
# application in an esp-load-test project. The following will activate
# esp-load-test service account.
# The esp-load-test key will expire in 3 months, need to re-create a new one every 3 months.
function activate_service_account() {
local project="${1}"
local remote_json_name=''
local service_account=''
local local_json="$(mktemp /tmp/XXXXXX.secret.json)"
case "${project}" in
esp-load-test)
remote_json_name='esp-load-test-9a393df12218.json'
service_account='service-control-in-load-test@esp-load-test.iam.gserviceaccount.com'
;;
esp-long-run)
remote_json_name='esp-long-run-4f1701e6f180.json'
service_account='[email protected]'
;;
*)
echo "Project ${project} does not have associated credentials."
return 1
;;
esac
echo "Downloading ${project} service account"
retry -n 5 -s 10 gsutil cp \
"gs://esp-testing-secret-files/${remote_json_name}" "${local_json}" \
|| error_exit "Could not download secret keys for ${project}"
echo "Gcloud auth activate ${project} service account."
retry -n 2 ${GCLOUD} auth activate-service-account --key-file "${local_json}" \
|| error_exit "Could not activate ${service_account}."
trap "${GCLOUD} auth revoke ${service_account}" EXIT
}
# Clearing apt cache.
function clear_apt() {
echo 'Clearing apt source lists'
${SUDO} rm -rf /var/lib/apt/lists/*
${SUDO} apt-get update --fix-missing -qq && return 0
return 1
}
function update_tool() {
local tool_name="${1}"
local tool_version="${2}"
local local_path="${3}"
local remote_path="gs://${TOOLS_BUCKET}/${tool_name}/${tool_version}/${PLATFORM}/${tool_name}"
[[ -z "${TOOLS_BUCKET}" ]] && return 1
echo "Uploading ${local_path} to ${remote_path}."
${GSUTIL} cp "${local_path}" "${remote_path}" \
|| { echo "Failed to upload ${tool_name} to ${TOOLS_BUCKET}"; return 1; }
return 0
}
function get_tool() {
local tool_name="${1}"
local tool_version="${2}"
local local_path="${3}"
local remote_path="gs://${TOOLS_BUCKET}/${tool_name}/${tool_version}/${PLATFORM}/${tool_name}"
[[ -z "${TOOLS_BUCKET}" ]] && return 1
echo "Downloading ${remote_path} to ${local_path}."
${GSUTIL} cp "${remote_path}" "${local_path}" \
|| { echo "Failed to upload ${tool_name} to ${TOOLS_BUCKET}"; return 1; }
return 0
}