-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathrun_docker.sh
executable file
·209 lines (175 loc) · 5.95 KB
/
run_docker.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
#!/usr/bin/env bash
# Get this script's path
pushd `dirname $0` > /dev/null
SCRIPTPATH=`pwd`
popd > /dev/null
set -e
source $SCRIPTPATH/support/fun.cfg
# Help/Usage text with all options
USAGE="Usage: run_docker [OPTIONS...]
\n
Help Options:
\n
-h, --help Show help options
\n
Application Options:
\n
-r, --robot Robot model [spot|go1], example: -r spot
\n
-d, --device Input device type [ps3|xbox|twist|keyboard], example: -d ps3
\n
-w, --world World name [empty|ruins|pyramid|ramps|stairs|office], example: -w ruins
\n
-g, --gui Launch rviz
\n
-n, --net Launch docker with shared network, useful to visualize the ROS topics on the host machine
\n
-l, --local Run a local ROS workspace inside the container [workspace], example: -l ros_ws
\n
-i, --image Specify the Docker image name [wolf-base|wolf-app], example: -i wolf-app
\n
-t, --tag Specify the Docker image tag [focal|jammy], example: -t focal
"
# Default
ROBOT_NAME=
ROBOT_MODEL=spot
DEVICE=keyboard
WORLD_NAME=empty
GUI=false
RUN_LOCAL_WS=false
DOCKER_NET=bridge
ROS_WS=
IMAGE_NAME="wolf-app"
IMAGE_TAG="focal"
CONTAINER_NAME="wolf-container"
DOCKER_REGISTRY=serger87
function run_local_ros_workspace()
{
docker run --user root:root --hostname $HOSTNAME --ipc=host --net=$DOCKER_NET --device=/dev/dri:/dev/dri --privileged -e "QT_X11_NO_MITSHM=1" -e GAZEBO_MODEL_PATH=$GAZEBO_MODEL_PATH:/opt/ros/$2/share/wolf_gazebo_resources/models/ -e SHELL -e DISPLAY -e DOCKER=1 --name $CONTAINER_NAME \
--gpus all \
--device=/dev/ttyUSB0 \
--workdir="/home/$USER" \
--volume="/tmp/.X11-unix:/tmp/.X11-unix:rw" \
--volume="$HOME/$1/src:$HOME/$1/src" \
--volume="/etc/group:/etc/group:rw" \
--volume="/etc/passwd:/etc/passwd:rw" \
--volume="/etc/shadow:/etc/shadow:rw" \
--volume="/etc/sudoers:/etc/sudoers:rw" \
--volume="/etc/sudoers.d:/etc/sudoers.d:rw" \
--volume="$HOME/.ros:$HOME/.ros" \
--volume="$HOME/.gazebo:$HOME/.gazebo" \
--volume="$HOME/.ignition:$HOME/.ignition" \
--volume="$HOME/.rviz:$HOME/.rviz" \
-it $FULL_IMAGE_NAME $SHELL -c "eval export HOME=$HOME; cd $HOME; export XBOT_ROOT=$HOME/$1/install.sh; source /opt/ros/$2/setup.bash; bash"
}
function run_docker_ros_workspace()
{
docker run --user root:root --hostname $HOSTNAME --ipc=host --net=$DOCKER_NET --device=/dev/dri:/dev/dri --privileged -e "QT_X11_NO_MITSHM=1" -e GAZEBO_MODEL_PATH=$GAZEBO_MODEL_PATH:/opt/ros/$1/share/wolf_gazebo_resources/models/ -e SHELL -e DISPLAY -e DOCKER=1 --name $CONTAINER_NAME \
--gpus all \
--device=/dev/ttyUSB0 \
--volume="/tmp/.X11-unix:/tmp/.X11-unix:rw" \
-it $FULL_IMAGE_NAME $SHELL -c "export XBOT_ROOT=/opt/ros/$1; source /opt/ros/$1/setup.bash; $CMD robot_model:=$ROBOT_MODEL world_name:=$WORLD_NAME full_gui:=$GUI input_device:=$DEVICE"
}
wolf_banner
if [[ ( $1 == "--help") || $1 == "-h" ]]
then
echo -e $USAGE
exit 0
fi
while [ -n "$1" ]; do # while loop starts
case "$1" in
-r|--robot)
ROBOT_MODEL="$2"
shift
;;
-d|--device)
DEVICE="$2"
shift
;;
-w|--world)
WORLD_NAME="$2"
shift
;;
-g|--gui)
GUI=true
;;
-n|--net)
DOCKER_NET=host
;;
-l|--local)
ROS_WS="$2"
RUN_LOCAL_WS=true
shift
;;
-i|--image)
IMAGE_NAME="$2"
shift
;;
-t|--tag)
IMAGE_TAG="$2"
shift
;;
*) print_warn "Option $1 not recognized!"
echo -e $USAGE
exit 0;;
esac
shift
done
# Validating options
valid_robots=("spot" "go1")
valid_devices=("ps3" "xbox" "twist" "keyboard")
valid_worlds=("empty" "ruins" "pyramid" "ramps" "stairs" "office")
valid_tags=("focal" "jammy")
if [[ ! " ${valid_robots[*]} " =~ " $ROBOT_MODEL " ]]; then
print_warn "Invalid robot model!"
echo -e "$USAGE"
exit 1
fi
if [[ ! " ${valid_devices[*]} " =~ " $DEVICE " ]]; then
print_warn "Invalid input device!"
echo -e "$USAGE"
exit 1
fi
if [[ ! " ${valid_worlds[*]} " =~ " $WORLD_NAME " ]]; then
print_warn "Invalid world name!"
echo -e "$USAGE"
exit 1
fi
if [[ ! " ${valid_tags[*]} " =~ " $IMAGE_TAG " ]]; then
print_warn "Invalid image tag!"
echo -e "$USAGE"
exit 1
elif [[ $IMAGE_TAG == "focal" ]]; then
ROS=noetic
CMD='roslaunch wolf_controller wolf_controller_bringup.launch'
elif [[ $IMAGE_TAG == "jammy" ]]; then
ROS=humble
CMD='ros2 launch wolf_controller wolf_controller_bringup.launch.xml'
fi
# Define the full Docker image name
FULL_IMAGE_NAME="$IMAGE_NAME:$IMAGE_TAG"
# Ensure Docker is running
if ! sudo systemctl is-active --quiet docker; then
echo "Docker inactive. Starting docker..."
sudo systemctl restart docker
fi
# Pull Docker image if not present
if ! docker image inspect "$FULL_IMAGE_NAME" > /dev/null 2>&1; then
print_warn "Image $FULL_IMAGE_NAME not found. Pulling from registry..."
docker pull "$DOCKER_REGISTRY/$FULL_IMAGE_NAME"
docker tag "$DOCKER_REGISTRY/$FULL_IMAGE_NAME" "$FULL_IMAGE_NAME"
fi
# Cleanup existing Docker container if necessary
docker rm -f "$CONTAINER_NAME" > /dev/null 2>&1 || true
# xhost settings to allow Docker GUI access
xhost +local:docker
# Run the container with shared X11
# Opt1 run the code within the docker container by sourcing the local ROS workspace (useful for development)
# Opt2 run the code within the docker container by sourcing the ROS workspace INSIDE docker (useful as a demo)
if $RUN_LOCAL_WS;
then
print_info "Selected ros workspace: $ROS_WS"
run_local_ros_workspace $ROS_WS $ROS
else
run_docker_ros_workspace $ROS
fi