-
Notifications
You must be signed in to change notification settings - Fork 27
/
init.sh
executable file
·576 lines (421 loc) · 15.3 KB
/
init.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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
#!/bin/bash
SCRIPT_BASE_DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
IOT_OCP_PROJECT="iot-ocp"
MQ_USER="iotuser"
MQ_PASSWORD="iotuser"
KIE_USER="kieuser"
KIE_PASSWORD="kieuser1!"
GIT_BRANCH="master"
POSTGRESQL_APP_NAME="postgresql"
POSTGRESQL_USERNAME="postgresiot"
POSTGRESQL_PASSWORD="postgresiot"
POSTGRESQL_DATABASE="iot"
AMQ_SSL_PASSWORD="iotocp"
BUILD_CHECK_INTERVAL=5
BUILD_CHECK_TIMES=60
DEPLOYMENT_CHECK_INTERVAL=10
DEPLOYMENT_CHECK_TIMES=60
CURRENT_STAGE=
POSTGRESQL_LABEL="iotphase=postgresql"
AMQ_LABEL="iotphase=amq"
KIE_LABEL="iotphase=rules"
FIS_LABEL="iotphase=integration"
SOFTWARE_SENSOR_LABEL="iotphase=software-sensor"
ZEPPELIN_LABEL="iotphase=visualization"
ZEPPELIN_RHEL_BASE_IMAGESTREAM="rhel7:7.2"
ZEPPELIN_CENTOS_BASE_IMAGESTREAM="centos:7"
ZEPPELIN_BASE_IMAGESTREAM=${ZEPPELIN_RHEL_BASE_IMAGESTREAM}
PROJECT_SUFFIX=
ADMIN_ADDL_USERNAME=
SKIP_STEPS=
STAGES=(configure-ocp postgresql amq rules integration software-sensor build-zeppelin configure-zeppelin)
trap exit_message EXIT
function exit_message() {
exit_code=$?
if [ ! -z ${CURRENT_STAGE} ]; then
echo
echo "Provisioning Failed. Execute \"$0 --restart-from=$CURRENT_STAGE\" Along With Other Provided Parameters to Restart From The Failed Stage"
echo
fi
exit $exit_code
}
# Show script usage
usage() {
echo "
Usage: $0 [options]
Options:
--zeppelin-base=<rhel7|centos> : Base used to build Zeppelin Image (Default: rhel7)
--restart-from=<phase> : Phase to restart execution
--project-suffix=<suffix> : Name of the suffix to apply to a project
--skip-steps=<steps> : Comma separated list of steps to skip
--user=<user> : User to add as an Administrator to the project
-h|--help : Show script usage
"
}
function findIndex() {
QUERY=$1
START=-1
if [ ! -z ${QUERY} ]; then
for i in "${!STAGES[@]}"; do
if [[ "${STAGES[$i]}" = "${QUERY}" ]]; then
START=${i}
fi
done
fi
echo ${START}
}
function check_restart() {
if [ ! -z $1 ] && [ "$1" == "restart" ]; then
return 0
else
return 1
fi
}
function validate_build_success() {
FIRST_BUILD_NAME="${1}-1"
# Get status of finished build
BUILD_STATUS=$(oc get -n ${IOT_OCP_PROJECT} build ${FIRST_BUILD_NAME} --template='{{ .status.phase }}')
if [ ${BUILD_STATUS} != "Complete" ]; then
echo
echo "Build did not complete successfully. Status is: ${BUILD_STATUS}"
echo
exit 1
fi
}
function wait_for_running_build() {
BUILD_NAME="$1-1"
COUNTER=0
# Check to see if build exists
while [ ${COUNTER} -lt $BUILD_CHECK_TIMES ]
do
BUILD_COUNT=$(oc get -n ${IOT_OCP_PROJECT} builds ${BUILD_NAME} --no-headers | wc -l | awk '{ print $1 }')
if [ $BUILD_COUNT == "1" ]; then
break
fi
if [ $COUNTER -lt $BUILD_CHECK_TIMES ]; then
COUNTER=$(( $COUNTER + 1 ))
fi
if [ $COUNTER -eq $BUILD_CHECK_TIMES ]; then
echo "Max Validation Attempts Exceeded. Failed Verifying Application Build..."
exit 1
fi
sleep $BUILD_CHECK_INTERVAL
done
COUNTER=0
# Check to see we have a running build
while [ ${COUNTER} -lt $BUILD_CHECK_TIMES ]
do
BUILD_STATUS=$(oc get -n ${IOT_OCP_PROJECT} builds ${APP_NAME}-1 --template='{{ .status.phase }}')
if [ $BUILD_STATUS == "Running" ]; then
break
elif [ $BUILD_STATUS == "Failed" ]; then
echo "Build has failed"
exit 2
fi
if [ $COUNTER -lt $BUILD_CHECK_TIMES ]; then
COUNTER=$(( $COUNTER + 1 ))
fi
if [ $COUNTER -eq $BUILD_CHECK_TIMES ]; then
echo "Max Validation Attempts Exceeded. Failed Verifying Application Build..."
exit 1
fi
sleep $BUILD_CHECK_INTERVAL
done
}
function wait_for_application_deployment() {
DC_NAME=$1
DEPLOYMENT_VERSION=
RC_NAME=
COUNTER=0
# Validate Deployment is Active
while [ ${COUNTER} -lt $DEPLOYMENT_CHECK_TIMES ]
do
DEPLOYMENT_VERSION=$(oc get -n ${IOT_OCP_PROJECT} dc ${DC_NAME} --template='{{ .status.latestVersion }}')
RC_NAME="${DC_NAME}-${DEPLOYMENT_VERSION}"
if [ "${DEPLOYMENT_VERSION}" == "1" ]; then
break
fi
if [ $COUNTER -lt $DEPLOYMENT_CHECK_TIMES ]; then
COUNTER=$(( $COUNTER + 1 ))
fi
if [ $COUNTER -eq $DEPLOYMENT_CHECK_TIMES ]; then
echo "Max Validation Attempts Exceeded. Failed Verifying Application Deployment..."
exit 1
fi
sleep $DEPLOYMENT_CHECK_INTERVAL
done
COUNTER=0
# Validate Deployment Complete
while [ ${COUNTER} -lt $DEPLOYMENT_CHECK_TIMES ]
do
DEPLOYMENT_STATUS=$(oc get -n ${IOT_OCP_PROJECT} rc/${RC_NAME} --template '{{ index .metadata.annotations "openshift.io/deployment.phase" }}')
if [ ${DEPLOYMENT_STATUS} == "Complete" ]; then
break
elif [ ${DEPLOYMENT_STATUS} == "Failed" ]; then
echo "Deployment Failed!"
exit 1
fi
if [ $COUNTER -lt $DEPLOYMENT_CHECK_TIMES ]; then
COUNTER=$(( $COUNTER + 1 ))
fi
if [ $COUNTER -eq $DEPLOYMENT_CHECK_TIMES ]; then
echo "Max Validation Attempts Exceeded. Failed Verifying Application Deployment..."
exit 1
fi
sleep $DEPLOYMENT_CHECK_INTERVAL
done
}
function validate_build_deploy() {
APP_NAME=$1
# Wait for a build to be running
echo
echo "Waiting for a running build.."
echo
wait_for_running_build "${APP_NAME}"
oc logs -n ${IOT_OCP_PROJECT} build/${APP_NAME}-1 -f
sleep 10
BUILD_STATUS=$(oc get -n ${IOT_OCP_PROJECT} build ${APP_NAME}-1 --template='{{ .status.phase }}')
if [ ${BUILD_STATUS} != "Complete" ]; then
echo
echo "${APP_NAME} build did not complete successfully. Status is: ${BUILD_STATUS}"
echo
exit 1
fi
# Pause 10 seconds
sleep 10
echo
echo "Waiting for ${APP_NAME} to deploy..."
echo
wait_for_application_deployment "${APP_NAME}"
}
# Begin Chained Components
function do_configure_ocp() {
if check_restart $1
then
ACTION="replace"
else
ACTION="create"
fi
CURRENT_STAGE="configure-ocp"
ZEPPELIN_IMAGE=$(echo ${ZEPPELIN_BASE_IMAGESTREAM} | cut -d: -f1)
echo "Setting up OpenShift IoT Example Project"
echo
echo "Creating Project: ${IOT_OCP_PROJECT}..."
echo
oc new-project ${IOT_OCP_PROJECT} --description="Showcases an Intelligent Internet-of-Things (IoT) Gateway on Red Hat’s OpenShift Container Platform" --display-name="Internet of Things (IoT) OpenShift Demo Project" >/dev/null 2>&1
if [ ! -z ${ADMIN_ADDL_USERNAME} ]; then
echo
echo "Adding admin role to ${ADMIN_ADDL_USERNAME}..."
echo
oc policy add-role-to-user admin ${ADMIN_ADDL_USERNAME} -n ${IOT_OCP_PROJECT}
fi
echo
echo "Creating ImageStreams..."
echo
oc $ACTION -n ${IOT_OCP_PROJECT} -f ${SCRIPT_BASE_DIR}/support/templates/jboss-image-streams.json
oc $ACTION -n ${IOT_OCP_PROJECT} -f ${SCRIPT_BASE_DIR}/support/templates/fis-image-streams.json
oc $ACTION -n ${IOT_OCP_PROJECT} -f ${SCRIPT_BASE_DIR}/support/templates/openjdk18-openshift-is.json
oc $ACTION -n ${IOT_OCP_PROJECT} -f ${SCRIPT_BASE_DIR}/support/templates/${ZEPPELIN_IMAGE}-is.json
echo
echo "Pausing 10 Seconds..."
echo
sleep 10
echo
echo "Importing ImageStreams..."
echo
oc import-image -n ${IOT_OCP_PROJECT} jboss-decisionserver63-openshift --all=true >/dev/null 2>&1
oc import-image -n ${IOT_OCP_PROJECT} jboss-amq-62 --all=true >/dev/null 2>&1
oc import-image -n ${IOT_OCP_PROJECT} fis-karaf-openshift --all=true >/dev/null 2>&1
oc import-image -n ${IOT_OCP_PROJECT} openjdk18-openshift --all=true >/dev/null 2>&1
oc import-image -n ${IOT_OCP_PROJECT} ${ZEPPELIN_IMAGE} --all=true >/dev/null 2>&1
}
function do_postgresql() {
CURRENT_STAGE="postgresql"
if check_restart $1
then
oc delete all -l ${POSTGRESQL_LABEL}
oc delete pvc -l ${POSTGRESQL_LABEL}
sleep 15
fi
echo
echo "Deploying PostgreSQL..."
echo
oc process -v=POSTGRESQL_DATABASE=${POSTGRESQL_DATABASE} -v=POSTGRESQL_USER=${POSTGRESQL_USERNAME} -v=POSTGRESQL_PASSWORD=${POSTGRESQL_PASSWORD} -l ${POSTGRESQL_LABEL} -f ${SCRIPT_BASE_DIR}/support/templates/database-postgresql.json | oc create -n ${IOT_OCP_PROJECT} -f-
echo
echo "Waiting for PostgreSQL to deploy..."
echo
wait_for_application_deployment "${POSTGRESQL_APP_NAME}"
echo
echo "Creating Database Schema..."
echo
POSTGRESQL_POD_NAME=$(oc get pods -l "name=${POSTGRESQL_APP_NAME}" --template='{{ index .items 0 "metadata" "name" }}')
oc rsync "${SCRIPT_BASE_DIR}/support/sql" $POSTGRESQL_POD_NAME:/tmp/
sleep 10
oc rsh -t ${POSTGRESQL_POD_NAME} bash -c 'psql -f /tmp/sql/postgresql-iot.sql --variable=measureOwner=$POSTGRESQL_USER $POSTGRESQL_DATABASE'
}
function do_amq() {
CURRENT_STAGE="amq"
if check_restart $1
then
oc delete all -l ${AMQ_LABEL}
sleep 15
fi
echo
echo "Deploying AMQ..."
echo
oc process -v=MQ_USERNAME=${MQ_USER} -v=MQ_PASSWORD=${MQ_PASSWORD} -v=IMAGE_STREAM_NAMESPACE=${IOT_OCP_PROJECT} -v=AMQ_TRUSTSTORE_PASSWORD=${AMQ_SSL_PASSWORD} -v=AMQ_KEYSTORE_PASSWORD=${AMQ_SSL_PASSWORD} -l ${AMQ_LABEL} -f ${SCRIPT_BASE_DIR}/support/templates/broker-amq.json | oc create -n ${IOT_OCP_PROJECT} -f-
echo
echo "Waiting for AMQ to deploy..."
echo
wait_for_application_deployment "broker-amq"
}
function do_rules() {
CURRENT_STAGE="rules"
if check_restart $1
then
oc delete all -l ${KIE_LABEL}
sleep 15
fi
echo
echo "Deploying Decision Server..."
echo
oc process -v=KIE_SERVER_USER="${KIE_USER}" -v=KIE_SERVER_PASSWORD="${KIE_PASSWORD}" -v=IMAGE_STREAM_NAMESPACE=${IOT_OCP_PROJECT} -v=SOURCE_REPOSITORY_REF=${GIT_BRANCH} -l ${KIE_LABEL} -f ${SCRIPT_BASE_DIR}/support/templates/rules-brms.json | oc create -n ${IOT_OCP_PROJECT} -f-
validate_build_deploy "rules-brms"
}
function do_integration() {
CURRENT_STAGE="integration"
if check_restart $1
then
oc delete all -l ${FIS_LABEL}
sleep 15
fi
echo
echo "Deploying Integration Application..."
echo
oc process -v=GIT_REF=${GIT_BRANCH} -v=IMAGE_STREAM_NAMESPACE=${IOT_OCP_PROJECT} -l ${FIS_LABEL} -f ${SCRIPT_BASE_DIR}/support/templates/integration-fis.json | oc create -n ${IOT_OCP_PROJECT} -f-
validate_build_deploy "integration-fis"
}
function do_software_sensor() {
CURRENT_STAGE="software-sensor"
if check_restart $1
then
oc delete all -l ${SOFTWARE_SENSOR_LABEL}
sleep 15
fi
echo
echo "Deploying Software Sensor Application..."
echo
oc process -v=SOURCE_REPOSITORY_REF=${GIT_BRANCH} -v=IMAGE_STREAM_NAMESPACE=${IOT_OCP_PROJECT} -l ${SOFTWARE_SENSOR_LABEL} -f ${SCRIPT_BASE_DIR}/support/templates/software-sensor.json | oc create -n ${IOT_OCP_PROJECT} -f-
validate_build_deploy "software-sensor"
}
function do_build_zeppelin() {
CURRENT_STAGE="build-zeppelin"
if check_restart $1
then
oc delete all -l ${ZEPPELIN_LABEL}
oc delete pvc -l ${ZEPPELIN_LABEL}
sleep 15
fi
echo
echo "Deploying Visualization Tool..."
echo
oc process -v=SOURCE_REPOSITORY_REF=${GIT_BRANCH} -v=BASE_IMAGESTREAMTAG=${ZEPPELIN_BASE_IMAGESTREAM} -l ${ZEPPELIN_LABEL} -f ${SCRIPT_BASE_DIR}/support/templates/visualization-zeppelin.json | oc create -n ${IOT_OCP_PROJECT} -f-
validate_build_deploy "visualization-zeppelin"
sleep 10
}
function do_configure_zeppelin() {
CURRENT_STAGE="configure-zeppelin"
echo
echo "Adding IOT Postgresql Interperter"
echo
# Get Route
ZEPPELIN_ROUTE=$(oc get routes visualization-zeppelin --template='{{ .spec.host }}')
ZEPPELIN_STATUS=$(curl -s -o /dev/null -w '%{http_code}' -H "Content-Type: application/json" -X POST -d "{\"name\":\"iot-ocp\",\"group\":\"psql\",\"properties\":{\"postgresql.password\":\"${POSTGRESQL_PASSWORD}\",\"postgresql.max.result\":\"1000\",\"postgresql.user\":\"${POSTGRESQL_USERNAME}\",\"postgresql.url\":\"jdbc:postgresql://postgresql:5432/iot\",\"postgresql.driver.name\":\"org.postgresql.Driver\"},\"dependencies\":[],\"option\":{\"remote\":true,\"isExistingProcess\":false,\"perNoteSession\":false,\"perNoteProcess\":false},\"propertyValue\":\"\",\"propertyKey\":\"\"}" http://${ZEPPELIN_ROUTE}/api/interpreter/setting)
if [ $ZEPPELIN_STATUS -ne 201 ]; then
echo "Failed to configure zeppelin."
fi
}
# Prerequisites
oc whoami >/dev/null 2>&1 || { echo "Cannot validate connectivity to OpenShift. Ensure oc client tool installed and logged in" ; exit 1; }
RESTART_OPTION=
# Process Input
for i in "$@"
do
case $i in
--zeppelin-base=*)
USER_ZEPPELIN_BASE="${i#*=}"
shift;;
--restart-from=*)
RESTART_OPTION="${i#*=}"
shift;;
--project-suffix=*)
IOT_OCP_PROJECT="${IOT_OCP_PROJECT}-${i#*=}"
shift;;
--skip-steps=*)
SKIP_STEPS="${i#*=}"
shift;;
--user=*)
ADMIN_ADDL_USERNAME="${i#*=}"
shift;;
-h|--help|*)
usage
exit
esac
done
# Validate Zeppelin Base
if [ ! -z ${USER_ZEPPELIN_BASE} ]; then
if [ "${USER_ZEPPELIN_BASE}" == "rhel" ]; then
ZEPPELIN_BASE_IMAGESTREAM=${ZEPPELIN_RHEL_BASE_IMAGESTREAM}
elif [ "${USER_ZEPPELIN_BASE}" == "centos" ]; then
ZEPPELIN_BASE_IMAGESTREAM=${ZEPPELIN_CENTOS_BASE_IMAGESTREAM}
else
echo "Invalid Zeppelin Base. Either rhel or centos must be provided"
exit
fi
fi
############################################
START_STAGE_INDEX=0
# Determine step to restart from
if [ ! -z ${RESTART_OPTION} ]; then
# Check position to restart at
START_STAGE_INDEX=$(findIndex ${RESTART_OPTION})
if [ $START_STAGE_INDEX -eq -1 ]; then
echo "Invalid Restart Option: $RESTART_OPTION"
echo
echo "Restart Options:"
for i in "${STAGES[@]}"; do
echo " * ${i}"
done
exit 1
fi
echo "Restarting at Step: ${RESTART_OPTION}"
echo
fi
#Run through steps
for step in "${STAGES[@]:${START_STAGE_INDEX}}"
do
for skipped_step in ${SKIP_STEPS//,/ } ; do
if [[ "${step}" = "${skipped_step}" ]]; then
echo "Skipping Step: ${step}"
echo
continue 2
fi
done
# Execute Step
echo "Executing Step: ${step}"
echo
if [ "$step" == "${RESTART_OPTION}" ]; then
RESTART_SWITCH="restart"
else
RESTART_SWITCH=""
fi
eval do_${step//-/_} ${RESTART_SWITCH}
done
##############################################
echo "============================================="
echo
echo "OpenShift IoT Example Project Setup Complete."
echo
echo "============================================="
echo
CURRENT_STAGE=