forked from MachineKoderCompany/linuxcnc-mt-docker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
docker-dev.sh
executable file
·249 lines (233 loc) · 7.12 KB
/
docker-dev.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
#!/bin/bash -e
usage() {
if test -z "$*"; then
RC=0
else
echo "Error: $*" >&2
RC=1
fi
cat >&2 <<EOF
Usage: $0 [args]
run args: [-n NAME] [-t IMAGE] [-N] [-l LINK] [CMD [ARG ...]]
build args: -b [-t IMAGE] [-N] [-- docker build args]
bump-ver args: -B
push args: -p [-N]
check-env args: -c
EOF
exit $RC
}
while getopts :bBpct:N-n:l:h ARG; do
case $ARG in
# Script mode (default: docker run)
b) BUILD=true ;;
B)
BUMP_IMAGE_VERSION=true
WANT_ENV='bare-metal docker-run'
;;
p) PUSH=true ;;
# Global options
t)
IMAGE_BASE=$OPTARG
USE_OVERLAY=false
;;
N) USE_OVERLAY=false ;;
-) break ;; # Following args passed to docker command
# Run options
n) NAME=$OPTARG ;;
l) LINK_CONTAINER=$OPTARG ;;
c) CHECK_ENV=true ;;
# Usage
h) usage ;;
:) usage "Option -$OPTARG requires an argument" ;;
*) usage "Illegal option -$OPTARG" ;;
esac
done
shift $(($OPTIND - 1))
# Set params
# - Build image? (-b)
BUILD=${BUILD:-false}
# - Bump image version? (-B)
BUMP_IMAGE_VERSION=${BUMP_IMAGE_VERSION:-false}
# - Push image? (-p)
PUSH=${PUSH:-false}
# - Container name (-n)
NAME=${NAME:-linuxcnc-mk}
HOSTNAME=lcnc-mk-devel
# - Container name to link (-l NAME): for sim
LINK_CONTAINER=${LINK_CONTAINER:+--link=${LINK_CONTAINER}}
# - Check if running bare metal? (-c)
CHECK_ENV=${CHECK_ENV:-false}
# Read common settings
WANT_ENV="${WANT_ENV:-bare-metal}"
if ${CHECK_ENV}; then
WANT_ENV_CHECK="${WANT_ENV}"
WANT_ENV=
fi
source "$(dirname $0)/docker/env.sh"
###########################
# Check wanted environment
if ${CHECK_ENV}; then
if test_environment ${WANT_ENV_CHECK}; then
exit 0 # outside docker
else
exit 1 # in docker
fi
fi
###########################
# Bump image version
if ${BUMP_IMAGE_VERSION}; then
echo "Bumping image version"
save_image_version
$BUILD || exit $(test -z ${IMAGE_VERSION_UPDATED})
fi
###########################
# Docker build base or overlay image
if $BUILD; then
if ! ${USE_OVERLAY}; then
# Build base image when -N is supplied
# - Use a local mirror if specified
test -z "${DEBIAN_MIRROR}" || DOCKER_DEV_BUILD_OPTS+=(
--build-arg DEBIAN_MIRROR=${DEBIAN_MIRROR})
test -z "${DEBIAN_SECURITY_MIRROR}" || DOCKER_DEV_BUILD_OPTS+=(
--build-arg DEBIAN_SECURITY_MIRROR=${DEBIAN_SECURITY_MIRROR})
test -z "${HTTP_PROXY}" || DOCKER_DEV_BUILD_OPTS+=(
--build-arg HTTP_PROXY=${HTTP_PROXY})
# - Docker build
set -x
exec docker build -t "${IMAGE_BASE}" \
--build-arg ROS_DISTRO=$ROS_DISTRO \
--build-arg DEBIAN_SUITE=$DEBIAN_SUITE \
--build-arg IMAGE_VERSION=$IMAGE_VERSION \
"${DOCKER_DEV_BUILD_OPTS[@]}" \
${BUILD_ARGS} \
"$@" "${DOCKER_SCRIPTS_DIR}"
else
# Build the overlay image when -N isn't supplied
# - OVERLAY_DIR may be set in the rc file or the environment
test -z "${OVERLAY_DIR}" && usage "Please specify overlay directory"
test -d "${OVERLAY_DIR}" ||
usage "Overlay directory ${OVERLAY_DIR} does not exist"
# - Docker build
set -x
exec docker build -t "${IMAGE_OVERLAY}" \
--build-arg IMAGE_BASE="${IMAGE_BASE}" \
${OVERLAY_DOCKER_PATH:+--file=${OVERLAY_DOCKER_PATH}} \
"${DOCKER_DEV_OVERLAY_BUILD_OPTS[@]}" \
${OVERLAY_BUILD_ARGS} \
"$@" \
"${OVERLAY_DIR}"
fi
fi
###########################
# Docker push
if $PUSH; then
set -x
exec docker push ${IMAGE}
fi
###########################
# Docker run
# Check for existing containers
EXISTING="$(docker ps -aq --filter=name=^/${NAME}$)"
RUNNING=false
if test -n "${EXISTING}"; then
# Container exists; is it running?
RUNNING=$(docker inspect ${NAME} | awk -F '[ ,]+' '/"Running":/ { print $3 }')
if test "${RUNNING}" = "false"; then
# FIXME If container exists but stopped, what to do?
echo "Error: Container '${NAME}' exists but stopped" >&2
echo "Please fix this and restart this script" >&2
exit 1
elif test "${RUNNING}" = "true"; then
echo "Container '${NAME}' already running" >&2
else
# Something went wrong
echo "Error: unable to determine status of " \
"existing container '${EXISTING}'" >&2
exit 1
fi
else
# Container doesn't exist yet; detect hardware
# - Video driver
case "${OGL_VENDOR}::${OGL_RENDERER}" in
"Intel Open Source Technology Center"::*) # Brix; John's ThinkPad X201t
echo "Detected Intel graphics card"
# No special config
;;
"NVIDIA Corporation"::*) # Alexander's NVidia w/proprietary drivers
echo "Detected NVIDIA graphics card"
DOCKER_DEV_OPTS+=(
--privileged
--runtime=nvidia
-e NVIDIA_VISIBLE_DEVICES=all
-e NVIDIA_DRIVER_CAPABILITIES=graphics
)
;;
"VMware, Inc."::*) # SSH forwarded X connection
# (Probably will never see this)
echo "Detected virtual graphics hardware"
echo "WARNING: The robot_ui will not run with this hardware!"
;;
"nouveau"::*) # Bas's NVidia w/FOSS drivers
echo "Detected Nouveau driver"
# No special config
;;
"X.Org"::*POLARIS*) # Rob's AMD RX 580 GPU
echo "Detected Polaris driver"
DOCKER_DEV_OPTS+=(-v /dev/kfd:/dev/kfd)
;;
*)
echo "Unable to detect graphics hardware"
echo "WARNING: The robot_ui may not run with this hardware!"
echo "Please add your hardware to the '$0' script"
;;
esac
fi
if tty -s; then
# interactive shell
DOCKER_INTERACTIVE=-i
fi
# Give the user a shell
if ! ${RUNNING}; then
# No existing container; start new one
C_UID=$(id -u)
C_GID=$(id -g)
TZ=$(cat /etc/timezone)
set -x
exec docker run --rm \
--privileged \
${DOCKER_INTERACTIVE} \
-t \
--network host \
-e UID=${C_UID} \
-e GID=${C_GID} \
-e TZ=${TZ} \
-e XDG_RUNTIME_DIR \
-e HOME \
-e USER \
-e TERM \
-e DISPLAY \
-v $HOME:$HOME \
-v $XDG_RUNTIME_DIR:$XDG_RUNTIME_DIR \
-e DBUS_SESSION_BUS_ADDRESS \
-e PULSE_SERVER=unix:${XDG_RUNTIME_DIR}/pulse/native \
-v ${XDG_RUNTIME_DIR}/pulse/native:${XDG_RUNTIME_DIR}/pulse/native \
-v /tmp/.X11-unix:/tmp/.X11-unix \
-v /var/run/dbus/system_bus_socket:/var/run/dbus/system_bus_socket \
-v /opt/mycroft:/opt/mycroft \
-v $PWD:$PWD \
"${DOCKER_OPTS[@]}" \
-w $PWD \
-h ${HOSTNAME} --name ${NAME} \
"${DOCKER_DEV_OPTS[@]}" \
${LINK_CONTAINER} \
${IMAGE} "$@"
else
# Container already started: Exec a new shell in the existing container
if test -z "$*"; then
set -x
exec docker exec ${DOCKER_INTERACTIVE} -tu ${USER} ${NAME} bash -i
else
set -x
exec docker exec ${DOCKER_INTERACTIVE} -tu ${USER} ${NAME} "$@"
fi
fi