-
Notifications
You must be signed in to change notification settings - Fork 1
/
watod
executable file
·187 lines (167 loc) · 7.13 KB
/
watod
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
#!/bin/bash
set -e
programname="$(basename "$(test -L "$0" && readlink "$0" || echo "$0")")"
MONO_DIR="$(dirname "$(realpath "$0")")"
PROFILES_DIR="$MONO_DIR/profiles"
ANSIBLE_TMP_DIR=/tmp/ansible-tmp-$USER
function usage {
echo "Usage: $programname [OPTIONS]... [COMMAND]..."
echo "Executes docker-compose COMMAND in the the monorepo with appropriate environment variables."
echo " launch_autonomous launch the can_interface for autonomous mode"
echo " -v --verbose verbose mode. Currently, prints .env variables"
echo " -lp --local-ports displays ports exposed by applications and shows the"
echo " ssh local port forwarding commands for each VNC Server found"
echo " if a docker-compose command is specified, it will be run in the background."
echo " -t --terminal [service_name] open a bash terminal into the desired service (eg: perception)."
echo " -a --all run COMMAND with all profiles enabled. Profiles are defined in ./profiles/."
echo " -np don't run fix-permissions.sh, makes permissions consistent across all systems"
echo " so that docker caching doesn't break."
echo ""
echo "Examples:"
echo " watod up start containers (docker-compose up)"
echo " watod down stop and remove containers (docker-compose down)"
echo " watod ps [--services] list containers in the current project and show their current status (docker-compose ps)"
echo " watod --all pull pull all new containers from the GitLab container registry (docker-compose pull)"
echo " watod --all build build all new containers using Dockerfiles in docker/ (docker-compose build)"
echo " watod -t perception open a bash terminal into the perception container."
echo ""
echo " More Info on Docker Compose: https://docs.docker.com/compose/reference/overview/"
exit 0
}
function run_compose {
cd $MONO_DIR
if [ ! -z "$(source $PROFILES_DIR/.env && echo $ACTIVE_PROFILES)" ] && [ -z "$PROFILES" ]; then
PROFILES="$(source $PROFILES_DIR/.env && printf -- "-f profiles/docker-compose.%s.yaml " ${ACTIVE_PROFILES[@]})"
fi
DOCKER_BUILDKIT=${DOCKER_BUILDKIT:-1} docker-compose ${PROFILES[@]} "$@"
}
# in case you need help
if [ "$1" == 'help' ]; then
usage
fi
# in case you have nothing to say
if [ $# == 0 ]; then
usage
fi
# generate .env file from scripts/watod-setup-env.sh
if [ ! -z $VERBOSE ]; then # if we want to see all verbose coming from setting up env
cd $MONO_DIR && bash scripts/watod-setup-env.sh
else
cd $MONO_DIR && bash scripts/watod-setup-env.sh &> /dev/null
fi
# run options
COMPOSE_CMD=""
while [[ $# -gt 0 ]] ; do
key="$1"
case $key in
-v|--verbose)
VERBOSE=1
shift # past argument
;;
-lp|--local-ports)
PRINT_LOCAL_PORTS=1
shift
;;
-t|--terminal)
START_TERMINAL=1
shift
SERVICE_NAME=$1
if [ -z "$SERVICE_NAME" ]; then
echo "Expected watod -t SERVICE_NAME"
usage
fi
shift
;;
-a|--all)
PROFILE_BLACKLIST="$(source $PROFILES_DIR/.env && echo $PROFILE_BLACKLIST)"
# Find all the profiles
PROFILES=$(ls $PROFILES_DIR)
# Remove profiles in the blacklist
for TO_REMOVE in ${PROFILE_BLACKLIST[@]}; do
PROFILES="${PROFILES//"docker-compose.$TO_REMOVE.yaml"/}"
done
# format properly for docker-compose
PROFILES=$(printf -- "-f profiles/%s " $PROFILES)
shift
;;
-np)
NO_FIX_PERMISSIONS=1
shift
;;
launch_autonomous)
LAUNCH_AUTONOMOUS=1
shift
;;
-h|--help) # in case you got this far, but still need help :)
usage
;;
*) # unknown option
break
;;
esac
done
if [[ $# -gt 0 ]]; then
COMPOSE_CMD="${COMPOSE_CMD} $@"
fi
# launch autonomy on the car
if [ ! -z $LAUNCH_AUTONOMOUS ]; then
echo "============================================================ "
echo "WARNING: You are about to enter autonomous mode. "
echo ""
echo "Make sure that you are running the autonomous pipeline and have "
echo "verified the output of the /feedback_desired_output rostopic. "
echo "Turn on the car and put car in drive with the brake off. "
echo "Press <CTRL-C> twice to exit autonomous mode "
echo ""
read -p "Press <ENTER> to continue"
# Run actual CAN devices, not VCAN
echo "CAN_DEBUG=False" >> "$PROFILES_DIR/.env"
PROFILES="-f profiles/docker-compose.can_interface.yaml"
run_compose up can_interface
exit 0
fi
if [ ! -z $PRINT_LOCAL_PORTS ]; then
# get list of services to traverse
SERVICE_LIST="$(run_compose ps --services | sort)"
# fixes hostnames for VMs so that by default $HOSTNAME is the first element
# in the array, which often is the domain name that can be accessed
# externally
read -ra HOSTNAME <<< $(hostname -A)
echo "========================================================================="
echo "Ports exposed by running containers:"
echo "========================================================================="
LOCAL_FORWARD_ALL=""
PORTS_STRING=""
for SERVICE in $SERVICE_LIST; do
ENV_VARS="$(run_compose exec "$SERVICE" env || true)"
VNC_PORT=$(echo "$ENV_VARS" | grep ^VNC_PORT= | cut -d '=' -f2)
VNC_PORT=${VNC_PORT:0:5} # Strip unncessary characters
if [ ! -z $VNC_PORT ]; then
echo "$SERVICE service exposes a VNC Server at $HOSTNAME:$VNC_PORT"
LOCAL_FORWARD="-L ${VNC_PORT}:localhost:${VNC_PORT}"
LOCAL_FORWARD_ALL="${LOCAL_FORWARD_ALL} $LOCAL_FORWARD"
PORTS_STRING="${PORTS_STRING},${VNC_PORT}:${SERVICE}_VNC"
echo " To forward it locally, run"
echo " ssh$LOCAL_FORWARD $USER@$HOSTNAME"
echo " on your local machine attach to VNC at localhost:${VNC_PORT}"
echo
fi
done
echo "========================================================================="
echo "To forward all ports locally:"
echo "ssh $LOCAL_FORWARD_ALL $USER@$HOSTNAME"
echo "========================================================================="
fi
if [ ! -z "$COMPOSE_CMD" ]; then
ADDITIONAL_ARGS=""
# If we are starting a terminal, run docker-compose up with the -d argument
if [ ! -z "$START_TERMINAL" ] && [[ ! " ${COMPOSE_CMD[@]} " =~ " -d " ]] && [[ " ${COMPOSE_CMD[@]} " =~ " up " ]]; then
ADDITIONAL_ARGS="-d"
fi
echo "Running docker-compose$COMPOSE_CMD $ADDITIONAL_ARGS:"
run_compose ${COMPOSE_CMD[@]} ${ADDITIONAL_ARGS}
fi
if [ ! -z "$START_TERMINAL" ]; then
echo "Starting bash shell in service $SERVICE_NAME":
run_compose exec $SERVICE_NAME /bin/bash
fi