Skip to content

Commit 72a1cb6

Browse files
committed
inject-cmdline-to-template.sh: add check location existence
Some ubuntu kernel images are provided without initrd image. e.g. https://cloud-images.ubuntu.com/minimal/releases/noble/release-20240823/unpacked/ Signed-off-by: Norio Nomura <norio.nomura@gmail.com> inject-cmdline-to-template.sh: use `-e` again Signed-off-by: Norio Nomura <norio.nomura@gmail.com>
1 parent 214879e commit 72a1cb6

File tree

1 file changed

+19
-10
lines changed

1 file changed

+19
-10
lines changed

hack/inject-cmdline-to-template.sh

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,17 @@ readonly yq_filter="
3636
parsed=$(yq eval "${yq_filter}" "${template}")
3737

3838
# 3. get the image location
39+
function check_location() {
40+
local location=$1 http_code
41+
http_code=$(curl -sIL -w "%{http_code}" "${location}" -o /dev/null)
42+
[[ ${http_code} -eq 200 ]]
43+
}
3944
while IFS= read -r line; do arr+=("${line}"); done <<<"${parsed}"
4045
readonly locations=("${arr[@]}")
4146
for ((i = 0; i < ${#locations[@]}; i++)); do
4247
[[ ${locations[i]} != "null" ]] || continue
43-
http_code=$(curl -sIL -w "%{http_code}" "${locations[i]}" -o /dev/null)
44-
if [[ ${http_code} -eq 200 ]]; then
48+
# shellcheck disable=SC2310
49+
if check_location "${locations[i]}"; then
4550
location=${locations[i]}
4651
index=${i}
4752
break
@@ -78,14 +83,18 @@ initrd_digest=$(awk "/${initrd_basename}/{print \"sha256:\"\$1}" <<<"${sha256sum
7883
initrd_location="${location_dirname}/${initrd_basename}"
7984

8085
# 6. inject the kernel and initrd location, digest, and cmdline to the template
81-
yq -i eval "
82-
[(.images.[] | select(.arch == \"${arch}\") | path)].[${index}] + \"kernel\" as \$path|
83-
setpath(\$path; { \"location\": \"${kernel_location}\", \"digest\": \"${kernel_digest}\", \"cmdline\": \"${cmdline}\" })
84-
" "${template}"
85-
yq -i eval "
86-
[(.images.[] | select(.arch == \"${arch}\") | path)].[${index}] + \"initrd\" as \$path|
87-
setpath(\$path ; { \"location\": \"${initrd_location}\", \"digest\": \"${initrd_digest}\" })
88-
" "${template}"
86+
function inject_to() {
87+
# shellcheck disable=SC2034
88+
local template=$1 arch=$2 index=$3 key=$4 location=$5 digest=$6 cmdline=${7:-} fields=() IFS=,
89+
# shellcheck disable=SC2310
90+
check_location "${location}" || return 0
91+
for field_name in location digest cmdline; do
92+
[[ -z ${!field_name} ]] || fields+=("\"${field_name}\": \"${!field_name}\"")
93+
done
94+
yq -i -I 2 eval "setpath([(.images[] | select(.arch == \"${arch}\") | path)].[${index}] + \"${key}\"; { ${fields[*]}})" "${template}"
95+
}
96+
inject_to "${template}" "${arch}" "${index}" "kernel" "${kernel_location}" "${kernel_digest}" "${cmdline}"
97+
inject_to "${template}" "${arch}" "${index}" "initrd" "${initrd_location}" "${initrd_digest}"
8998

9099
# 7. output kernel_location, kernel_digest, cmdline, initrd_location, initrd_digest
91100
readonly outputs=(kernel_location kernel_digest cmdline initrd_location initrd_digest)

0 commit comments

Comments
 (0)