@@ -49,29 +49,37 @@ function linuxkit_build() {
49
49
fi
50
50
fi
51
51
52
+ # A dictionary (bash associative array) with variables and their values, for templating using envsubst.
53
+ declare -A -g hook_template_vars=(
54
+ [" HOOK_VERSION" ]=" ${HOOK_VERSION} "
55
+ [" HOOK_KERNEL_IMAGE" ]=" ${kernel_oci_image} "
56
+ [" HOOK_KERNEL_ID" ]=" ${inventory_id} "
57
+ [" HOOK_KERNEL_VERSION" ]=" ${kernel_oci_version} "
58
+ )
59
+
52
60
# 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
61
+ build_all_hook_linuxkit_containers # sets HOOK_CONTAINER_BOOTKIT_IMAGE, HOOK_CONTAINER_DOCKER_IMAGE and others in the hook_template_vars dict
54
62
55
63
# Template the linuxkit configuration file.
56
64
# - You'd think linuxkit would take --build-args or something by now, but no.
57
65
# - Linuxkit does have @pkg but that's only useful in its own repo (with pkgs/ dir)
58
66
# - 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
67
log info " Using Linuxkit template '${kernel_info['TEMPLATE']} '..."
61
68
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"
69
+ # Calculate, from hook_template_vars dictionary:
70
+ # envsubst_arg_string: a space separated list of dollar-prefixed variables name to be substituted
71
+ # envusbst_env: the environment variables to be passed to envsubst (array of KEY=var) to be used via 'env'
72
+ declare envsubst_arg_string=" "
73
+ declare -a envsubst_envs=()
74
+ for key in " ${! hook_template_vars[@]} " ; do
75
+ envsubst_arg_string+=" \$ ${key} " # extra space at the end doesn't hurt
76
+ envsubst_envs+=(" ${key} =${hook_template_vars["${key}"]} " )
77
+ done
78
+ log debug " envsubst_arg_string: ${envsubst_arg_string} "
79
+ log debug " envsubst_envs: ${envsubst_envs[*]} "
80
+
81
+ # Run envsubst on the template file, output to a new file; pass the envs and the arg string
82
+ env " ${envsubst_envs[@]} " envsubst " ${envsubst_arg_string} " < " linuxkit-templates/${kernel_info['TEMPLATE']} .template.yaml" > " hook.${inventory_id} .yaml"
75
83
76
84
declare -g linuxkit_bin=" "
77
85
obtain_linuxkit_binary_cached # sets "${linuxkit_bin}"
@@ -101,7 +109,7 @@ function linuxkit_build() {
101
109
" ${linuxkit_bin} " build " ${lk_iso_args[@]} "
102
110
return 0
103
111
fi
104
-
112
+
105
113
declare -a lk_args=(
106
114
" --docker"
107
115
" --arch" " ${kernel_info['DOCKER_ARCH']} "
0 commit comments