error_handler
This commit is contained in:
parent
569384b5d0
commit
837108265a
@ -21,19 +21,41 @@ verb_ip6() {
|
|||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
# This function catches errors and handles them with the error handler function
|
|
||||||
catch_errors() {
|
|
||||||
set -Eeuo pipefail
|
set -Eeuo pipefail
|
||||||
trap 'error_handler $LINENO "$BASH_COMMAND"' ERR
|
trap 'error_handler $? $LINENO "$BASH_COMMAND"' ERR
|
||||||
|
trap on_exit EXIT
|
||||||
|
trap on_interrupt INT
|
||||||
|
trap on_terminate TERM
|
||||||
|
|
||||||
|
error_handler() {
|
||||||
|
local exit_code="$1"
|
||||||
|
local line_number="$2"
|
||||||
|
local command="$3"
|
||||||
|
|
||||||
|
# Exitcode 0 = kein Fehler → ignorieren
|
||||||
|
if [[ "$exit_code" -eq 0 ]]; then
|
||||||
|
return 0
|
||||||
|
fi
|
||||||
|
|
||||||
|
printf "\e[?25h"
|
||||||
|
echo -e "\n${RD}[ERROR]${CL} in line ${RD}$line_number${CL}: exit code ${RD}$exit_code${CL}: while executing command ${YW}$command${CL}\n"
|
||||||
|
exit "$exit_code"
|
||||||
}
|
}
|
||||||
|
|
||||||
# This function handles errors
|
on_exit() {
|
||||||
error_handler() {
|
|
||||||
local exit_code="$?"
|
local exit_code="$?"
|
||||||
local line_number="$1"
|
[[ -n "${lockfile:-}" && -e "$lockfile" ]] && rm -f "$lockfile"
|
||||||
local command="$2"
|
exit "$exit_code"
|
||||||
local error_message="${RD}[ERROR]${CL} in line ${RD}$line_number${CL}: exit code ${RD}$exit_code${CL}: while executing command ${YW}$command${CL}"
|
}
|
||||||
echo -e "\n$error_message\n"
|
|
||||||
|
on_interrupt() {
|
||||||
|
echo -e "\n${RD}Interrupted by user (SIGINT)${CL}"
|
||||||
|
exit 130
|
||||||
|
}
|
||||||
|
|
||||||
|
on_terminate() {
|
||||||
|
echo -e "\n${RD}Terminated by signal (SIGTERM)${CL}"
|
||||||
|
exit 143
|
||||||
}
|
}
|
||||||
|
|
||||||
# This function sets up the Container OS by generating the locale, setting the timezone, and checking the network connection
|
# This function sets up the Container OS by generating the locale, setting the timezone, and checking the network connection
|
||||||
|
|||||||
@ -28,34 +28,41 @@ elif command -v wget >/dev/null 2>&1; then
|
|||||||
#echo "(build.func) Loaded core.func via wget"
|
#echo "(build.func) Loaded core.func via wget"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# This function enables error handling in the script by setting options and defining a trap for the ERR signal.
|
set -Eeuo pipefail
|
||||||
catch_errors() {
|
trap 'error_handler $? $LINENO "$BASH_COMMAND"' ERR
|
||||||
set -Eeo pipefail
|
trap on_exit EXIT
|
||||||
# if [ -n "$BASH_VERSION" ] && command -v shopt >/dev/null 2>&1; then
|
trap on_interrupt INT
|
||||||
# shopt -s errtrace
|
trap on_terminate TERM
|
||||||
# fi
|
|
||||||
trap 'error_handler $LINENO "$BASH_COMMAND"' ERR
|
error_handler() {
|
||||||
|
local exit_code="$1"
|
||||||
|
local line_number="$2"
|
||||||
|
local command="$3"
|
||||||
|
|
||||||
|
# Exitcode 0 = kein Fehler → ignorieren
|
||||||
|
if [[ "$exit_code" -eq 0 ]]; then
|
||||||
|
return 0
|
||||||
|
fi
|
||||||
|
|
||||||
|
printf "\e[?25h"
|
||||||
|
echo -e "\n${RD}[ERROR]${CL} in line ${RD}$line_number${CL}: exit code ${RD}$exit_code${CL}: while executing command ${YW}$command${CL}\n"
|
||||||
|
exit "$exit_code"
|
||||||
}
|
}
|
||||||
|
|
||||||
# This function is called when an error occurs. It receives the exit code, line number, and command that caused the error, and displays an error message.
|
on_exit() {
|
||||||
error_handler() {
|
|
||||||
local exit_code="$?"
|
local exit_code="$?"
|
||||||
local line_number="$1"
|
[[ -n "${lockfile:-}" && -e "$lockfile" ]] && rm -f "$lockfile"
|
||||||
local command="$2"
|
|
||||||
printf "\e[?25h"
|
|
||||||
local error_message="[ERROR] in line $line_number: exit code $exit_code: while executing command $command"
|
|
||||||
post_update_to_api "failed" "$command"
|
|
||||||
echo -e "\n$error_message\n"
|
|
||||||
exit "$exit_code"
|
exit "$exit_code"
|
||||||
|
}
|
||||||
|
|
||||||
if [[ -n "$CT_ID" ]]; then
|
on_interrupt() {
|
||||||
read -p "Remove this Container? <y/N> " prompt
|
echo -e "\n${RD}Interrupted by user (SIGINT)${CL}"
|
||||||
if [[ "${prompt,,}" =~ ^(y|yes)$ ]]; then
|
exit 130
|
||||||
pct stop "$CT_ID" &>/dev/null
|
}
|
||||||
pct destroy "$CT_ID" &>/dev/null
|
|
||||||
msg_ok "Removed this Container"
|
on_terminate() {
|
||||||
fi
|
echo -e "\n${RD}Terminated by signal (SIGTERM)${CL}"
|
||||||
fi
|
exit 143
|
||||||
}
|
}
|
||||||
|
|
||||||
# Check if the shell is using bash
|
# Check if the shell is using bash
|
||||||
|
|||||||
@ -25,34 +25,42 @@ fi
|
|||||||
# Strict error handling
|
# Strict error handling
|
||||||
# ------------------------------------------------------------------------------
|
# ------------------------------------------------------------------------------
|
||||||
set -Eeuo pipefail
|
set -Eeuo pipefail
|
||||||
trap 'error_handler $LINENO "$BASH_COMMAND"' ERR
|
trap 'error_handler $? $LINENO "$BASH_COMMAND"' ERR
|
||||||
trap on_exit EXIT
|
trap on_exit EXIT
|
||||||
trap on_interrupt INT
|
trap on_interrupt INT
|
||||||
trap on_terminate TERM
|
trap on_terminate TERM
|
||||||
|
|
||||||
|
error_handler() {
|
||||||
|
local exit_code="$1"
|
||||||
|
local line_number="$2"
|
||||||
|
local command="$3"
|
||||||
|
|
||||||
|
# Exitcode 0 = kein Fehler → ignorieren
|
||||||
|
if [[ "$exit_code" -eq 0 ]]; then
|
||||||
|
return 0
|
||||||
|
fi
|
||||||
|
|
||||||
|
printf "\e[?25h"
|
||||||
|
echo -e "\n${RD}[ERROR]${CL} in line ${RD}$line_number${CL}: exit code ${RD}$exit_code${CL}: while executing command ${YW}$command${CL}\n"
|
||||||
|
exit "$exit_code"
|
||||||
|
}
|
||||||
|
|
||||||
on_exit() {
|
on_exit() {
|
||||||
local exit_code="$?"
|
local exit_code="$?"
|
||||||
[[ -n "${lockfile:-}" && -e "$lockfile" ]] && rm -f "$lockfile"
|
[[ -n "${lockfile:-}" && -e "$lockfile" ]] && rm -f "$lockfile"
|
||||||
exit "$exit_code"
|
exit "$exit_code"
|
||||||
}
|
}
|
||||||
|
|
||||||
error_handler() {
|
|
||||||
local exit_code="$?"
|
|
||||||
local line_number="$1"
|
|
||||||
local command="$2"
|
|
||||||
printf "\e[?25h"
|
|
||||||
echo -e "\n${RD}[ERROR]${CL} in line ${RD}$line_number${CL}: exit code ${RD}$exit_code${CL}: while executing command ${YW}$command${CL}\n"
|
|
||||||
exit "$exit_code"
|
|
||||||
}
|
|
||||||
|
|
||||||
on_interrupt() {
|
on_interrupt() {
|
||||||
echo -e "\n${RD}Interrupted by user (SIGINT)${CL}"
|
echo -e "\n${RD}Interrupted by user (SIGINT)${CL}"
|
||||||
exit 130
|
exit 130
|
||||||
}
|
}
|
||||||
|
|
||||||
on_terminate() {
|
on_terminate() {
|
||||||
echo -e "\n${RD}Terminated by signal (SIGTERM)${CL}"
|
echo -e "\n${RD}Terminated by signal (SIGTERM)${CL}"
|
||||||
exit 143
|
exit 143
|
||||||
}
|
}
|
||||||
|
|
||||||
exit_script() {
|
exit_script() {
|
||||||
clear
|
clear
|
||||||
printf "\e[?25h"
|
printf "\e[?25h"
|
||||||
|
|||||||
@ -576,5 +576,29 @@ if [ "$START_VM" == "yes" ]; then
|
|||||||
msg_ok "Started Debian 13 VM"
|
msg_ok "Started Debian 13 VM"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
msg_info "Installing resize tools in VM"
|
||||||
|
qm set "$VMID" --sshkeys ~/.ssh/id_rsa.pub >/dev/null 2>&1 || true
|
||||||
|
qm exec "$VMID" -- bash -c "apt-get update && apt-get install -y cloud-guest-utils e2fsprogs xfsprogs"
|
||||||
|
msg_ok "Installed resize tools in VM"
|
||||||
|
|
||||||
|
msg_info "Injecting auto-resize script into VM"
|
||||||
|
cat <<'EOF' >/var/lib/vz/snippets/resize-root.sh
|
||||||
|
#!/bin/bash
|
||||||
|
set -e
|
||||||
|
DEVICE=$(lsblk -no pkname $(findmnt -n -o SOURCE /))
|
||||||
|
PART=$(findmnt -n -o SOURCE / | sed 's|/dev/||')
|
||||||
|
growpart /dev/${DEVICE} ${PART##*[a-z]}
|
||||||
|
FSTYPE=$(findmnt -n -o FSTYPE /)
|
||||||
|
if [ "$FSTYPE" = "ext4" ]; then
|
||||||
|
resize2fs /dev/$PART
|
||||||
|
elif [ "$FSTYPE" = "xfs" ]; then
|
||||||
|
xfs_growfs /
|
||||||
|
fi
|
||||||
|
EOF
|
||||||
|
chmod +x /var/lib/vz/snippets/resize-root.sh
|
||||||
|
|
||||||
|
qm set "$VMID" --hookscript local:snippets/resize-root.sh >/dev/null
|
||||||
|
msg_ok "Injected auto-resize script"
|
||||||
|
|
||||||
msg_ok "Completed Successfully!\n"
|
msg_ok "Completed Successfully!\n"
|
||||||
echo "More Info at https://github.com/community-scripts/ProxmoxVE/discussions/836"
|
echo "More Info at https://github.com/community-scripts/ProxmoxVE/discussions/836"
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user