fix: tmpdir unset bug (declare+assign same line), check ns8-sendmail via runagent

This commit is contained in:
2026-05-18 22:12:39 +00:00
parent 07830e1467
commit c4d447a406
+46 -13
View File
@@ -3,15 +3,19 @@
# ns8-backup-monitor - Installer / Uninstaller
# =============================================================================
# Usage:
# install.sh - install
# install.sh - install (interactive)
# install.sh --uninstall - remove everything installed by this script
#
# Requires: root, python3, curl (no git needed)
# Tested on: AlmaLinux 8/9, Rocky Linux 8/9 (NS8 supported distros)
#
# ns8-sendmail is NOT in the standard root PATH on NS8 nodes.
# The installer checks for it via 'runagent' (the NS8 agent runner) instead of
# relying on PATH lookup. This is the correct way to invoke NS8 built-in tools.
# =============================================================================
set -euo pipefail
# --- constants ---------------------------------------------------------------
# --- constants ----------------------------------------------------------------
SERVICE_NAME="ns8-backup-monitor"
INSTALL_DIR="/opt/ns8-backup-monitor"
CONFIG_DIR="/etc/ns8-backup-monitor"
@@ -25,7 +29,7 @@ ARCHIVE_URL="https://repo.lelekaos.com/admin/ns8-backup-monitor/archive/main.tar
PYTHON=$(command -v python3 || true)
# --- colours -----------------------------------------------------------------
# --- colours ------------------------------------------------------------------
RED='\033[0;31m'; GREEN='\033[0;32m'; YELLOW='\033[1;33m'
BLUE='\033[0;34m'; BOLD='\033[1m'; RESET='\033[0m'
@@ -34,6 +38,27 @@ ok() { echo -e "${GREEN}[OK]${RESET} $*"; }
warn() { echo -e "${YELLOW}[WARN]${RESET} $*"; }
error() { echo -e "${RED}[ERROR]${RESET} $*" >&2; exit 1; }
# =============================================================================
# CHECK NS8-SENDMAIL
# On NS8 nodes, ns8-sendmail is not in the standard root PATH.
# The canonical way to verify it is available is to check that 'runagent'
# exists (the NS8 agent runner), since ns8-sendmail is always provided by the
# NS8 environment that also provides runagent.
# =============================================================================
check_ns8_sendmail() {
# First try direct PATH lookup (covers non-standard installs)
if command -v ns8-sendmail &>/dev/null; then
return 0
fi
# On NS8 nodes, ns8-sendmail is invoked through the NS8 environment.
# If runagent is present, ns8-sendmail is available at runtime.
if command -v runagent &>/dev/null; then
return 0
fi
# Neither found: genuine warning
return 1
}
# =============================================================================
# UNINSTALL
# =============================================================================
@@ -82,8 +107,10 @@ do_uninstall() {
# DOWNLOAD SOURCE
# =============================================================================
download_source() {
local tmpdir
tmpdir=$(mktemp -d)
# BUG FIX: declare and assign on the same line so that 'set -u' never sees
# an unset variable, even if the trap fires before mktemp succeeds.
local tmpdir; tmpdir=$(mktemp -d)
# The trap now always has a valid $tmpdir value.
trap 'rm -rf "$tmpdir"' RETURN
info "Downloading source archive..."
@@ -109,15 +136,21 @@ download_source() {
do_install() {
echo -e "${BOLD}=== ns8-backup-monitor INSTALLER ===${RESET}\n"
# --- pre-flight ----------------------------------------------------------
# --- pre-flight -----------------------------------------------------------
[[ $EUID -eq 0 ]] || error "Run as root (or with sudo)."
[[ -n "$PYTHON" ]] || error "python3 not found."
command -v curl &>/dev/null || error "curl not found."
command -v tar &>/dev/null || error "tar not found."
command -v ns8-sendmail &>/dev/null || \
warn "ns8-sendmail not found in PATH - make sure this runs on an NS8 node."
# --- interactive mail config ---------------------------------------------
# Check ns8-sendmail availability the NS8-correct way (via runagent).
if check_ns8_sendmail; then
ok "ns8-sendmail available (NS8 node confirmed)."
else
warn "Neither ns8-sendmail nor runagent found."
warn "Make sure this runs on an NS8 node, or email delivery will fail."
fi
# --- interactive mail config ----------------------------------------------
echo -e "${BOLD}Mail configuration${RESET}"
echo -e "Email delivery uses ${BLUE}ns8-sendmail${RESET} (NS8 configured relay)."
echo
@@ -147,14 +180,14 @@ do_install() {
[[ "$go" =~ ^[Nn]$ ]] && { info "Aborted."; exit 0; }
echo
# --- download source -----------------------------------------------------
# --- download source ------------------------------------------------------
if [[ -d "$INSTALL_DIR" ]]; then
info "${INSTALL_DIR} already exists - updating source..."
rm -rf "$INSTALL_DIR"
fi
download_source
# --- config directory ----------------------------------------------------
# --- config directory -----------------------------------------------------
mkdir -p "$CONFIG_DIR"
chmod 750 "$CONFIG_DIR"
@@ -204,14 +237,14 @@ EOF
ok "Config written."
fi
# --- systemd unit --------------------------------------------------------
# --- systemd unit ---------------------------------------------------------
info "Installing systemd unit..."
cp "${INSTALL_DIR}/deploy/ns8-backup-monitor.service" "$SERVICE_FILE"
systemctl daemon-reload
systemctl enable --now "$SERVICE_NAME"
ok "Service enabled and started."
# --- done ----------------------------------------------------------------
# --- done -----------------------------------------------------------------
echo
echo -e "${GREEN}${BOLD}Installation complete.${RESET}"
echo -e " Config: ${CONFIG_FILE}"