Newer
Older

Mark OLESEN
committed
#!/bin/sh

Mark OLESEN
committed
imageBasename="opencfd/openfoam"
imageFlavour="-run"

Mark OLESEN
committed
#------------------------------------------------------------------------------
# ========= |
# \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
# \\ / O peration |
# \\ / A nd | www.openfoam.com
# \\/ M anipulation |
#------------------------------------------------------------------------------

Mark OLESEN
committed
#------------------------------------------------------------------------------
# SPDX-License-Identifier: (GPL-3.0-or-later)

Mark OLESEN
committed
#
# Script
# openfoam-docker
#
# Description
# Run script for openfoam container images

Mark OLESEN
committed
# (https://hub.docker.com/u/opencfd)
#
# Copy/link to automatically specify OpenFOAM version and image 'flavour'
# For example,
#

Mark OLESEN
committed
#
#------------------------------------------------------------------------------
printHelp()
{
defaultImage="${imageBasename}${imageFlavour}:${openfoamVersion}"

Mark OLESEN
committed
cat<<HELP_HEAD
Usage: ${0##*/} [OPTION] [-- application ...]
${0##*/} [OPTION] [command_string]
options:
-c Shell commands read from the first non-option argument
-data=DIR Specify mount dir for container '/data'
-dir=DIR Specify mount dir for container '~/' (default: pwd)

Mark OLESEN
committed
-base | -dev | -run | -tut | -default
Image flavour (default: -run).
Not all image flavours are necessarily available.
-X | -x X11 forwarding: enable (-X) or disable (-x)
-update Update (pull) the image, do not run
-dry-run Report the start command, without running
-verbose Additional verbosity when starting (FOAM_VERBOSE)
HELP_HEAD
if [ -n "$1" ]
then
cat<<'HELP_FULL'
-entry=PATH Alternative entry point
-image=NAME | -i=NAME
Specify image to run
-docker Use docker (default)

Mark OLESEN
committed
-podman Use podman instead of docker
-sudo Prefix container calls with 'sudo'
-xhost Allow container access to host network (implies -X)
--shm-size=BYTES Size of /dev/shm (eg, --shm-size=4G)

Mark OLESEN
committed
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
HELP_FULL
fi
cat<<TAIL_OPTIONS
-- The end of option processing.
An argument of - or / is equivalent to --.
-h | -help Display short help and exit
-help-full Display full help and exit
Run OpenFOAM container image (${defaultImage})
- Simulations are stored on the host system (not the container)
- Mounts the current or specified directory (except the HOME directory).
Script Version: ${scriptVersion:-[]}
Note:
The user name within the container is 'openfoam',
but matches the user/group id from the host.
Requires docker or podman.
TAIL_OPTIONS
if [ -n "$1" ]
then
cat<<HELP_FULL
Equivalent options:
| -dir=DIR | -case=DIR | -case DIR | -home=DIR
Example:
${0##*/} -dir=/path/to/simulation
or cd /path/to/simulation && ${0##*/}
Note:
The '-xhost' option can be useful in combination with 'ssh -X',
but should be used sparingly since it permits the container full
access to network communication (potentially insecure).
HELP_FULL
fi
cat<<'FOOTER'
Note:
Different OpenFOAM components and modules may be present (or missing)
on any particular container image.
For example, source code, tutorials, in-situ visualization,
paraview plugins, external linear-solver interfaces etc.
For more information: www.openfoam.com
FOOTER
exit 0 # A clean exit
}
# Report error and exit
die()
{
exec 1>&2
echo
echo "Error encountered:"
while [ "$#" -ge 1 ]; do echo " $1"; shift; done
echo
echo "See '${0##*/} -help' for usage"
echo
exit 1
}
#------------------------------------------------------------------------------
# Constants - user name/locations MUST correspond to the image assets
toolChain=docker
container_home='/home/openfoam' # Home for container user
container_tmphome='/tmp/.home.openfoam' # Fake home for container user
#------------------------------------------------------------------------------
# Select 'podman' toolchain if mentioned in script name:
case "${0##*/}" in (*-podman*) toolChain=podman;; esac

Mark OLESEN
committed
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
# Get openfoam version and/or image flavour from script name
# "/path/openfoam{VERSION}"
# "/path/openfoam{VERSION}-{FLAVOUR}"
imageTag="$(echo "${0##*/}" | sed -ne 's/openfoam\([1-9][0-9]*\).*/\1/ip')"
# Version:
if [ -n "$imageTag" ]
then
openfoamVersion="$imageTag"
fi
# Flavour:
imageTag="-${0##*-}"
case "$imageTag" in
(-base)
unset imageFlavour
;;
(-run | -default)
imageFlavour="$imageTag"
;;
(-dev*)
imageFlavour="-dev"
;;
(-tut*)
imageFlavour="-tutorials"
;;
esac
#------------------------------------------------------------------------------
# Parse options
unset image sudo
unset mount1Dir mount2Dir
unset optDryrun optEntrypoint optShellCommand optUpdate optVerbose optShmSize

Mark OLESEN
committed
unset optX11Forwarding
while [ "$#" -gt 0 ]
do
case "$1" in
('') ;;
(- | -- | /)
shift
break # Stop option parsing
;;
# OpenFOAM versions (eg, -2112, -2106, -2012, etc)

Mark OLESEN
committed
(-[1-9]*) openfoamVersion="${1#*-}" ;;
(-v[1-9]*) openfoamVersion="${1#*-v}" ;;
# Image flavours
(-base) unset imageFlavour ;;
(-run) imageFlavour="-run" ;;
(-def*) imageFlavour="-default" ;;
(-dev*) imageFlavour="-dev" ;;
(-tut*) imageFlavour="-tutorials" ;;
(-c) # Shell command
optShellCommand="-c"
;;

Mark OLESEN
committed
printHelp -full
;;

Mark OLESEN
committed
printHelp
;;
(-docker | -podman)
toolChain="${1#*-}"
;;
(-sudo) # Use sudo
sudo="sudo"
;;
(--shm-size=*)
optShmSize="${1#*=}"
;;
(-case)

Mark OLESEN
committed
# OpenFOAM-style '-case' for specfying the HOME mount
[ "$#" -ge 2 ] || die "'$1' option requires an argument"
shift
mount1Dir="$1"
;;
# Various ways to specify the HOME mount

Mark OLESEN
committed
mount1Dir="${1#*=}"
;;
# Additional DATA mount

Mark OLESEN
committed
mount2Dir="${1#*=}"
;;

Mark OLESEN
committed
optEntrypoint="${1#*=}"
;;
(-i=* | -image=*) # Alternative image name

Mark OLESEN
committed
image="${1#*=}"
;;
(-update | -upgrade) # Also allow 'upgrade' (for Ubunutu people)

Mark OLESEN
committed
optUpdate=true
;;
(-dry-run | -dryrun)
optDryrun=true
;;
(-verbose)
optVerbose=true
;;
(-X) # Enable X11 forwarding
: "${optX11Forwarding:=X}"
;;
(-x) # Disable X11 forwarding
unset optX11Forwarding
;;
(-xhost) # Any host X11 forwarding
optX11Forwarding="host"
;;

Mark OLESEN
committed
die "Invalid option '$1'"
;;

Mark OLESEN
committed
if [ -n "$optShellCommand" ]
then
break
else
die "Unexpected argument '$1'"
fi
;;
esac
shift
done
if [ -z "$image" ]
then
image="${imageBasename}${imageFlavour}:${openfoamVersion}"

Mark OLESEN
committed
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
fi
if [ -n "$optUpdate" ]
then
if [ -n "$optDryrun" ]
then
runPrefix="echo"
echo "(dry-run)" 1>&2
echo 1>&2
else
runPrefix="$sudo"
echo "Update image: $image" 1>&2
fi
$runPrefix ${toolChain:?} pull "$image"
exitCode="$?"
echo 1>&2
echo "Done" 1>&2
exit "$exitCode"
fi
if [ -n "$optShellCommand" ] && [ "$#" -eq 0 ]
then
die "-c: option requires an argument"
fi
#------------------------------------------------------------------------------
# Sanity and setup
guest_uid="$(id -u 2>/dev/null)"
guest_gid="$(id -g 2>/dev/null)"
[ -n "$guest_uid" ] || die "Cannot determine current user id"
[ -n "$guest_gid" ] || die "Cannot determine current group id"
# Mount directory (mandatory)
if [ -z "$mount1Dir" ]
then
mount1Dir="$(pwd -P)" # Default is current directory
elif [ -d "$mount1Dir" ]
then
mount1Dir="$(cd "$mount1Dir" && pwd -P)"
else
die "No such mount directory: $mount1Dir"
fi
if [ "$mount1Dir" = "$(cd "$HOME" && pwd -P)" ]
then
die "Cannot use home directory as the mount-point" \
"Run from a subdirectory instead"
fi
# Data directory (optional)
if [ -n "$mount2Dir" ]
then
if [ -d "$mount2Dir" ]
then
mount2Dir="$(cd "$mount2Dir" && pwd -P)"
else
echo "${0##*/}: ignore invalid -data directory: $mount2Dir" 1>&2
unset mount2Dir
fi
fi
# Older (non-nss) user/group handling
# userMapping()
# {
# echo '--volume=/etc/group:/etc/group:ro'
# echo '--volume=/etc/passwd:/etc/passwd:ro'
# echo '--volume=/etc/shadow:/etc/shadow:ro'
# echo '--volume=/etc/sudoers.d:/etc/sudoers.d:ro'
# }
# Environment and settings for X11 forwarding
#
# See Also
# https://stackoverflow.com/questions/48235040/run-x-application-in-a-docker-container-reliably-on-a-server-connected-via-ssh-w
# https://blog.yadutaf.fr/2017/09/10/running-a-graphical-app-in-a-docker-container-on-a-remote-server/
# Prepare X11 mapping
# - use tmp Xauthority file in local (mounted) directory
unset display_host tmpXauth_host tmpXauth_guest
if [ -n "$DISPLAY" ] \
&& [ -n "$optX11Forwarding" ] \
&& [ -d "$mount1Dir" ] \
&& command -v xauth >/dev/null
then
# DISPLAY="host:0" vs DISPLAY=":0"
display_host="${DISPLAY%%:*}"
tmpXauth_host="$(mktemp --tmpdir="$mount1Dir" .Xauthority.container.XXXX)"
trap "rm -f \"$tmpXauth_host\"; exit 0" EXIT TERM INT # Remove on exit
# X-authority file to allow any hostname. Generally reasonably safe
xauth nlist "$DISPLAY" | sed -e 's/^..../ffff/' | \
xauth -f "$tmpXauth_host" nmerge -
# The mounted name
tmpXauth_guest="${container_home}/${tmpXauth_host##*/}"
fi
# Define container run options for X11 mapping
x11Mapping()
{
if [ -n "$tmpXauth_guest" ]
then
echo "--env=DISPLAY=$DISPLAY"
echo "--env=XAUTHORITY=$tmpXauth_guest"
# Accessing via ssh -X ('localhost:0' etc) or forced with -xhost
if [ -n "$display_host" ] || [ "$optX11Forwarding" = host ]
then
echo "--net=host"
elif [ -d /tmp/.X11-unix ] # No display host, bind sockets
then
echo "--volume=/tmp/.X11-unix:/tmp/.X11-unix"
fi
fi
}
if [ "$optVerbose" = true ]
then
set -x
fi
if [ -n "$optDryrun" ]
then
runPrefix="echo"
echo "(dry-run)" 1>&2
echo 1>&2
else
runPrefix="$sudo"
fi
if [ "$optVerbose" = true ]
then
set -x
fi

Mark OLESEN
committed
--rm -t -i \
--user="$guest_uid:$guest_gid" \
${mount1Dir:+--volume="$mount1Dir:$container_home"} \
${mount2Dir:+--volume="$mount2Dir:/data"} \
$(x11Mapping) \
${optShmSize:+--shm-size="$optShmSize"} \

Mark OLESEN
committed
${optEntrypoint:+--entrypoint="$optEntrypoint"} \
"$image" $optShellCommand "$@"
# ---------------------------------------------------------------------------