-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathstartup.sh
executable file
·592 lines (476 loc) · 18.2 KB
/
startup.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
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
#!/bin/bash
# set -e
show_help()
{
local ___script_name="$(basename "${0}")"
cat << EOF
DESCRIPTION:
This script can be used for starting Frinx-Machine (FM) in specific modes.
- If no options are specified, starts Workflow-Manager and UniConfig services on local single node
with development resources allocation and http protocol.
- For starting FM in multi-node is need to generate uniconfig compose files with script:
./generate_uc_compose.sh --service-name <arg> --node-id <arg> --folder-path <arg>
Then you can use ./startup.sh --multinode for start
For more info see './generate_uc_compose.sh -h' or README
- To use FM with production resource allocation settings, you can use own settings
stored in ${productPerformSettingFile} or use predefined.
For enabling use --prod option.
For more info see README
OPTIONS:
${___script_name} [OPTIONS]
FRINX-MACHINE CONFIGURATION
--https Deploy Frinx-Machine in https mode
- KrakenD with certificates
- https://localhost
--auth Deploy Frinx-Machine with authorization
--shell Deploy Frinx-Machine with UC shell
--prod|--dev|--high
Deploy Frinx-Machine in production or development mode
- different allocation of resources
- default: production
--workflow-manager Deploy Workflow-Manager services
--uniconfig Deploy UniConfig services
--unistore Deploy Unistore services (not started by default)
--with-unistore Deploy Full FM with Unistore services
--monitoring Deploy Monitoring services
--no-monitoring Deploy Workflow-Manager and UniConfig services
--with-kafka Deploy FM with Kafka and Debezium connector
MULTI-NODE DEPLOYMENT
--multinode <PATH> Deploy FM in multinode mode
- Set path to folder witg Uniconfig compose files
- Default ${uniconfigServiceFilesPath}
- Workflow-Manager on manager node
- Uniconfig on worker nodes
For more info about multi-node deployment see README
COMMON SETTINGS
-h|--help Print this help and exit
-d|--debug Enable verbose
EOF
}
show_last_info()
{
echo -e "${INFO} Startup finished:"
cat << EOF
================================================================================
Use 'docker service ls' to check status of services.
Each service has REPLICAS 1/1 when everything works (it may take several minutes to start all services).
Use './teardown.sh' to stop all services
or './teardown.sh -v' to remove also old data if needed.
EOF
echo -e "${INFO} USE localhost instead of 127.0.0.1"
}
function argumentsCheck {
while [ $# -gt 0 ]
do
case "${1}" in
-h|--help) show_help
exit 0;;
--https)
export TLS_DISABLED="false"
export KRAKEND_TLS_PROTOCOL="https"
export KRAKEND_PORT=443;;
--auth)
export AUTH_ENABLED="true";;
--shell)
export UNICONFIG_SHELL_ENABLED="true";;
--proxy)
export PROXY_ENABLED="true"
setProxyEnv;;
--prod|--dev|--high)
if [ -z ${__only_one_perf_config} ]; then
if [ ${1} == "--prod" ]; then
__only_one_perf_config="true"
performSettings="${productPerformSettingFile}"
elif [ ${1} == "--dev" ]; then
__only_one_perf_config="true";
performSettings="${devPerformSettingFile}"
elif [ ${1} == "--high" ]; then
__only_one_perf_config="true";
performSettings="${highPerformSettingFile}"
fi
else
echo -e "Conflict parameters: --prod|--dev|--high !!! Just one can be selected !!!"
echo -e "Use '${scriptName} --help' for more details"
exit 1
fi;;
--workflow-manager|--uniconfig|--monitoring|--no-monitoring|--unistore|--with-unistore)
if [ -z ${__only_one_config} ]; then
if [ ${1} == "--workflow-manager" ]; then
__only_one_config="true"
startupType="workflow-manager"
elif [ ${1} == "--uniconfig" ]; then
__only_one_config="true"
startupType="uniconfig"
elif [ ${1} == "--monitoring" ]; then
__only_one_config="true"
startupType="monitoring"
elif [ ${1} == "--no-monitoring" ]; then
__only_one_config="true"
startupType="nomonitoring"
elif [ ${1} == "--unistore" ]; then
__only_one_config="true"
startupType="unistore"
elif [ ${1} == "--with-unistore" ]; then
__only_one_config="true"
startupType="withunistore"
fi
else
echo -e "Conflict parameters: --workflow-manager|--uniconfig|--monitoring !!! Just one can be selected !!!"
echo -e "Use '${scriptName} --help' for more details"
exit 1
fi;;
--with-kafka)
__kafka_deploy="true"
export CONDUCTOR_OUTBOX_TABLE_ENABLED="true";;
-m|--multinode)
if [[ ${2} != '-'* ]] || [[ ${2} != '' ]]; then
if [[ -d ${2} ]]; then
__multinode="true";
# todo check is one or more uc composes exist
uniconfigServiceFilesPath="${2}"
else
echo -e "${ERROR} Wrong path to folder with uniconfig composefiles: '${2}'"
exit 1
fi; shift
fi;;
-d|--debug)
set -x;;
*)
echo -e "${ERROR} Unknown option: ${1}. See help!"
exit 1;;
esac
shift
done
}
function startMonitoring {
echo -e "${INFO} Monitoring swarm node id: ${UF_SWARM_NODE_ID}"
export TELEGRAF_GROUP=$(stat -c '%g' /var/run/docker.sock)
export TELEGRAF_HOTNAME=$(stat -c '%g' /var/run/docker.sock)
export HOSTNAME
docker stack deploy --compose-file composefiles/$dockerSwarmMetrics $stackName
}
function startWorkflowManager {
echo -e "${INFO} Workflow-Manager swarm worker node id: ${UF_SWARM_NODE_ID}"
if [[ "$(docker service ls --format {{.Name}} | grep fm_unistore)" != "" ]]; then
echo -e "${INFO} Update Frinx-Frontend: enable L3VPN automation"
export L3VPN_ENABLED=true
fi
docker stack deploy --compose-file composefiles/$dockerSwarmWorkflowManager $stackName
status=$?
if [[ $status -ne 0 ]]; then
echo -e "${ERROR} Problem with starting Workflow-Manager."
echo -e "${ERROR} If 'network frinx-machine not found!', wait a while and start Workflow-Manager again."
exit 1
fi
if [[ ${__kafka_deploy} == "true" ]]; then
docker stack deploy --compose-file composefiles/$dockerSwarmWorkflowManagerKafka $stackName
fi
status=$?
if [[ $status -ne 0 ]]; then
echo -e "${ERROR} Problem with starting Workflow-Manager."
echo -e "${ERROR} If 'network frinx-machine not found!', wait a while and start Workflow-Manager again."
exit 1
fi
}
function startUnistore {
echo -e "${INFO} Update Frinx-Frontend and KrakenD: enable L3VPN automation"
export L3VPN_ENABLED=true
docker service update --env-add L3VPN_ENABLED=${L3VPN_ENABLED} fm_frinx-frontend > /dev/null 2>&1
docker service update --env-add L3VPN_ENABLED=${L3VPN_ENABLED} fm_krakend > /dev/null 2>&1
echo -e "${INFO} Unistore swarm worker node id: ${UF_SWARM_NODE_ID}"
docker stack deploy --compose-file composefiles/$dockerSwarmUnistore $stackName
status=$?
if [[ $status -ne 0 ]]; then
echo -e "${ERROR} Problem with starting Unistore."
echo -e "${ERROR} If 'network frinx-machine not found!', wait a while and start FM again."
exit 1
fi
}
function startUniconfig {
if [[ ${__multinode} == "true" ]]; then
echo -e "${INFO} Multi-node deployment - compose files are stored on this path: ${uniconfigServiceFilesPath}"
echo -e "${INFO} Make sure the UniConfig configuration files are present on remote node in /opt/frinx folder"
shopt -s lastpipe
find ${uniconfigServiceFilesPath} -iname "swarm-uniconfig.yml" -print0 | \
while IFS= read -r -d '' compose_name; do
echo -e "${INFO} Checking swarm nodes for ${compose_name} uniconfig deployment"
node_id="$(grep node "${compose_name}" | sed -e 's/^[[:space:]]*//')"
checkUcSwarmMode "${node_id}"
docker stack deploy --compose-file ${compose_name} $stackName
done
else
echo -e "${INFO} Single-node deployment - composefiles/${dockerSwarmUniconfig}"
echo -e "${INFO} Uniconfig swarm worker node hostname: ${UC_SWARM_NODE_ID}"
chmod -R 777 ${UF_CONFIG_PATH}/uniconfig/cache
docker stack deploy --compose-file "composefiles/${dockerSwarmUniconfig}" $stackName
fi
}
function startContainers {
setUniconfigZoneEnv
setNodeIdLocalDeploy
case $startupType in
workflow-manager)
echo -e "${INFO} Deploying Workflow-Manager only"
startWorkflowManager
;;
uniconfig)
echo -e "${INFO} Deploying Uniconfig only"
startUniconfig
;;
unistore)
echo -e "${INFO} Deploying Unistore only"
startUnistore
;;
monitoring)
echo -e "${INFO} Deploying Monitoring services only"
startMonitoring
;;
nomonitoring)
echo -e "${INFO} Deploying Workflow-Manager and Uniconfig services only"
startWorkflowManager
startUniconfig
;;
withunistore)
echo -e "${INFO} Deploying Workflow-Manager, Uniconfig, Unistore and Monitoring services"
startMonitoring
startWorkflowManager
startUniconfig
startUnistore
;;
full)
echo -e "${INFO} Deploying Workflow-Manager, Uniconfig and Monitoring services"
startMonitoring
startWorkflowManager
startUniconfig
;;
*)
echo -e "${ERROR} Unknown option: ${startupType}. See help!"
exit 1
;;
esac
}
function checkSuccess {
if [[ $? -ne 0 ]]
then
echo -e "${ERROR} System is not healthy. Shutting down..."
docker stack rm $stackName
exit 1
fi
}
function isNodeInSwarm {
if [ -z "$(docker node ls | grep "${1}")" ]
then
echo -e "${ERROR} Node ${1} not in swarm for compose: ${2}!"
docker swarm join-token worker
exit 1
else
if [[ ${__multi_uniconfig} == "true" ]]; then
echo -e "${INFO} Service" ${2} "on swarm node id:" ${1}
fi
fi
}
function checkSwarmMode {
if [ "$(docker info --format '{{.Swarm.LocalNodeState}}')" == "inactive" ]
then
echo -e "${ERROR} Docker not in swarm mode! Initialize the swarm first using 'docker swarm init'"
echo -e "Exiting..."
exit 1
else
echo -e "${INFO} Docker running in swarm mode"
fi
}
function checkUcSwarmMode {
local TYPE="$(echo ${1}| cut -d ' ' -f 2)"
local PLACEMENT_INPUT="$(echo ${1}| cut -d ' ' -f 4)"
if [[ $PLACEMENT_INPUT != '' ]] && [[ $TYPE != '' ]] ; then
case $TYPE in
'node.id')
__id=($(docker node ls -f id=${PLACEMENT_INPUT} --format {{.ID}}))
if [[ "${__id}" == "${PLACEMENT_INPUT}" ]];then
__name=$(docker node ls -f id=${PLACEMENT_INPUT} --format {{.Hostname}})
__status=$(docker node inspect ${__name} --format {{.Status.State}})
else
__name="node.id=${PLACEMENT_INPUT}"
__status='not found'
fi;;
'node.hostname')
__name=($(docker node ls -f name=${PLACEMENT_INPUT} --format {{.Hostname}}))
if [[ "${__name}" == "${PLACEMENT_INPUT}" ]];then
__status=$(docker node inspect ${__name} --format {{.Status.State}})
else
__name="node.hostname=${PLACEMENT_INPUT}"
__status='not found'
fi;;
'node.labels.zone')
__name=($(docker node ls -f node.label=zone --format {{.Hostname}})) || true
if [[ "${__name}" != '' ]];then
__status=$(docker node inspect ${__name} --format {{.Status.State}})
else
__name="node.label=${PLACEMENT_INPUT}"
__status='not found'
fi;;
'node.role')
__name=($(docker node ls -f role=${PLACEMENT_INPUT} --format {{.Hostname}})) || true
if [[ "${__name}" != '' ]];then
__name=$(docker node ls -f role=${PLACEMENT_INPUT} --format {{.Hostname}})
__status=$(docker node inspect ${__name} --format {{.Status.State}})
else
__name="node.role=${PLACEMENT_INPUT}"
__status='not found'
fi;;
*)
echo -e "${ERROR} ${TYPE} is not supported"
return;;
esac
if [[ "${__status}" != "ready" ]]; then
echo -e "${ERROR} Swarm node: ${__name} - ${__node_id} ${__status:-not exist}"
elif [[ "${__status}" == "ready" ]]; then
echo -e "${OK} Swarm node ${__name} with placement type ${TYPE} = ${PLACEMENT_INPUT} is ${__status}"
fi
fi
}
function checkSwarmNodeActive {
local __node_id=${1}
local __status=$(docker node ls --filter id=${__node_id} --format {{.Status}})
local __name=$(docker node ls --filter id=${__node_id} --format {{.Hostname}})
if [[ "${__status}" != "Ready" ]]; then
echo -e "${ERROR} Swarm node: ${__name} - ${__node_id} ${__status:-not exist}"
elif [[ "${__status}" == "Ready" ]]; then
echo -e "${OK} Swarm node: ${__name} - ${__node_id} is ${__status}"
fi
}
function setNodeIdLocalDeploy {
if [ -z "${__multinode}" ]; then
export UC_SWARM_NODE_ID="${nodeID}"
fi
export UF_SWARM_NODE_ID="${nodeID}"
}
function validateAzureAD {
if [[ ${AUTH_ENABLED} == "false" ]]; then
echo -e "${WARNING} For Authorization is used Frinx Fake Token"
elif [[ ${AUTH_ENABLED} == "true" ]]; then
echo -e "${WARNING} For Authorization is used Azure Active Directory"
fi
}
function enableUcShell {
if [[ ${UNICONFIG_SHELL_ENABLED} == "true" ]]; then
echo -e "${WARNING} Exposing websocket proxy on port 8001"
docker service update --publish-add mode=host,target=8001,published=8001 fm_krakend
fi
}
function setUniconfigZoneEnv {
if [[ ${__multinode} == "true" ]]; then
shopt -s lastpipe
find ${uniconfigServiceFilesPath} -iname "swarm-uniconfig.yml" -print0 | \
while IFS= read -r -d '' traefik_name; do
if [[ -z $name ]]; then
line_num=$(($(cat ${traefik_name} | grep -n "services:" | cut -d ':' -f 1)+1))
name="$(sed -n ${line_num}p ${traefik_name} | cut -d ':' -f 1 | sed 's/ //g')"
else
line_num=$(($(cat ${traefik_name} | grep -n "services:" | cut -d ':' -f 1)+1))
name="$(sed -n ${line_num}p ${traefik_name} | cut -d ':' -f 1 | sed 's/ //g'),${name}"
fi
done
name=${name}
else
name='uniconfig'
fi
export UNICONFIG_ZONES_LIST=${name}
}
function setProxyEnv {
export HTTP_PROXY="${http_proxy:-$HTTP_PROXY}"
export HTTPS_PROXY="${https_proxy:-$HTTPS_PROXY}"
export NO_PROXY="$(echo ${no_proxy:-$NO_PROXY} | sed 's\ \\g')"
# load .proxy settings
setVariableFile "${stackProxyFile}"
echo -e "${INFO} Proxy settings enabled!"
echo -e "${INFO} http: ${HTTP_PROXY:-${WARNING}NaN}, https: ${HTTPS_PROXY:-${WARNING}NaN}, no: ${NO_PROXY:-${WARNING}NaN}"
}
function addEnvToFile {
unset __old_env_var
unset __new_env_var
if [[ -f ${stackEnvFile} ]]; then
if grep -Fq ${1} ${stackEnvFile}
then
__old_env_var=$(grep -n ^${1}= ${stackEnvFile} | cut -d':' -f1)
__new_env_var="${1}=${2}"
sed -i "${__old_env_var}d" "${stackEnvFile}"
__old_env_var=$((__old_env_var-1))
sed -i "${__old_env_var}a ${__new_env_var}" "${stackEnvFile}"
else
echo "${1}=${2}" >> ${stackEnvFile}
fi
fi
}
function setVariableFile {
local __filePath="${1}"
# check if the file exists and if contain at least one config parameter
if [[ -f ${__filePath} ]] && [[ "$(grep -v '^\s*$\|^\s*\#' ${__filePath})" != "" ]]; then
source "${__filePath}"
local __name=$(grep ^[[:alpha:]] ${__filePath})
for ((i=0; i< ${#__name[@]}; i++ ))
do
export $(echo "${__name[$i]}" | cut -d '=' -f1)
done
fi
}
function unsetVariableEnvFile {
# check if the file exists and if contain at least one config parameter
if [[ -f ${stackEnvFile} ]] && [[ "$(grep -v '^\s*$\|^\s*\#' ${stackEnvFile})" != "" ]]; then
local __name=$(grep ^[[:alpha:]] ${stackEnvFile})
for ((i=0; i< ${#__name[@]}; i++ ))
do
unset $(echo "${__name[$i]}" | cut -d '=' -f1)
done
fi
}
# =======================================
# Program starts here
# =======================================
# COMMON SETTINGS
scriptName="$(basename "${0}")"
FM_DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
stackProxyFile="${FM_DIR}/.proxy"
ERROR="\033[0;31m[ERROR]:\033[0;0m"
WARNING="\033[0;33m[WARNING]:\033[0;0m"
INFO="\033[0;96m[INFO]:\033[0;0m"
OK="\033[0;92m[OK]:\033[0;0m"
# DEFAULT COMPOSE SETTINGS
stackName="fm"
dockerSwarmWorkflowManager='swarm-workflow-manager.yml'
dockerSwarmWorkflowManagerKafka='swarm-kafka-connector.yml'
dockerSwarmUniconfig='swarm-uniconfig.yml'
dockerSwarmUnistore='swarm-unistore.yml'
dockerSwarmMetrics='support/swarm-monitoring.yml'
uniconfigServiceFilesPath="${FM_DIR}/composefiles/uniconfig"
## Default http
export TLS_DISABLED="true"
export KRAKEND_TLS_PROTOCOL="http"
export KRAKEND_PORT=80
## Default Auth settings
export AUTH_ENABLED=false
## Default UC SHELL settings
export UNICONFIG_SHELL_ENABLED=false
# DEFAULT PERFORM SETTINGS
devPerformSettingFile='./config/dev_settings.txt'
productPerformSettingFile='./config/prod_settings.txt'
highPerformSettingFile='./config/high_settings.txt'
performSettings="${productPerformSettingFile}"
# DEFAULT FM START SETTINGS
startupType="full"
nodeID=$(docker node ls --filter role=manager --format {{.Hostname}})
export UF_CONFIG_PATH="${FM_DIR}/config"
export DEPLOY_SETTINGS_TIMESTAMP=$(date +%s)
# =======================================
# FM starting
# =======================================
pushd ${FM_DIR} > /dev/null
argumentsCheck "$@"
checkSwarmMode
echo -e "${INFO} Selected resource configuration: ${performSettings}"
setVariableFile "${performSettings}" # load performance settings
validateAzureAD
startContainers
enableUcShell
show_last_info
popd > /dev/null