Skip to content

Commit

Permalink
inject-cmdline-to-template.sh: add check location existence
Browse files Browse the repository at this point in the history
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 <[email protected]>

inject-cmdline-to-template.sh: use `-e` again

Signed-off-by: Norio Nomura <[email protected]>
  • Loading branch information
norio-nomura committed Sep 5, 2024
1 parent 214879e commit 72a1cb6
Showing 1 changed file with 19 additions and 10 deletions.
29 changes: 19 additions & 10 deletions hack/inject-cmdline-to-template.sh
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,17 @@ readonly yq_filter="
parsed=$(yq eval "${yq_filter}" "${template}")

# 3. get the image location
function check_location() {
local location=$1 http_code
http_code=$(curl -sIL -w "%{http_code}" "${location}" -o /dev/null)
[[ ${http_code} -eq 200 ]]
}
while IFS= read -r line; do arr+=("${line}"); done <<<"${parsed}"
readonly locations=("${arr[@]}")
for ((i = 0; i < ${#locations[@]}; i++)); do
[[ ${locations[i]} != "null" ]] || continue
http_code=$(curl -sIL -w "%{http_code}" "${locations[i]}" -o /dev/null)
if [[ ${http_code} -eq 200 ]]; then
# shellcheck disable=SC2310
if check_location "${locations[i]}"; then
location=${locations[i]}
index=${i}
break
Expand Down Expand Up @@ -78,14 +83,18 @@ initrd_digest=$(awk "/${initrd_basename}/{print \"sha256:\"\$1}" <<<"${sha256sum
initrd_location="${location_dirname}/${initrd_basename}"

# 6. inject the kernel and initrd location, digest, and cmdline to the template
yq -i eval "
[(.images.[] | select(.arch == \"${arch}\") | path)].[${index}] + \"kernel\" as \$path|
setpath(\$path; { \"location\": \"${kernel_location}\", \"digest\": \"${kernel_digest}\", \"cmdline\": \"${cmdline}\" })
" "${template}"
yq -i eval "
[(.images.[] | select(.arch == \"${arch}\") | path)].[${index}] + \"initrd\" as \$path|
setpath(\$path ; { \"location\": \"${initrd_location}\", \"digest\": \"${initrd_digest}\" })
" "${template}"
function inject_to() {
# shellcheck disable=SC2034
local template=$1 arch=$2 index=$3 key=$4 location=$5 digest=$6 cmdline=${7:-} fields=() IFS=,
# shellcheck disable=SC2310
check_location "${location}" || return 0
for field_name in location digest cmdline; do
[[ -z ${!field_name} ]] || fields+=("\"${field_name}\": \"${!field_name}\"")
done
yq -i -I 2 eval "setpath([(.images[] | select(.arch == \"${arch}\") | path)].[${index}] + \"${key}\"; { ${fields[*]}})" "${template}"
}
inject_to "${template}" "${arch}" "${index}" "kernel" "${kernel_location}" "${kernel_digest}" "${cmdline}"
inject_to "${template}" "${arch}" "${index}" "initrd" "${initrd_location}" "${initrd_digest}"

# 7. output kernel_location, kernel_digest, cmdline, initrd_location, initrd_digest
readonly outputs=(kernel_location kernel_digest cmdline initrd_location initrd_digest)
Expand Down

0 comments on commit 72a1cb6

Please sign in to comment.