Skip to content

Commit 6b32674

Browse files
committed
sdk_lib,run_sdk_container: Modernize a bit
- Make cosmetic fixes in help output. - There is usually no need for putting variables inside quotes in assignments. - Use [[ ]] to avoid putting everything into strings. - Use arrays instead of relying on strings to be split on whitespace as it was the case for invoking docker and getting GPG volume flags for docker. - Make sure that some cleanup and trap strings quote variables properly. - Add a "call_docker" function to avoid dealing with "docker" and a new "docker_a" variables when willing to invoke docker. The "docker" variable rather shouldn't be used, but it is still there in case some other scripts were using it.
1 parent 5ec3b7b commit 6b32674

File tree

2 files changed

+93
-75
lines changed

2 files changed

+93
-75
lines changed

run_sdk_container

Lines changed: 69 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -12,38 +12,38 @@ 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=""
2121

2222
usage() {
2323
echo " Usage:"
24-
echo " $0 [-t] [-v <version>] [-V sdk version] [-a arch] [-n <name> ] [-x <script>] [-C custom-container] [--rm] [container-command]"
24+
echo " $0 [-t] [-v <version>] [-V <SDK version>] [-a <amd64|arm64|all>] [-n <name> ] [-x <script>] [-C <custom-container>] [--rm] [-U] [<container-command>]"
2525
echo " Start an SDK container of a given SDK release version."
2626
echo " This will create the container if it does not exist, otherwise start the existing container."
2727
echo " If the container is already running then it will exec into the container."
2828
echo
29-
echo " container-command - command to be run in the container instead of"
29+
echo " <container-command> - Command to be run in the container instead of"
3030
echo " an interactive shell."
31-
echo " -t Attach docker to a TTY (docker -t)"
31+
echo " -t Attach docker to a TTY (docker -t)."
3232
echo " -v <version> - Sourcetree (OS image) version to use."
3333
echo " Defaults to '$os_version' (current git commit)."
3434
echo " FLATCAR_VERSION[_ID] in '$sdk_container_common_versionfile'"
3535
echo " will be updated accordingly."
36-
echo " -V <SDK ver> - SDK version to use. Defaults to '${sdk_version}'"
36+
echo " -V <SDK version> - SDK version to use. Defaults to '${sdk_version}'"
3737
echo " (FLATCAR_SDK_VERSION from '$sdk_container_common_versionfile')."
3838
echo " -a <amd64|arm64|all> - Target architecture (board support) of the SDK."
3939
echo " 'all' (the default) contains support for both amd64 and arm64."
4040
echo " -n <name> - Custom name to use for the container."
41-
echo " --rm Remove container afterwards"
41+
echo " --rm Remove container afterwards."
4242
echo " -x <script> - For each resource generated during build (container etc.)"
4343
echo " add a cleanup line to <script> which, when run, will free"
4444
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]"
45+
echo " -C <custom-container> - Use an entirely custom container image instead of the SDK's"
46+
echo " $sdk_container_common_registry/flatcar-sdk-[ARCH]:[SDK VERSION]."
4747
echo " Useful for CI."
4848
echo " -U Do not update the versionfile. Instead, use the version from the versionfile as-is."
4949
echo " -h Print this help."
@@ -52,49 +52,49 @@ usage() {
5252
# --
5353

5454
update_versionfile=x
55-
while [ 0 -lt $# ] ; do
55+
while [[ $# -gt 0 ]] ; do
5656
case "$1" in
5757
-h) usage; exit 0;;
5858
--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)"
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=x; 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)
6969
update_versionfile=
7070
shift;;
7171
*) break;;
7272
esac
7373
done
7474

75-
if [ -n "$custom_image" ] ; then
76-
container_image_name="${custom_image}"
75+
if [[ -n ${custom_image} ]] ; then
76+
container_image_name=${custom_image}
7777
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}"
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}"
8080
fi
8181

8282
if [[ -n ${update_versionfile} ]] ; then
8383
create_versionfile "$sdk_version" "$os_version"
8484
fi
8585

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}")"
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}")
8989
name="flatcar-sdk-${arch}-${docker_sdk_vernum}_os-${docker_os_vernum}"
9090
fi
9191

9292
filter="^/"
9393
if "${is_podman}"; then
94-
filter=""
94+
filter=""
9595
fi
96-
stat="$($docker ps --all --no-trunc --filter name="${filter}$name\$" --format '{{.Status}}'\
97-
| cut -f1 -d' ')"
96+
stat=$(call_docker ps --all --no-trunc --filter name="${filter}${name}\$" --format '{{.Status}}' \
97+
| cut -f1 -d' ')
9898

9999
# pass SDK related environment variables and gcloud auth
100100
# into container
@@ -104,59 +104,66 @@ setup_gsutil
104104
mkdir -p "__build__/images"
105105
mkdir -p "sdk_container/.cache/sdks"
106106

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

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

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

117-
gpg_volumes=$(gnupg_ssh_gcloud_mount_opts)
117+
gpg_volumes=()
118+
gnupg_ssh_gcloud_mount_opts gpg_volumes
118119

119-
if [ -z "$custom_image" ]; then
120-
(
120+
if [[ -z ${custom_image} ]]; then
121+
(
121122
source ci-automation/ci_automation_common.sh
122123
docker_image_from_registry_or_buildcache "flatcar-sdk-${arch}" "${docker_sdk_vernum}"
123-
)
124+
)
124125
else
125126
# We could split the container_image_name in parts to call docker_image_from_registry_or_buildcache
126127
# bur for now just try to ensure that we use the latest image if using a container registry,
127128
# for the tar-ball-imported images we rely on the ci-automation scripts to call
128129
# docker_image_from_registry_or_buildcache explicitly.
129-
$docker pull "${container_image_name}" || true
130+
call_docker pull "${container_image_name}" || true
130131
fi
131132

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
133+
docker_flags=(
134+
"${tty[@]}"
135+
-i
136+
-v /dev:/dev
137+
-v "${PWD}/sdk_container:/mnt/host/source/"
138+
-v "${PWD}/__build__/images:/mnt/host/source/src/build"
139+
-v "${PWD}:/mnt/host/source/src/scripts"
140+
"${gpg_volumes[@]}"
141+
--privileged
142+
--network host
143+
-e SDK_USER_ID="$(id -u)"
144+
-e SDK_GROUP_ID="$(id -g)"
145+
--name="${name}"
146+
--hostname="${hostname}"
147+
--entrypoint /bin/bash
148+
"${container_image_name}"
149+
-l
150+
)
151+
152+
call_docker create "${docker_flags[@]}"
146153
fi
147154

148-
if [ "$stat" != "Up" ] ; then
155+
if [[ ${stat} != "Up" ]] ; then
149156
yell "Starting stopped container '$name'"
150-
if [ "${remove}" = "true" ]; then
151-
remove_command="$docker rm -f $name"
157+
if [[ -n ${remove} ]]; then
158+
remove_command="call_docker rm -f ${name@Q}"
152159
else
153160
remove_command=":"
154161
fi
155-
trap "$docker stop -t 0 $name ; ${remove_command}" EXIT
156-
$docker start "$name"
162+
trap "call_docker stop -t 0 ${name@Q} ; ${remove_command}" EXIT
163+
call_docker start "${name}"
157164
fi
158165

159166
# 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'
167+
call_docker exec "${name}" sh -c 'cp /etc/hosts /etc/hosts2; umount /etc/hosts ; mv /etc/hosts2 /etc/hosts'
161168

162-
$docker exec $tty -i "$name" /mnt/host/source/src/scripts/sdk_lib/sdk_entry.sh "$@"
169+
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)