Skip to content

Commit def3c96

Browse files
authored
Merge pull request #1306 from flatcar/krnowak/mount-in-sdk
run_sdk_container: Clean up a bit and add mounting of custom volumes
2 parents 86ea05a + 8145788 commit def3c96

File tree

2 files changed

+98
-75
lines changed

2 files changed

+98
-75
lines changed

run_sdk_container

Lines changed: 74 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -12,89 +12,93 @@ source sdk_lib/sdk_container_common.sh
1212
arch="all"
1313
name=""
1414

15-
os_version="$(get_git_version)"
16-
sdk_version="$(get_sdk_version_from_versionfile)"
15+
os_version=$(get_git_version)
16+
sdk_version=$(get_sdk_version_from_versionfile)
1717
custom_image=""
18-
tty=""
18+
tty=()
1919
remove=""
2020
cleanup=""
21+
mounts=()
2122

2223
usage() {
2324
echo " Usage:"
24-
echo " $0 [-t] [-v <version>] [-V sdk version] [-a arch] [-n <name> ] [-x <script>] [-C custom-container] [--rm] [container-command]"
25+
echo " $0 [-t] [-v <version>] [-V <SDK version>] [-a <amd64|arm64|all>] [-n <name> ] [-x <script>] [-C <custom-container>] [--rm] [-U] [-m <src>:<dest>] [<container-command>]"
2526
echo " Start an SDK container of a given SDK release version."
2627
echo " This will create the container if it does not exist, otherwise start the existing container."
2728
echo " If the container is already running then it will exec into the container."
2829
echo
29-
echo " container-command - command to be run in the container instead of"
30+
echo " <container-command> - Command to be run in the container instead of"
3031
echo " an interactive shell."
31-
echo " -t Attach docker to a TTY (docker -t)"
32+
echo " -t Attach docker to a TTY (docker -t)."
3233
echo " -v <version> - Sourcetree (OS image) version to use."
3334
echo " Defaults to '$os_version' (current git commit)."
3435
echo " FLATCAR_VERSION[_ID] in '$sdk_container_common_versionfile'"
3536
echo " will be updated accordingly."
36-
echo " -V <SDK ver> - SDK version to use. Defaults to '${sdk_version}'"
37+
echo " -V <SDK version> - SDK version to use. Defaults to '${sdk_version}'"
3738
echo " (FLATCAR_SDK_VERSION from '$sdk_container_common_versionfile')."
3839
echo " -a <amd64|arm64|all> - Target architecture (board support) of the SDK."
3940
echo " 'all' (the default) contains support for both amd64 and arm64."
4041
echo " -n <name> - Custom name to use for the container."
41-
echo " --rm Remove container afterwards"
42+
echo " --rm Remove container afterwards."
4243
echo " -x <script> - For each resource generated during build (container etc.)"
4344
echo " add a cleanup line to <script> which, when run, will free"
4445
echo " the resource. Useful for CI."
45-
echo " -C - Use an entirely custom container image instead of the SDK's"
46-
echo " $sdk_container_common_registry/flatcar-sdk-[ARCH]:[SDK VERSION]"
46+
echo " -C <custom-container> - Use an entirely custom container image instead of the SDK's"
47+
echo " $sdk_container_common_registry/flatcar-sdk-[ARCH]:[SDK VERSION]."
4748
echo " Useful for CI."
4849
echo " -U Do not update the versionfile. Instead, use the version from the versionfile as-is."
50+
echo " -m <src>:<dest> - Mount local file or directory inside the container."
51+
echo " Can be specified multiple times."
4952
echo " -h Print this help."
5053
echo
5154
}
5255
# --
5356

5457
update_versionfile=x
55-
while [ 0 -lt $# ] ; do
58+
while [[ $# -gt 0 ]] ; do
5659
case "$1" in
5760
-h) usage; exit 0;;
5861
--help) usage; exit 0;;
59-
-t) tty="-t"; shift;;
60-
-v) os_version="$2"; shift; shift;;
61-
-V) sdk_version="$2"; shift; shift;;
62-
-a) arch="$2"; shift; shift;;
63-
-n) name="$2"; shift; shift;;
64-
--rm) remove=true; shift;;
65-
-x) cleanup="$2"; shift; shift;;
66-
-C) custom_image="$2"; shift; shift;;
67-
-U) sdk_version="$(get_sdk_version_from_versionfile)"
68-
os_version="$(get_version_from_versionfile)"
62+
-t) tty=( -t ); shift;;
63+
-v) os_version=$2; shift; shift;;
64+
-V) sdk_version=$2; shift; shift;;
65+
-a) arch=$2; shift; shift;;
66+
-n) name=$2; shift; shift;;
67+
--rm) remove=x; shift;;
68+
-x) cleanup=$2; shift; shift;;
69+
-C) custom_image=$2; shift; shift;;
70+
-U) sdk_version=$(get_sdk_version_from_versionfile)
71+
os_version=$(get_version_from_versionfile)
6972
update_versionfile=
7073
shift;;
74+
-m) mounts+=( -v "$2" ); shift; shift;;
7175
*) break;;
7276
esac
7377
done
7478

