-
Notifications
You must be signed in to change notification settings - Fork 23
/
verify-functions.sh
executable file
·293 lines (275 loc) · 7.14 KB
/
verify-functions.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
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
#! /usr/bin/env bash
# Copyright 2018 Google LLC
#
# 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
#
# https://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.
# Library of functions used by the deployment/teardown scripts
# Check if a cluster's firewalls exist
# Globals:
# None
# Arguments:
# PROJECT
# CLUSTER
# Returns:
# 1
function firewall_exists() {
local PROJECT="$1"
local CLUSTER="$2"
local EXISTS
EXISTS=$(gcloud compute firewall-rules list --project "$PROJECT" --filter "name=$CLUSTER" --format "value(name)")
if [[ "${EXISTS}" != "" ]]; then
echo ""
echo "the $CLUSTER_NAME firewalls exist"
echo ""
return 0
fi
return 1
}
# Check if a GCP project with the provided ID exists
# Globals:
# None
# Arguments:
# PROJECT
# Returns:
# None
function project_exists() {
local PROJECT="${1}"
local EXISTS
EXISTS=$(gcloud projects list --filter "projectId=${PROJECT}" --format "value(projectId)")
if [[ "${EXISTS}" != "" ]]; then
echo "The project ${PROJECT} exists"
return 0
fi
return 1
}
# Check if a given network exists
# Globals:
# None
# Arguments:
# PROJECT
# NETWORK
# Returns:
# None
function network_exists() {
local PROJECT="${1}"
local NETWORK="${2}"
local EXISTS
EXISTS=$(gcloud compute networks list --project "${PROJECT}" --filter "name=${NETWORK}" --format "value(name)")
if [[ "${EXISTS}" != "" ]]; then
echo "The ${NETWORK} network exists"
return 0
fi
return 1
}
# Check if a given network is not the last in the project
# Globals:
# None
# Arguments:
# PROJECT
# NETWORK
# Returns:
# None
function network_is_not_last() {
local PROJECT="${1}"
local NETWORK="${2}"
local EXISTS
EXISTS=$(gcloud compute networks list --project "${PROJECT}" --filter "NOT name=${NETWORK}" --format "value(name)")
if [[ "${EXISTS}" != "" ]]; then
echo "The ${NETWORK} network is not the last one in the project"
return 0
fi
return 1
}
# Check if a cluster's firewalls exist
# Globals:
# None
# Arguments:
# PROJECT
# INSTANCE
# Returns:
# None
function instance_exists() {
local PROJECT="${1}"
local INSTANCE="${2}"
local EXISTS
EXISTS=$(gcloud compute instances list --project "${PROJECT}" --filter "name=${INSTANCE}" --format "value(name)")
if [[ "${EXISTS}" == "${INSTANCE}" ]]; then
echo "The instance ${INSTANCE} exists"
return 0
fi
return 1
}
# Check if a cluster exists
# Globals:
# None
# Arguments:
# PROJECT
# CLUSTER
# Returns:
# None
function cluster_exists() {
local PROJECT="${1}"
local CLUSTER="${2}"
local EXISTS
EXISTS=$(gcloud container clusters list --project "${PROJECT}" --filter "name=${CLUSTER}" --format "value(name)")
if [[ "${EXISTS}" == "${CLUSTER}" ]]; then
echo "The cluster ${CLUSTER} exists"
return 0
fi
return 1
}
# Check if a directory exists
# Globals:
# None
# Arguments:
# DIR
# Returns:
# None
function directory_exists() {
local DIR="${1}"
if [[ -d "${DIR}" ]]; then
echo "The directory ${DIR} exists"
return 0
fi
return 1
}
# Check if a file exists
# Globals:
# None
# Arguments:
# FILE
# Returns:
# None
function file_exists() {
local FILE="${1}"
if [[ -e "${FILE}" ]]; then
echo "The file ${FILE} exists"
return 0
fi
return 1
}
# Check if required binaries exist
# Globals:
# None
# Arguments:
# None
# Returns:
# None
function dependency_installed () {
command -v "${1}" >/dev/null 2>&1 || exit 1
}
# Enable required API's that are not already enabled
# Globals:
# None
# Arguments:
# PROJECT
# API
# Returns:
# None
function enable_project_api() {
# Check if the API is already enabled for the sake of speed
if [[ $(gcloud services list --project="${1}" \
--format="value(serviceConfig.name)" \
--filter="serviceConfig.name:${2}" 2>&1) != \
"${2}" ]]; then
echo "Enabling the API ${2}"
gcloud services enable "${2}" --project="${1}"
else
echo "The API ${2} is already enabled for project ${1}"
fi
}
# Check if a service with a given name is installed
# Globals:
# None
# Arguments:
# SERVICE_NAME - Name of service to check
# RETRY_COUNT - Number of times to retry
# INTERVAL - Amount of time to sleep between retries
# NAMESPACE - k8s namespace the service lives in
# Returns:
# None
function service_is_installed () {
# local SERVICE_NAME="${1}"
# local RETRY_COUNT="${2}"
# local SLEEP_INTERVAL="${3}"
# local NAMESPACE="${4}"
for ((i=0; i<${2}; i++)); do
SERVICE=$(kubectl get -n "${4}" service "${1}" -o=name)
if [ "${1}" == "" ] ; then
echo "Attempt $((i + 1)): Service ${1} was not yet found in namespace ${4}" >&1
sleep "${3}"
else
echo "Attempt $((i + 1)): Service ${1} has been created" >&1
return 0
fi
done
return 1
}
# Check if a service with the given label is running
# Globals:
# None
# Arguments:
# SERVICE_NAME - Name of service to check
# RETRY_COUNT - Number of times to retry
# INTERVAL - Amount of time to sleep between retries
# NAMESPACE - k8s namespace the service lives in
# Returns:
# None
function service_ip_is_allocated () {
local SERVICE="${1}"
local RETRY_COUNT="${2}"
local SLEEP="${3}"
local NAMESPACE="${4}"
for ((i=0; i<"${RETRY_COUNT}"; i++)); do
IP=$(kubectl get -n "${NAMESPACE}" service "${SERVICE}" \
-o jsonpath='{.status.loadBalancer.ingress[0].ip}')
if [ "${IP}" == "" ] ; then
echo "Attempt $((i + 1)): IP not yet allocated for service ${SERVICE}" >&1
else
echo "Attempt $((i + 1)): IP has been allocated for service ${SERVICE}" >&1
return 0
fi
sleep "${SLEEP}"
done
echo "Timed out waiting for service ${SERVICE} to be allocated an IP address." >&1
return 1
}
# Check if a pod with the given label is running
# Globals:
# None
# Arguments:
# POD_LABEL - label applied to pod to check
# RETRY_COUNT - Number of times to retry
# INTERVAL - Amount of time to sleep between retries
# NAMESPACE - k8s namespace the pod lives in
# Returns:
# None
function pod_is_running () {
local POD_LABEL="${1}"
local RETRY_COUNT="${2}"
local SLEEP="${3}"
local NAMESPACE="${4}"
for ((i=0; i<"${RETRY_COUNT}"; i++)); do
POD=$(kubectl get -n "${NAMESPACE}" pod --selector="${POD_LABEL}" \
--output=jsonpath="{.items[*].metadata.name}" \
--field-selector=status.phase=Running)
if [ "${POD}" == "" ] ; then
echo "Attempt $((i + 1)): Waiting for pod ${POD_LABEL} in namespace ${NAMESPACE}..." >&1
sleep "${SLEEP}"
else
echo "Attempt $((i + 1)): Pod ${POD_LABEL} is up and running" >&1
return 0
fi
done
echo "Timed out waiting for pod ${POD_LABEL} to start. Exiting..." >&1
return 1
}