Skip to content

Commit 86d67af

Browse files
authored
Refactor: harmonize tools/addon and move boilerplate into core.func (#16202)
1 parent e489c7f commit 86d67af

35 files changed

Lines changed: 1170 additions & 1382 deletions

.github/workflows/auto-update-app-headers.yml

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.github/workflows/scripts/generate-app-headers.sh

Lines changed: 3 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

misc/core.func

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,72 @@ root_check() {
299299
fi
300300
}
301301

302+
# ------------------------------------------------------------------------------
303+
# on_pve_host()
304+
#
305+
# - Returns 0 when executed on a Proxmox VE node, 1 otherwise
306+
# - Cheap probe used by the guards below; never exits on its own
307+
# ------------------------------------------------------------------------------
308+
on_pve_host() {
309+
command -v pveversion &>/dev/null
310+
}
311+
312+
# ------------------------------------------------------------------------------
313+
# require_pve_host()
314+
#
315+
# - Guard for tools that drive the node itself (pct/pveam/pvesm/pvesh/qm)
316+
# - Aborts when not on a PVE node, then validates the PVE version
317+
# ------------------------------------------------------------------------------
318+
require_pve_host() {
319+
if ! on_pve_host; then
320+
msg_error "${APP:-This script} must be run on the Proxmox VE host."
321+
msg_error "Proxmox tooling (pct/pveam/pvesm/qm) is not available here."
322+
exit 232
323+
fi
324+
pve_check
325+
}
326+
327+
# ------------------------------------------------------------------------------
328+
# confirm_not_pve_host()
329+
#
330+
# - Guard for tools that belong inside an LXC/VM but would technically run
331+
# on the node as well
332+
# - Warns and asks for confirmation instead of hard-failing
333+
# ------------------------------------------------------------------------------
334+
confirm_not_pve_host() {
335+
on_pve_host || return 0
336+
337+
msg_error "Running on the Proxmox VE host is NOT recommended!"
338+
msg_error "${APP:-This script} is meant to be executed inside an LXC container."
339+
echo ""
340+
echo -n "${TAB:- }Continue anyway? (y/N): "
341+
local confirm
342+
read -r confirm
343+
if [[ ! "${confirm,,}" =~ ^(y|yes)$ ]]; then
344+
msg_warn "Aborted. Please run this inside an LXC container."
345+
exit 0
346+
fi
347+
msg_warn "Proceeding on the Proxmox VE host at your own risk!"
348+
}
349+
350+
# ------------------------------------------------------------------------------
351+
# require_debian_like()
352+
#
353+
# - Guard for addons that have no Alpine code path
354+
# - Aborts early instead of failing halfway through with apt/systemd errors
355+
# ------------------------------------------------------------------------------
356+
require_debian_like() {
357+
if is_alpine; then
358+
msg_error "${APP:-This script} does not support Alpine Linux."
359+
msg_error "Please use a Debian or Ubuntu based LXC container."
360+
exit 238
361+
fi
362+
if ! command -v apt-get &>/dev/null; then
363+
msg_error "${APP:-This script} requires a Debian or Ubuntu based system."
364+
exit 238
365+
fi
366+
}
367+
302368
# ------------------------------------------------------------------------------
303369
# pve_check()
304370
#
@@ -308,6 +374,10 @@ root_check() {
308374
# ------------------------------------------------------------------------------
309375
pve_check() {
310376
local PVE_VER
377+
if ! on_pve_host; then
378+
msg_error "${APP:-This script} must be run on the Proxmox VE host."
379+
exit 232
380+
fi
311381
PVE_VER="$(pveversion | awk -F'/' '{print $2}' | awk -F'-' '{print $1}')"
312382

313383
# Check for Proxmox VE 8.x: allow 8.0–8.9

misc/tools.func

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4628,6 +4628,53 @@ _docker_is_noninteractive() {
46284628
[[ "${DOCKER_NONINTERACTIVE:-}" == "1" || "${DOCKER_NONINTERACTIVE:-}" == "true" || "${DOCKER_NONINTERACTIVE:-}" == "TRUE" ]] || [[ ! -t 0 ]]
46294629
}
46304630
4631+
# ------------------------------------------------------------------------------
4632+
# ensure_docker()
4633+
#
4634+
# - Docker precondition for addon scripts that ship a compose stack
4635+
# - Reuses an existing installation, otherwise offers to install Docker
4636+
# (Debian/Ubuntu via setup_docker, Alpine via apk)
4637+
# - Aborts when the user declines or Compose is unavailable afterwards
4638+
# ------------------------------------------------------------------------------
4639+
ensure_docker() {
4640+
if command -v docker &>/dev/null; then
4641+
msg_ok "Docker $(docker --version | cut -d' ' -f3 | tr -d ',') is available"
4642+
if docker compose version &>/dev/null; then
4643+
msg_ok "Docker Compose is available"
4644+
return 0
4645+
fi
4646+
msg_error "Docker Compose plugin is not available. Please install it."
4647+
exit 237
4648+
fi
4649+
4650+
msg_warn "Docker is not installed."
4651+
echo -n "${TAB:- }Install Docker now? (y/N): "
4652+
local install_docker_prompt
4653+
read -r install_docker_prompt
4654+
if [[ ! "${install_docker_prompt,,}" =~ ^(y|yes)$ ]]; then
4655+
msg_error "Docker is required for ${APP:-this script}. Exiting."
4656+
exit 254
4657+
fi
4658+
4659+
if is_alpine; then
4660+
msg_info "Installing Docker"
4661+
$STD apk add --no-cache docker docker-cli-compose
4662+
$STD rc-update add docker default
4663+
$STD rc-service docker start
4664+
msg_ok "Installed Docker"
4665+
else
4666+
setup_docker || {
4667+
msg_error "Docker installation failed."
4668+
exit 237
4669+
}
4670+
fi
4671+
4672+
if ! command -v docker &>/dev/null || ! docker compose version &>/dev/null; then
4673+
msg_error "Docker or the Compose plugin is still unavailable after installation."
4674+
exit 237
4675+
fi
4676+
}
4677+
46314678
setup_docker() {
46324679
local docker_installed=false
46334680
local USE_DOCKER_REPO="${USE_DOCKER_REPO:-true}"

tools/addon/actual-budget-prometheus-exporter.sh

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,13 @@
77

88
if ! command -v curl &>/dev/null; then
99
printf "\r\e[2K%b" '\033[93m Setup Source \033[m' >&2
10-
apt-get update >/dev/null 2>&1
11-
apt-get install -y curl >/dev/null 2>&1
10+
if [[ -f /etc/alpine-release ]]; then
11+
apk update >/dev/null 2>&1
12+
apk add --no-cache curl >/dev/null 2>&1
13+
else
14+
apt-get update >/dev/null 2>&1
15+
apt-get install -y curl >/dev/null 2>&1
16+
fi
1217
fi
1318
source <(curl -fsSL https://raw.githubusercontent.com/community-scripts/ProxmoxVE/main/misc/core.func)
1419
source <(curl -fsSL https://raw.githubusercontent.com/community-scripts/ProxmoxVE/main/misc/tools.func)
@@ -20,6 +25,7 @@ declare -f init_tool_telemetry &>/dev/null && init_tool_telemetry "actual-budget
2025
set -Eeuo pipefail
2126
trap 'error_handler' ERR
2227
load_functions
28+
require_debian_like
2329

2430
# ==============================================================================
2531
# CONFIGURATION
@@ -31,14 +37,6 @@ INSTALL_PATH="/opt/actual-budget-prometheus-exporter"
3137
CONFIG_PATH="/opt/actual-budget-prometheus-exporter.env"
3238
SERVICE_PATH="/etc/systemd/system/actual-budget-prometheus-exporter.service"
3339

34-
# ==============================================================================
35-
# OS DETECTION
36-
# ==============================================================================
37-
if ! grep -qE 'ID=debian|ID=ubuntu' /etc/os-release 2>/dev/null; then
38-
echo -e "${CROSS} Unsupported OS detected. This script only supports Debian and Ubuntu."
39-
exit 238
40-
fi
41-
4240
# ==============================================================================
4341
# UNINSTALL
4442
# ==============================================================================

tools/addon/add-netbird-lxc.sh

Lines changed: 56 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -6,39 +6,45 @@
66
# License: MIT | https://github.com/community-scripts/ProxmoxVE/raw/main/LICENSE
77
# Source: https://netbird.io/ | Github: https://github.com/netbirdio/netbird
88

9-
function header_info {
10-
clear
11-
cat <<"EOF"
12-
_ __ __ ____ _ __
13-
/ | / /__ / /_/ __ )(_)________/ /
14-
/ |/ / _ \/ __/ __ / / ___/ __ /
15-
/ /| / __/ /_/ /_/ / / / / /_/ /
16-
/_/ |_/\___/\__/_____/_/_/ \__,_/
9+
APP="add-netbird-lxc"
10+
APP_TYPE="addon"
1711

18-
EOF
19-
}
20-
header_info
21-
set -e
22-
23-
# Telemetry
12+
if ! command -v curl &>/dev/null; then
13+
printf "\r\e[2K%b" '\033[93m Setup Source \033[m' >&2
14+
if [[ -f /etc/alpine-release ]]; then
15+
apk update >/dev/null 2>&1
16+
apk add --no-cache curl >/dev/null 2>&1
17+
else
18+
apt-get update >/dev/null 2>&1
19+
apt-get install -y curl >/dev/null 2>&1
20+
fi
21+
fi
22+
source <(curl -fsSL https://raw.githubusercontent.com/community-scripts/ProxmoxVE/main/misc/core.func)
23+
source <(curl -fsSL https://raw.githubusercontent.com/community-scripts/ProxmoxVE/main/misc/tools.func)
24+
source <(curl -fsSL https://raw.githubusercontent.com/community-scripts/ProxmoxVE/main/misc/error_handler.func)
2425
source <(curl -fsSL https://raw.githubusercontent.com/community-scripts/ProxmoxVE/main/misc/api.func) 2>/dev/null || true
2526
declare -f init_tool_telemetry &>/dev/null && init_tool_telemetry "add-netbird-lxc" "addon"
2627

28+
# Enable error handling
29+
set -Eeuo pipefail
30+
trap 'error_handler' ERR
31+
32+
# Initialize all core functions (colors, formatting, icons, STD mode)
33+
load_functions
34+
35+
header_info
36+
require_pve_host
37+
2738
while true; do
28-
read -p "This will add NetBird to an existing LXC Container ONLY. Proceed(y/n)?" yn
39+
read -r -p "This will add NetBird to an existing LXC Container ONLY. Proceed(y/n)? " yn
2940
case $yn in
3041
[Yy]*) break ;;
31-
[Nn]*) exit ;;
42+
[Nn]*) exit 0 ;;
3243
*) echo "Please answer yes or no." ;;
3344
esac
3445
done
35-
header_info
36-
echo "Loading..."
3746

38-
function msg() {
39-
local TEXT="$1"
40-
echo -e "$TEXT"
41-
}
47+
echo -e "Loading container list..."
4248

4349
NODE=$(hostname)
4450
MSG_MAX_LENGTH=0
@@ -61,37 +67,51 @@ done
6167

6268
LXC_STATUS=$(pct status "$CTID" | awk '{print $2}')
6369
if [[ "$LXC_STATUS" != "running" ]]; then
64-
msg "\e[1;33m The container $CTID is not running. Starting it now...\e[0m"
70+
msg_info "Container $CTID is not running. Starting it now..."
6571
pct start "$CTID"
6672
while [[ "$(pct status "$CTID" | awk '{print $2}')" != "running" ]]; do
67-
msg "\e[1;33m Waiting for the container to start...\e[0m"
73+
msg_info "Waiting for the container to start..."
6874
sleep 2
6975
done
70-
msg "\e[1;32m Container $CTID is now running.\e[0m"
76+
msg_ok "Container $CTID is now running."
7177
fi
7278

7379
DISTRO=$(pct exec "$CTID" -- cat /etc/os-release | grep -w "ID" | cut -d'=' -f2 | tr -d '"')
7480
if [[ "$DISTRO" != "debian" && "$DISTRO" != "ubuntu" ]]; then
75-
msg "\e[1;31m Error: This script only supports Debian or Ubuntu LXC containers. Detected: $DISTRO. Aborting...\e[0m"
81+
msg_error "This script only supports Debian or Ubuntu LXC containers. Detected: $DISTRO. Aborting..."
7682
exit 238
7783
fi
7884

79-
CTID_CONFIG_PATH=/etc/pve/lxc/${CTID}.conf
80-
cat <<EOF >>$CTID_CONFIG_PATH
85+
CTID_CONFIG_PATH="/etc/pve/lxc/${CTID}.conf"
86+
cat <<EOF >>"$CTID_CONFIG_PATH"
8187
lxc.cgroup2.devices.allow: c 10:200 rwm
8288
lxc.mount.entry: /dev/net/tun dev/net/tun none bind,create=file
8389
EOF
84-
header_info
85-
msg "Installing NetBird..."
90+
91+
msg_info "Installing NetBird"
8692
pct exec "$CTID" -- bash -c '
93+
set -e
8794
if ! command -v curl &>/dev/null; then
8895
apt-get update -qq
8996
apt-get install -y curl >/dev/null
9097
fi
91-
apt install -y ca-certificates gpg &>/dev/null
92-
curl -fsSL "https://pkgs.netbird.io/debian/public.key" | gpg --dearmor >/usr/share/keyrings/netbird-archive-keyring.gpg
93-
echo "deb [signed-by=/usr/share/keyrings/netbird-archive-keyring.gpg] https://pkgs.netbird.io/debian stable main" >/etc/apt/sources.list.d/netbird.list
94-
apt-get update &>/dev/null
98+
apt-get install -y ca-certificates gpg &>/dev/null
99+
100+
# Reuse the shared repository helper instead of hand-rolling the sources entry
101+
source <(curl -fsSL https://raw.githubusercontent.com/community-scripts/ProxmoxVE/main/misc/core.func)
102+
source <(curl -fsSL https://raw.githubusercontent.com/community-scripts/ProxmoxVE/main/misc/tools.func)
103+
load_functions
104+
105+
# Drop the legacy keyring from the pre-deb822 layout
106+
rm -f /usr/share/keyrings/netbird-archive-keyring.gpg
107+
108+
setup_deb822_repo \
109+
"netbird" \
110+
"https://pkgs.netbird.io/debian/public.key" \
111+
"https://pkgs.netbird.io/debian" \
112+
"stable" \
113+
"main"
114+
95115
apt-get install -y netbird-ui &>/dev/null
96116
if systemctl list-unit-files docker.service &>/dev/null; then
97117
mkdir -p /etc/systemd/system/netbird.service.d
@@ -103,6 +123,5 @@ OVERRIDE
103123
systemctl daemon-reload
104124
fi
105125
'
106-
msg "\e[1;32m ✔ Installed NetBird.\e[0m"
107-
sleep 2
108-
msg "\e[1;31m Reboot ${CTID} LXC to apply the changes, then run netbird up in the LXC console\e[0m"
126+
msg_ok "Installed NetBird."
127+
echo -e "${YW}Reboot ${CTID} LXC${CL} to apply the changes, then run 'netbird up' in the LXC console"

0 commit comments

Comments
 (0)