-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathros-docker.sh
More file actions
executable file
·266 lines (235 loc) · 7.24 KB
/
ros-docker.sh
File metadata and controls
executable file
·266 lines (235 loc) · 7.24 KB
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
#!/bin/bash
#
# Spine - Spine - MCU code for robotics.
# Copyright (C) 2019-2021 Codam Robotics
#
# This file is part of Spine.
#
# Spine is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Spine is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with Spine. If not, see <http://www.gnu.org/licenses/>.
#
#
### ros-docker.sh
# Build a ROS build container for development and testing
# Checkout the README.md or run 'ros-docker.sh help' to figure out how to get started
#
## Core dependency: Returns absolute path of a folder
# source: http://stackoverflow.com/a/18443300/441757
realpath() {
OURPWD=$PWD
cd "$(dirname "$1")"
LINK=$(readlink "$(basename "$1")")
while [ "$LINK" ]; do
cd "$(dirname "$LINK")"
LINK=$(readlink "$(basename "$1")")
done
REALPATH="$PWD/$(basename "$1")"
[ "${REALPATH: -1}" = "." ] && [ ! ${#REALPATH} -eq 1 ] && REALPATH=$(echo "$REALPATH" | sed 's/.$//')
[ "${REALPATH: -1}" = "/" ] && [ ! ${#REALPATH} -eq 1 ] && REALPATH=$(echo "$REALPATH" | sed 's/\/$//')
cd "$OURPWD"
echo "$REALPATH"
} # End of realpath()
## environment variables
#
THIS="$(realpath $0)"
BASEDIR="$(realpath "$(dirname "$0")")"
STAGING="$BASEDIR/build_staging"
LIB="$BASEDIR/lib"
## includes
#
# shellcheck source=lib/colours.sh
source "$LIB"/colours.sh
## setup clean up traps
#
clean_up()
{
[ -d "$STAGING" ] && rm -rf "$STAGING"
}
trap "clean_up INT" INT
trap "clean_up TERM" TERM
trap "clean_up EXIT" EXIT
#
## Sanitize input
# Sna-ke becomes sna_ke
# reads from stdin or arguments list
sanitize() {
[ $# -eq 0 ] && read input || input="$*"
echo $input | sed 's/-/_/g' | tr -dc '[:alnum:]_\n\r' | tr '[:upper:]' '[:lower:]'
} # End of sanitize()
#
## check a condition and quit if it is false
#
assert() {
[ $# -lt 2 ] && { echo "assert called without condition + linenumber!"; exit 1; }
(eval "$1" &>/dev/null) || { echo -e "${COLOR_RED}assertion failed!${COLOR_NC} : $*"; usage; exit 1; }
}
#
## install bindings
#
install() {
local alias="alias ros='$THIS'"
local rcs=("$HOME/.zshrc" "$HOME/.bashrc" )
for rc in "${rcs[@]}"; do
rc=$(realpath $rc)
local _op="echo $alias >> $rc"
[ -f $rc ] && [[ "$(grep -e '^alias ros=' $rc )" == "" ]] && { [ -z "${DRYRUN+x}" ] && eval "$_op" || dryrun @ $LINENO: "$_op"; }
done
} # end of install
#
## Display usage options
#
usage() {
cat<<-EOF
$0 - Develop and test ROS in Docker
usage:
$0 [options] package [package directory] [shell command] -> for single package
$0 [options] workspace [catkin_ws directory] [shell command] -> for entire workspace
options:
-h, --help -> will display these very words
-i, --install -> will add alias 'ros' to ~/.bashrc and ~/.zshrc
-c, --clean -> do a clean build (no preinstalled rosdeps)
-r, --rosdep -> install rosdependency before execution of commands
-b, --rebuild -> force rebuilding of Docker image
-d, --dryrun -> do a dryrun (show commands that would be executed)
-D, --debug -> internal debugging -> stop on first error
examples:
$0 (-c) (-r) (-d) (-b) package /opt/catkin_ws/src/[package dir] [command]
$0 (-c) (-r) (-d) (-b) workspace /opt/catkin_ws [command]
EOF
} # End of usage()
#
## Read input arguments given on the commandline
#
read_input() {
# set defaults
FLAVOUR=TARGETED
ROSDEP=NOROSDEP
# display usage if no arguments were given
[ $# -eq 0 ] && { usage; exit 1; }
while [ $# -gt 0 ]; do
case "$1" in
-h|--help) usage; exit 0; ;;
-i|--install) export TARGET=install; shift; ;;
-c|--clean) export FLAVOUR=CLEAN; shift; ;;
-r|--rosdep) export ROSDEP=ROSDEP; shift; ;;
-b|--rebuild) export FORCE_REBUILD=TRUE; shift; ;;
-d|--dryrun) export DRYRUN=TRUE; shift; ;;
-D|--debug) export DEBUG=TRUE; set -e; shift; ;;
package) export TARGET=PACKAGE; shift; ;;
workspace) export TARGET=WORKSPACE; shift; ;;
*)
# read directory or command
if [ -d $1 ]; then
LPATH=$(realpath $1)
elif [ ! -z ${TARGET+x} ] && [ ! -z ${LPATH} ]; then
CMD="$*"; break;
else
usage; exit 1
fi
shift; ;;
esac
done
# catch install target
[[ "$TARGET" == "install" ]] && { install; exit 0; }
# these values must be set
assert '[ ! -z ${LPATH+x} ] && [ -d ${LPATH} ]' $LINENO : "No path specified!"
assert '[ ! -z ${FLAVOUR+x} ]' $LINENO
assert '[ ! -z ${TARGET+x} ]' $LINENO : "Must specify: workspace / package!"
} # End of read_input()
dryrun() {
echo -e "${COLOR_YELLOW}DRYRUN${COLOR_NC} @ $*"
} # End of drurun()
notify() {
echo -e "${COLOR_GREEN}STATUS${COLOR_NC} @ $*"
} # End of drurun()
#
## Build the Docker image
#
build() {
local catkin_ws=/opt/catkin_ws
# setup empty stub folder
mkdir $STAGING
case ${FLAVOUR} in
CLEAN)
local project="clean"
;;
TARGETED)
local project;
project="$(sanitize "$(echo "$LPATH" | rev | cut -f1 -d/ | rev)")"
# copy over files from project to temporarily insert into docker container for rosdep
local _op="cp -a $LPATH $STAGING/"
if [ -z "${DRYRUN+x}" ]; then
eval "$_op" || exit 1
else
dryrun $LINENO: "$_op"
fi
;;
esac
assert '[ ! -z ${project+x} ]' $LINENO
IMAGE=ros-docker-$project
IMAGE_TAG=0.1
echo "Docker building image \"$IMAGE:$IMAGE_TAG\"..."
local _op="docker build $BASEDIR -t $IMAGE:$IMAGE_TAG"
if [ -z ${DRYRUN+x} ]; then
if [ ! -z ${FORCE_REBUILD} ] || [[ "$(docker images -q $IMAGE:$IMAGE_TAG 2>/dev/null)" == "" ]]; then
if [[ ! "$(docker images -q ros:noetic-ros-core 2> /dev/null)" == "" ]]; then
# TODO: HARCODED for noetic
notify $LINENO: "Image ros:noetic-ros-core already exists. Trying to pull latest.."
docker pull ros:noetic-ros-core
fi
eval "$_op" || exit 1
else
notify $LINENO: "Image $IMAGE:$IMAGE_TAG already exists. Continuing.."
fi
else
dryrun $LINENO: "$_op"
fi
} # End of build()
#
## Run the Docker image
#
run() {
local catkin_ws=/opt/catkin_ws
case ${TARGET} in
PACKAGE)
RPATH="$catkin_ws/src/$(echo $LPATH | rev | cut -f1 -d/ | rev)"
;;
WORKSPACE)
RPATH="$catkin_ws"
;;
esac
assert '[ ! -z ${LPATH+x} ]' $LINENO
assert '[ ! -z ${RPATH+x} ]' $LINENO
assert '[ ! -z ${IMAGE+x} ]' $LINENO
assert '[ ! -z ${IMAGE_TAG+x} ]' $LINENO
assert '[ ! -z ${CMD+x} ]' $LINENO "No command specified -> Try appending 'bash' as argument."
assert '[ ! -z ${ROSDEP+x} ]' $LINENO
echo "Docker shadowing image \"$IMAGE:$IMAGE_TAG\"..."
#if [[ $- == *i* ]]; then
if [ -t 0 ]; then
# running interactively
local _op="docker run -it --rm --init -v "$LPATH:$RPATH" $IMAGE:$IMAGE_TAG $ROSDEP sh -c $CMD"
else
# running headless
Related JIRA tickets or pull requests.
local _op="docker run --rm --init -v "$LPATH:$RPATH" $IMAGE:$IMAGE_TAG $ROSDEP sh -c $CMD"
fi
if [ -z ${DRYRUN+x} ]; then
eval "$_op" || exit
else
dryrun "$_op"
fi
} # End of run()
read_input "$@" || exit 1
build || exit 1
run || exit 1