-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconfigure_devcontainer.sh
More file actions
executable file
·487 lines (451 loc) · 13.6 KB
/
Copy pathconfigure_devcontainer.sh
File metadata and controls
executable file
·487 lines (451 loc) · 13.6 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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
#!/usr/bin/env bash
# Created by Pietro Califano and GPT-5.2 Codex, Dec 2025
set -euo pipefail
# Get paths to files
ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
DEVCONTAINER_DIR="${ROOT_DIR}/.devcontainer"
DEVCONTAINER_JSON="${DEVCONTAINER_DIR}/devcontainer.json"
DOCKERFILE="${DEVCONTAINER_DIR}/Dockerfile"
DEVCONTAINER_JSON_WRITER="${DEVCONTAINER_DIR}/update_devcontainer_json.py"
# Define supported options
BASE_OPTIONS=("ubuntu-24.04" "ubuntu-22.04" "ubuntu-20.04" "ubuntu-18.04" "debian-12" "debian-11" "custom")
ROS1_DISTROS=("noetic" "melodic")
ROS2_DISTROS=("humble" "iron" "jazzy" "rolling")
ROS_PROFILES=("ros-base" "desktop" "desktop-full")
ROS2_PROFILES=("ros-base" "desktop")
if [[ ! -f "$DOCKERFILE" ]]; then
echo "Dockerfile not found at: $DOCKERFILE"
exit 1
fi
if [[ ! -f "$DEVCONTAINER_JSON_WRITER" ]]; then
echo "Devcontainer JSON writer not found at: $DEVCONTAINER_JSON_WRITER"
exit 1
fi
usage() {
cat <<'EOF'
Usage: ./configure_devcontainer.sh [options]
Options:
--cuda Enable CUDA support.
--base <name> Base image tag (ubuntu-24.04, ubuntu-22.04, ubuntu-20.04, ubuntu-18.04, debian-12, debian-11, custom).
--base-image <img> Full base image name (overrides --base).
--ros <distro> Install ROS 1 with selected distro (noetic, melodic).
--ros2 <distro> Install ROS 2 with selected distro (humble, iron, jazzy, rolling).
--ros-profile <p> ROS package profile (ros-base, desktop, desktop-full).
--non-interactive Fail if required options are missing instead of prompting.
-h, --help Show this help.
Examples:
./configure_devcontainer.sh --cuda --base ubuntu-24.04 --ros2 jazzy
./configure_devcontainer.sh --no-cuda --base debian-12
./configure_devcontainer.sh --base-image ubuntu:20.04 --ros noetic
Note:
CUDA is off by default (use --cuda to enable).
ROS 1 supports Ubuntu 18.04/20.04; ROS 2 supports Ubuntu 22.04+.
EOF
}
# Auxiliary functions
contains_value() {
local value="$1"
shift
local item
for item in "$@"; do
if [[ "$item" == "$value" ]]; then
return 0
fi
done
return 1
}
file_contains() {
local pattern="$1"
local file="$2"
if command -v rg >/dev/null 2>&1; then
rg -q "$pattern" "$file"
else
grep -q "$pattern" "$file"
fi
}
prompt_bool() {
local prompt="$1"
local default="$2"
local answer
while true; do
read -r -p "${prompt} [y/n] (default: ${default}) " answer
if [[ -z "$answer" ]]; then
answer="$default"
fi
case "$answer" in
y|Y) echo "on"; return 0 ;;
n|N) echo "off"; return 0 ;;
*) echo "Please enter y or n." ;;
esac
done
}
prompt_select() {
local prompt="$1"
local default="$2"
shift 2
local -a options=("$@")
local i=1
echo "$prompt" >&2
for opt in "${options[@]}"; do
if [[ "$opt" == "$default" ]]; then
printf " %d) %s (default)\n" "$i" "$opt" >&2
else
printf " %d) %s\n" "$i" "$opt" >&2
fi
((i++))
done
while true; do
read -r -p "> " choice
if [[ -z "$choice" ]]; then
echo "$default"
return 0
fi
if [[ "$choice" =~ ^[0-9]+$ ]] && (( choice>=1 && choice<=${#options[@]} )); then
echo "${options[choice-1]}"
return 0
fi
for opt in "${options[@]}"; do
if [[ "$choice" == "$opt" ]]; then
echo "$opt"
return 0
fi
done
echo "Invalid selection." >&2
done
}
prompt_text() {
local prompt="$1"
local default="$2"
local value
while true; do
read -r -p "${prompt} (default: ${default}) " value
if [[ -z "$value" ]]; then
value="$default"
fi
if [[ -n "$value" ]]; then
echo "$value"
return 0
fi
echo "Value cannot be empty."
done
}
detect_ubuntu_version() {
local value="$1"
case "$value" in
*ubuntu-24.04*|*ubuntu:24.04*) echo "24.04" ;;
*ubuntu-22.04*|*ubuntu:22.04*) echo "22.04" ;;
*ubuntu-20.04*|*ubuntu:20.04*) echo "20.04" ;;
*ubuntu-18.04*|*ubuntu:18.04*) echo "18.04" ;;
*) echo "" ;;
esac
}
#######################################
# Main script
#######################################
# Get current settings from file
current_base="ubuntu-24.04"
current_from=""
if [[ -f "$DOCKERFILE" ]]; then
current_from="$(sed -n 's/^FROM[[:space:]]\+\([^[:space:]]\+\).*/\1/p' "$DOCKERFILE" | head -n1)"
detected_base="$(sed -n 's/^FROM .*devcontainers\/cpp:1-\([a-z0-9.\-]*\).*/\1/p' "$DOCKERFILE" | head -n1)"
if [[ -n "$detected_base" ]]; then
current_base="$detected_base"
fi
fi
cuda_default="off"
if [[ -f "$DEVCONTAINER_JSON" ]] && file_contains "nvidia-cuda" "$DEVCONTAINER_JSON"; then
cuda_default="on"
fi
ros_mode_default="none"
ros_distro_default=""
ros_profile_default="ros-base"
if [[ -f "$DEVCONTAINER_JSON" ]]; then
ros_mode_detected="$(sed -n 's/.*"ROS_MODE"[[:space:]]*:[[:space:]]*"\([^"]*\)".*/\1/p' "$DEVCONTAINER_JSON" | head -n1)"
ros_distro_detected="$(sed -n 's/.*"ROS_DISTRO"[[:space:]]*:[[:space:]]*"\([^"]*\)".*/\1/p' "$DEVCONTAINER_JSON" | head -n1)"
ros_profile_detected="$(sed -n 's/.*"ROS_PROFILE"[[:space:]]*:[[:space:]]*"\([^"]*\)".*/\1/p' "$DEVCONTAINER_JSON" | head -n1)"
if [[ -z "$ros_profile_detected" ]]; then
ros_profile_detected="$(sed -n 's/.*"ROS_INSTALL_TYPE"[[:space:]]*:[[:space:]]*"\([^"]*\)".*/\1/p' "$DEVCONTAINER_JSON" | head -n1)"
fi
if [[ -n "$ros_mode_detected" ]]; then
ros_mode_default="$ros_mode_detected"
fi
if [[ -n "$ros_distro_detected" ]]; then
ros_distro_default="$ros_distro_detected"
fi
if [[ -n "$ros_profile_detected" ]]; then
ros_profile_default="$ros_profile_detected"
fi
fi
# Set default values from current configuration
CUDA="$cuda_default"
BASE="$current_base"
BASE_IMAGE=""
ROS_MODE="$ros_mode_default"
ROS_DISTRO="$ros_distro_default"
ROS_PROFILE="$ros_profile_default"
NON_INTERACTIVE="no"
CUDA_SET="no"
BASE_SET="no"
BASE_IMAGE_SET="no"
ROS_MODE_SET="no"
ROS_DISTRO_SET="no"
ROS_PROFILE_SET="no"
# Parse command-line arguments
while [[ $# -gt 0 ]]; do
case "$1" in
--cuda)
CUDA="on"
CUDA_SET="yes"
;;
--no-cuda)
CUDA="off"
CUDA_SET="yes"
;;
--base)
shift
BASE="${1:-}"
BASE_SET="yes"
;;
--base-image)
shift
BASE_IMAGE="${1:-}"
BASE_IMAGE_SET="yes"
;;
--ros)
ROS_MODE="ros"
ROS_MODE_SET="yes"
if [[ $# -ge 2 && "${2}" != -* ]]; then
shift
ROS_DISTRO="$1"
ROS_DISTRO_SET="yes"
fi
;;
--ros2)
ROS_MODE="ros2"
ROS_MODE_SET="yes"
if [[ $# -ge 2 && "${2}" != -* ]]; then
shift
ROS_DISTRO="$1"
ROS_DISTRO_SET="yes"
fi
;;
--ros-profile)
shift
ROS_PROFILE="${1:-}"
if [[ -n "$ROS_PROFILE" ]]; then
ROS_PROFILE_SET="yes"
fi
;;
--non-interactive) NON_INTERACTIVE="yes" ;;
-h|--help) usage; exit 0 ;;
*)
echo "Unknown argument: $1"
usage
exit 1
;;
esac
shift
done
# Pre-fill defaults for interactive prompts
if [[ "$NON_INTERACTIVE" != "yes" ]]; then
if [[ "$ROS_MODE" == "ros" && -z "$ROS_DISTRO" ]]; then
ROS_DISTRO="${ROS1_DISTROS[0]}"
elif [[ "$ROS_MODE" == "ros2" && -z "$ROS_DISTRO" ]]; then
ROS_DISTRO="${ROS2_DISTROS[0]}"
fi
fi
# Prompt user for options
if [[ "$NON_INTERACTIVE" != "yes" ]]; then
# Interactive mode
# CUDA
if [[ "$CUDA_SET" != "yes" ]]; then
cuda_prompt_default="n"
if [[ "$CUDA" == "on" ]]; then
cuda_prompt_default="y"
fi
CUDA="$(prompt_bool "Enable CUDA support?" "$cuda_prompt_default")"
fi
echo "Selected CUDA support: $CUDA"
# Base image
if [[ -z "$BASE_IMAGE" && "$BASE_SET" != "yes" ]]; then
if ! contains_value "$BASE" "${BASE_OPTIONS[@]}"; then
BASE="${BASE_OPTIONS[0]}"
fi
echo "Current base image: $BASE"
BASE="$(prompt_select "Select base image:" "$BASE" "${BASE_OPTIONS[@]}")"
fi
if [[ -z "$BASE_IMAGE" && "$BASE" != "custom" ]]; then
if ! contains_value "$BASE" "${BASE_OPTIONS[@]}"; then
echo "Invalid --base value: $BASE"
exit 1
fi
fi
if [[ -z "$BASE_IMAGE" && "$BASE" == "custom" ]]; then
default_custom="${current_from:-mcr.microsoft.com/devcontainers/cpp:1-ubuntu-24.04}"
BASE_IMAGE="$(prompt_text "Enter full base image" "$default_custom")"
fi
echo "Selected base image: $BASE"
# ROS / ROS 2
if [[ "$ROS_MODE_SET" != "yes" ]]; then
ROS_MODE="$(prompt_select "Select ROS option:" "$ROS_MODE" "none" "ros" "ros2")"
fi
case "$ROS_MODE" in
ros)
if ! contains_value "$ROS_DISTRO" "${ROS1_DISTROS[@]}"; then
if [[ "$ROS_DISTRO_SET" == "yes" ]]; then
echo "Invalid --ros distro: $ROS_DISTRO"
exit 1
fi
ROS_DISTRO="${ROS1_DISTROS[0]}"
fi
if [[ "$ROS_DISTRO_SET" != "yes" ]]; then
ROS_DISTRO="$(prompt_select "Select ROS 1 distro:" "$ROS_DISTRO" "${ROS1_DISTROS[@]}")"
fi
;;
ros2)
if ! contains_value "$ROS_DISTRO" "${ROS2_DISTROS[@]}"; then
if [[ "$ROS_DISTRO_SET" == "yes" ]]; then
echo "Invalid --ros2 distro: $ROS_DISTRO"
exit 1
fi
ROS_DISTRO="${ROS2_DISTROS[0]}"
fi
if [[ "$ROS_DISTRO_SET" != "yes" ]]; then
ROS_DISTRO="$(prompt_select "Select ROS 2 distro:" "$ROS_DISTRO" "${ROS2_DISTROS[@]}")"
fi
;;
none)
ROS_DISTRO=""
;;
esac
echo "Selected ROS mode: $ROS_MODE"
if [[ "$ROS_MODE" != "none" ]]; then
if [[ "$ROS_MODE" == "ros2" ]]; then
if ! contains_value "$ROS_PROFILE" "${ROS2_PROFILES[@]}"; then
if [[ "$ROS_PROFILE_SET" == "yes" ]]; then
echo "Invalid --ros-profile value for ROS 2: $ROS_PROFILE"
exit 1
fi
ROS_PROFILE="${ROS2_PROFILES[0]}"
fi
if [[ "$ROS_PROFILE_SET" != "yes" ]]; then
ROS_PROFILE="$(prompt_select "Select ROS 2 profile:" "$ROS_PROFILE" "${ROS2_PROFILES[@]}")"
fi
else
if ! contains_value "$ROS_PROFILE" "${ROS_PROFILES[@]}"; then
if [[ "$ROS_PROFILE_SET" == "yes" ]]; then
echo "Invalid --ros-profile value: $ROS_PROFILE"
exit 1
fi
ROS_PROFILE="${ROS_PROFILES[0]}"
fi
if [[ "$ROS_PROFILE_SET" != "yes" ]]; then
ROS_PROFILE="$(prompt_select "Select ROS profile:" "$ROS_PROFILE" "${ROS_PROFILES[@]}")"
fi
fi
fi
else
# Validate options in non-interactive mode
if [[ -z "$BASE_IMAGE" ]]; then
if ! contains_value "$BASE" "${BASE_OPTIONS[@]}"; then
echo "Invalid --base value: $BASE"
exit 1
fi
if [[ "$BASE" == "custom" ]]; then
echo "Use --base-image to specify a custom image in non-interactive mode."
exit 1
fi
fi
if [[ "$ROS_MODE" == "ros" ]]; then
if ! contains_value "$ROS_DISTRO" "${ROS1_DISTROS[@]}"; then
echo "Invalid --ros distro: $ROS_DISTRO"
exit 1
fi
if ! contains_value "$ROS_PROFILE" "${ROS_PROFILES[@]}"; then
echo "Invalid --ros-profile value: $ROS_PROFILE"
exit 1
fi
elif [[ "$ROS_MODE" == "ros2" ]]; then
if ! contains_value "$ROS_DISTRO" "${ROS2_DISTROS[@]}"; then
echo "Invalid --ros2 distro: $ROS_DISTRO"
exit 1
fi
if ! contains_value "$ROS_PROFILE" "${ROS2_PROFILES[@]}"; then
echo "Invalid --ros-profile value for ROS 2: $ROS_PROFILE"
exit 1
fi
elif [[ "$ROS_MODE" != "none" ]]; then
echo "Invalid ROS mode: $ROS_MODE"
exit 1
fi
fi
effective_base="$BASE"
if [[ -n "$BASE_IMAGE" ]]; then
effective_base="$BASE_IMAGE"
fi
ubuntu_version="$(detect_ubuntu_version "$effective_base")"
if [[ "$ROS_MODE" != "none" ]]; then
if [[ -n "$ubuntu_version" ]]; then
if [[ "$ROS_MODE" == "ros2" ]]; then
if [[ "$ubuntu_version" != "22.04" && "$ubuntu_version" != "24.04" ]]; then
echo "ROS 2 requires Ubuntu 22.04 or newer (detected ${ubuntu_version})."
exit 1
fi
else
if [[ "$ubuntu_version" != "18.04" && "$ubuntu_version" != "20.04" ]]; then
echo "ROS 1 requires Ubuntu 18.04 or 20.04 (detected ${ubuntu_version})."
exit 1
fi
if [[ "$ROS_DISTRO" == "noetic" && "$ubuntu_version" != "20.04" ]]; then
echo "ROS 1 noetic requires Ubuntu 20.04 (detected ${ubuntu_version})."
exit 1
fi
if [[ "$ROS_DISTRO" == "melodic" && "$ubuntu_version" != "18.04" ]]; then
echo "ROS 1 melodic requires Ubuntu 18.04 (detected ${ubuntu_version})."
exit 1
fi
fi
else
if [[ "$effective_base" == *debian* ]]; then
echo "ROS requires Ubuntu 18.04/20.04 (ROS 1) or 22.04+ (ROS 2); Debian base selected."
exit 1
fi
echo "Warning: Could not detect Ubuntu version for base image; ROS compatibility not enforced." >&2
fi
fi
# Backup existing file
timestamp="$(date +%Y%m%d%H%M%S)"
if [[ -f "$DEVCONTAINER_JSON" ]]; then
cp "$DEVCONTAINER_JSON" "${DEVCONTAINER_JSON}.bak.${timestamp}"
fi
if [[ -f "$DOCKERFILE" ]]; then
cp "$DOCKERFILE" "${DOCKERFILE}.bak.${timestamp}"
fi
# Write updated Dockerfile
tmp_dockerfile="$(mktemp)"
if [[ -n "$BASE_IMAGE" ]]; then
new_from="FROM ${BASE_IMAGE}"
else
new_from="FROM mcr.microsoft.com/devcontainers/cpp:1-${BASE}"
fi
if ! awk -v new_from="$new_from" '
BEGIN { replaced=0 }
/^FROM[[:space:]]+/ && replaced==0 {
print new_from
replaced=1
next
}
{ print }
END { if (replaced==0) exit 1 }
' "$DOCKERFILE" > "$tmp_dockerfile"; then
rm -f "$tmp_dockerfile"
echo "Failed to update Dockerfile base image."
exit 1
fi
mv "$tmp_dockerfile" "$DOCKERFILE"
# Write updated devcontainer.json using python script
CUDA="$CUDA" ROS_MODE="$ROS_MODE" ROS_DISTRO="$ROS_DISTRO" ROS_PROFILE="$ROS_PROFILE" \
python3 "$DEVCONTAINER_JSON_WRITER" > "$DEVCONTAINER_JSON"
echo "Updated:"
echo " - ${DOCKERFILE}"
echo " - ${DEVCONTAINER_JSON}"