Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

[API] Update build.func / Improve error messages #2 #2050

Merged
merged 4 commits into from
Feb 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions misc/api.func
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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 <<EOF
{
Expand Down
38 changes: 33 additions & 5 deletions misc/build.func
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ variables() {
METHOD="default" # sets the METHOD variable to "default", used for the API call.
RANDOM_UUID="$(cat /proc/sys/kernel/random/uuid)" # generates a random UUID and sets it to the RANDOM_UUID variable.
}

source <(curl -s https://raw.githubusercontent.com/community-scripts/ProxmoxVE/main/misc/api.func)

# This function sets various color variables using ANSI escape codes for formatting text in the terminal.
Expand Down Expand Up @@ -68,12 +69,14 @@ catch_errors() {

# 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.
error_handler() {
source /dev/stdin <<< $(wget -qLO - https://raw.githubusercontent.com/community-scripts/ProxmoxVE/main/misc/api.func)
if [ -n "$SPINNER_PID" ] && ps -p $SPINNER_PID > /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"
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 $?

}

Expand Down Expand Up @@ -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