-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenCompose
executable file
·583 lines (426 loc) · 13.7 KB
/
genCompose
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
#!/bin/bash
# --------------------------------------------------------------------------------------------
# Name: genCompose
# Language: bash
# Author: Jorge Torralba
# Version: 1
# Date: May 9, 2024
# Location: https://github.com/jtorral/DockerPgHa
#
# This script will generate a docker-compose file based on the arguments passed to it.
# Since this is yaml based file creation, do not adjust the spacing you may see in the script.
# Very sensitive to spacing.
# It's a pain, but it works for now.
# --------------------------------------------------------------------------------------------
# -- show help info
function usage() {
echo -e
echo -e "Usage:"
echo -e " $0 [OPTION]"
echo -e
echo -e " -d Number of data centers to simulate. (default = 1)"
echo -e " -c number of nodes per data center. (default = 2)"
echo -e " -n Prefix name to use for db containers (lower case no special characters)."
echo -e " -v Postgres Major version number. i.e 16"
echo -e " -b Start patroni in background and keep container running even if it stops. "
echo -e " Good for upgrades and maintenance tasks."
echo -e
echo -e " Number of db nodes is capped at 9. So (-d * -c) should be <= 9 "
echo -e " Number of etcd nodes are calculated on (-d * -c ) / 2 + 1 "
echo -e
if [ ! -z "$usageMessage" ]; then
echo -e "${usageMessage}"
echo -e
fi
exit
}
# -- build depends_on list for start order of containers
# -- Not a reliable way to control starting but it's a start.
# -- Consider using scripts that check
buildDependList() {
# -- Which service block (nodename) are we building this depends on list for?
buildingListFor=$1
if [ -z $2 ]; then
addEtcd="etcd"
else
addEtcd=$2
fi
local etcdNode
pgDependList="depends_on: "
# -- lets add etcd nodes first
# -- Also add capabaility of not adding the etcd nodes with a value other than "etcd" passed
if [ "${addEtcd}" == "etcd" ]; then
for etcdNode in ${etcdNameList};
do
# -- Preserve spaces below
pgDependList="${pgDependList}\n - ${etcdNode}"
done
fi
# -- We build the depends_on list of postgres nodes. However, the first node does not depend on any other node
# -- So, we skip this for the first node
if [ "$buildingListFor" == "$firstNode" ]; then
return
fi
# -- For now lets just depend on first node. If we want others, we can build a loop instead
# -- Preserve spaces below
pgDependList="${pgDependList}\n - ${firstNode}"
}
# -- Lets check running containers so we don't duplicatethem with the same name and ports
function checkUsed() {
# -- Check for postgres ports already mapped
usedPgPorts=$(docker ps --format "{{.Ports}}" | grep ":" | grep 5432 | awk -F ":" '{print $2}' | awk -F "-" '{print $1}' | sort | tail -1)
# -- Lets grab a container number. We focus on etcd1- since we know that will always be running if we spin up pgha clusters
# -- using these scripts. Otherwise, the awk might fail and grab another name to check that does not match the naming convention used
# -- remember the first number after the first hyphen in the name represents the container number
usedContainers=$(docker ps --format "{{.Names}}" | grep "etcd1-" | awk -F "-" '{print $2}' | sort | tail -1)
}
# -- Determin how many etcd nodes we need
function etcdHostsNeeded() {
etcdNeeded=$(( ($totalNodes / 2) + 1))
if [ $(($etcdNeeded%2)) -eq 0 ];
then
etcdNeeded=$(( $etcdNeeded + 1 ))
fi
if [ $etcdNeeded -lt 3 ]; then
etcdNeeded=3
fi
}
# -- Build string of etcd node names to use
function buildEtcdNodeNames() {
etcdHostsNeeded
local x
etcdNameList=""
for (( x=1; x<=$etcdNeeded; x++ ))
do
etcdNameList="${etcdNameList} etcd${x}-${containerNumber}"
done
}
# -- Build string of postgres node names to use
function buildPgNodeNames() {
local d
local c
local nodeCount
nodeCount=0
pgNameList=""
# --- nodeName is provided at command line as argument
for (( d=1; d<=$numDatacenters; d++ ))
do
for (( c=1; c<=$nodesPerDataCenter; c++ ))
do
nodeCount=$(( $nodeCount + 1 ))
if [ $d -eq 1 ] && [ $c -eq 1 ]; then
firstNode="${nodeName}${d}-${containerNumber}-node${c}"
fi
pgNameList="${pgNameList} ${nodeName}${d}-${containerNumber}-node${c}"
done
done
}
# -- Build string of pgbackrest node names to use. In reality just 1. But you never know
function builPgBackrestNodeNames() {
# -- Only one bgbackrest server is actually needed.
# -- So, just hardcoding the name here
pgBackrestNeeded=1
pgBackrestNameList="pgbackrest1-${containerNumber}"
pgBackrestNodeName="pgbackrest1-${containerNumber}"
# -- If you want to use the loop to build the server name, just uncomment the code below
# -- Which is what is used for building multiple server names for etcd and pg servers
#pgBackrestNeeded=1
#pgBackrestNameList=""
#for (( x=1; x<=$pgBackrestNeeded; x++ ))
#do
#pgBackrestNameList="${pgBackrestNameList} pgbackrest${x}-${containerNumber}"
#done
}
function buildEtcdEnv() {
local x
endPoints=""
initialCluster=""
etcdNodeList=""
for etcdNode in ${etcdNameList}
do
endPoints="${endPoints}${etcdNode}:2380,"
initialCluster="${initialCluster}${etcdNode}=http://${etcdNode}:2380,"
etcdNodeList="${etcdNodeList}${etcdNode}:2379,"
done
# - remove trailing commas
endPoints=$(echo $endPoints | sed 's/,$//g')
initialCluster=$(echo $initialCluster | sed 's/,$//g')
etcdNodeList=$(echo $etcdNodeList | sed 's/,$//g')
}
# -- Build the etcd service block s=for the yaml file. One for each etcd node
buildEtcdService() {
buildEtcdEnv
for etcdNode in ${etcdNameList};
do
echo "
${etcdNode}:
image: pgha-etcd-3.5
restart: unless-stopped
environment:
ENDPOINTS: $endPoints
command:
- '/usr/bin/etcd'
- '--name=${etcdNode}'
- '--initial-advertise-peer-urls=http://${etcdNode}:2380'
- '--listen-peer-urls=http://0.0.0.0:2380'
- '--listen-client-urls=http://0.0.0.0:2379'
- '--advertise-client-urls=http://${etcdNode}:2379'
- '--heartbeat-interval=250'
- '--election-timeout=1250'
- '--initial-cluster=$initialCluster'
- '--initial-cluster-state=new'
- '--initial-cluster-token=pgha'
networks:
net1-${containerNumber}:
priority: 1000
net2-${containerNumber}:
priority: 10
ports:
- 2379
volumes:
- ${etcdNode}:/etcd_data
hostname: ${etcdNode}
container_name: ${etcdNode}
" >> $composeFile
done
}
function getNodeInfo() {
local NodeName
nodeName=$1
dataCenter=$(echo $nodeName | awk -F "-" '{print $1}' | grep -o -E '[0-9]+')
nodeNumber=$(echo $nodeName | awk -F "-" '{print $3}' | grep -o -E '[0-9]+')
}
buildPgBackrestService() {
local nodeCount
local pgNode
local backrestNode
nodeCount=1
pgBackrestEnvList="environment:"
for backrestNode in ${pgBackrestNameList};
do
buildDependList $backrestNode "noetcd"
# -- Lets build the pgBackrest node lists that will be used for the config
for pgNode in ${pgNameList};
do
pgBackrestEnvList="${pgBackrestEnvList}\n NODE${nodeCount}: ${pgNode}"
nodeCount=$(( $nodeCount + 1 ))
done
echo -e "
${backrestNode}:
${pgDependList}
${pgBackrestEnvList}
CFG_DIR: /pgha/config
REPO_PATH: /pgha/pgbackrest
DATADIR: /pgdata/${pgMajor}
STANZA_NAME: pgha_db
PGPORT: 5432
image: pgha-pgbackrest
restart: unless-stopped
networks:
net1-${containerNumber}:
priority: 1000
net2-${containerNumber}:
priority: 10
net3-${containerNumber}:
priority: 10
volumes:
- ${backrestNode}-data:/pgha/pgbackrest
- ${backrestNode}-home:/var/lib/postgresql
hostname: ${backrestNode}
container_name: ${backrestNode}
cap_add:
- SYS_ADMIN
entrypoint: /entrypoint.sh
" >> $composeFile
done
}
buildNetworks() {
networkList="networks: "
# -- Network creation variables for custom networks
networkList="${networkList}\n net1-${containerNumber}:"
networkList="${networkList}\n net2-${containerNumber}:"
networkList="${networkList}\n net3-${containerNumber}:"
echo -e "
$networkList
" >> $composeFile
}
buildVolumes() {
volumeList=""
etcdVolumeList=""
pgbackrestVolumeList=""
# -- Build volumes used by pg containers
for pgNode in ${pgNameList};
do
v1="${pgNode}-data:"
v2="${pgNode}-home:"
v3="${pgNode}-conf:"
volumeList="${volumeList}\n ${v1}\n ${v2}\n ${v3}"
done
# -- Build volumes used by etcd containers
etcdVolumes=""
for etcdNode in ${etcdNameList};
do
etcdVolumeList="${etcdVolumeList}\n ${etcdNode}:"
done
# -- Build volumes used by pgbackrest containers
pgbackrestVolumeList=""
for pgBackrestNode in ${pgBackrestNameList};
do
pgbackrestVolumeList="${pgbackrestVolumeList}\n ${pgBackrestNode}-data:";
pgbackrestVolumeList="${pgbackrestVolumeList}\n ${pgBackrestNode}-home:";
done
echo -e "volumes: $etcdVolumeList $volumeList $pgbackrestVolumeList " >> $composeFile
}
buildPgService() {
buildEtcdEnv
local nodeCount
local dataCenter
nodeCount=0
for pgNode in ${pgNameList};
do
nodeCount=$(( $nodeCount + 1 ))
# -- We know the nodename has the datacenter number at the end of it. So lets pull that out since we need
# -- that data center to assign network priority
# -- So the call sets dataCenter and NodeNumber. We only use dataCenter here
getNodeInfo $pgNode
# - Assign network priority based on dataCenter
priority1=10;
priority2=10;
priority3=10;
case $dataCenter in
1 | 4 | 7 | 10 | 13 | 16 | 19)
priority1=1000
;;
2 | 5 | 8 | 11 | 14 | 17 | 20)
priority2=1000
;;
3 | 6 | 9 | 12 | 15 | 18 | 21)
priority3=1000
;;
esac
# -- lets generate unique ports to map
portMap=$(( startingPort + $nodeCount ))
# - Lets build the depend_on list. We let it know which service we are builing it for by passing the node
# - We also pass a 2nd argument "etcd" so that the function adss the etcd nodes to the depends on list
buildDependList $pgNode "etcd"
echo -e "
${pgNode}:
${pgDependList}
environment:
ETCD_NODES: $etcdNodeList
NODE_NAME: ${pgNode}
CFG_DIR: /pgha/config
PATRONI_CFG: /pgha/config/patroni.conf
DATADIR: /pgdata/${pgMajor}
PG_BIN_DIR: /usr/lib/postgresql/${pgMajor}/bin
NAMESPACE: pgha
SCOPE: pgha_cluster
STANZA_NAME: pgha_db
PGBACKREST_SERVER: ${pgBackrestNodeName}
BACKGROUND: $backGround
image: pgha-pg16-patroni
restart: unless-stopped
cap_add:
- SYS_ADMIN
entrypoint: /entrypoint.sh
networks:
net1-${containerNumber}:
priority: ${priority1}
net2-${containerNumber}:
priority: ${priority2}
net3-${containerNumber}:
priority: ${priority3}
ports:
- "${portMap}:5432"
volumes:
- ${pgNode}-data:/pgdata
- ${pgNode}-home:/var/lib/postgresql
- ${pgNode}-conf:/pgha/config
hostname: ${pgNode}
container_name: ${pgNode}
" >> $composeFile
done
}
# --- Validate its a number
function checkNumber() {
num=$1
var=$2
valid=0
regexp='^[0-9]+$'
if ! [[ $num =~ $regexp ]] ; then
echo -e
echo -e "Invalid number format of \"$num\" entered for \"$var\". Please correct and try again"
echo -e
exit
fi
}
function checkAlpha() {
str=$1
var=$2
valid=0
regexp='^[a-z]+$'
if ! [[ $str =~ $regexp ]] ; then
echo -e
echo -e "Only lower case letters are allowed. No special characters. Invalid format of \"$str\" entered for \"$var\". Please correct and try again"
echo -e
exit
fi
}
composeFile='docker-compose.yaml'
nodeName=""
numDatacenters=1
nodesPerDataCenter=2
backGround=0
while getopts bn:d:c:v: name
do
case $name in
n) nodeName="$OPTARG";;
v) pgMajor="$OPTARG";;
b) backGround="1";;
d) numDatacenters="$OPTARG";;
c) nodesPerDataCenter="$OPTARG";;
*) usage;;
?) usage;;
esac
done
shift $(($OPTIND - 1))
if [ -z "$nodeName" ] || [ -z "$pgMajor" ]; then
usage
fi
checkAlpha $nodeName "-n"
checkNumber $pgMajor "-v"
checkNumber $numDatacenters "-d"
checkNumber $nodesPerDataCenter "-c"
maxNodes=9
totalNodes=$(($numDatacenters * $nodesPerDataCenter))
if [ $totalNodes -gt $maxNodes ]; then
usageMessage="You have exceeded the max number of nodes (${maxNodes}) allowed. "
usage
fi
if [ $totalNodes -lt 2 ]; then
usageMessage="You must have at least 2 nodes for Patroni. You have specified $numDatacenters data center and $nodesPerDataCenter node per data center."
usage
fi
# -- Lets find used ports and container numbers
checkUsed
if [ -z $usedPgPorts ]; then
startingPort=5432
else
startingPort=$usedPgPorts
fi
if [ -z $usedContainers ]; then
containerNumber=1
else
containerNumber=$(( $usedContainers + 1 ))
fi
# -- Lets start populating the compose file
echo "services:" > $composeFile
buildEtcdNodeNames
buildPgNodeNames
builPgBackrestNodeNames
buildEtcdService
buildPgService
buildPgBackrestService
buildVolumes
buildNetworks
echo -e "\n--- Finished. File $composeFile generated\n"