-
Notifications
You must be signed in to change notification settings - Fork 4
/
lm-docker
executable file
·118 lines (105 loc) · 3.61 KB
/
lm-docker
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
#!/usr/bin/env bash
# Copyright (c) 2020-2024 CRS4
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
set -o nounset
set -o errexit
function debug_log() {
if [[ -n "${DEBUG:-}" ]]; then
log "${@}"
fi
}
function log() {
printf "%s [lm-docker] %s\n" "$(date +"%F %T")" "${*}" >&2
}
# Configure DEBUG mode
DEBUG="${DEBUG:-0}"
DEBUG_OPT=""
# Configure Docker image
DOCKER_IMAGE="crs4/lifemonitor:latest"
# Configure default LOCAL_PATH
LOCAL_PATH="."
# Collect arguments to be passed on to the next program in an array, rather than
# a simple string. This choice lets us deal with arguments that contain spaces.
ENTRYPOINT_ARGS=()
# Configure Docker command
docker_cmd=$(which docker)
# Configure default volume mount option
docker_volume_opt=""
docker_volume_arg=""
# parse arguments
while [ -n "${1:-}" ]; do
# Copy so we can modify it (can't modify $1)
OPT="$1"
# Detect argument termination
if [ x"$OPT" = x"--" ]; then
shift
for OPT; do
# append to array
ENTRYPOINT_ARGS+=("$OPT")
done
break
fi
# Parse current opt
while [ x"$OPT" != x"-" ]; do
case "$OPT" in
-d | --debug)
DEBUG="1"
DEBUG_OPT="--debug"
;;
-o | --output-path)
LOCAL_PATH="$2"
docker_volume_opt="-v ${LOCAL_PATH}:${LOCAL_PATH}"
docker_volume_arg="--output-path ${LOCAL_PATH}"
mkdir -p ${LOCAL_PATH}
shift
;;
-*=* | --*=*) # unsupported option format
echo "Error: Unsupported option format "$1". Retry without '='" >&2
exit 1
;;
*)
# append to array
ENTRYPOINT_ARGS+=("$OPT")
break
;;
esac
# Check for multiple short options
# NOTICE: be sure to update this pattern to match valid options
NEXTOPT="${OPT#-[cfr]}" # try removing single short opt
if [ x"$OPT" != x"$NEXTOPT" ]; then
OPT="-$NEXTOPT" # multiple short opts, keep going
else
break # long form, exit inner loop
fi
done
# move to the next param
shift
done
if [[ -n "${ENTRYPOINT_ARGS:-}" ]]; then
debug_log "ENTRYPOINT ARGS: ${ENTRYPOINT_ARGS}"
debug_log "LOCAL PATH: ${LOCAL_PATH}"
# Run Docker container
${docker_cmd} run -it --rm \
${docker_volume_opt} --entrypoint /bin/bash ${DOCKER_IMAGE} \
lm "${DEBUG_OPT}" "${ENTRYPOINT_ARGS[@]}" "${docker_volume_arg}"
else
${docker_cmd} run --rm --entrypoint /bin/bash ${DOCKER_IMAGE} lm --help
echo -e "\nError: no command given." >&2
fi