-
Notifications
You must be signed in to change notification settings - Fork 238
/
.industrial_training_ci.bash
272 lines (227 loc) · 7.82 KB
/
.industrial_training_ci.bash
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
267
268
269
270
271
272
#!/bin/bash
#********************************************************************
# Software License Agreement (BSD License)
#
# Copyright (c) 2016, University of Colorado, Boulder
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the following
# disclaimer in the documentation and/or other materials provided
# with the distribution.
# * Neither the name of the Univ of CO, Boulder nor the names of its
# contributors may be used to endorse or promote products derived
# from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#********************************************************************/
# Author: Dave Coleman <[email protected]>, Robert Haschke, Levi Armstrong
# Desc: Utility functions used to make CI work better
#######################################
RED='\e[1;91m'
GREEN='\e[1;92m'
YELLOW='\e[1;93m'
BLUE='\e[1;94m'
MAGENTA='\e[1;95m'
CYAN='\e[1;96m'
NC='\e[0m' # No Color
function logError {
printf "${RED}$1${NC}\n"
}
function logWarn {
printf "${YELLOW}$1${NC}\n"
}
function logInfo {
printf "${NC}$1${NC}"
}
function logHighlightGreen {
printf "${GREEN}$1${NC}\n"
}
function logHighlightBlue {
printf "${BLUE}$1${NC}\n"
}
function logHighlightMagenta {
printf "${MAGENTA}$1${NC}\n"
}
function logHighlightCyan {
printf "${CYAN}$1${NC}\n"
}
#######################################
# Start a Command with timer
#
# Arguments:
# info: info of action being ran
#######################################
function ci_time_start {
CI_START_TIME=$SECONDS
local COMMAND=$@
# Output command being executed
logHighlightGreen "$ ${COMMAND}"
}
#######################################
# Wraps up the timer section on CI (that's started mostly by ci_time_start function).
#######################################
function ci_time_end {
if [ -z $CI_START_TIME ]; then
printf '[ci_time_end] var CI_START_TIME is not set. You need to call `ci_time_start` in advance.';
return;
fi
local CI_ELAPSED_SECONDS=$(( $SECONDS - $CI_START_TIME ))
if (( $CI_ELAPSED_SECONDS > 3600 )) ; then
let "hours=CI_ELAPSED_SECONDS/3600"
let "minutes=(CI_ELAPSED_SECONDS%3600)/60"
let "seconds=(CI_ELAPSED_SECONDS%3600)%60"
logHighlightCyan "Completed in $hours hour(s), $minutes minute(s) and $seconds second(s)"
elif (( $CI_ELAPSED_SECONDS > 60 )) ; then
let "minutes=(CI_ELAPSED_SECONDS%3600)/60"
let "seconds=(CI_ELAPSED_SECONDS%3600)%60"
logHighlightCyan "Completed in $minutes minute(s) and $seconds second(s)"
else
logHighlightCyan "Completed in $CI_ELAPSED_SECONDS seconds"
fi
unset CI_START_TIME
}
#######################################
# Display command in CI console with start time, end time and duration
#
# Arguments:
# command: action to run
#######################################
function ci_run_impl() {
local command=$@
ci_time_start $command
# actually run command
eval "${command}"
result=$?
ci_time_end
return $result
}
#######################################
# Same as ci_run_impl() but silent command
#
# Arguments:
# command: action to run
#######################################
function ci_run_silent_impl() {
local command=$@
ci_time_start "${command} > /dev/null"
# actually run command
eval "${command} > /dev/null"
result=$?
ci_time_end
return $result
}
#######################################
# Run a command and do timing for it
# Return the exit status of the command
#######################################
function ci_run() {
ci_run_impl $@ || exit $?
}
#######################################
# Same as ci_run but return 0 exit status, thus ignoring any error
#######################################
function ci_run_true() {
ci_run_impl $@ || return 0
}
#######################################
# Same as ci_run but silent output
#######################################
function ci_run_silent() {
ci_run_silent_impl $@ || exit $?
}
function setup()
{
ci_run_silent apt update -y
ci_run_silent apt install python-catkin-tools -y
ci_run_silent apt install ros-$ROS_DISTRO-moveit -y
ci_run_silent apt install ros-$ROS_DISTRO-pcl-ros -y
ci_run_silent apt upgrade -y
}
######################################################
## Build a catkin workspace with rosdep and wstool ##
######################################################
function build_ws()
{
ci_run source /opt/ros/$ROS_DISTRO/setup.bash
ci_run cd $HOME
# Make a new catkin ws
ci_run mkdir catkin_ws
ci_run cd catkin_ws
# Copy the exercise workspace into the home directory
ci_run cp -r $TRAVIS_BUILD_DIR/$1 .
# Initialize the workspace
ci_run catkin init
# Perform rosinstall
ci_run wstool init
if [ -f src/.rosinstall ]; then
ci_run wstool merge -t src/ src/.rosinstall
ci_run wstool update -t src
fi
# Install dependencies with rosdep
ci_run_silent rosdep -q install --from-paths src --ignore-src -r -y
# Build the workspace
ci_run catkin build --no-status --summarize
}
################################################################################
## Update the container image to save time later when installing dependencies ##
################################################################################
function update_image()
{
# Run the docker container
ci_run docker run --name $CONTAINER_NAME --network=host -d -i \
-v $TRAVIS_BUILD_DIR:$TRAVIS_BUILD_DIR \
-e TRAVIS_BUILD_DIR=$TRAVIS_BUILD_DIR \
$CONTAINER_IMAGE
# Source this file in the docker container and run the setup routine
ci_run docker exec -t $CONTAINER_NAME \
bash -c \"source $TRAVIS_BUILD_DIR/.industrial_training_ci.bash\; ci_run setup\"
# Create a new name for the updated image
ci_run export NEW_CONTAINER_IMAGE=$CONTAINER_IMAGE\_update
# Commit the changes to the docker and close the container
ci_run docker commit -m \"docker setup\" $CONTAINER_NAME $NEW_CONTAINER_IMAGE
ci_run docker stop $CONTAINER_NAME
}
#########################
## Build the exercises ##
#########################
function build_exercises()
{
# Create a counter for naming containers
N=1
for EXERCISE_SRC in "$@"
do
# Update the container name
NEW_CONTAINER_NAME=$CONTAINER_NAME\_$N
# Run the updated docker image in a new container
ci_run docker run --name $NEW_CONTAINER_NAME --network=host -d -i \
-v $TRAVIS_BUILD_DIR:$TRAVIS_BUILD_DIR \
-e TRAVIS_BUILD_DIR=$TRAVIS_BUILD_DIR \
$NEW_CONTAINER_IMAGE
ci_run docker exec -t $NEW_CONTAINER_NAME \
bash -c \"source $TRAVIS_BUILD_DIR/.industrial_training_ci.bash\; ci_run build_ws $EXERCISE_SRC\"
# Close the container
ci_run docker stop $NEW_CONTAINER_NAME
# Increment the counter
N=$((N+1))
logHighlightGreen "Successfully built $EXERCISE_SRC\n"
done
logHighlightGreen "Successfully built all exercises\n"
}