Skip to content

Commit bcc78c8

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>
1 parent bb599ed commit bcc78c8

File tree

1 file changed

+18
-11
lines changed

1 file changed

+18
-11
lines changed

hack/inject-cmdline-to-template.sh

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
# 6. inject the kernel and initrd location, digest, and cmdline to the template
1010
# 7. output kernel_location, kernel_digest, cmdline, initrd_location, initrd_digest
1111

12-
set -eu -o pipefail
12+
set -u -o pipefail
1313

1414
template="$1"
1515
appending_options="$2"
@@ -36,12 +36,16 @@ 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+
if check_location "${locations[i]}"; then
4549
location=${locations[i]}
4650
index=${i}
4751
break
@@ -78,14 +82,17 @@ initrd_digest=$(awk "/${initrd_basename}/{print \"sha256:\"\$1}" <<<"${sha256sum
7882
initrd_location="${location_dirname}/${initrd_basename}"
7983

8084
# 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}"
85+
function inject_to() {
86+
# shellcheck disable=SC2034
87+
local template=$1 arch=$2 index=$3 key=$4 location=$5 digest=$6 cmdline=${7:-} fields=() IFS=,
88+
check_location "${location}" || return 0
89+
for field_name in location digest cmdline; do
90+
[[ -z ${!field_name} ]] || fields+=("\"${field_name}\": \"${!field_name}\"")
91+
done
92+
yq -i -I 2 eval "setpath([(.images[] | select(.arch == \"${arch}\") | path)].[${index}] + \"${key}\"; { ${fields[*]}})" "${template}"
93+
}
94+
inject_to "${template}" "${arch}" "${index}" "kernel" "${kernel_location}" "${kernel_digest}" "${cmdline}"
95+
inject_to "${template}" "${arch}" "${index}" "initrd" "${initrd_location}" "${initrd_digest}"
8996

9097
# 7. output kernel_location, kernel_digest, cmdline, initrd_location, initrd_digest
9198
readonly outputs=(kernel_location kernel_digest cmdline initrd_location initrd_digest)

0 commit comments

Comments
 (0)