-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.sh
executable file
·271 lines (247 loc) · 9.2 KB
/
test.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
#!/bin/bash
# ------------------------------------------------------------------------
#
# Copyright 2005-2015 WSO2, Inc. (http://wso2.com)
#
# 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
# ------------------------------------------------------------------------
########################### Simple Test Script ##############################
# #
# This script will build the default version of specified docker images, #
# scp them to the Kubernetes nodes and deploy the Kubernetes artifacts. #
# #
# Tested with the Kubernetes setup at: #
# https://github.com/pires/kubernetes-vagrant-coreos-cluster #
# #
# Usage: #
# 1. set PUPPET_HOME, KUBERNETES_NODE_USER, KUBERNETES_NODE, #
# DOCKERFILES_HOME and KUBERNETES_HOME in environment.bash # #
# #
# 2. add the products, versions and profiles that need to be tested #
# to the array 'products' as comma separated tuples. #
# ex.: products=(wso2am,1.9.1,default wso2is,5.0.0,manager) #
# #
# 3. run the script #
# #
# #
#############################################################################
set -e
kubernetes_artifact_version=1.0.0
source environment.bash
source ~/.bash_profile
prgdir=`dirname "$0"`
current_path=`cd "$prgdir"; pwd`
#root_dir=${current_path}/..
#common_dir=${current_path}/../common
#base_image_dir=`cd "${common_dir}/docker/base-image"; pwd`
user=`whoami`
echo "user executing the script $user"
# products to be tested
products=(wso2am,1.9.1,default wso2das,3.0.1,default wso2is,5.1.0,default)
declare -A results
function echoError {
echo $'\e[1;31m'"${1}"$'\e[0m'
}
function echoSuccess {
echo $'\e[1;32m'"${1}"$'\e[0m'
}
function echoBold {
echo $'\e[1m'"${1}"$'\e[0m'
}
# build base image
function build_base_image {
# navigate to base image dir
pushd ${DOCKERFILES_HOME}/common/base-image > /dev/null
# build
# sudo bash build.sh ${kubernetes_artifact_version}
# go back to previous directory location
popd > /dev/null
}
function build_docker_image_and_scp {
# switch to IS 5 docker directory and build
pushd ${DOCKERFILES_HOME}/$1 > /dev/null
sudo --preserve-env bash build.sh -v "$2" -i ${kubernetes_artifact_version} -l "$3" <<< 'y' || cleanup
# save the image as a tar file
sudo bash save.sh -v "$2" -i ${kubernetes_artifact_version} -l "$3" || cleanup
sudo chown ${user}:${user} -R ~/docker/
bash scp.sh -h ${KUBERNETES_NODE_USER}@${KUBERNETES_NODE} -v "$2" -i ${kubernetes_artifact_version} -l "$3" || cleanup
popd > /dev/null
}
function undeploy_kubernetes_artifacts {
# switch to IS 5 kubernetes directory deploy kubernetes artifacts
pushd ${KUBERNETES_HOME}/$1 > /dev/null
pushd ${KUBERNETES_HOME}/$1 > /dev/null
bash undeploy.sh
popd > /dev/null
}
function deploy_kubernetes_service {
# switch to IS 5 kubernetes directory deploy kubernetes artifacts
bash ${KUBERNETES_HOME}/common/scripts/deploy-kubernetes-service.sh "$1" "$2" || cleanup
}
function deploy_kubernetes_rc {
# switch to IS 5 kubernetes directory deploy kubernetes artifacts
bash ${KUBERNETES_HOME}/common/scripts/deploy-kubernetes-rc.sh "$1" "$2" || cleanup
}
function check_status {
error_count=0
success_count=0
while true; do
# check the pod status
result=`kubectl get pods | grep $1-$3`
if [[ -z "$result" ]]; then
echo "no pod found for product $1"
error_count=$((error_count + 1))
success_count=0
fi
IFS=' ' read -r -a array <<< "$result"
pod_name=${array[0]}
ready=${array[1]}
state=${array[2]}
restarts=${array[3]}
if [[ ${ready} != '1/1' ]] || [[ $state != 'Running' ]] || [[ $restarts != '0' ]]; then
echo "error condition detected in pod=$pod_name, ready=$ready, status=$state, restarts=$restarts"
error_count=$((error_count + 1))
success_count=0
else
success_count=$((success_count + 1))
error_count=0
fi
if [[ ${error_count} -gt 5 ]]; then
echo "error condition threshold reached: $error_count, aborting"
#undeploy_kubernetes_artifacts "$1"
results[$1-$2-$3]="ERROR, last pod name=$pod_name, state=$state, restarts=$restarts"
break
elif [[ ${success_count} -gt 5 ]]; then
echo "success condition threshold reached: $success_count"
server_started=$(check_carbon_server_has_started "$pod_name")
if [[ ${server_started} -eq 0 ]]; then
msg="Carbon Server $1-$2 in pod $pod_name has started successfully"
echo ${msg}
results[$1-$2-$3]="SUCCESS, $msg"
break
else
msg="Carbon Server $1-$2 in pod $pod_name has failed to start"
echo ${msg}
#undeploy_kubernetes_artifacts "$1"
results[$1-$2-$3]="ERROR, $msg"
break
fi
else
sleep 6s
fi
done
}
function check_carbon_server_has_started {
tries=0
while true; do
carbon_logs=`kubectl logs $1`
if [[ ${carbon_logs} =~ "Mgt Console URL" ]]; then
echo 0
break
else
tries=$((tries + 1))
# allow the Carbon server to start for 5 minutes (6s * 50 = 5 mins)
if [[ ${tries} -gt 50 ]]; then
echo 1
break
fi
fi
sleep 6s
done
}
function validate_environment {
if [ -z "$PUPPET_HOME" ]; then
echo "please set environment variable PUPPET_HOME"
exit
fi
if [ -z "$DOCKERFILES_HOME" ]; then
echo "please set environment variable DOCKERFILES_HOME"
exit
fi
if [ -z "$KUBERNETES_HOME" ]; then
echo "please set environment variable KUBERNETES_HOME"
exit
fi
if [ -z "$KUBERNETES_NODE" ]; then
echo "please set environment variable KUBERNETES_NODE"
exit
fi
if [ -z "$KUBERNETES_NODE_USER" ]; then
echo "please set environment variable KUBERNETES_NODE_USER"
exit
fi
}
function test {
# build and scp
for product in ${products[@]}; do
IFS=","
set ${product}
echo "building docker image for=$1 version=$2 profile=$3"
build_docker_image_and_scp "$1" "$2" "$3"
unset IFS
done
# deploy all Services
for product in ${products[@]}; do
IFS=","
set ${product}
echo "deploying kubernetes Service for=$1 version=$2 profile=$3"
deploy_kubernetes_service "$1" "$3"
unset IFS
done
# deploy RCs and check status
for product in ${products[@]}; do
IFS=","
set ${product}
echo "deploying kubernetes RC for=$1 version=$2 profile=$3"
deploy_kubernetes_rc "$1" "$3"
check_status "$1" "$2" "$3"
echo "undeploying kubernetes artifacts for=$1 version=$2 profile=$3"
undeploy_kubernetes_artifacts "$1"
unset IFS
done
}
function print_results {
unset IFS
echoBold "################################################# Results #################################################"
for product in ${products[@]}; do
IFS=","
set ${product}
test_result=${results[$1-$2-$3]}
if [[ ${test_result} == ERROR* ]]; then
echoError "$1-$2-$3: ${test_result}"
else
echoSuccess "$1-$2-$3: ${test_result}"
fi
unset IFS
done
echoBold "############################################## End of Results #############################################"
}
function cleanup {
unset IFS
for product in ${products[@]}; do
IFS=","
set ${product}
echo "Cleaning up for=$1 version=$2 profile=$3"
undeploy_kubernetes_artifacts "$1"
unset IFS
done
exit 1
}
# validate
validate_environment
# build the base images
build_base_image
# build and deploy the products
test
# print results
print_results