Implement graceful cancellation for cloud-init image compression</message>
<message>Add a cleanup function to handle cancellation of the xz compression process in the build-cloudinit-image.sh script. This enhancement allows for a more robust response to cancellation requests, ensuring that resources are properly released and status messages are updated accordingly. The script now traps termination signals and cleans up temporary files, improving the overall reliability of the cloud-init image building workflow.
This commit is contained in:
@@ -193,21 +193,31 @@ cp "$IMG_FILE" "$OUT_PATH"
|
||||
check_cancel
|
||||
write_status "finalizing" "Compressing image (xz)…" "" ""
|
||||
OUT_XZ="${OUT_PATH}.xz"
|
||||
XZ_PID=""
|
||||
cleanup_xz_and_exit() {
|
||||
local phase="${1:-cancelled}"
|
||||
if [[ -n "$XZ_PID" ]] && kill -0 "$XZ_PID" 2>/dev/null; then
|
||||
kill "$XZ_PID" 2>/dev/null
|
||||
wait "$XZ_PID" 2>/dev/null || true
|
||||
fi
|
||||
write_status "$phase" "Build cancelled." "" ""
|
||||
rm -f "$REQUEST_FILE" "$CANCEL_FILE"
|
||||
exit 0
|
||||
}
|
||||
trap 'cleanup_xz_and_exit "cancelled"' TERM INT
|
||||
xz -T 0 -z -k -f "$OUT_PATH" 2>/dev/null &
|
||||
XZ_PID=$!
|
||||
while kill -0 "$XZ_PID" 2>/dev/null; do
|
||||
if [[ -f "$CANCEL_FILE" ]]; then
|
||||
kill "$XZ_PID" 2>/dev/null
|
||||
wait "$XZ_PID" 2>/dev/null
|
||||
write_status "cancelled" "Build cancelled." "" ""
|
||||
rm -f "$REQUEST_FILE" "$CANCEL_FILE"
|
||||
exit 0
|
||||
cleanup_xz_and_exit "cancelled"
|
||||
fi
|
||||
sleep 2
|
||||
done
|
||||
wait "$XZ_PID" 2>/dev/null || true
|
||||
trap - TERM INT
|
||||
if [[ ! -f "$OUT_XZ" ]]; then
|
||||
xz -T 1 -z -k -f "$OUT_PATH" 2>/dev/null || true
|
||||
|
||||
fi
|
||||
if [[ -f "$OUT_XZ" ]]; then
|
||||
rm -f "$OUT_PATH"
|
||||
|
||||
Reference in New Issue
Block a user