Skip to content

Commit 771c947

Browse files
committed
Refactor Core
Refactored misc/alpine-install.func to improve error handling, network checks, and MOTD setup. Added misc/alpine-tools.func and misc/error_handler.func for modular tool installation and error management. Enhanced misc/api.func with detailed exit code explanations and telemetry functions. Updated misc/core.func for better initialization, validation, and execution helpers. Removed misc/create_lxc.sh as part of cleanup.
1 parent d0f82d7 commit 771c947

File tree

7 files changed

+1813
-651
lines changed

7 files changed

+1813
-651
lines changed

misc/alpine-install.func

Lines changed: 50 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -7,41 +7,56 @@ if ! command -v curl >/dev/null 2>&1; then
77
apk update && apk add curl >/dev/null 2>&1
88
fi
99
source <(curl -fsSL https://raw.githubusercontent.com/community-scripts/ProxmoxVE/main/misc/core.func)
10+
source <(curl -fsSL https://raw.githubusercontent.com/community-scripts/ProxmoxVE/main/misc/error_handler.func)
1011
load_functions
12+
catch_errors
1113

1214
# This function enables IPv6 if it's not disabled and sets verbose mode
1315
verb_ip6() {
1416
set_std_mode # Set STD mode based on VERBOSE
1517

16-
if [ "$IPV6_METHOD" == "disable" ]; then
17-
msg_info "Disabling IPv6 (this may affect some services)"
18+
if [ "$DISABLEIPV6" == "yes" ]; then
1819
$STD sysctl -w net.ipv6.conf.all.disable_ipv6=1
19-
$STD sysctl -w net.ipv6.conf.default.disable_ipv6=1
20-
$STD sysctl -w net.ipv6.conf.lo.disable_ipv6=1
21-
mkdir -p /etc/sysctl.d
22-
$STD tee /etc/sysctl.d/99-disable-ipv6.conf >/dev/null <<EOF
23-
net.ipv6.conf.all.disable_ipv6 = 1
24-
net.ipv6.conf.default.disable_ipv6 = 1
25-
net.ipv6.conf.lo.disable_ipv6 = 1
26-
EOF
20+
echo "net.ipv6.conf.all.disable_ipv6 = 1" >>/etc/sysctl.conf
2721
$STD rc-update add sysctl default
28-
msg_ok "Disabled IPv6"
2922
fi
3023
}
3124

32-
# This function catches errors and handles them with the error handler function
33-
catch_errors() {
34-
set -Eeuo pipefail
35-
trap 'error_handler $LINENO "$BASH_COMMAND"' ERR
36-
}
25+
set -Eeuo pipefail
26+
trap 'error_handler $? $LINENO "$BASH_COMMAND"' ERR
27+
trap on_exit EXIT
28+
trap on_interrupt INT
29+
trap on_terminate TERM
3730

38-
# This function handles errors
3931
error_handler() {
32+
local exit_code="$1"
33+
local line_number="$2"
34+
local command="$3"
35+
36+
# Exitcode 0 = kein Fehler → ignorieren
37+
if [[ "$exit_code" -eq 0 ]]; then
38+
return 0
39+
fi
40+
41+
printf "\e[?25h"
42+
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"
43+
exit "$exit_code"
44+
}
45+
46+
on_exit() {
4047
local exit_code="$?"
41-
local line_number="$1"
42-
local command="$2"
43-
local error_message="${RD}[ERROR]${CL} in line ${RD}$line_number${CL}: exit code ${RD}$exit_code${CL}: while executing command ${YW}$command${CL}"
44-
echo -e "\n$error_message\n"
48+
[[ -n "${lockfile:-}" && -e "$lockfile" ]] && rm -f "$lockfile"
49+
exit "$exit_code"
50+
}
51+
52+
on_interrupt() {
53+
echo -e "\n${RD}Interrupted by user (SIGINT)${CL}"
54+
exit 130
55+
}
56+
57+
on_terminate() {
58+
echo -e "\n${RD}Terminated by signal (SIGTERM)${CL}"
59+
exit 143
4560
}
4661

4762
# This function sets up the Container OS by generating the locale, setting the timezone, and checking the network connection
@@ -70,10 +85,10 @@ network_check() {
7085
set +e
7186
trap - ERR
7287
if ping -c 1 -W 1 1.1.1.1 &>/dev/null || ping -c 1 -W 1 8.8.8.8 &>/dev/null || ping -c 1 -W 1 9.9.9.9 &>/dev/null; then
73-
msg_ok "Internet Connected"
88+
ipv4_status="${GN}✔${CL} IPv4"
7489
else
75-
msg_error "Internet NOT Connected"
76-
read -r -p "Would you like to continue anyway? <y/N> " prompt
90+
ipv4_status="${RD}✖${CL} IPv4"
91+
read -r -p "Internet NOT connected. Continue anyway? <y/N> " prompt
7792
if [[ "${prompt,,}" =~ ^(y|yes)$ ]]; then
7893
echo -e "${INFO}${RD}Expect Issues Without Internet${CL}"
7994
else
@@ -82,16 +97,20 @@ network_check() {
8297
fi
8398
fi
8499
RESOLVEDIP=$(getent hosts github.com | awk '{ print $1 }')
85-
if [[ -z "$RESOLVEDIP" ]]; then msg_error "DNS Lookup Failure"; else msg_ok "DNS Resolved github.com to ${BL}$RESOLVEDIP${CL}"; fi
100+
if [[ -z "$RESOLVEDIP" ]]; then
101+
msg_error "Internet: ${ipv4_status} DNS Failed"
102+
else
103+
msg_ok "Internet: ${ipv4_status} DNS: ${BL}${RESOLVEDIP}${CL}"
104+
fi
86105
set -e
87106
trap 'error_handler $LINENO "$BASH_COMMAND"' ERR
88107
}
89108

90109
# This function updates the Container OS by running apt-get update and upgrade
91110
update_os() {
92111
msg_info "Updating Container OS"
93-
$STD apk -U upgrade
94-
source <(curl -fsSL https://raw.githubusercontent.com/community-scripts/ProxmoxVE/main/misc/tools.func)
112+
$STD apk update && $STD apk upgrade
113+
source <(curl -fsSL https://raw.githubusercontent.com/community-scripts/ProxmoxVE/main/misc/alpine-tools.func)
95114
msg_ok "Updated Container OS"
96115
}
97116

@@ -112,18 +131,15 @@ motd_ssh() {
112131
echo "echo -e \"\"" >"$PROFILE_FILE"
113132
echo -e "echo -e \"${BOLD}${APPLICATION} LXC Container${CL}"\" >>"$PROFILE_FILE"
114133
echo -e "echo -e \"${TAB}${GATEWAY}${YW} Provided by: ${GN}community-scripts ORG ${YW}| GitHub: ${GN}https://github.com/community-scripts/ProxmoxVE${CL}\"" >>"$PROFILE_FILE"
134+
echo -e "echo -e \"${YW} OS: ${GN}${OS_NAME} - Version: ${OS_VERSION}${CL}\"" >>"$PROFILE_FILE"
135+
echo -e "echo -e \"${YW} Hostname: ${GN}\$(hostname)${CL}\"" >>"$PROFILE_FILE"
136+
echo -e "echo -e \"${YW} IP Address: ${GN}${IP}${CL}\"" >>"$PROFILE_FILE"
137+
echo -e "echo -e \"${YW} Repository: ${GN}https://github.com/community-scripts/ProxmoxVE${CL}\"" >>"$PROFILE_FILE"
115138
echo "echo \"\"" >>"$PROFILE_FILE"
116-
echo -e "echo -e \"${TAB}${OS}${YW} OS: ${GN}${OS_NAME} - Version: ${OS_VERSION}${CL}\"" >>"$PROFILE_FILE"
117-
echo -e "echo -e \"${TAB}${HOSTNAME}${YW} Hostname: ${GN}\$(hostname)${CL}\"" >>"$PROFILE_FILE"
118-
echo -e "echo -e \"${TAB}${INFO}${YW} IP Address: ${GN}\$(ip -4 addr show eth0 | awk '/inet / {print \$2}' | cut -d/ -f1 | head -n 1)${CL}\"" >>"$PROFILE_FILE"
119139

120-
# Configure SSH if enabled
121140
if [[ "${SSH_ROOT}" == "yes" ]]; then
122-
# Enable sshd service
123141
$STD rc-update add sshd
124-
# Allow root login via SSH
125142
sed -i "s/#PermitRootLogin prohibit-password/PermitRootLogin yes/g" /etc/ssh/sshd_config
126-
# Start the sshd service
127143
$STD /etc/init.d/sshd start
128144
fi
129145
}
@@ -162,11 +178,4 @@ EOF
162178

163179
echo "bash -c \"\$(curl -fsSL https://raw.githubusercontent.com/community-scripts/ProxmoxVE/main/ct/${app}.sh)\"" >/usr/bin/update
164180
chmod +x /usr/bin/update
165-
166-
if [[ -n "${SSH_AUTHORIZED_KEY}" ]]; then
167-
mkdir -p /root/.ssh
168-
echo "${SSH_AUTHORIZED_KEY}" >/root/.ssh/authorized_keys
169-
chmod 700 /root/.ssh
170-
chmod 600 /root/.ssh/authorized_keys
171-
fi
172181
}

0 commit comments

Comments
 (0)