75-
if [ -n "$custom_image" ] ; then
76-
container_image_name="${custom_image}"
79+
if [[ -n ${custom_image} ]] ; then
80+
container_image_name=${custom_image}
7781
else
78-
docker_sdk_vernum="$(vernum_to_docker_image_version "${sdk_version}")"
79-
container_image_name="$sdk_container_common_registry/flatcar-sdk-${arch}:${docker_sdk_vernum}"
82+
docker_sdk_vernum=$(vernum_to_docker_image_version "${sdk_version}")
83+
container_image_name="${sdk_container_common_registry}/flatcar-sdk-${arch}:${docker_sdk_vernum}"
8084
fi
8185

8286
if [[ -n ${update_versionfile} ]] ; then
8387
create_versionfile "$sdk_version" "$os_version"
8488
fi
8589

86-
if [ -z "$name" ] ; then
87-
docker_sdk_vernum="$(vernum_to_docker_image_version "${sdk_version}")"
88-
docker_os_vernum="$(vernum_to_docker_image_version "${os_version}")"
90+
if [[ -z ${name} ]] ; then
91+
docker_sdk_vernum=$(vernum_to_docker_image_version "${sdk_version}")
92+
docker_os_vernum=$(vernum_to_docker_image_version "${os_version}")
8993
name="flatcar-sdk-${arch}-${docker_sdk_vernum}_os-${docker_os_vernum}"
9094
fi
9195

9296
filter="^/"
9397
if "${is_podman}"; then
94-
filter=""
98+
filter=""
9599
fi
96-
stat="$($docker ps --all --no-trunc --filter name="${filter}$name\$" --format '{{.Status}}'\
97-
| cut -f1 -d' ')"
100+
stat=$(call_docker ps --all --no-trunc --filter name="${filter}${name}\$" --format '{{.Status}}' \
101+
| cut -f1 -d' ')
98102

99103
# pass SDK related environment variables and gcloud auth
100104
# into container
@@ -104,59 +108,67 @@ setup_gsutil
104108
mkdir -p "__build__/images"
105109
mkdir -p "sdk_container/.cache/sdks"
106110

107-
hostname="${name:0:63}"
108-
hostname="${hostname//./_}"
111+
hostname=${name:0:63}
112+
hostname=${hostname//./_}
109113

110-
if [ -n "$cleanup" ] ; then
111-
echo "$docker container rm -f '${name}'" >> "$cleanup"
114+
if [[ -n ${cleanup} ]] ; then
115+
echo "${docker_a[@]@Q} container rm -f ${name@Q}" >>"${cleanup}"
112116
fi
113117

114-
if [ -z "$stat" ] ; then
118+
if [[ -z ${stat} ]] ; then
115119
yell "Creating a new container '$name'"
116120

117-
gpg_volumes=$(gnupg_ssh_gcloud_mount_opts)
121+
gpg_volumes=()
122+
gnupg_ssh_gcloud_mount_opts gpg_volumes
118123

119-
if [ -z "$custom_image" ]; then
120-
(
124+
if [[ -z ${custom_image} ]]; then
125+
(
121126
source ci-automation/ci_automation_common.sh
122127
docker_image_from_registry_or_buildcache "flatcar-sdk-${arch}" "${docker_sdk_vernum}"
123-
)
128+
)
124129
else
125130
# We could split the container_image_name in parts to call docker_image_from_registry_or_buildcache
126131
# bur for now just try to ensure that we use the latest image if using a container registry,
127132
# for the tar-ball-imported images we rely on the ci-automation scripts to call
128133
# docker_image_from_registry_or_buildcache explicitly.
129-
$docker pull "${container_image_name}" || true
134+
call_docker pull "${container_image_name}" || true
130135
fi
131136

132-
$docker create $tty -i \
133-
-v /dev:/dev \
134-
-v "$(pwd)/sdk_container:/mnt/host/source/" \
135-
-v "$(pwd)/__build__/images:/mnt/host/source/src/build" \
136-
-v "$(pwd):/mnt/host/source/src/scripts" \
137-
$gpg_volumes \
138-
--privileged \
139-
--network host \
140-
-e SDK_USER_ID="$(id -u)" \
141-
-e SDK_GROUP_ID="$(id -g)" \
142-
--name="$name" \
143-
--hostname="$hostname" \
144-
--entrypoint /bin/bash \
145-
"${container_image_name}" -l
137+
docker_flags=(
138+
"${tty[@]}"
139+
-i
140+
-v /dev:/dev
141+
-v "${PWD}/sdk_container:/mnt/host/source/"
142+
-v "${PWD}/__build__/images:/mnt/host/source/src/build"
143+
-v "${PWD}:/mnt/host/source/src/scripts"
144+
"${gpg_volumes[@]}"
145+
"${mounts[@]}"
146+
--privileged
147+
--network host
148+
-e SDK_USER_ID="$(id -u)"
149+
-e SDK_GROUP_ID="$(id -g)"
150+
--name="${name}"
151+
--hostname="${hostname}"
152+
--entrypoint /bin/bash
153+
"${container_image_name}"
154+
-l
155+
)
156+
157+
call_docker create "${docker_flags[@]}"
146158
fi
147159

148-
if [ "$stat" != "Up" ] ; then
160+
if [[ ${stat} != "Up" ]] ; then
149161
yell "Starting stopped container '$name'"
150-
if [ "${remove}" = "true" ]; then
151-
remove_command="$docker rm -f $name"
162+
if [[ -n ${remove} ]]; then
163+
remove_command="call_docker rm -f ${name@Q}"
152164
else
153165
remove_command=":"
154166
fi
155-
trap "$docker stop -t 0 $name ; ${remove_command}" EXIT
156-
$docker start "$name"
167+
trap "call_docker stop -t 0 ${name@Q} ; ${remove_command}" EXIT
168+
call_docker start "${name}"
157169
fi
158170

159171
# Workaround: The SDK expects to be able to write to /etc/hosts
160-
$docker exec "$name" sh -c 'cp /etc/hosts /etc/hosts2; umount /etc/hosts ; mv /etc/hosts2 /etc/hosts'
172+
call_docker exec "${name}" sh -c 'cp /etc/hosts /etc/hosts2; umount /etc/hosts ; mv /etc/hosts2 /etc/hosts'
161173

162-
$docker exec $tty -i "$name" /mnt/host/source/src/scripts/sdk_lib/sdk_entry.sh "$@"
174+
call_docker exec "${tty[@]}" -i "${name}" /mnt/host/source/src/scripts/sdk_lib/sdk_entry.sh "$@"

sdk_lib/sdk_container_common.sh

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,16 @@ if command -v podman >/dev/null; then
3232
fi
3333
fi
3434

35-
docker="docker"
35+
docker_a=( docker )
3636
if "${is_podman}"; then
37-
docker="sudo podman"
37+
docker_a=( sudo podman )
3838
fi
39+
docker=${docker_a[*]}
40+
41+
function call_docker() {
42+
"${docker_a[@]}" "${@}"
43+
}
44+
# --
3945

4046
# Common "echo" function
4147

@@ -263,34 +269,39 @@ EOF
263269
export GOOGLE_APPLICATION_CREDENTIALS
264270
}
265271

