/
tormach_ros_dev_container.sh
executable file
·238 lines (204 loc) · 6.76 KB
/
tormach_ros_dev_container.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
#!/bin/bash -e
EXECUTABLE_NAME="$(basename $0 | sed 's/\(\..*\)$//')"
HARDWARE_MODE=${HARDWARE_MODE:-"sim"}
_write_out() {
local tag=$1
local message="${@:2}"
local timestamp="$(date +'%Y-%m-%dT%H:%M:%S%z')"
printf "%b" \
"$tag::$timestamp:: $message\n"
}
log_info() {
_write_out "$EXECUTABLE_NAME INFO" "$@"
} >&2
log_error() {
_write_out "$EXECUTABLE_NAME ERROR" "$@"
} >&2
success() {
exit 0
} >&2
failure() {
_write_out "$EXECUTABLE_NAME" "Execution failed!"
exit ${1:-"1"}
} >&2
usage() {
_write_out "$EXECUTABLE_NAME USAGE" \
"Printout of executable help and usage informations\n\n" \
"$0 [-d] [-d] [-v] [-t IMAGE] [-n NAME] [-r REGISTRY]\n" \
"Run the launcher container:\n" \
" -d: Detach from container (background mode)\n" \
" -x: Run the image in execution mode (on real hardware, not sim)\n" \
" -t IMAGE: Specify the complete image tag\n" \
" -n NAME: Set container name and hostname to NAME\n"
}
DOCKER_CLI="$(which docker)"
DOCKER_RUN_OPTS+=(-e LAUNCHER=1)
if [[ "$DOCKER_CLI" == "" ]]; then
log_error "Docker CLI executable not found in \${PATH}!" \
"Do you have Docker installed?"
failure 4
fi
DOCKER_RUN_OPTS=(${DOCKER_ARGS})
STARTER_RUN_ARGS=()
while getopts :dxt:n:h ARG; do
case $ARG in
d) DOCKER_RUN_OPTS+=("-d") ;;
x) HARDWARE_MODE="ethercat" ;;
t) IMAGE="$OPTARG" ;;
n) NAME="$OPTARG" ;;
h)
usage
success
;;
:)
usage
log_error "Option -$OPTARG requires an argument"
failure
;;
*)
usage
log_error "Illegal option -$OPTARG"
failure
;;
esac
done
shift $(($OPTIND - 1))
# Docker registry used for the launcher
MAIN_DOCKER_REPOSITORY=${MAIN_DOCKER_REPOSITORY:-tormach_ros_dev}
#DOCKER_REGISTRY=${DOCKER_REGISTRY:-docker.pathpilot.com}
# Set params
IMAGE_TYPE=${IMAGE_TYPE:-dist}
DOCKER_TAG_PATTERN="[^-]+-$IMAGE_TYPE-[^-]+-[0-9]+\.[0-9a-f]+$"
# - Image tag (-t)
if ! [[ -v IMAGE ]]; then
declare -a VALID_IMAGES=()
declare -a sorted_VALID_IMAGES=()
DEFAULT_IMAGE=""
mapfile -t POSSIBLE_IMAGES < <(${DOCKER_CLI} image ls \
--filter=label=com.tormach.pathpilot.robot.image.type=$IMAGE_TYPE \
--format='{{.Repository}}:{{.Tag}}')
for image in ${POSSIBLE_IMAGES[@]}; do
if [[ $image =~ $DOCKER_TAG_PATTERN ]] || [[ $image =~ "latest" ]]; then
created="$(${DOCKER_CLI} inspect -f '{{ index .Config.Labels "com.tormach.pathpilot.robot.createdAt"}}' ${image})" 2>&1
retval="$?"
if ((retval != 0)); then
log_error "Command '${DOCKER_CLI} inspect -f '{{ index .Config.Labels " \
"\"com.tormach.pathpilot.robot.createdAt\"}}' ${image})' returned " \
"error $retval!"
continue
fi
if [[ "$created" == "" ]]; then
log_error "Label 'com.tormach.pathpilot.robot.createdAt' not set on image '$image'" 2>&1
created="$(${DOCKER_CLI} inspect -f '{{ index .Created }}' $image)"
retval="$?"
if ((retval != 0)); then
log_error "${DOCKER_CLI} inspect -f '{{ index .Created }}' $image) " \
"returned error $retval!"
continue
fi
fi
if [[ "$created" == "" ]]; then
log_error "Cannot inspect the image for creation date '$image'. Skipping!"
continue
fi
VALID_IMAGES+=("$image;$created")
fi
done
IFS=$'\n' sorted_VALID_IMAGES=($(sort -t\; -k2 -r <<<"${VALID_IMAGES[*]}"))
unset IFS
VALID_IMAGES=()
for image_tuple in ${sorted_VALID_IMAGES[@]}; do
image=(${image_tuple//;/ })
VALID_IMAGES+=($image)
done
# Prefer the main PathPilot channel for the Robot Launcher before anything else
for image in ${VALID_IMAGES[@]}; do
if [[ $image =~ $DOCKER_REGISTRY/$MAIN_DOCKER_REPOSITORY ]]; then
DEFAULT_IMAGE="$image"
break
fi
done
if [[ "$DEFAULT_IMAGE" == "" ]]; then
DEFAULT_IMAGE="${VALID_IMAGES[0]}"
fi
IMAGE="$DEFAULT_IMAGE"
fi
# - Container name (-n)
NAME=${NAME:-tormach_ros_dev}
# Run Docker
log_info "Launching Za6 container"
CONTAINER_NAME=${NAME}
DOCKER_RUN_OPTS+=(-e HARDWARE_MODE=${HARDWARE_MODE})
test -z "$DOCKER_CONFIG" || DOCKER_RUN_OPTS+=(-e DOCKER_CONFIG="$DOCKER_CONFIG")
test -z "$DOCKER_REGISTRY" || DOCKER_RUN_OPTS+=(-e DOCKER_REGISTRY="$DOCKER_REGISTRY")
test -z "$ROS_SETUP" || DOCKER_RUN_OPTS+=(-e ROS_SETUP="$ROS_SETUP")
# PathPilot on real display
DOCKER_RUN_OPTS+=(
-e DISPLAY=:0
-v /tmp/.X11-unix:/tmp/.X11-unix
-v /dev/dri:/dev/dri
--network host
)
# Check for existing containers: If a container exists, script will
# continue only if it is stopped (& after removing it)
EXISTING="$(docker ps -aq --filter=name=^/${CONTAINER_NAME}$)"
RUNNING=false
if test -n "${EXISTING}"; then
# Container exists; is it running?
RUNNING=$(docker inspect $CONTAINER_NAME |
awk -F '[ ,]+' '/"Running":/ { print $3 }')
if test "${RUNNING}" = "false"; then
log_info "Stopped container '${CONTAINER_NAME}' exists; removing"
${DOCKER_CLI} rm ${CONTAINER_NAME}
elif test "${RUNNING}" = "true"; then
log_error "Container '${CONTAINER_NAME}' already running; exiting"
failure
else
# Something went wrong
log_error "Error: unable to determine status of " \
"existing container '${EXISTING}'"
failure
fi
fi
if tty -s; then
# interactive shell
DOCKER_INTERACTIVE=-i
fi
if test -n "$XDG_RUNTIME_DIR"; then
DOCKER_RUN_OPTS+=(-v $XDG_RUNTIME_DIR:$XDG_RUNTIME_DIR)
fi
# Determine user
C_UID=$(id -u)
C_GID=$(id -g)
# Test if IMAGE to run exists
if [[ -v IMAGE && "$IMAGE" != "" ]]; then
log_info "Starting container from image $IMAGE"
else
log_error "ERROR: No runnable image found!"
failure 5
fi
# Run the launcher container
set -x
exec ${DO} ${DOCKER_CLI} run --rm \
${DOCKER_INTERACTIVE} \
-t --privileged \
-e UID=${C_UID} \
-e GID=${C_GID} \
-e QT_X11_NO_MITSHM=1 \
-e XDG_RUNTIME_DIR \
-e HOME \
-e USER \
-e TERM \
-e HARDWARE_MODE \
-e CURRENT_BASE_OS_VENDOR="$(. /etc/os-release && echo $ID)" \
-e CURRENT_BASE_OS_DEBIAN_SUITE="$(. /etc/os-release && echo $VERSION_CODENAME)" \
-e DBUS_SESSION_BUS_ADDRESS \
-v $HOME:$HOME \
-v $PWD:$PWD \
-v /var/run/dbus/system_bus_socket:/var/run/dbus/system_bus_socket \
-v /var/run/docker.sock:/var/run/docker.sock \
-w $PWD \
-h ${CONTAINER_NAME} --name ${CONTAINER_NAME} \
"${DOCKER_RUN_OPTS[@]}" \
--entrypoint ./entrypoint \
${IMAGE} "$@"