forked from FRINXio/FRINX-machine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
startup.sh
executable file
·527 lines (421 loc) · 15.9 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
#!/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 UniFlow 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
- If you do not wish to use the default UniConfig 30 day trial license, change
the license key in ${licenseKeyFile} before running this script.
- 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
--prod|--dev|--high
Deploy Frinx-Machine in production or development mode
- different allocation of resources
- default: production
--uniflow Deploy UniFlow services
--uniconfig Deploy UniConfig services
--monitoring Deploy Monitoring services
--no-monitoring Deploy UniFlow and UniConfig services
MULTI-NODE DEPLOYMENT
--multinode <PATH> Deploy FM in multinode mode
- Set path to folder witg Uniconfig compose files
- Default ${uniconfigServiceFilesPath}
- Uniflow 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;;
--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;;
--uniflow|--uniconfig|--monitoring|--no-monitoring)
if [ -z ${__only_one_config} ]; then
if [ ${1} == "--uniflow" ]; then
__only_one_config="true"
startupType="uniflow"
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"
fi
else
echo -e "Conflict parameters: --uniflow|--uniconfig|--monitoring !!! Just one can be selected !!!"
echo -e "Use '${scriptName} --help' for more details"
exit 1
fi;;
-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} Unknow 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)
docker stack deploy --compose-file composefiles/$dockerSwarmMetrics $stackName
}
function startUniflow {
generateFrinxFrontendFile
echo -e "${INFO} Uniflow swarm worker node id: ${UF_SWARM_NODE_ID}"
docker stack deploy --compose-file composefiles/$dockerSwarmUniflow $stackName
status=$?
if [[ $status -ne 0 ]]; then
echo -e "${ERROR} Problem with starting Uniflow."
echo -e "${ERROR} If 'network frinx-machine not found!', wait a while and start Uniflow again."
exit 1
fi
}
function startUniconfig {
export LICENSE=$(cat $licenseKeyFile)
echo -e "${INFO} UniConfig license: ${LICENSE}"
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}"
docker stack deploy --compose-file "composefiles/${dockerSwarmUniconfig}" $stackName
fi
}
function startContainers {
generateUniconfigKrakendFile
setNodeIdLocalDeploy
setManagerIpAddrEnv
case $startupType in
uniflow)
echo -e "${INFO} Deploying Uniflow only"
startUniflow
;;
uniconfig)
echo -e "${INFO} Deploying UniConfig only"
startUniconfig
;;
monitoring)
echo -e "${INFO} Deploying Monitoring services only"
startMonitoring
;;
nomonitoring)
echo -e "${INFO} Deploying Uniflow and Uniconfig services only"
startUniflow
startUniconfig
;;
full)
echo -e "${INFO} Deploying Uniflow, Uniconfig and Monitoring services"
startMonitoring
startUniflow
startUniconfig
;;
*)
echo -e "${ERROR} Unknow 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=${PLACEMENT_INPUT} --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 [[ ${JWT_PRODUCTION} == "false" ]]; then
echo -e "${WARNING} For Autorization is used Frinx Fake Token"
elif [[ ${JWT_PRODUCTION} == "true" ]]; then
echo -e "${WARNING} For Autorization is used Azure Active Directory"
echo -e "${INFO} Validating Azure AD configs from '${stackEnvFile}'"
. azure_ad.sh validate
fi
}
function generateFrinxFrontendFile {
local __ff_conf_tmpl="${UF_CONFIG_PATH}/frinx-frontend/config_template.json"
local __ff_conf="${UF_CONFIG_PATH}/frinx-frontend/config.json"
sed " s|AZURE_LOGIN_URL|${AZURE_LOGIN_URL}|; s|AZURE_CLIENT_ID|${AZURE_CLIENT_ID}|; s|AZURE_TENANT_NAME|${AZURE_TENANT_NAME}|; s|AZURE_ENABLED|${JWT_PRODUCTION}|; \
s|KRAKEND_URL|${KRAKEND_TLS_PROTOCOL}://${REDIRECT_URI}|" ${__ff_conf_tmpl} > ${__ff_conf}
}
function generateUniconfigKrakendFile {
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')\",\n\t\t${name}"
fi
done
name=${name}
else
name='"uniconfig"'
fi
sed "s/\"UNICONFIG-NAME\"/${name}/" ${krakendUniconfigTmplFile} > ${krakendUniconfigFile}
}
function setManagerIpAddrEnv {
MANAGER_IP_ADDR=$(hostname -I | cut -d ' ' -f 1)
export MANAGER_IP_ADDR
}
function createEnvFile {
if [[ ! -f ${stackEnvFile} ]]; then
cp "${FM_DIR}/env.template" ${stackEnvFile}
fi
}
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}"
if [[ -f ${__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 {
if [[ -f ${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 )
stackEnvFile="${FM_DIR}/.env"
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"
licenseKeyFile="${FM_DIR}/config/uniconfig/uniconfig_license.txt"
dockerSwarmUniflow='swarm-uniflow.yml'
dockerSwarmUniconfig='swarm-uniconfig.yml'
dockerSwarmMetrics='support/swarm-monitoring.yml'
uniconfigServiceFilesPath="${FM_DIR}/composefiles/uniconfig"
# DEFAULT KRAKEND SETTINGS
krakendUniconfigNode="${FM_DIR}/config/krakend/settings"
krakendUniconfigTmplFile="${krakendUniconfigNode}/uniconfig_settings_template.json"
krakendUniconfigFile="${krakendUniconfigNode}/uniconfig_settings.json"
## Default http
export TLS_DISABLED="true"
export KRAKEND_TLS_PROTOCOL="http"
export KRAKEND_PORT=80
# 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 UC_CONFIG_PATH="${FM_DIR}/config/uniconfig/frinx/uniconfig"
export UF_CONFIG_PATH="${FM_DIR}/config"
# =======================================
# FM starting
# =======================================
pushd ${FM_DIR} > /dev/null
createEnvFile
argumentsCheck "$@"
checkSwarmMode
echo -e "${INFO} Selected resource configuration: ${performSettings}"
setVariableFile "${performSettings}" # load performance settings
setVariableFile "${stackEnvFile}" # load .env settings
validateAzureAD
startContainers
show_last_info
popd > /dev/null