-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdate_base.sh
More file actions
executable file
·162 lines (141 loc) · 4.54 KB
/
update_base.sh
File metadata and controls
executable file
·162 lines (141 loc) · 4.54 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
#! /bin/bash
set -eu
set -o pipefail
usage() {
echo "Usage:"
echo "./update_base.sh <BASE_IMAGENAME> <BASE_CONTAINERFILE> <BASE_CONTAINERNAME> [<extra_args>]"
}
# check that we have exactly 3 params
if [ $# -lt 3 ]; then
usage
exit 1
fi
DEVC_REPO_DIR=/home/shank/code/misc/dev-container
# params - base
BASE_IMAGENAME=$1
BASE_CONTAINERFILE=$2
BASE_CONTAINERNAME=$3
echo "BASE_IMAGENAME:${BASE_IMAGENAME}"
echo "BASE_CONTAINERFILE:${BASE_CONTAINERFILE}"
echo "BASE_CONTAINERNAME:${BASE_CONTAINERNAME}"
# default mount volumes
DEFAULT_MOUNTS=(
"--volume /tmp/.X11-unix:/tmp/.X11-unix"
"--volume $HOME:/host_home"
"--volume $HOME/code:/home/shank/code"
"--volume $HOME/.ssh:/home/shank/.ssh"
"--volume $SSH_AUTH_SOCK:/tmp/ssh-agent.socket"
# "--volume $XDG_RUNTIME_DIR/podman/podman.sock:/run/podman/podman.sock"
)
if [ -d $HOME/.rustup ]; then
DEFAULT_MOUNTS+=("--volume $HOME/.rustup:/home/shank/.rustup")
elif [ -d /evaldisk/shank/.rustup ]; then
DEFAULT_MOUNTS+=("--volume /evaldisk/shank/.rustup:/home/shank/.rustup")
fi
if [ -d $HOME/.cargo ]; then
DEFAULT_MOUNTS+=("--volume $HOME/.cargo:/home/shank/.cargo")
elif [ -d /evaldisk/shank/.cargo ]; then
DEFAULT_MOUNTS+=("--volume /evaldisk/shank/.cargo:/home/shank/.cargo")
fi
# if /workdisk exists, then add it to the list of mounts as well
if [ -d /workdisk ]; then
DEFAULT_MOUNTS+=("--volume /workdisk:/workdisk")
fi
# if /evaldisk exists, then add it to the list of mounts as well
if [ -d /evaldisk ]; then
DEFAULT_MOUNTS+=("--volume /evaldisk:/evaldisk")
fi
# if both /home/common and /evaldisk/shank exist, then as user which to mount at /home/common
if [ -d /home/common ] && [ -d /evaldisk/shank ]; then
echo "Mount which to /home/common? (enter 1/2/3)"
echo "1. /home/common"
echo "2. /evaldisk/shank"
echo "3. None"
read selection
# ensure that the user enters a valid selection
if [[ $selection != "1" && $selection != "2" && $selection != "3" ]]; then
echo "Invalid selection"
exit 1
fi
if [[ $selection == "1" ]]; then
DEFAULT_MOUNTS+=("--volume /home/common:/home/common")
elif [[ $selection == "2" ]]; then
DEFAULT_MOUNTS+=("--volume /evaldisk/shank:/home/common")
fi
elif [ -d /home/common ]; then
# if /home/common exists, then add it to the list of mounts as well
read -p "Mount /home/common to /home/common? (y/n) : " mount_home_common
if [[ $mount_home_common == "y" || $mount_home_common == "Y" ]]; then
DEFAULT_MOUNTS+=("--volume /home/common:/home/common")
fi
elif [ -d /evaldisk/shank ]; then
# if /evaldisk exists, then add /evaldisk/shank to the list of mounts as well
read -p "Mount /evaldisk/shank to /home/common? (y/n) : " mount_evaldisk_shank_to_common
if [[ $mount_evaldisk_shank_to_common == "y" || $mount_evaldisk_shank_to_common == "Y" ]]; then
DEFAULT_MOUNTS+=("--volume /evaldisk/shank:/home/common")
fi
fi
# default published ports
DEFAULT_PORTS=(
# "--publish 3306:3306"
)
# stop running devc container
echo ">> [0] stopping (and removing) existing container if any..."
! docker container stop $BASE_CONTAINERNAME
! docker container rm --force $BASE_CONTAINERNAME
# update base image
echo ">> [1] updating base image..."
# Separate build args from run args
BUILD_ARGS=()
RUN_ARGS=()
i=4 # Start from 4th argument (after the 3 required params)
while [ $i -le $# ]; do
arg="${!i}"
case "$arg" in
-v | --volume | --mount)
# Volume args go to run command (this arg + next arg)
RUN_ARGS+=("$arg")
i=$((i + 1))
if [ $i -le $# ]; then
RUN_ARGS+=("${!i}")
fi
;;
-v=* | --volume=* | --mount=*)
# Volume args with = go to run command
RUN_ARGS+=("$arg")
;;
*)
# Everything else goes to build
BUILD_ARGS+=("$arg")
;;
esac
i=$((i + 1))
done
docker image build \
"${BUILD_ARGS[@]}" \
--build-arg UID=$(id -u) \
--build-arg GID=$(id -g) \
--tag ${BASE_IMAGENAME} \
--file "$DEVC_REPO_DIR/$BASE_CONTAINERFILE" .
# build container
echo ">> [2] creating and running the proj container..."
docker container run \
-it \
--label="no-prune" \
--name ${BASE_CONTAINERNAME} \
--hostname "${BASE_CONTAINERNAME}-$(cat /etc/hostname)" \
--security-opt label=disable \
--security-opt seccomp=unconfined \
--cap-add=SYS_PTRACE \
--cap-add=NET_RAW \
--cap-add=MKNOD \
--cap-add=AUDIT_WRITE \
--env DISPLAY=${DISPLAY:-:0} \
${DEFAULT_MOUNTS[@]} \
${DEFAULT_PORTS[@]} \
"${RUN_ARGS[@]}" \
${BASE_IMAGENAME}
# --userns=keep-id \
# --env CONTAINER_HOST=unix:///run/podman/podman.sock \
# --env DISPLAY=$DISPLAY \
# --userns=keep-id \