diff --git a/misc/api.func b/misc/api.func index b1faa0c2a..b9ab5b4de 100644 --- a/misc/api.func +++ b/misc/api.func @@ -42,14 +42,11 @@ EOF RESPONSE=$(curl -s -o response.txt -w "%{http_code}" -L -X POST "$API_URL" --post301 --post302 \ -H "Content-Type: application/json" \ -d "$JSON_PAYLOAD") || true - - } post_to_api_vm() { DIAGNOSTICS=$(grep -i "^DIAGNOSTICS=" /usr/local/community-scripts/diagnostics | awk -F'=' '{print $2}') - if ! command -v curl &> /dev/null; then return fi @@ -91,7 +88,6 @@ EOF RESPONSE=$(curl -s -o response.txt -w "%{http_code}" -L -X POST "$API_URL" --post301 --post302 \ -H "Content-Type: application/json" \ -d "$JSON_PAYLOAD") || true - } POST_UPDATE_DONE=false @@ -106,7 +102,7 @@ post_update_to_api() { fi local API_URL="http://api.community-scripts.org/upload/updatestatus" local status="${1:-failed}" - local error="${2:-unknown}" + local error="${2:-No error message}" JSON_PAYLOAD=$(cat < /dev/null; then kill $SPINNER_PID > /dev/null; fi printf "\e[?25h" local exit_code="$?" local line_number="$1" local command="$2" local error_message="${RD}[ERROR]${CL} in line ${RD}$line_number${CL}: exit code ${RD}$exit_code${CL}: while executing command ${YW}$command${CL}" + post_update_to_api "failed" "${command}" echo -e "\n$error_message\n" } @@ -1054,7 +1057,7 @@ build_container() { $PW " # This executes create_lxc.sh and creates the container and .conf file - bash -c "$(wget -qLO - https://raw.githubusercontent.com/community-scripts/ProxmoxVE/main/ct/create_lxc.sh)" || exit + bash -c "$(wget -qLO - https://raw.githubusercontent.com/community-scripts/ProxmoxVE/main/ct/create_lxc.sh)" || exit $? LXC_CONFIG=/etc/pve/lxc/${CTID}.conf if [ "$CT_TYPE" == "0" ]; then @@ -1116,7 +1119,7 @@ http://dl-cdn.alpinelinux.org/alpine/latest-stable/community EOF' pct exec "$CTID" -- ash -c "apk add bash >/dev/null" fi - lxc-attach -n "$CTID" -- bash -c "$(wget -qLO - https://raw.githubusercontent.com/community-scripts/ProxmoxVE/main/install/$var_install.sh)" || exit + lxc-attach -n "$CTID" -- bash -c "$(wget -qLO - https://raw.githubusercontent.com/community-scripts/ProxmoxVE/main/install/$var_install.sh)" || exit $? } @@ -1166,7 +1169,32 @@ EOF post_update_to_api "done" "none" } -trap 'post_update_to_api "failed" "unknown error"' EXIT -trap 'post_update_to_api "failed" "SIG INTERUPT"' SIGINT -trap 'post_update_to_api "failed" "SIG TERM"' SIGTERM + +exit_script() { + exit_code=$? # Capture the exit status of the last executed command + #200 exit codes indicate error in create_lxc.sh + #100 exit codes indicate error in install.func + +if [ $exit_code -ne 0 ]; then # Check if exit code is nonzero + case $exit_code in + 200) post_update_to_api "failed" "create_lxc.sh: Error during LXC creation" ;; + 201) post_update_to_api "failed" "create_lxc.sh Invalid Storage class" ;; + 202) post_update_to_api "failed" "create_lxc.sh Invalid Menu aborted" ;; + 203) post_update_to_api "failed" "create_lxc.sh CTID was unset" ;; + 204) post_update_to_api "failed" "create_lxc.sh PCT_OSTYPE was unset" ;; + 205) post_update_to_api "failed" "create_lxc.sh ID cannot be less than 100" ;; + 206) post_update_to_api "failed" "create_lxc.sh ID already in use" ;; + 207) post_update_to_api "failed" "create_lxc.sh Template not found" ;; + 208) post_update_to_api "failed" "create_lxc.sh Error downloading template" ;; + 101) post_update_to_api "failed" "create_lxc.sh No Network connection" ;; + *) post_update_to_api "failed" "Unknown error, exit code: $exit_code" ;; + esac +fi +} + + +trap 'exit_script' EXIT +trap 'post_update_to_api "failed" "$BASH_COMMAND"' ERR +trap 'post_update_to_api "failed" "INTERRUPTED"' SIGINT +trap 'post_update_to_api "failed" "TERMINATED"' SIGTERM