-
Notifications
You must be signed in to change notification settings - Fork 57
/
build.sh
executable file
·507 lines (472 loc) · 11.4 KB
/
build.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
#!/bin/sh
set -e
if [ ! $(which docker) ]; then
echo "REQUIREMENT: docker not found in system."
echo " Follow guide at https://docs.docker.com/engine/install/"
echo " or use scripts in sysadmin folder."
exit 1
fi
if [ ! "$(docker compose version 2> /dev/null)" ]; then
if [ ! $(which docker-compose) ]; then
echo "REQUIREMENT: docker-compose not found in system."
echo " Follow guide at https://docs.docker.com/compose/install/"
echo " or use scripts in sysadmin folder."
exit 1
else
export DOCKER_COMPOSE="docker-compose"
fi
else
export DOCKER_COMPOSE="docker compose"
fi
# We need docker-compose >= 1.28 to use service profiles
# docker-compose >= 1.27.3 to use depends_on with service_healthy
# docker-compose < 1.26 preserves environment variable quotations
# Use SKIP_CHECK_DOCKER_COMPOSE_VERSION=true environment variable to skip the check
REQUIRED_DOCKER_COMPOSE_VERSION="1.28"
GITLAB_PROJECT_ID="21522757"
CHANGELOG_URL="https://gitlab.com/isard/isardvdi/-/releases/"
PARTS_PATH=docker-compose-parts
ALLINONE_KEY="all-in-one"
ALLINONE_PARTS="
network
db
db-stats
engine
static
portal
hypervisor
websockify
squid
squid-hypervisor
webapp
stats
monitor
api
scheduler
authentication
vpn
guac
redis
storage
backupninja
core_worker
nc
postgres
infrastructure
check
notifier
sessions
bastion
"
HYPERVISOR_KEY="hypervisor"
HYPERVISOR_PARTS="
network
video
hypervisor
websockify
squid
squid-hypervisor
stats
storage
"
HYPERVISOR_STANDALONE_KEY="hypervisor-standalone"
HYPERVISOR_STANDALONE_PARTS="
network
hypervisor
stats
storage
"
VIDEO_STANDALONE_KEY="video-standalone"
VIDEO_STANDALONE_PARTS="
network
video
websockify
squid
stats
"
STORAGE_KEY="storage"
STORAGE_PARTS="
network
storage
stats
"
WEB_KEY="web"
WEB_PARTS="
network
db
db-stats
engine
static
portal
webapp
api
scheduler
authentication
vpn
stats
guac
redis
core_worker
nc
postgres
infrastructure
notifier
sessions
bastion
"
MONITOR_STANDALONE_KEY="monitor"
MONITOR_STANDALONE_PARTS="
network
stats
monitor
monitor-proxy
"
WEB_MONITOR_KEY="web+monitor"
# We remove the stats part, since it's already in the web parts. Also the proxy, since web already has one
WEB_MONITOR_PARTS="
$WEB_PARTS
$(echo "$MONITOR_STANDALONE_PARTS" | sed -e '/stats/d' -e '/monitor-proxy/d')
"
BACKUPNINJA_STANDALONE_KEY="backupninja"
BACKUPNINJA_STANDALONE_PARTS="
network
backupninja
"
CHECK_STANDALONE_KEY="check"
CHECK_STANDALONE_PARTS="
network
check
"
NEXTCLOUD_INSTANCE_KEY="nextcloud"
NEXTCLOUD_INSTANCE_PARTS="
network
postgres
nc
nc-proxy
"
# BASE image builds
HAPROXY_BUILD_KEY="haproxy"
HAPROXY_BUILD_PARTS="
haproxy
"
docker_compose_version(){
$DOCKER_COMPOSE version --short | sed 's/^docker-compose version \([^,]\+\),.*$/\1/'
}
check_docker_compose_version(){
# We cannot use sort -C because is not included in BusyBox v1.33.1 of docker:dind image
{
echo "$REQUIRED_DOCKER_COMPOSE_VERSION"
docker_compose_version
} | sort -c -V 2> /dev/null
}
get_config_files(){
ls isardvdi*.cfg
}
get_config_name(){
echo "$1" | sed -n 's/^isardvdi\.\?\(.*\)\.cfg$/\1/p'
}
is_official_build(){
if \
${GITLAB_CI-false} \
&& [ "$CI_PROJECT_ID" = "$GITLAB_PROJECT_ID" ] \
&& (
[ "$CI_COMMIT_BRANCH" = "$CI_DEFAULT_BRANCH" ] \
|| echo "$CI_COMMIT_TAG" | grep -q "^v"
)
then
return 0
else
return 1
fi
}
create_env(){
cp "$1" .env
## BUILD_ROOT_PATH env
# This is a workarround for
# https://github.com/docker/compose/issues/7873
# See also BUILD_ROOT_PATH sed section at the end of file
echo "BUILD_ROOT_PATH=$(pwd)" >> .env
. ./.env
# Only display numbered version in official builds via gitlab-ci
if is_official_build && test -e .VERSION
then
version="$(cat .VERSION)"
version_date="$(date +%Y-%m-%d)"
version_id="$version $version_date"
echo SRC_VERSION_ID="$version_id" >> .env
echo SRC_VERSION_LINK="${CHANGELOG_URL}v${version}" >> .env
else
echo SRC_VERSION_LINK= >> .env
if [ -n "$CI_COMMIT_REF_SLUG" ]
then
echo SRC_VERSION_ID="$CI_COMMIT_REF_SLUG" >> .env
else
version="$(git name-rev --name-only --always --no-undefined HEAD)"
if ! git diff --quiet
then
version="$version-dirty"
fi
echo SRC_VERSION_ID="$version" >> .env
fi
fi
}
parts_files(){
for part in $@
do
local file="$PARTS_PATH/$part.yml"
if [ -f "$file" ]
then
echo -n "-f $file "
fi
done
}
merge(){
local version_args="$1"
shift || return 0
local config_name="$1"
shift || return 0
local args="$(parts_files $@)"
if [ -n "$*" -a -n "$args" ]
then
if [ -z "$config_name" ]
then
local delimiter=""
else
local delimiter="."
fi
$DOCKER_COMPOSE $version_args $args config > "docker-compose$delimiter$config_name.yml"
fi
}
parts_variant(){
local variant="$1"
shift || return 0
for part in $@
do
if ! echo $parts | grep -q "\(^\|\s\)$part\(\s\|$\)"
then
parts="$parts $part "
fi
parts="$parts $part.$variant "
done
echo -n $parts
}
variants(){
local config_name="$1"
shift || return 0
if check_docker_compose_version
then
local version="current"
# docker-compose config v2 hide not specified profiles
local version_args="--profile test"
else
local version="legacy"
local version_args=""
fi
case $USAGE in
production)
merge "$version_args" "$config_name" $(parts_variant $version $(parts_variant $FLAVOUR $@))
;;
build)
merge "$version_args" "$config_name" $(parts_variant $version $(parts_variant $FLAVOUR $@) $(parts_variant build $@))
;;
test)
merge "$version_args" "$config_name" $(parts_variant $version $(parts_variant $FLAVOUR $@) $(parts_variant build $@) $(parts_variant test $@))
;;
devel)
merge "$version_args" "$config_name" $(parts_variant $version $(parts_variant $FLAVOUR $@) $(parts_variant test $@) $(parts_variant build $@) $(parts_variant devel $@))
;;
*)
echo "Error: unknow usage $USAGE"
exit 1
;;
esac
}
flavour(){
## Usage of flavour function
#
# flavour <config-name> <part-1> <part-2> ...
# - <config-name> is used for the filename: docker-compose.<config-name>.yml
# - <part-1> and <part-2> ... should be files like:
# docker-compose-parts/<part>[.<current/legacy>].yml
# docker-compose-parts/<part>.<test/build/devel>[.<current/legacy>].yml
# docker-compose-parts/<part>.<flavour>[.<current/legacy>].yml
local config_name="$1"
shift || return 0
local parts=""
for part in $@
do
parts="$parts $part"
if [ "$part" = "hypervisor" -a -n "$HYPERVISOR_HOST_TRUNK_INTERFACE" ]
then
parts="$parts hypervisor-vlans"
echo "WARNING: Will take host interface $HYPERVISOR_HOST_TRUNK_INTERFACE and put it inside hypervisor container"
echo " So interface WILL DISSAPPEAR from host"
echo " With this configuration, when you restart container the interface could be missing,"
echo " so, better do '$DOCKER_COMPOSE -f docker-compose.hypervisor.yml down' and wait a minute"
echo " till the interface $HYPERVISOR_HOST_TRUNK_INTERFACE is visible in the host again!"
echo ""
fi
done
variants "$config_name" $parts
}
create_docker_compose_file(){
config_file="$1"
create_env "$config_file"
config_name="$(get_config_name "$config_file")"
if [ -z "$USAGE" ]
then
USAGE="production"
fi
if [ -z "$ENABLE_STATS" ]
then
ENABLE_STATS="true"
fi
if [ -z "$BACKUP_DB_ENABLED" ]
then
BACKUP_DB_ENABLED="false"
fi
if [ -z "$BACKUP_DISKS_ENABLED" ]
then
BACKUP_DISKS_ENABLED="false"
fi
if [ -z "$GOLANG_BUILD_IMAGE" ]
then
export GOLANG_BUILD_IMAGE="golang:1.23-alpine3.20"
fi
if [ -z "$GOLANG_RUN_IMAGE" ]
then
export GOLANG_RUN_IMAGE="alpine:3.20"
fi
if [ -z "$FLAVOUR" ]
then
FLAVOUR="all-in-one"
fi
case $FLAVOUR in
$ALLINONE_KEY)
parts=$ALLINONE_PARTS
;;
$HYPERVISOR_KEY)
parts=$HYPERVISOR_PARTS
;;
$HYPERVISOR_STANDALONE_KEY)
parts=$HYPERVISOR_STANDALONE_PARTS
;;
$VIDEO_STANDALONE_KEY)
parts=$VIDEO_STANDALONE_PARTS
;;
$STORAGE_KEY)
parts=$STORAGE_PARTS
;;
$WEB_KEY)
parts=$WEB_PARTS
;;
$MONITOR_STANDALONE_KEY)
parts=$MONITOR_STANDALONE_PARTS
;;
$WEB_MONITOR_KEY)
parts=$WEB_MONITOR_PARTS
;;
$BACKUPNINJA_STANDALONE_KEY)
parts=$BACKUPNINJA_STANDALONE_PARTS
;;
$CHECK_STANDALONE_KEY)
parts=$CHECK_STANDALONE_PARTS
;;
$NEXTCLOUD_INSTANCE_KEY)
parts=$NEXTCLOUD_INSTANCE_PARTS
;;
$HAPROXY_BUILD_KEY)
parts=$HAPROXY_BUILD_PARTS
;;
*)
echo "Error: Flavour $FLAVOUR of $config_file not found"
exit 1
;;
esac
if [ -n "$ENABLE_STATS" -a "$ENABLE_STATS" != "true" ]
then
parts="$(echo $parts | sed 's/monitor//' | sed 's/db-stats//' | sed 's/stats//')"
fi
if [ "$BACKUP_DB_ENABLED" = "false" ] && [ "$BACKUP_DISKS_ENABLED" = "false" ]
then
if [ "$FLAVOUR" = "backupninja" ]
then
echo "ERROR: flavour backupninja needs at least BACKUP_DB_ENABLED or BACKUP_DISKS_ENABLED in cfg"
exit 1
fi
parts="$(echo $parts | sed 's/backupninja//')"
fi
if [ "$NEXTCLOUD_INSTANCE" != "true" ] && [ "$FLAVOUR" != "nextcloud" ]
then
parts="$(echo $parts | sed 's/postgres//' | sed 's/nc//' )"
fi
if [ -z "$INFRASTRUCTURE_MANAGEMENT" ] || [ "$INFRASTRUCTURE_MANAGEMENT" != "true" ]
then
parts="$(echo $parts | sed 's/infrastructure//')"
# If the flavour is check, it doesn't require INFRASTRUCTURE_MANAGEMENT to be set
if [ "$FLAVOUR" != "check" ]
then
parts="$(echo $parts | sed 's/check//')"
fi
fi
# If BASTION_ENABLED is not true, remove the bastion part
if [ -z "$BASTION_ENABLED" ] || [ "$BASTION_ENABLED" != "true" ]
then
echo "BASTION_ENABLED is not true, removing bastion part"
parts="$(echo $parts | sed 's/bastion//')"
fi
# Build the docker-compose.yml
flavour "$config_name" $parts
if [ "$BACKUP_NFS_ENABLED" = "true" ]
then
if [ -z "$BACKUP_NFS_SERVER" ] || [ -z "$BACKUP_NFS_FOLDER" ]
then
echo "ERROR: backupninja with nfs enabled needs setting BACKUP_NFS_SERVER and BACKUP_NFS_FOLDER in cfg"
exit 1
fi
if [ ! -z "$BACKUP_DIR" ]
then
echo "ERROR: backupninja with nfs enabled needs BACKUP_DIR to be commented/removed as conflicts"
exit 1
fi
sed -i '/\/backup:\/backup/d' docker-compose*.yml
fi
}
generate_code(){
create_env "$1"
case "$USAGE" in
build | test | devel)
DOCKER_IMAGE="${DOCKER_IMAGE_PREFIX}codegen:${DOCKER_IMAGE_TAG}"
docker pull $DOCKER_IMAGE
docker build -t "$DOCKER_IMAGE" -f ./docker/codegen/Dockerfile .
docker run --rm -u $(id -u) -v "$(pwd):/build" "$DOCKER_IMAGE"
echo "Generated the code successfully"
;;
*)
;;
esac
}
if !(${SKIP_CHECK_DOCKER_COMPOSE_VERSION-false} || check_docker_compose_version)
then
echo "ERROR: Please use docker-compose greather than or equal to $REQUIRED_DOCKER_COMPOSE_VERSION.
Use SKIP_CHECK_DOCKER_COMPOSE_VERSION=true environment variable to skip the check" >&2
exit 1
fi
git submodule init
git submodule update --recursive --remote
get_config_files | while read config_file
do
(create_docker_compose_file "$config_file")
if [ "$CODEGEN" != "false" ]; then
(generate_code "$config_file")
fi
done
## BUILD_ROOT_PATH sed section
# Fix the context parameter in the docker-compose file
# See also BUILD_ROOT_PATH env section above
sed -i "s|$(pwd)|.|g" docker-compose*.yml
echo "You have the docker-compose files. Have fun!"
echo "You can download the prebuild images and bring it up:"
echo " $DOCKER_COMPOSE pull && $DOCKER_COMPOSE up -d"
echo "Or build it yourself:"
echo " $DOCKER_COMPOSE build && $DOCKER_COMPOSE up -d"