-
Notifications
You must be signed in to change notification settings - Fork 6
/
docker-run.sh
executable file
·565 lines (475 loc) · 16 KB
/
docker-run.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
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
#!/usr/bin/env bash
#
# Builds and runs the docker image for the publisher process.
#
# Optionally use "--shell" to get into the container.
#
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd -P)" || exit 1
export ONTPUB_ORG="edmcouncil"
export ONTPUB_FAMILY="${ONTPUB_FAMILY:-fibo}"
export HYGIENE_TEST_PARAMETER_VALUE="${HYGIENE_TEST_PARAMETER_VALUE:-edmcouncil}"
export ONTPUB_VERSION="$(git -C "${SCRIPT_DIR}" rev-parse --abbrev-ref HEAD)"
if [[ -f ${SCRIPT_DIR}/publisher/lib/_functions.sh ]] ; then
# shellcheck source=publisher/lib/_functions.sh
source ${SCRIPT_DIR}/publisher/lib/_functions.sh || exit $?
else # This else section is to trick IntelliJ Idea to actually load _functions.sh during editing
source publisher/lib/_functions.sh || exit $?
fi
function getDirectoryOfGitRepo() {
local -r gitRepoName="$1" # names like "fibo"
#
# First check some common places as they're used on Mac OS X or Linux desktops/laptops.
# Then if that fails check whether we're running in a separate docker container or in Windows WSL
#
if [[ -d "${HOME}/Work/${gitRepoName}" ]] ; then # Used by Jacobus Geluk
echo -n "${HOME}/Work/${gitRepoName}"
return 0
elif [[ -d "${HOME}/Documents/${gitRepoName}" ]] ; then
echo -n "${HOME}/Documents/${gitRepoName}"
return 0
elif [[ -d "${HOME}/${gitRepoName}" ]] ; then
echo -n "${HOME}/${gitRepoName}"
return 0
fi
if isRunningInDockerContainer ; then
#
# When we're running inside a docker container we cannot test for the existence of
# the windows directory that's supposed to be the input directory.
# That shell-container should pass the current user id through somehow. Windows has it in USERNAME env var.
#
#
# JG>Dean and/or Pete can you check if there's an environment variable available when you run this
# that contains your user name as RivettPJ or Dean so that we can automate at least part of the
# path name below?
#
# echo -n "/c/Users/RivettPJ/Documents/${gitRepoName}"
echo -n "/c/Users/Dean/Documents/${gitRepoName}"
return 0
fi
local -r windowsUserName="$(getWindowsEnvironmentVariable USERNAME)"
if [[ -n "${windowsUserName}" ]] ; then
if [[ -d "/c/Users/${windowsUserName}/Documents/${gitRepoName}" ]] ; then
echo -n "/c/Users/${windowsUserName}/Documents/${gitRepoName}"
return 0
elif [[ -d "/cygdrive/c/Users/${windowsUserName}/Documents/${gitRepoName}" ]] ; then
echo -n "c:/Users/${windowsUserName}/Documents/${gitRepoName}"
return 0
fi
fi
if [[ -d "/c/Users/RivettPJ/Documents/${gitRepoName}" ]] ; then
echo -n "/c/Users/RivettPJ/Documents/${gitRepoName}"
return 0
elif [[ -d "/cygdrive/c/Users/Dean/Documents/${ONTPUB_FAMILY}" ]] ; then
echo -n "c:/Users/Dean/Documents/${ONTPUB_FAMILY}"
return 0
fi
error "No ${gitRepoName} root found"
return 1
}
#
# Get the directory of your local git clone of your ontologies that we need to use
# as input to the publisher.
#
# TODO: Make this configurable outside this script
#
function inputDirectory() {
local -r gitRepoName="$1" # names like "fibo"
#
# First try what you specified as the primary ontology name
#
if getDirectoryOfGitRepo "${gitRepoName}" >/dev/null 2>&1 ; then
getDirectoryOfGitRepo "${gitRepoName}"
return 0
fi
#
# Then try the lowercase version of it
#
if getDirectoryOfGitRepo "${gitRepoName,,}" >/dev/null 2>&1 ; then
getDirectoryOfGitRepo "${gitRepoName,,}"
return 0
fi
#
# Then try the uppercase version of it
#
if getDirectoryOfGitRepo "${gitRepoName^^}" >/dev/null 2>&1 ; then
getDirectoryOfGitRepo "${gitRepoName^^}"
return 0
fi
#
# Then try other variations like FIBO-Development
#
if getDirectoryOfGitRepo "${gitRepoName^^}-Development" >/dev/null 2>&1 ; then
getDirectoryOfGitRepo "${gitRepoName^^}-Development"
return 0
fi
error "Could not find the git repo directory for \"${gitRepoName}\""
return 1
}
#
# Find the "output directory" which is the directory that gets the end results of the build/publish process.
#
function outputDirectory() {
#
# JG>Dean, same thing here, we need to test whether we're inside your shell container
# or not. If inside that container, then do not execute the mkdir statement
#
if isRunningInDockerContainer ; then
#
# When we're running inside a docker container we cannot test for the existence of
# the windows directory that's supposed to be the output directory.
# That shell-container should pass the current user id through somehow. Windows has it in USERNAME env var.
#
# Dean, the input and output directory should be different from each other
#
echo -n "c:/Users/Dean/Documents/${ONTPUB_FAMILY}-output"
return 0
fi
mkdir -p "${SCRIPT_DIR}/../target" >/dev/null 2>&1
echo -n "$(cd ${SCRIPT_DIR}/../target && pwd -L)"
}
function temporaryFilesDirectory() {
#
# JG>Dean, same thing here, we need to test whether we're inside your shell container
# or not. If inside that container, then do not execute the mkdir statement
#
if isRunningInDockerContainer ; then
#
# When we're running inside a docker container we cannot test for the existence of
# the windows directory that's supposed to be the tmp directory.
# That shell-container should pass the current user id through somehow. Windows has it in USERNAME env var.
#
echo -n "c:/Users/Dean/Documents/${ONTPUB_FAMILY}-tmp"
return 0
fi
if ! mkdir -p "${SCRIPT_DIR}/../tmp" >/dev/null 2>&1 ; then
error "Could not create directory ${SCRIPT_DIR}/../tmp"
fi
echo -n "$(cd ${SCRIPT_DIR}/../tmp && pwd -L)"
}
function checkCommandLine() {
#
# The --pushimage option publishes the image, after a successful build, to Dockerhub
#
if [[ "$*" =~ .*--pushimage($|[[:space:]]) || "$*" =~ .*--push($|[[:space:]]) ]] ; then
cli_option_pushimage=1
else
cli_option_pushimage=0
fi
#
# The --build option builds the image
#
if [[ "$*" =~ .*--buildimage($|[[:space:]]) || "$*" =~ .*--build($|[[:space:]]) ]] ; then
cli_option_buildimage=1
else
cli_option_buildimage=0
fi
#
# The --rebuild option builds the image from scratch with the ability to change the default values for certain environment variables
#
if [[ "$*" =~ .*--rebuildimage($|[[:space:]]) || "$*" =~ .*--rebuild($|[[:space:]]) ]] ; then
cli_option_buildimage=1
cli_option_rebuildimage=1
else
cli_option_rebuildimage=0
fi
#
# The --run option runs the container
#
if [[ "$*" =~ .*--run($|[[:space:]]) ]] ; then
cli_option_runimage=1
else
cli_option_runimage=0
fi
#
# The --shell option allows you to end up in the shell of the publisher container itself
#
if [[ "$*" =~ .*--shell($|[[:space:]]) ]] ; then
cli_option_buildimage=1
cli_option_runimage=1
cli_option_shell=1
else
cli_option_shell=0
fi
#
# The --dev option makes the container use the local publisher directory for its sources rather than copying
# that into the image.
#
if [[ "$*" =~ .*--dev($|[[:space:]]) ]] ; then
cli_option_dev_mode=1
else
cli_option_dev_mode=0
fi
#
# The --clean option wipes out the contents of the target directory before the container starts
#
if [[ "$*" =~ .*--clean($|[[:space:]]) ]] ; then
cli_option_clean=1
else
cli_option_clean=0
fi
#
# The --dark option forces dark mode for all the colors being used.
#
if [[ "$*" =~ .*--dark($|[[:space:]]) ]] ; then
cli_option_dark=1
else
cli_option_dark=$(getIsDarkMode ; echo $?)
fi
#
# The --verbose option shows more logging
#
if [[ "$*" =~ .*--verbose($|[[:space:]]) ]] ; then
cli_option_verbose=1
else
cli_option_verbose=0
fi
if ((cli_option_dev_mode == 1 && cli_option_pushimage == 1)) ; then
error "Cannot push a dev-mode image to docker hub, the publisher code has to be copied into the image"
return 1
fi
if ((cli_option_verbose)) ; then
logBoolean cli_option_buildimage
logBoolean cli_option_rebuildimage
logBoolean cli_option_runimage
logBoolean cli_option_pushimage
logBoolean cli_option_shell
logBoolean cli_option_clean
logBoolean cli_option_dark
logBoolean cli_option_dev_mode
fi
return 0
}
function dockerFile() {
if ((cli_option_dev_mode)) ; then
cat "${SCRIPT_DIR}/Dockerfile" | sed 's#toolkit_url="[^"]\+"#toolkit_url="https://jenkins.edmcouncil.org/view/rdf-toolkit/job/rdf-toolkit-build-dev/lastSuccessfulBuild/artifact/target/rdf-toolkit.jar"#g ; s#/onto-viewer-publish/#/onto-viewer-build-dev/#g' | sed '/skip in dev mode begin/,/skip in dev mode end/ d' > "${SCRIPT_DIR}/Dockerfile.dev"
echo -n "${SCRIPT_DIR}/Dockerfile.dev"
else
echo -n "${SCRIPT_DIR}/Dockerfile"
fi
}
function containerName() {
echo -n "ontology-publisher"
if ((cli_option_dev_mode)) ; then
#
# Just to make sure that the dev-mode version of the image is not being pushed to Docker Hub because it
# can't run on its own, it doesn't contain the /publisher directory
#
echo -n "-dev"
fi
}
function idOfRunningContainer() {
local -r containerName="$(containerName)"
docker ps -a --no-trunc --filter name=^/${containerName}$ --quiet
}
function isContainerRunning() {
local -r id="$(idOfRunningContainer)"
[[ -n "${id}" ]]
}
function buildImage() {
((cli_option_buildimage)) || return 0
local -r containerName="$(containerName)"
if isContainerRunning ; then
warning "Container ${containerName} is running so we skip the build"
return 0
fi
cd "${SCRIPT_DIR}" || return $?
local -a opts=()
opts+=('build')
#
# The --rebuild option builds the image from scratch with the ability to change the default values for certain environment variables
#
if ((cli_option_rebuildimage)) ; then
opts+=('--no-cache')
opts+=('--build-arg')
opts+=("ONTPUB_FAMILY=${ONTPUB_FAMILY}")
opts+=('--build-arg')
opts+=("ONTPUB_IS_DARK_MODE=${cli_option_dark}")
opts+=('--build-arg')
opts+=("HYGIENE_TEST_PARAMETER_VALUE=${HYGIENE_TEST_PARAMETER_VALUE}")
if [ -n "${RDFTOOLKIT_VERSION}" ] ; then
opts+=('--build-arg')
opts+=("RDFTOOLKIT_VERSION=${RDFTOOLKIT_VERSION}")
fi
fi
opts+=('--label')
opts+=("org.${ONTPUB_ORG}.ontology-publisher.version="${ONTPUB_VERSION}"")
opts+=('--label')
opts+=("org.${ONTPUB_ORG}.ontology-publisher.release-date="$(date "+%Y-%m-%d")"")
opts+=('--tag')
opts+=("edmcouncil/${containerName}:${ONTPUB_VERSION}")
opts+=('--file')
opts+=("$(dockerFile) .")
#
# Build the image and tag it as ontology-publisher:${ONTPUB_VERSION}
#
log "docker ${opts[@]}"
if docker ${opts[@]} ; then
log "--------- Finished Building the Docker Image ---------"
return 0
fi
error "Could not build the image"
return 1
}
function pushImage() {
((cli_option_pushimage == 0)) && return 0
log "docker push ${ONTPUB_ORG}/ontology-publisher:${ONTPUB_VERSION}"
docker push "${ONTPUB_ORG}/ontology-publisher:${ONTPUB_VERSION}"
}
function run() {
((cli_option_runimage == 0)) && return 0
if isContainerRunning ; then
return 0
fi
requireValue ONTPUB_FAMILY || return $?
cd "${SCRIPT_DIR}" || return $?
local inputDirectory
local outputDirectory ; outputDirectory=$(outputDirectory) || return $?
local temporaryFilesDirectory ; temporaryFilesDirectory=$(temporaryFilesDirectory) || return $?
local containerName="ontology-publisher"
if ((cli_option_dev_mode)) ; then
#
# Just to make sure that the dev-mode version of the image is not being pushed to Docker Hub because it
# can't run on its own, it doesn't contain the /publisher directory
#
containerName+='-dev'
fi
if ((cli_option_clean)) ; then
log "Cleaning ${outputDirectory}"
rm -rf "${outputDirectory:?}/"*
log "Cleaning ${temporaryFilesDirectory}"
rm -rf "${temporaryFilesDirectory:?}/"*
else
log "Not cleaning ${outputDirectory}"
fi
local -a opts=()
opts+=('run')
opts+=('--rm')
opts+=('--tty')
opts+=('-u')
opts+=("${UID}:65534")
opts+=('--name')
opts+=("${containerName}")
#
# Remove the --read-only option if you want to experiment with adding new tools to the running container.
# The --read-only flag is set by default (by this script) to protect the image from being overwritten by anything
# that runs inside the container itself.
#
opts+=('--read-only')
opts+=('--env')
opts+=("ONTPUB_IS_DARK_MODE=${cli_option_dark}")
opts+=('--env')
opts+=("ONTPUB_FAMILY=${ONTPUB_FAMILY}")
if [ -n "${ONTPUB_SPEC_HOST}" ] ; then
opts+=('--env')
opts+=("ONTPUB_SPEC_HOST=${ONTPUB_SPEC_HOST}")
fi
if [ -n "${DEV_SPEC}" ] ; then
opts+=('--env')
opts+=("DEV_SPEC=${DEV_SPEC}")
fi
if [ -n "${PROD_SPEC}" ] ; then
opts+=('--env')
opts+=("PROD_SPEC=${PROD_SPEC}")
fi
if [ -n "${HYGIENE_WARN_INCONSISTENCY_SPEC_FILE_NAME}" ] ; then
opts+=('--env')
opts+=("HYGIENE_WARN_INCONSISTENCY_SPEC_FILE_NAME=${HYGIENE_WARN_INCONSISTENCY_SPEC_FILE_NAME}")
fi
if [ -n "${HYGIENE_ERROR_INCONSISTENCY_SPEC_FILE_NAME}" ] ; then
opts+=('--env')
opts+=("HYGIENE_ERROR_INCONSISTENCY_SPEC_FILE_NAME=${HYGIENE_ERROR_INCONSISTENCY_SPEC_FILE_NAME}")
fi
if [ -n "${HYGIENE_TEST_PARAMETER_VALUE}" ] ; then
opts+=('--env')
opts+=("HYGIENE_TEST_PARAMETER_VALUE=${HYGIENE_TEST_PARAMETER_VALUE}")
fi
if [ -n "${ONTPUB_MERGED_INFIX}" ] ; then
opts+=('--env')
opts+=("ONTPUB_MERGED_INFIX=${ONTPUB_MERGED_INFIX}")
fi
if [ -n "${URI_SPACE}" ] ; then
opts+=('--env')
opts+=("URI_SPACE=${URI_SPACE}")
fi
if [ -n "${BRANCH_TAG}" ] ; then
opts+=('--env')
opts+=("BRANCH_TAG=${BRANCH_TAG}")
fi
if [ -n "${DATADICTIONARY_COLUMNS}" ] ; then
opts+=('--env')
opts+=("DATADICTIONARY_COLUMNS=${DATADICTIONARY_COLUMNS}")
fi
if [ -n "${CONSISTENCY_CHECK_TIMEOUT}" ] ; then
opts+=('--env')
opts+=("CONSISTENCY_CHECK_TIMEOUT=${CONSISTENCY_CHECK_TIMEOUT}")
fi
logVar ONTPUB_FAMILY
#
# Now add the mount parameters to the docker command line that mount each given input ontology repo into the
# /input directory (so ontolory repo fibo ends up as /input/fibo inside the container)
#
log "Mounted:"
inputDirectory=$(inputDirectory "${ONTPUB_FAMILY}") || return $?
logItem "/input/${ONTPUB_FAMILY}" "${inputDirectory}"
opts+=("--mount type=bind,source=${inputDirectory},target=/input/${ONTPUB_FAMILY},readonly,consistency=cached")
logItem "/output" "${outputDirectory}"
opts+=("--mount type=bind,source=${outputDirectory},target=/output,consistency=delegated")
# logItem "/var/tmp" "${temporaryFilesDirectory}"
# opts+=("--mount type=bind,source=${temporaryFilesDirectory},target=/var/tmp,consistency=delegated")
#
# When running in dev mode we mount the ontology publisher's repo's root directory as well
#
if ((cli_option_dev_mode)) ; then
logItem "/publisher" "${SCRIPT_DIR}/publisher"
opts+=("--mount type=bind,source=${SCRIPT_DIR}/publisher,target=/publisher,readonly,consistency=cached")
fi
if ((cli_option_shell)) ; then
log "Launching the ${containerName} container in shell mode."
logShellWelcome
opts+=('--interactive')
opts+=('--tty')
opts+=('--entrypoint')
opts+=('/bin/bash')
else
log "Launching the container"
fi
opts+=("${ONTPUB_ORG}/${containerName}:${ONTPUB_VERSION}")
if ((cli_option_shell)) ; then
opts+=('-l')
fi
log "docker ${opts[@]}"
docker ${opts[@]}
local -r rc=$?
return ${rc}
}
function logShellWelcome() {
log "Type $(bold ./publish.sh) to start the build and $(bold exit) to leave this container."
log ""
log "If you want to run the publication of just one or more \"products\" then"
log "specify the names of these products after $(bold ./publish.sh), for instance:"
log ""
log ""
log ""
log "$(bold ./publish.sh ontology vocabulary)"
log ""
}
#
# Connect to the running container with a bash login shell
#
function shell() {
if ! isContainerRunning ; then
return 0
fi
local -r id="$(idOfRunningContainer)"
logShellWelcome
docker exec --interactive --tty ${id} bash --login
}
function main() {
checkCommandLine "$@" || return $?
buildImage || return $?
pushImage || return $?
run || return $?
shell
}
main $@
exit $?