266-
267272
# --
268273

269274
# Generate volume mount command line options for docker
270275
# to pass gpg, ssh, and gcloud auth host directories
271276
# into the SDK container.
272277

273278
function gnupg_ssh_gcloud_mount_opts() {
279+
local -n args_ref="${1}"; shift
280+
274281
local sdk_gnupg_home="/home/sdk/.gnupg"
275282
local gpgagent_dir="/run/user/$(id -u)/gnupg"
276283

284+
args_ref=()
277285
# pass host GPG home and Agent directories to container
278-
if [ -d "$GNUPGHOME" ] ; then
279-
echo "-v $GNUPGHOME:$sdk_gnupg_home"
286+
if [[ -d ${GNUPGHOME} ]] ; then
287+
args_ref+=( -v "$GNUPGHOME:$sdk_gnupg_home" )
280288
fi
281-
if [ -d "$gpgagent_dir" ] ; then
282-
echo "-v $gpgagent_dir:$gpgagent_dir"
289+
if [[ -d ${gpgagent_dir} ]] ; then
290+
args_ref+=( -v "${gpgagent_dir}:${gpgagent_dir}" )
283291
fi
284292

285-
if [ -e "${SSH_AUTH_SOCK:-}" ] ; then
286-
local sshsockdir="$(dirname "$SSH_AUTH_SOCK")"
287-
echo "-v $sshsockdir:/run/sdk/ssh"
293+
local sshsockdir
294+
if [[ -e ${SSH_AUTH_SOCK:-} ]] ; then
295+
sshsockdir=$(dirname "$SSH_AUTH_SOCK")
296+
args_ref+=( -v "${sshsockdir}:/run/sdk/ssh" )
288297
fi
289298

290-
if [ -e "${GOOGLE_APPLICATION_CREDENTIALS:-}" ] ; then
291-
local creds_dir="$(dirname "${GOOGLE_APPLICATION_CREDENTIALS}")"
292-
if [ -d "$creds_dir" ] ; then
299+
local creds_dir
300+
if [[ -e ${GOOGLE_APPLICATION_CREDENTIALS:-} ]] ; then
301+
creds_dir=$(dirname "${GOOGLE_APPLICATION_CREDENTIALS}")
302+
if [[ -d ${creds_dir} ]] ; then
293303
echo "-v $creds_dir:$creds_dir"
304+
args_ref+=( -v "${creds_dir}:${creds_dir}" )
294305
fi
295306
fi
296307
}

0 commit comments

Comments
 (0)