-
Notifications
You must be signed in to change notification settings - Fork 21
/
generate_uc_compose.sh
executable file
·372 lines (298 loc) · 11.3 KB
/
generate_uc_compose.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
#!/bin/bash
show_help()
{
local ___script_name="$(basename "${0}")"
cat << EOF
DESCRIPTION:
Create compose file for specific uniconfig instances
Each service must have unique name.
These compose files can be stored to any folder and then can be
started witg FRINX-Machine ./startup.sh --multinode <PATH>
OPTIONS:
${___script_name} [OPTIONS]
FRINX-MACHINE CONFIGURATION
-s|--service-name [UNICONFIG-SERVICE-NAME]
- Unique uniconfig service name
-i|--instances [__UC_INSTANCES]
- Default : 2
-f|--folder-path [UNICONFIG FOLDER PATH]
- Define path, where will be
compose file stored.
SWARM NODE-PLACEMENT CONFIGURATION
--force [__FORCE_GENERATE]
- Allow re/generation of composefile with wrong parameters
- Default '${__FORCE_GENERATE}'
[USE ONLY ONE OPTION]
-n|--node-id [TYPE]
- Define services placement by node id
--hostname [TYPE]
- Define services placement by node hostname
--label [TYPE]
- Define services placement by node zone label
--role [TYPE]
- Define services placement by node role
COMMON SETTINGS
-h|--help Print this help and exit
-d|--debug Enable verbose
EOF
}
function argumentsCheck {
if [ "$#" -eq 0 ]
then
show_help
exit 1
fi
while [ $# -gt 0 ]
do
case "${1}" in
-h|--help)
show_help
exit 0;;
-s|--service-name)
if [[ ${2} != "-"* ]] && [[ ! -z ${2} ]]; then
checkUniconfigServiceName "${2}"
__SERVICE_NAME="${2}"
__SERVICE_NAME_DEFINED='true'
shift
else
echo "Service name not defined. See help!"
exit 1
fi;;
-n|--node-id|--hostname|--label|--role)
if [ -z ${__only_one_config} ]; then
if [ ${1} == "-n" ]; then
__only_one_config="true"
TYPE="id"
elif [ ${1} == "--node-id" ]; then
__only_one_config="true"
TYPE="id"
elif [ ${1} == "--hostname" ]; then
__only_one_config="true"
TYPE="name"
elif [ ${1} == "--label" ]; then
__only_one_config="true"
TYPE="node.label"
elif [ ${1} == "--role" ]; then
__only_one_config="true"
TYPE="role"
fi
if [[ ${2} == '-'* ]] || [[ ${2} == '' ]] || [[ -z ${2} ]]; then
echo -e "${ERROR} Missing Swarm node placement input parameter for deployment: '${1}', type: 'node ${TYPE}'"
exit 1
else
__NODE_ID=${2}
shift
fi
else
echo -e "${ERROR} Conflict parameters: -n|--node-id|--hostname|--label|--role !!! Just one can be selected !!!"
echo -e "Use '${scriptName} --help' for more details"
exit 1
fi;;
-i|--instances)
local regex='^[0-9]+$'
if [[ ${2} =~ ${regex} ]]; then
__UC_INSTANCES="${2}"; shift
else
echo -e "${ERROR} Bad instances input defined. See help!"
exit 1
fi;;
-f|--folder-path)
if [[ ${2} != "-"* ]] && [[ ! -z ${2} ]] && [[ -d ${2} ]] ; then
__FOLDER_PATH="$(readlink -f ${2})"; shift
else
echo -e "${ERROR} Bad folder path defined. See help!"
exit 1
fi;;
--force)
SWARM_STATUS=${WARNING}
__FORCE_GENERATE='true';;
-d|--debug)
set -x;;
*)
echo -e "Unknown argument option! ${1} \nSee '${__SCRIPT_NAME} --help'"
exit 1;;
esac
shift
done
if [[ -z $TYPE ]]; then
echo -e "${ERROR} Node placement type not defined. See help !!!"
exit 1
fi
if [[ -z $__SERVICE_NAME_DEFINED ]]; then
echo -e "${ERROR} Service name not defined. See help !!!"
exit 1
fi
}
function checkUniconfigServiceName {
local __service_name="${1}"
local regex='^[a-z0-9_-]*$'
echo -e "${INFO} Verify Uniconfig service name: ${__service_name}"
if [[ "${__service_name}" =~ ${regex} ]]; then
echo -e "${OK} Uniconfig service name: ${__service_name}"
else
echo -e "${ERROR} Uniconfig service name '${__service_name}' contain illegal characters"
echo -e "${ERROR} Allowed characters are: a-z 0-9 _- "
exit 1
fi
}
function isNodeInSwarm {
bold=$(tput bold)
normal=$(tput sgr0)
if [[ $TYPE == 'node.label' ]]; then
node_hostname=($(docker node ls -f ${TYPE}=zone=${1} --format {{.Hostname}})) ||
{ { echo -e "${ERROR} Bad node definition parameter ${bold}${TYPE}=${1}${normal}";} ;}
db_node_hostname=$(docker node ls -f ${TYPE}=db=${1} --format {{.Hostname}})
if [[ ${db_node_hostname} == '' ]] || [[ $( echo ${db_node_hostname} | tr -cd ' ' | wc -c) -ne 0 ]]; then
echo -e "${SWARM_STATUS} Only one node can have ${TYPE}=db=${1}"
if [[ $__FORCE_GENERATE == 'false' ]]; then
echo -e "${SWARM_STATUS} Composefiles was not generated"
exit 1
fi
echo -e "${SWARM_STATUS} Composefiles will be force-generated with wrong placement settings for db"
fi
else
node_hostname=($(docker node ls -f ${TYPE}=${1} --format {{.Hostname}})) ||
{ { echo -e "${ERROR} Bad node definition parameter ${bold}${TYPE}=${1}${normal}";} ;}
fi
if [[ $node_hostname != '' ]] || [[ $__FORCE_GENERATE == 'true' ]] ; then
case $TYPE in
'id')
node=$(docker node inspect ${node_hostname} --format {{.ID}})
message="with ID"
deployment="node.id == ${1}"
deployment_psql="node.id == ${1}"
;;
'name')
for i in "${node_hostname[@]}"; do
node=$(docker node inspect ${node_hostname} --format {{.Description.Hostname}})
message="with hostname"
deployment="node.hostname == ${1}"
deployment_psql="node.hostname == ${1}"
if [[ $node != "" ]]; then
break
fi
done
;;
'node.label')
for i in "${node_hostname[@]}"; do
node=$(docker node inspect ${i} --format {{.Spec.Labels.zone}})
message="with label zone ="
deployment="node.labels.zone == ${1}"
deployment_psql="node.labels.db == ${1}"
if [[ $node != "" ]]; then
break
fi
done
;;
'role')
for i in "${node_hostname[@]}"; do
node=$(docker node inspect ${node_hostname} --format {{.Spec.Role}})
message="with role"
deployment="node.role == ${1}"
deployment_psql="node.role == ${1}"
if [[ $node != "" ]]; then
break
fi
done
;;
*)
echo -e "${ERROR} ${TYPE} is not supported"
exit 1
;;
esac
fi
if [[ "${node}" == "${1}" ]]; then
echo -e "${INFO} Node ${message} ${bold}${1}${normal} is in swarm - Hostname: ${bold}${node_hostname}${normal}"
else
echo -e "${SWARM_STATUS} Node ${message} ${bold}${1}${normal} not in swarm"
if [[ $__FORCE_GENERATE == 'false' ]]; then
echo -e "${SWARM_STATUS} Composefiles was not generated"
exit 1
fi
echo -e "${SWARM_STATUS} Composefiles were force-generated with wrong placement settings"
fi
}
function prepareConfigFiles {
local __CONFIG_PATH="${__DEF_CONFIG_PATH}/${__SERVICE_NAME}"
local __DEF_UC_CONFIG_MIDDLE_PATH="config"
# prepare uniconfig cache folder and files
mkdir -p "${__FOLDER_PATH}/${__CONFIG_PATH}/${__UNICONFIG_SERVICE_SUFIX}/cache"
chmod -R 777 "${__FOLDER_PATH}/${__CONFIG_PATH}/${__UNICONFIG_SERVICE_SUFIX}/cache"
}
function computeDbPools {
if [[ ${__UC_INSTANCES} -eq 1 ]]; then
DB_POOLS=300;
UC_POOLS=300;
elif [[ ${__UC_INSTANCES} -eq 2 ]]; then
DB_POOLS=300;
UC_POOLS=150;
else
DB_POOLS=$((${__UC_INSTANCES} * 100));
UC_POOLS=100;
fi
}
function generateUcCompose {
if [[ "${__SERVICE_NAME}" == "uniconfig" ]]; then
__SERVICE_FULL_NAME="${__UNICONFIG_SERVICE_SUFIX}"
else
__SERVICE_FULL_NAME="${__SERVICE_NAME}-${__UNICONFIG_SERVICE_SUFIX}"
fi
local __COMPOSE_PATH="${__FOLDER_PATH}/${__UC_COMPOSE_NAME}"
local __CONFIG_PATH="${__DEF_CONFIG_PATH}/${__SERVICE_NAME}"
prepareConfigFiles
cp "${FM_COMPOSE_DIR}/${__UC_COMPOSE_NAME}" "${__COMPOSE_PATH}"
# service names
sed -i "s/ uniconfig:/ ${__SERVICE_NAME}:/g" "${__COMPOSE_PATH}"
sed -i "s/ uniconfig-controller:/ ${__SERVICE_FULL_NAME}:/g" "${__COMPOSE_PATH}"
sed -i "s/ uniconfig-postgres:/ ${__SERVICE_NAME}-postgres:/g" "${__COMPOSE_PATH}"
# swarm node deployment
sed -i '/&placement-controller$/{n;n;n; s/node.role == manager/uc_deployment/}' ${__COMPOSE_PATH}
sed -i '/&placement-postgres$/{n;n;n; s/node.role == manager/ucp_deployment/}' ${__COMPOSE_PATH}
sed -i "s/uc_deployment/${deployment}/g" "${__COMPOSE_PATH}"
sed -i "s/ucp_deployment/${deployment_psql}/g" "${__COMPOSE_PATH}"
# swarm config paths
sed -i 's|${UF_CONFIG_PATH}/uniconfig/cache|'"/${__CONFIG_PATH}/${__UNICONFIG_SERVICE_SUFIX}/cache|g" "${__COMPOSE_PATH}"
# swarm persistent volume paths
sed -i "s/uniconfig-postgresql_data/${__SERVICE_NAME}-postgresql_data/g" "${__COMPOSE_PATH}"
# swarm uniconfig-controller replicas
sed -i 's|replicas: ${UC_CONTROLLER_REPLICAS:-1}|'"replicas: ${__UC_INSTANCES}|g" "${__COMPOSE_PATH}"
# labels
sed -i 's|entrypoints=uniconfig|'"entrypoints=${__SERVICE_NAME/_/-}|g" "${__COMPOSE_PATH}"
sed -i 's|\.uniconfig\.|'"\.${__SERVICE_NAME}\.|g" "${__COMPOSE_PATH}"
# env
sed -i 's|_0_HOST=uniconfig-postgres|'"_0_HOST=${__SERVICE_NAME}-postgres|g" "${__COMPOSE_PATH}"
sed -i 's|TRAEFIK_ENTRYPOINTS_UNICONFIG|'"TRAEFIK_ENTRYPOINTS_${__SERVICE_NAME/_/-}|g" "${__COMPOSE_PATH}"
# networks
sed -i 's|uniconfig-network|'"${__SERVICE_NAME}-network|g" "${__COMPOSE_PATH}"
# db pools
sed -i 's|DBPERSISTENCE_CONNECTION_MAXDBPOOLSIZE=300|'"DBPERSISTENCE_CONNECTION_MAXDBPOOLSIZE=${UC_POOLS}|g" "${__COMPOSE_PATH}"
sed -i 's|max_connections=300|'"max_connections=${DB_POOLS}|g" "${__COMPOSE_PATH}"
}
function prepareFolder {
if [ -d ${__FOLDER_PATH} ]; then
rm -rf "${__FOLDER_PATH}/swarm-uniconfig.yml" "${__FOLDER_PATH}/opt"
mkdir -p ${__FOLDER_PATH}/${__DEF_CONFIG_PATH}
fi
}
# =======================================
# Program starts here
# =======================================
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"
SWARM_STATUS=${ERROR}
FM_DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
FM_COMPOSE_DIR="${FM_DIR}/composefiles"
__SERVICE_NAME="uniconfig-controller"
__FOLDER_PATH="${FM_COMPOSE_DIR}/uniconfig"
__UC_COMPOSE_NAME="swarm-uniconfig.yml"
__DEF_CONFIG_PATH="opt/frinx"
__FORCE_GENERATE='false'
__UC_INSTANCES=2
__UNICONFIG_SERVICE_SUFIX="uniconfig-controller"
argumentsCheck "$@"
isNodeInSwarm ${__NODE_ID}
prepareFolder
computeDbPools
generateUcCompose