Skip to content

Commit 5451e61

Browse files
[API] Update create-lxc.sh / Improve error messages #1 (#2049)
* update create-lxc.sh * [core] Ignore create_lxc.sh at validate filename (#2059) * Update validate-filenames.yml --------- Co-authored-by: CanbiZ <[email protected]>
1 parent 778b6be commit 5451e61

File tree

2 files changed

+19
-10
lines changed

2 files changed

+19
-10
lines changed

.github/workflows/validate-filenames.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,13 @@ jobs:
5050
5151
NON_COMPLIANT_FILES=""
5252
for FILE in $CHANGED_FILES; do
53+
# Datei "ct/create_lxc.sh" explizit überspringen
54+
if [[ "$FILE" == "ct/create_lxc.sh" ]]; then
55+
continue
56+
fi
5357
BASENAME=$(echo "$(basename "${FILE%.*}")")
5458
if [[ ! "$BASENAME" =~ ^[a-z0-9-]+$ ]]; then
55-
NON_COMPLIANT_FILES="$NON_COMPLIANT_FILES $FILE"
59+
NON_COMPLIANT_FILES="$NON_COMPLIANT_FILES $FILE"
5660
fi
5761
done
5862

ct/create_lxc.sh

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ function error_handler() {
4343
local command="$2"
4444
local error_message="${RD}[ERROR]${CL} in line ${RD}$line_number${CL}: exit code ${RD}$exit_code${CL}: while executing command ${YW}$command${CL}"
4545
echo -e "\n$error_message\n"
46+
exit 200
4647
}
4748

4849
# This function displays a spinner.
@@ -112,7 +113,7 @@ function select_storage() {
112113
CONTENT='vztmpl'
113114
CONTENT_LABEL='Container template'
114115
;;
115-
*) false || exit "Invalid storage class." ;;
116+
*) false || { msg_error "Invalid storage class."; exit 201; };
116117
esac
117118

118119
# This Queries all storage locations
@@ -138,7 +139,7 @@ function select_storage() {
138139
STORAGE=$(whiptail --backtitle "Proxmox VE Helper Scripts" --title "Storage Pools" --radiolist \
139140
"Which storage pool you would like to use for the ${CONTENT_LABEL,,}?\nTo make a selection, use the Spacebar.\n" \
140141
16 $(($MSG_MAX_LENGTH + 23)) 6 \
141-
"${MENU[@]}" 3>&1 1>&2 2>&3) || exit "Menu aborted."
142+
"${MENU[@]}" 3>&1 1>&2 2>&3) || { msg_error "Menu aborted."; exit 202; }
142143
if [ $? -ne 0 ]; then
143144
echo -e "${CROSS}${RD} Menu aborted by user.${CL}"
144145
exit 0
@@ -148,17 +149,18 @@ function select_storage() {
148149
fi
149150
}
150151
# Test if required variables are set
151-
[[ "${CTID:-}" ]] || exit "You need to set 'CTID' variable."
152-
[[ "${PCT_OSTYPE:-}" ]] || exit "You need to set 'PCT_OSTYPE' variable."
152+
[[ "${CTID:-}" ]] || { msg_error "You need to set 'CTID' variable."; exit 203; }
153+
[[ "${PCT_OSTYPE:-}" ]] || { msg_error "You need to set 'PCT_OSTYPE' variable."; exit 204; }
153154

154155
# Test if ID is valid
155-
[ "$CTID" -ge "100" ] || exit "ID cannot be less than 100."
156+
[ "$CTID" -ge "100" ] || { msg_error "ID cannot be less than 100."; exit 205; }
156157

157158
# Test if ID is in use
158159
if pct status $CTID &>/dev/null; then
159160
echo -e "ID '$CTID' is already in use."
160161
unset CTID
161-
exit "Cannot use ID that is already in use."
162+
msg_error "Cannot use ID that is already in use."
163+
exit 206
162164
fi
163165

164166
# Get template storage
@@ -177,14 +179,15 @@ msg_ok "Updated LXC Template List"
177179
# Get LXC template string
178180
TEMPLATE_SEARCH=${PCT_OSTYPE}-${PCT_OSVERSION:-}
179181
mapfile -t TEMPLATES < <(pveam available -section system | sed -n "s/.*\($TEMPLATE_SEARCH.*\)/\1/p" | sort -t - -k 2 -V)
180-
[ ${#TEMPLATES[@]} -gt 0 ] || exit "Unable to find a template when searching for '$TEMPLATE_SEARCH'."
182+
[ ${#TEMPLATES[@]} -gt 0 ] || { msg_error "Unable to find a template when searching for '$TEMPLATE_SEARCH'."; exit 207; }
181183
TEMPLATE="${TEMPLATES[-1]}"
182184

183185
# Download LXC template if needed
184186
if ! pveam list $TEMPLATE_STORAGE | grep -q $TEMPLATE; then
185187
msg_info "Downloading LXC Template"
186188
pveam download $TEMPLATE_STORAGE $TEMPLATE >/dev/null ||
187-
exit "A problem occured while downloading the LXC template."
189+
msg_error "A problem occured while downloading the LXC template."
190+
exit 208
188191
msg_ok "Downloaded LXC Template"
189192
fi
190193

@@ -198,5 +201,7 @@ PCT_OPTIONS=(${PCT_OPTIONS[@]:-${DEFAULT_PCT_OPTIONS[@]}})
198201
# Create container
199202
msg_info "Creating LXC Container"
200203
pct create $CTID ${TEMPLATE_STORAGE}:vztmpl/${TEMPLATE} ${PCT_OPTIONS[@]} >/dev/null ||
201-
exit "A problem occured while trying to create container."
204+
msg_error "A problem occured while trying to create container."
205+
exit 200
202206
msg_ok "LXC Container ${BL}$CTID${CL} ${GN}was successfully created."
207+

0 commit comments

Comments
 (0)