feat: Improve error logging (#2106)

This commit is contained in:
Kroese
2026-08-07 23:35:01 +02:00
committed by GitHub
parent e51c597821
commit 221cf5ad73
2 changed files with 303 additions and 69 deletions
+64 -11
View File
@@ -223,18 +223,25 @@ generateAnswerFile() {
local directory install_count install_to_count tmp
if [ -n "$index" ] && [[ ! "$index" =~ ^[1-9][0-9]*$ ]]; then
enabled "$DEBUG" && echo "The $type answer file received an invalid image index: $index." >&2
error "Invalid $type image index: $index"
return 1
fi
directory=$(dirname "$target") || return 1
directory=$(dirname "$target") || {
enabled "$DEBUG" && echo "dirname failed for the $type answer file target: $target" >&2
error "Failed to determine the $type answer file directory!"
return 1
}
if ! tmp=$(mktemp -p "$directory" ".${id}.XXXXXX"); then
enabled "$DEBUG" && echo "mktemp failed in the $type answer file directory: $directory" >&2
error "Failed to create a temporary $type answer file!"
return 1
fi
if ! cp -L -- "$source" "$tmp"; then
enabled "$DEBUG" && echo "Failed to copy the $type answer template from $source to $tmp." >&2
rm -f "$tmp"
error "Failed to generate $type answer file from $source!"
return 1
@@ -243,6 +250,7 @@ generateAnswerFile() {
# Keep empty ProductKey structures because some Windows installers require
# the node to exist, but remove a concrete key that could select another edition.
if ! removeEmbeddedProductKeys "$tmp"; then
enabled "$DEBUG" && echo "Failed while removing embedded product keys from the $type answer file." >&2
rm -f "$tmp"
error "Failed to remove the embedded $type product key!"
return 1
@@ -251,6 +259,7 @@ generateAnswerFile() {
if [ "$type" != "evaluation" ] || [ "$remove_selector" = "Y" ]; then
if ! xmlstarlet ed -L -N "$XML_NS_UNATTEND_ARG" -d "$install_from" "$tmp"; then
enabled "$DEBUG" && echo "Failed to remove the existing InstallFrom selector from the $type answer file." >&2
rm -f "$tmp"
error "Failed to generate $type answer file from $source!"
return 1
@@ -261,11 +270,14 @@ generateAnswerFile() {
if [ -n "$index" ]; then
install_count=$(getXMLNodeCount "$tmp" "$install_from") || {
enabled "$DEBUG" && echo "Failed to count InstallFrom selectors in the $type answer file." >&2
rm -f "$tmp"
error "Failed to read the $type image selector count!"
return 1
}
if (( install_count > 1 )); then
enabled "$DEBUG" && echo "The $type answer file contains $install_count InstallFrom selectors." >&2
rm -f "$tmp"
error "Multiple $type image selectors were found!"
return 1
@@ -276,11 +288,14 @@ generateAnswerFile() {
# InstallFrom must be inserted before InstallTo to preserve the ordering
# expected by the Windows Setup schema.
install_to_count=$(getXMLNodeCount "$tmp" "$install_to") || {
enabled "$DEBUG" && echo "Failed to count InstallTo nodes in the $type answer file." >&2
rm -f "$tmp"
error "Failed to read the $type installation target count!"
return 1
}
if [ "$install_to_count" != "1" ]; then
enabled "$DEBUG" && echo "The $type answer file contains $install_to_count InstallTo nodes instead of 1." >&2
rm -f "$tmp"
error "Failed to find a unique $type installation target!"
return 1
@@ -296,6 +311,7 @@ generateAnswerFile() {
-s "$os_image/*[local-name()='InstallFrom']/*[local-name()='MetaData']" -t elem -n 'Value' -v "$index" \
"$tmp"; then
enabled "$DEBUG" && echo "xmlstarlet failed while inserting image index $index into the $type answer file." >&2
rm -f "$tmp"
error "Failed to select $type image index $index!"
return 1
@@ -305,17 +321,27 @@ generateAnswerFile() {
fi
if ! markGeneratedXML "$tmp"; then
enabled "$DEBUG" && echo "Failed to mark the temporary $type answer file as generated: $tmp" >&2
rm -f "$tmp"
error "Failed to mark generated $type answer file!"
return 1
fi
if ! validateGeneratedXML "$tmp"; then
enabled "$DEBUG" && echo "Validation failed for the generated $type answer file: $tmp" >&2
rm -f "$tmp"
return 1
fi
if ! chmod 644 "$tmp" || ! mv -f "$tmp" "$target"; then
if ! chmod 644 "$tmp"; then
enabled "$DEBUG" && echo "Failed to set mode 644 on the generated $type answer file: $tmp" >&2
rm -f "$tmp"
error "Failed to create $type answer file: $target"
return 1
fi
if ! mv -f "$tmp" "$target"; then
enabled "$DEBUG" && echo "Failed to move the generated $type answer file from $tmp to $target." >&2
rm -f "$tmp"
error "Failed to create $type answer file: $target"
return 1
@@ -332,7 +358,10 @@ generateEvalXML() {
local id="$1"
local detected_index="${2:-}"
[[ "${id,,}" == *"-eval" ]] || return 1
if [[ "${id,,}" != *"-eval" ]]; then
enabled "$DEBUG" && echo "Evaluation XML generation was requested for a non-evaluation image: $id" >&2
return 1
fi
local normal="${id::-5}"
local remove_selector="N"
@@ -340,16 +369,25 @@ generateEvalXML() {
local target="/run/assets/$id.xml"
local source="/run/assets/$normal.xml"
removeGeneratedXML "$source" || return 2
removeGeneratedXML "$source" || {
enabled "$DEBUG" && echo "Failed to remove a previously generated evaluation source template: $source" >&2
return 2
}
if [ ! -s "$source" ]; then
source="/run/assets/${normal%%-*}.xml"
removeGeneratedXML "$source" || return 2
removeGeneratedXML "$source" || {
enabled "$DEBUG" && echo "Failed to remove a previously generated family evaluation template: $source" >&2
return 2
}
fi
[ -s "$source" ] || return 1
if [ ! -s "$source" ]; then
enabled "$DEBUG" && echo "No usable source template was found for evaluation image: $id" >&2
return 1
fi
if [ -n "$detected_index" ]; then
remove_selector="Y"
@@ -361,7 +399,10 @@ generateEvalXML() {
esac
fi
generateAnswerFile "$id" "$source" "$target" "$index" "evaluation" "$remove_selector" || return 2
generateAnswerFile "$id" "$source" "$target" "$index" "evaluation" "$remove_selector" || {
enabled "$DEBUG" && echo "Failed to generate the evaluation answer file for $id from $source." >&2
return 2
}
return 0
}
@@ -377,13 +418,25 @@ generateFallbackXML() {
local target="/run/assets/$id.xml"
local source="/run/assets/${id%%-*}.xml"
[ "$source" != "$target" ] || return 1
if [ "$source" = "$target" ]; then
enabled "$DEBUG" && echo "Fallback XML generation has no distinct source template for: $id" >&2
return 1
fi
removeGeneratedXML "$source" || return 2
removeGeneratedXML "$source" || {
enabled "$DEBUG" && echo "Failed to remove a previously generated fallback source template: $source" >&2
return 2
}
[ -s "$source" ] || return 1
if [ ! -s "$source" ]; then
enabled "$DEBUG" && echo "The fallback source template does not exist or is empty: $source" >&2
return 1
fi
generateAnswerFile "$id" "$source" "$target" "$index" "fallback" "Y" || return 2
generateAnswerFile "$id" "$source" "$target" "$index" "fallback" "Y" || {
enabled "$DEBUG" && echo "Failed to generate the fallback answer file for $id from $source." >&2
return 2
}
return 0
}