forked from ElasticBox/elastickube
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeploy.sh
executable file
·332 lines (292 loc) · 8.05 KB
/
deploy.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
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
#!/bin/bash
USAGE="Usage : deploy.sh [options]
Example:
deploy.sh -t 600
Options:
-t Timeout in seconds per operation
-u Master Kubernetes URL
-v ElasticKube version to install
-h Display this message
"
function help() {
echo "${USAGE}"
if [[ ${1} ]]
then
echo ${1}
fi
}
# Handle options
while getopts ":t:u:v:hr" ARGUMENT
do
case ${ARGUMENT} in
t ) export TIMEOUT=$OPTARG;;
r ) export REINSTALL=true;;
u ) export KUBERNETES_SERVICE_HOST=$OPTARG;;
v ) export ELASTICKUBE_VERSION=$OPTARG;;
h ) help; exit 0;;
: ) help "Missing option argument for -$OPTARG."; exit 1;;
\?) help "Option does not exist : $OPTARG."; exit 1;;
esac
done
# Default timeout
TIMEOUT=${TIMEOUT:-600}
# DEFAULT VERSION SECTION
VERSION=${ELASTICKUBE_VERSION:-latest}
# DEFAULT VERSION SECTION
if [ "${KUBERNETES_SERVICE_HOST}" ]
then
MASTER_URL_ENV_SECTION="
env:
- name: KUBERNETES_SERVICE_HOST
value: ${KUBERNETES_SERVICE_HOST}
"
fi
cat << \
'______________________________HEADER______________________________'
_____ _ _ _ _ __ _
| ____| | __ _ ___| |_(_) ___| |/ / _| |__ ___
| _| | |/ _` / __| __| |/ __| ' / | | | '_ \ / _ \
| |___| | (_| \__ \ |_| | (__| . \ |_| | |_) | __/
|_____|_|\__,_|___/\__|_|\___|_|\_\__,_|_.__/ \___| by ElasticBox
______________________________HEADER______________________________
PROGRESS_WIDTH=40
ELASTICKUBE_MONGO_RC="
apiVersion: v1
kind: ReplicationController
metadata:
name: elastickube-mongo
namespace: kube-system
labels:
name: elastickube-mongo
spec:
replicas: 1
selector:
name: elastickube-mongo
template:
metadata:
labels:
name: elastickube-mongo
spec:
containers:
- image: mongo
name: elastickube-mongo
args:
- --replSet=elastickube
ports:
- name: mongo
containerPort: 27017
hostPort: 27017
volumeMounts:
- name: mongo-persistent-storage
mountPath: /data/mongodb
volumes:
- name: mongo-persistent-storage
hostPath:
path: /data/mongodb
"
ELASTICKUBE_MONGO_SVC="
apiVersion: v1
kind: Service
metadata:
name: elastickube-mongo
namespace: kube-system
labels:
name: elastickube-mongo
spec:
ports:
- port: 27017
targetPort: 27017
selector:
name: elastickube-mongo
"
ELASTICKUBE_SERVER_RC="
apiVersion: v1
kind: ReplicationController
metadata:
name: elastickube-server
namespace: kube-system
labels:
name: elastickube-server
spec:
replicas: 1
selector:
name: elastickube-server
template:
metadata:
labels:
name: elastickube-server
spec:
containers:
- name: elastickube-api
image: elasticbox/elastickube-api:${VERSION}
resources:
limits:
cpu: 100m
memory: 300Mi
volumeMounts:
- name: elastickube-run
mountPath: /var/run
${MASTER_URL_ENV_SECTION}
- name: elastickube-charts
image: elasticbox/elastickube-charts:${VERSION}
resources:
limits:
cpu: 100m
memory: 300Mi
volumeMounts:
- name: elastickube-charts
mountPath: /var/elastickube/charts
- name: elastickube-nginx
image: elasticbox/elastickube-nginx:${VERSION}
resources:
limits:
cpu: 100m
memory: 300Mi
volumeMounts:
- name: elastickube-run
mountPath: /var/run
ports:
- containerPort: 80
hostPort: 80
name: http
protocol: TCP
- name: elastickube-diagnostics
image: elasticbox/elastickube-diagnostics:${VERSION}
resources:
limits:
cpu: 10m
memory: 32Mi
volumeMounts:
- name: elastickube-run
mountPath: /var/run
volumes:
- name: elastickube-charts
hostPath:
path: /var/elastickube/charts
- name: elastickube-run
hostPath:
path: /var/run/elastickube
"
ELASTICKUBE_SERVER_SVC="
apiVersion: v1
kind: Service
metadata:
name: elastickube-server
namespace: kube-system
labels:
name: elastickube-server
spec:
type: LoadBalancer
ports:
- port: 80
targetPort: 80
selector:
name: elastickube-server
"
SPINNER_STR='|/-\'
retry()
{
local PID=0
local COUNTER=0
local SPINNER_DELAY=0.25
local TEMP_SPINNER=${SPINNER_STR#?}
local RETURN_CODE=-1
SPINNER_STR=${TEMP_SPINNER}${SPINNER_STR%"$TEMP_SPINNER"}
printf "[ %c ] " "${SPINNER_STR}"
until [[ ${COUNTER} -ge $[${TIMEOUT} * 4] ]] || [[ ${RETURN_CODE} -eq 0 ]]
do
if [[ ${RETURN_CODE} -ne 0 ]]
then
eval "$@ 2>>elastickube.log >>elastickube.log &"
PID=$!
fi
while ps a | awk '{print $1}' | grep ${PID} 2>&1 > /dev/null
do
TEMP_SPINNER=${SPINNER_STR#?}
SPINNER_STR=${TEMP_SPINNER}${SPINNER_STR%"$TEMP_SPINNER"}
printf "\b\b\b\b\b\b[ %c ] " "${SPINNER_STR}"
sleep ${SPINNER_DELAY}
COUNTER=$[${COUNTER} + 1]
done
wait ${PID}
RETURN_CODE=$?
done
printf "\b\b\b\b\b\b \b\b\b\b"
return ${RETURN_CODE}
}
check_tool()
{
printf "%-${PROGRESS_WIDTH}s" "Checking ${1} is available"
if which ${1} > /dev/null 2>&1
then
echo [ ✓ ]
else
echo [ NOT FOUND ] && exit 1
fi
}
deploy_rc()
{
printf "%-${PROGRESS_WIDTH}s" "Setting up ${1}"
if ! kubectl --namespace=kube-system get rc ${1} 2>>elastickube.log >>elastickube.log
then
echo "${2}" | kubectl create --validate=false -f - 2>>elastickube.log >>elastickube.log
fi
if retry "kubectl --namespace=kube-system describe rc ${1} 2>>elastickube.log | grep '1 Running / 0 Waiting / 0 Succeeded / 0 Failed'"
then
echo [ ✓ ]
else
echo [ FAILED ] && exit 1
fi
}
deploy_svc()
{
printf "%-${PROGRESS_WIDTH}s" "Setting up ${1} svc"
if ! kubectl --namespace=kube-system get svc ${1} 2>>elastickube.log >>elastickube.log
then
if echo "${2}" | kubectl create --validate=false -f - 2>>elastickube.log >>elastickube.log
then
echo [ ✓ ]
else
echo [ FAILED ] && exit 1
fi
else
echo [ ✓ ]
fi
}
# Check gcloud and kubectl is installed
check_tool kubectl
# Check the cluster is configured
printf "%-${PROGRESS_WIDTH}s" "Verifying Kubernetes cluster"
if kubectl cluster-info 2>>elastickube.log >>elastickube.log
then
echo [ ✓ ]
else
echo [ NOT RUNNING ]
fi
deploy_svc elastickube-server "${ELASTICKUBE_SERVER_SVC}"
deploy_svc elastickube-mongo "${ELASTICKUBE_MONGO_SVC}"
deploy_rc elastickube-mongo "${ELASTICKUBE_MONGO_RC}"
deploy_rc elastickube-server "${ELASTICKUBE_SERVER_RC}"
# Detect whether this is a GKE LoadBalancer
if kubectl get pods --namespace=kube-system -l name=glbc 2>>elastickube.log | grep "Running" >>elastickube.log
then
LOADBALANCER_IP_PROPERTY='LoadBalancer Ingress:'
else
LOADBALANCER_IP_PROPERTY='IP:'
cat << \
______________________________RESULT______________________________
$(tput sgr0)$(tput bold)WARNING:$(tput sgr0) LoadBalancer Ingress not detected, please ensure the address is accessible from outside the cluster. Check http://kubernetes.io/docs/user-guide/ingress/ for more information.
______________________________RESULT______________________________
fi
printf "%-${PROGRESS_WIDTH}s" "Waiting for LB to be ready"
if retry "kubectl --namespace=kube-system describe svc elastickube-server | grep -e '${LOADBALANCER_IP_PROPERTY}'"
then
echo [ ✓ ]
else
echo [ FAILED ] && exit 1
fi
cat << \
______________________________RESULT______________________________
ElasticKube has been deployed!
Please complete the installation here: $(tput sgr0)$(tput bold)http://$(kubectl --namespace=kube-system describe svc elastickube-server | grep -e "${LOADBALANCER_IP_PROPERTY}" | cut -d ":" -f 2 | xargs)$(tput sgr0)
______________________________RESULT______________________________