-
Notifications
You must be signed in to change notification settings - Fork 0
/
run
87 lines (77 loc) · 2.81 KB
/
run
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
#!/bin/bash
# SPDX-FileCopyrightText: Alliander N. V.
#
# SPDX-License-Identifier: Apache-2.0
MENU_HEIGHT=$(($LINES - 3))
if (( $MENU_HEIGHT < 0 )); then
MENU_HEIGHT=20
fi
MENU_WIDTH=$(($COLUMNS - 60))
if (( $MENU_WIDTH < 0 )); then
MENU_WIDTH=50
fi
OPTION_HEIGHT=$(($MENU_HEIGHT - 10))
if (( $OPTION_HEIGHT < 0 )); then
OPTION_HEIGHT=10
fi
DOCKER_BUILDER_NAME=ram-constrained-builder
SCRIPT_DIR_PATH=$(
cd "$(dirname "${BASH_SOURCE[0]}")" || exit
pwd -P
)
# If required directories do not exist, create them
mkdir -p ~/docker_ws
mkdir -p ~/.vscode-server
# Define image options based on dockerfiles in dockerfiles directory
options=()
for dockerfile in "$SCRIPT_DIR_PATH"/dockerfiles/*.Dockerfile; do
name=${dockerfile%.*}
options+=("$(basename "$name")")
options+=("")
done
# Ask user for image option
image=$(whiptail --clear \
--title "RCDT Docker Launcher" \
--menu "Which docker image would you like to run?" \
$MENU_HEIGHT $MENU_WIDTH $OPTION_HEIGHT \
"${options[@]}" \
2>&1 >/dev/tty)
[[ -z "$image" ]] && return 1
if [ -z "$(docker images -q $image 2> /dev/null)" ]; then
use_cache=$(whiptail --clear \
--title "Build with cache?" \
--menu "Image '$image' is not found. Do you want to build it with or without cache?" \
$MENU_HEIGHT $MENU_WIDTH $OPTION_HEIGHT \
"with cache" "" \
"without cache" "" \
2>&1 >/dev/tty)
[[ -z "$use_cache" ]] && return 1
colcon_build_sequential=$(whiptail --clear \
--title "Build?" \
--menu "Image '$image' is not found. Do you want to build it parallel (higher load) or sequential (slower)?" \
$MENU_HEIGHT $MENU_WIDTH $OPTION_HEIGHT \
"parallel" "" \
"sequential" "" \
2>&1 >/dev/tty)
[[ -z "$colcon_build_sequential" ]] && return 1
ram_kb=$(grep MemTotal /proc/meminfo | awk '{print $2}')
RAM_CONSTRAINED=$([ $ram_kb -lt 16000000 ] && echo "true" || echo "")
if [ ! "$(docker buildx inspect $DOCKER_BUILDER_NAME 2> /dev/null)" ] && [ $RAM_CONSTRAINED = "true" ]; then
docker buildx create \
--name $DOCKER_BUILDER_NAME \
--driver=docker-container \
--driver-opt memory=14g \
--driver-opt default-load=true
fi
NO_CACHE=$([ "$use_cache" == "without cache" ] && echo "true" || echo "")
COLCON_BUILD_SEQUENTIAL=$([ "$colcon_build_sequential" == "sequential" ] && echo "true" || echo "")
docker build \
${NO_CACHE:+--no-cache} \
${RAM_CONSTRAINED:+--builder=$DOCKER_BUILDER_NAME} \
--build-arg COLCON_BUILD_SEQUENTIAL="$COLCON_BUILD_SEQUENTIAL" \
-t "$image" \
-f "$SCRIPT_DIR_PATH/dockerfiles/$image.Dockerfile" \
"$SCRIPT_DIR_PATH/dockerfiles"
fi
echo Running $image ...
docker compose -f $SCRIPT_DIR_PATH/.devcontainer/docker-compose.yml run --rm $image