forked from isard-vdi/isard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.sh
executable file
·342 lines (322 loc) · 7.65 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
#!/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 [ ! $(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
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
engine
static
portal
hypervisor
websockify
squid
webapp
grafana
stats
api
authentication
vpn
guac
toolbox
backupninja
"
HYPERVISOR_KEY="hypervisor"
HYPERVISOR_PARTS="
network
video
hypervisor
websockify
squid
stats
guac
guac-vpnc
"
HYPERVISOR_STANDALONE_KEY="hypervisor-standalone"
HYPERVISOR_STANDALONE_PARTS="
network
hypervisor
hypervisor-standalone
stats
"
VIDEO_STANDALONE_KEY="video-standalone"
VIDEO_STANDALONE_PARTS="
network
video
websockify
squid
guac
"
TOOLBOX_KEY="toolbox"
TOOLBOX_PARTS="
network
toolbox
"
TOOLBOXBASE_KEY="toolbox-base"
TOOLBOXBASE_PARTS="
toolbox-base
"
WEB_KEY="web"
WEB_PARTS="
network
db
engine
static
portal
webapp
grafana
api
authentication
vpn
"
docker_compose_version(){
docker-compose --version | 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 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 $args config > "docker-compose$delimiter$config_name.yml"
fi
}
parts_variant(){
local variant="$1"
shift || return 0
for part in $@
do
echo -n "$part.$variant "
done
}
variants(){
local config_name="$1"
shift || return 0
if check_docker_compose_version
then
version_parts="$(parts_variant current $@)"
else
version_parts="$(parts_variant legacy $@)"
fi
case $USAGE in
production)
merge "$config_name" $@ $version_parts
;;
test)
merge "$config_name" $@ $(parts_variant test $@) $version_parts
;;
build)
merge "$config_name" $@ $(parts_variant test $@) $(parts_variant build $@) $version_parts
;;
devel)
merge "$config_name" $@ $(parts_variant test $@) $(parts_variant build $@) $(parts_variant devel $@) $version_parts
;;
*)
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> ... shoud be files like docker-compose-parts/<part-1>.yml
# - variants build and devel sould be like docker-compose-parts/<part-1>.build.yml
# and docker-compose-parts/<part-1>.devel.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 "$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
;;
$TOOLBOX_KEY)
parts=$TOOLBOX_PARTS
;;
$TOOLBOXBASE_KEY)
parts=$TOOLBOXBASE_PARTS
;;
$WEB_KEY)
parts=$WEB_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/stats//')"
fi
if [ "$BACKUP_DB_ENABLED" = "false" ] && [ "$BACKUP_DISKS_ENABLED" = "false" ]
then
parts="$(echo $parts | sed 's/backupninja//')"
fi
flavour "$config_name" $parts
}
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")
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"