3
3
function obtain_linuxkit_binary_cached() {
4
4
# Grab linuxkit from official GitHub releases; account for arm64/amd64 differences
5
5
6
+ declare linuxkit_os=" linux"
7
+ [[ " $( uname -s) " == " Darwin" ]] && linuxkit_os=" darwin"
8
+
6
9
declare linuxkit_arch=" "
7
10
# determine the arch to download from current arch
8
11
case " $( uname -m) " in
9
12
" x86_64" ) linuxkit_arch=" amd64" ;;
10
- " aarch64" ) linuxkit_arch=" arm64" ;;
13
+ " aarch64" | " arm64 " ) linuxkit_arch=" arm64" ;;
11
14
* ) log error " ERROR: ARCH $( uname -m) not supported by linuxkit? check https://github.com/linuxkit/linuxkit/releases" && exit 1 ;;
12
15
esac
13
16
14
- declare linuxkit_down_url=" https://github.com/linuxkit/linuxkit/releases/download/v${LINUXKIT_VERSION} /linuxkit-linux -${linuxkit_arch} "
15
- declare -g linuxkit_bin=" ${CACHE_DIR} /linuxkit-linux -${linuxkit_arch} -${LINUXKIT_VERSION} "
17
+ declare linuxkit_down_url=" https://github.com/linuxkit/linuxkit/releases/download/v${LINUXKIT_VERSION} /linuxkit-${linuxkit_os} -${linuxkit_arch} "
18
+ declare -g linuxkit_bin=" ${CACHE_DIR} /linuxkit-${linuxkit_os} -${linuxkit_arch} -${LINUXKIT_VERSION} "
16
19
17
20
# Download using curl if not already present
18
21
if [[ ! -f " ${linuxkit_bin} " ]]; then
@@ -22,7 +25,7 @@ function obtain_linuxkit_binary_cached() {
22
25
fi
23
26
24
27
# Show the binary's version
25
- log info " LinuxKit binary version: ('0.8+' reported for 1.2.0, bug?): $( " ${linuxkit_bin} " version | xargs echo -n) "
28
+ log info " LinuxKit binary version: $( " ${linuxkit_bin} " version | xargs echo -n) "
26
29
27
30
}
28
31
@@ -49,29 +52,37 @@ function linuxkit_build() {
49
52
fi
50
53
fi
51
54
55
+ # A dictionary (bash associative array) with variables and their values, for templating using envsubst.
56
+ declare -A -g hook_template_vars=(
57
+ [" HOOK_VERSION" ]=" ${HOOK_VERSION} "
58
+ [" HOOK_KERNEL_IMAGE" ]=" ${kernel_oci_image} "
59
+ [" HOOK_KERNEL_ID" ]=" ${inventory_id} "
60
+ [" HOOK_KERNEL_VERSION" ]=" ${kernel_oci_version} "
61
+ )
62
+
52
63
# Build the containers in this repo used in the LinuxKit YAML;
53
- build_all_hook_linuxkit_containers # sets HOOK_CONTAINER_BOOTKIT_IMAGE, HOOK_CONTAINER_DOCKER_IMAGE, HOOK_CONTAINER_MDEV_IMAGE, HOOK_CONTAINER_CONTAINERD_IMAGE
64
+ build_all_hook_linuxkit_containers # sets HOOK_CONTAINER_BOOTKIT_IMAGE, HOOK_CONTAINER_DOCKER_IMAGE and others in the hook_template_vars dict
54
65
55
66
# Template the linuxkit configuration file.
56
67
# - You'd think linuxkit would take --build-args or something by now, but no.
57
68
# - Linuxkit does have @pkg but that's only useful in its own repo (with pkgs/ dir)
58
69
# - envsubst doesn't offer a good way to escape $ in the input, so we pass the exact list of vars to consider, so escaping is not needed
59
-
60
70
log info " Using Linuxkit template '${kernel_info['TEMPLATE']} '..."
61
71
62
- # HOOK_VERSION is read-only & already exported so is not listed in the env vars here, but is included in the dollar-sign list for envsubst to process
63
- # shellcheck disable=SC2002 # Again, no, I love my cat, leave me alone
64
- # shellcheck disable=SC2016 # I'm using single quotes to avoid shell expansion, envsubst wants the dollar signs.
65
- cat " linuxkit-templates/${kernel_info['TEMPLATE']} .template.yaml" |
66
- HOOK_KERNEL_IMAGE=" ${kernel_oci_image} " HOOK_KERNEL_ID=" ${inventory_id} " HOOK_KERNEL_VERSION=" ${kernel_oci_version} " \
67
- HOOK_CONTAINER_BOOTKIT_IMAGE=" ${HOOK_CONTAINER_BOOTKIT_IMAGE} " \
68
- HOOK_CONTAINER_DOCKER_IMAGE=" ${HOOK_CONTAINER_DOCKER_IMAGE} " \
69
- HOOK_CONTAINER_MDEV_IMAGE=" ${HOOK_CONTAINER_MDEV_IMAGE} " \
70
- HOOK_CONTAINER_CONTAINERD_IMAGE=" ${HOOK_CONTAINER_CONTAINERD_IMAGE} " \
71
- HOOK_CONTAINER_RUNC_IMAGE=" ${HOOK_CONTAINER_RUNC_IMAGE} " \
72
- HOOK_CONTAINER_EMBEDDED_IMAGE=" ${HOOK_CONTAINER_EMBEDDED_IMAGE} " \
73
- envsubst ' $HOOK_VERSION $HOOK_KERNEL_IMAGE $HOOK_KERNEL_ID $HOOK_KERNEL_VERSION $HOOK_CONTAINER_BOOTKIT_IMAGE $HOOK_CONTAINER_DOCKER_IMAGE $HOOK_CONTAINER_MDEV_IMAGE $HOOK_CONTAINER_CONTAINERD_IMAGE $HOOK_CONTAINER_RUNC_IMAGE $HOOK_CONTAINER_EMBEDDED_IMAGE' \
74
- > " hook.${inventory_id} .yaml"
72
+ # Calculate, from hook_template_vars dictionary:
73
+ # envsubst_arg_string: a space separated list of dollar-prefixed variables name to be substituted
74
+ # envusbst_env: the environment variables to be passed to envsubst (array of KEY=var) to be used via 'env'
75
+ declare envsubst_arg_string=" "
76
+ declare -a envsubst_envs=()
77
+ for key in " ${! hook_template_vars[@]} " ; do
78
+ envsubst_arg_string+=" \$ ${key} " # extra space at the end doesn't hurt
79
+ envsubst_envs+=(" ${key} =${hook_template_vars["${key}"]} " )
80
+ done
81
+ log debug " envsubst_arg_string: ${envsubst_arg_string} "
82
+ log debug " envsubst_envs: ${envsubst_envs[*]} "
83
+
84
+ # Run envsubst on the template file, output to a new file; pass the envs and the arg string
85
+ env " ${envsubst_envs[@]} " envsubst " ${envsubst_arg_string} " < " linuxkit-templates/${kernel_info['TEMPLATE']} .template.yaml" > " hook.${inventory_id} .yaml"
75
86
76
87
declare -g linuxkit_bin=" "
77
88
obtain_linuxkit_binary_cached # sets "${linuxkit_bin}"
@@ -101,7 +112,7 @@ function linuxkit_build() {
101
112
" ${linuxkit_bin} " build " ${lk_iso_args[@]} "
102
113
return 0
103
114
fi
104
-
115
+
105
116
declare -a lk_args=(
106
117
" --docker"
107
118
" --arch" " ${kernel_info['DOCKER_ARCH']} "
0 commit comments