Files
Kelx/install.sh
T
2026-11-30 16:32:37 +00:00

463 lines
17 KiB
Bash

#!/usr/bin/env bash
#
# ADPanel Infrastructure Installer (intermediate orchestrator)
# -----------------------------------------------------------------------------
# This script is a thin, user-friendly front-end that drives the two component
# installers:
#
# * initialize.sh -> installs / uninstalls ADPanel (panel web app)
# * install-daemon.sh -> installs / uninstalls ADaemon (node daemon)
#
# It can fetch those scripts from the CDN when installing, and re-uses the
# copies that the component installers leave on disk when uninstalling.
#
# Menu:
# 1) Install ADPanel
# 2) Install ADaemon
# 3) Install ADPanel and ADaemon
# 4) Uninstall
#
# Design notes for maintainers:
# * initialize.sh accepts "--choice <n>" (1 = install panel, 5 = uninstall).
# * install-daemon.sh accepts "--choice <n>" (1 = install daemon, 2 = remove).
# * Neither upstream script prints a literal "INSTALLED_OK" token, so success
# is detected via the process exit code (0 == success). We surface that as
# an INSTALLED_OK state internally so the option-3 chain only continues to
# ADaemon after ADPanel has fully succeeded.
# -----------------------------------------------------------------------------
set -Eeuo pipefail
# -----------------------------------------------------------------------------
# Presentation helpers (colors + fancy framed output)
# -----------------------------------------------------------------------------
if [ -t 1 ] && [ -z "${NO_COLOR:-}" ]; then
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
CYAN='\033[0;36m'
MAGENTA='\033[1;35m'
BLUE='\033[0;34m'
WHITE='\033[1;37m'
BOLD='\033[1m'
DIM='\033[2m'
NC='\033[0m'
else
RED='' GREEN='' YELLOW='' CYAN='' MAGENTA='' BLUE='' WHITE='' BOLD='' DIM='' NC=''
fi
RULE="==============================================================================="
SUBRULE="-------------------------------------------------------------------------------"
ui_rule() { printf "%b%s%b\n" "${1:-$DIM}" "$RULE" "$NC"; }
ui_subrule() { printf "%b%s%b\n" "${1:-$DIM}" "$SUBRULE" "$NC"; }
ui_banner() {
printf '\n'
ui_rule "${MAGENTA}${BOLD}"
printf "%b%s%b\n" "${CYAN}${BOLD}" " ADPanel Infrastructure Installer" "$NC"
printf "%b%s%b\n" "${DIM}" " ADPanel • ADaemon" "$NC"
ui_rule "${MAGENTA}${BOLD}"
printf '\n'
}
ui_section() {
printf '\n'
ui_subrule "${BLUE}${DIM}"
printf "%b%s%b\n" "${CYAN}${BOLD}" "$1" "$NC"
ui_subrule "${BLUE}${DIM}"
}
ui_info() { printf "%b•%b %s\n" "${CYAN}${BOLD}" "$NC" "$1"; }
ui_step() { printf "%b▸%b %s\n" "${MAGENTA}${BOLD}" "$NC" "$1"; }
ui_success() { printf "%b✔%b %s\n" "${GREEN}${BOLD}" "$NC" "$1"; }
ui_warn() { printf "%b!%b %s\n" "${YELLOW}${BOLD}" "$NC" "$1"; }
ui_error() { printf "%bx%b %s\n" "${RED}${BOLD}" "$NC" "$1" >&2; }
ui_menu() { printf " %b%s%b %s\n" "${WHITE}${BOLD}" "$1" "$NC" "$2"; }
ui_prompt() { printf "%b%s%b" "${MAGENTA}${BOLD}" "$1" "$NC"; }
die() { ui_error "$1"; exit "${2:-1}"; }
# -----------------------------------------------------------------------------
# Privilege handling
# -----------------------------------------------------------------------------
SUDO=""
if [ "${EUID:-$(id -u)}" -ne 0 ]; then
if command -v sudo >/dev/null 2>&1; then
SUDO="sudo"
else
die "This installer needs root privileges. Re-run as root or install sudo."
fi
fi
have() { command -v "$1" >/dev/null 2>&1; }
# -----------------------------------------------------------------------------
# Configuration: source URLs and well-known install locations
# -----------------------------------------------------------------------------
# The component installers are ALWAYS fetched from the CDN, for both install and
# uninstall. This is deliberate and safe:
# * initialize.sh resolves its target panel by CONTENT (it looks for
# index.js + initialize.sh under ADPANEL_PRODUCTION_ROOT), not by where the
# script file lives. So a CDN copy still finds and removes /var/www/adpanel.
# It also avoids the fragility of a script deleting its own directory.
# * install-daemon.sh operates on fixed paths (/var/lib/node, /var/lib/gonode,
# /usr/local/bin/adnode, the ADaemon/adaemon services), independent of where
# it is run from.
INITIALIZE_URL="${ADPANEL_INITIALIZE_URL:-https://cdn.jsdelivr.net/gh/antonndev/ADCDn/scripts/initialize.sh}"
DAEMON_URL="${ADPANEL_DAEMON_URL:-https://cdn.jsdelivr.net/gh/antonndev/ADCDn/scripts/install-daemon.sh}"
# ADPanel production root (matches initialize.sh ADPANEL_PRODUCTION_ROOT default).
PANEL_ROOT="${ADPANEL_PRODUCTION_ROOT:-/var/www/adpanel}"
# ADaemon install markers (match install-daemon.sh). All are overridable via
# environment so the detection can be sandboxed/tested without touching real
# system paths.
DAEMON_NODE_DIR="${ADPANEL_DAEMON_NODE_DIR:-/var/lib/node}"
DAEMON_GO_DIR="${ADPANEL_DAEMON_GO_DIR:-/var/lib/gonode}"
DAEMON_BIN="${ADPANEL_DAEMON_BIN:-/usr/local/bin/adnode}"
DAEMON_SERVICE_FILE="${ADPANEL_DAEMON_SERVICE_FILE:-/etc/systemd/system/ADaemon.service}"
DAEMON_SERVICE_ALIAS="${ADPANEL_DAEMON_SERVICE_ALIAS:-/etc/systemd/system/adaemon.service}"
DAEMON_INITD="${ADPANEL_DAEMON_INITD:-/etc/init.d/adaemon}"
# Temp files we create are cleaned up on exit.
TMP_FILES=()
cleanup() {
local f
for f in "${TMP_FILES[@]:-}"; do
[ -n "$f" ] && rm -f "$f" 2>/dev/null || true
done
}
trap cleanup EXIT
# -----------------------------------------------------------------------------
# Downloader
# -----------------------------------------------------------------------------
download() {
# download <url> <dest>
local url="$1" dest="$2"
if have curl; then
curl -fL --connect-timeout 15 --retry 3 --retry-delay 2 "$url" -o "$dest"
elif have wget; then
wget -q -O "$dest" "$url"
else
die "Neither curl nor wget is available; cannot download $url."
fi
}
# -----------------------------------------------------------------------------
# Detection helpers (local only; used to decide which questions to ask).
# -----------------------------------------------------------------------------
adpanel_installed() {
# ADPanel is considered installed when its production root holds the app
# entrypoint and its own installer (mirrors initialize.sh's own guard).
[ -f "${PANEL_ROOT}/index.js" ] && [ -f "${PANEL_ROOT}/initialize.sh" ]
}
adaemon_installed() {
# ADaemon is considered installed when an authoritative marker is present.
#
# Authoritative (any one is conclusive):
# * a registered service unit (systemd or SysV init), or
# * the managed node source checkout (DAEMON_NODE_DIR/.git).
#
# The bare binary (DAEMON_BIN) is deliberately NOT treated as conclusive on
# its own: an "adnode" executable can linger from an unrelated build or a
# partial removal and would otherwise cause false positives (e.g. wrongly
# claiming ADaemon is installed and suppressing the "nothing installed"
# message). It only counts when paired with the managed node directory.
[ -f "$DAEMON_SERVICE_FILE" ] && return 0
[ -f "$DAEMON_SERVICE_ALIAS" ] && return 0
[ -f "$DAEMON_INITD" ] && return 0
[ -d "${DAEMON_NODE_DIR}/.git" ] && return 0
[ -x "$DAEMON_BIN" ] && [ -d "$DAEMON_NODE_DIR" ] && return 0
return 1
}
# -----------------------------------------------------------------------------
# Script fetchers.
#
# Each component script is downloaded at most once per run and cached in a temp
# file (path stored in INITIALIZE_SCRIPT / DAEMON_SCRIPT). We set the global
# rather than echoing the path, because these also print progress to stdout;
# capturing via $(...) would mix that output into the path and break execution.
# -----------------------------------------------------------------------------
INITIALIZE_SCRIPT=""
DAEMON_SCRIPT=""
fetch_initialize_script() {
# Reuse an already-downloaded copy if present.
if [ -n "$INITIALIZE_SCRIPT" ] && [ -f "$INITIALIZE_SCRIPT" ]; then
return 0
fi
local tmp
tmp="$(mktemp "${TMPDIR:-/tmp}/adpanel-initialize.XXXXXX.sh")"
TMP_FILES+=("$tmp")
ui_step "Fetching ADPanel script (initialize.sh)..."
download "$INITIALIZE_URL" "$tmp" || die "Failed to download initialize.sh from $INITIALIZE_URL"
chmod +x "$tmp" 2>/dev/null || true
INITIALIZE_SCRIPT="$tmp"
}
fetch_daemon_script() {
if [ -n "$DAEMON_SCRIPT" ] && [ -f "$DAEMON_SCRIPT" ]; then
return 0
fi
local tmp
tmp="$(mktemp "${TMPDIR:-/tmp}/adpanel-install-daemon.XXXXXX.sh")"
TMP_FILES+=("$tmp")
ui_step "Fetching ADaemon script (install-daemon.sh)..."
download "$DAEMON_URL" "$tmp" || die "Failed to download install-daemon.sh from $DAEMON_URL"
chmod +x "$tmp" 2>/dev/null || true
DAEMON_SCRIPT="$tmp"
}
# -----------------------------------------------------------------------------
# Runners. Each returns the component script's exit code. A 0 exit is mapped to
# the internal "INSTALLED_OK" success state used to gate the option-3 chain.
# -----------------------------------------------------------------------------
run_panel_install() {
local status
fetch_initialize_script
ui_section "Installing ADPanel"
ui_info "Launching initialize.sh and auto-selecting option 1 (Initialize Panel)."
set +e
$SUDO env ADPANEL_INIT_CHOICE=1 bash "$INITIALIZE_SCRIPT" --choice 1
status=$?
set -e
if [ "$status" -eq 0 ]; then
ui_success "ADPanel install reported success (INSTALLED_OK)."
else
ui_error "ADPanel install failed (exit code ${status})."
fi
return "$status"
}
run_daemon_install() {
local status
fetch_daemon_script
ui_section "Installing ADaemon"
ui_info "Launching install-daemon.sh and auto-selecting option 1 (Install ADaemon)."
set +e
$SUDO env ADPANEL_DAEMON_CHOICE=1 bash "$DAEMON_SCRIPT" --choice 1
status=$?
set -e
if [ "$status" -eq 0 ]; then
ui_success "ADaemon install reported success (INSTALLED_OK)."
else
ui_error "ADaemon install failed (exit code ${status})."
fi
return "$status"
}
run_panel_uninstall() {
local status
# Always use the CDN copy. initialize.sh auto-detects the real install at
# ADPANEL_PRODUCTION_ROOT (/var/www/adpanel) by content, so a CDN copy
# uninstalls it correctly and avoids a script deleting its own directory.
fetch_initialize_script
ui_section "Uninstalling ADPanel"
ui_info "Launching initialize.sh and auto-selecting option 5 (Uninstall ADPanel)."
ui_info "initialize.sh will ask you to type the panel path or 'UNINSTALL' to confirm."
set +e
$SUDO env ADPANEL_INIT_CHOICE=5 bash "$INITIALIZE_SCRIPT" --choice 5
status=$?
set -e
if [ "$status" -eq 0 ]; then
ui_success "ADPanel uninstall finished."
else
ui_error "ADPanel uninstall did not complete (exit code ${status})."
fi
return "$status"
}
run_daemon_uninstall() {
local status
# Always use the CDN copy. install-daemon.sh uninstall targets fixed paths
# (/var/lib/node, /var/lib/gonode, /usr/local/bin/adnode, ADaemon services),
# independent of where the script runs from.
fetch_daemon_script
ui_section "Uninstalling ADaemon"
ui_info "Launching install-daemon.sh and auto-selecting option 2 (Uninstall ADaemon)."
# install-daemon.sh prompts for YES/adaemon when a terminal is attached, and
# requires ADPANEL_DAEMON_ASSUME_YES=1 when there is no terminal. We pass the
# latter so the uninstall also works when this orchestrator is piped.
set +e
$SUDO env ADPANEL_DAEMON_CHOICE=2 ADPANEL_DAEMON_ASSUME_YES=1 bash "$DAEMON_SCRIPT" --choice 2
status=$?
set -e
if [ "$status" -eq 0 ]; then
ui_success "ADaemon uninstall finished."
else
ui_error "ADaemon uninstall did not complete (exit code ${status})."
fi
return "$status"
}
# -----------------------------------------------------------------------------
# High-level option handlers
# -----------------------------------------------------------------------------
do_install_panel() {
run_panel_install || die "ADPanel installation did not complete successfully."
print_panel_summary
}
do_install_daemon() {
run_daemon_install || die "ADaemon installation did not complete successfully."
print_daemon_summary
}
do_install_both() {
ui_section "Installing ADPanel and ADaemon"
ui_info "Step 1/2: ADPanel must finish successfully before ADaemon begins."
# Step 1: ADPanel. Only proceed to ADaemon on INSTALLED_OK (exit 0).
if ! run_panel_install; then
die "ADPanel did not return INSTALLED_OK; aborting before ADaemon installation."
fi
ui_info "ADPanel returned INSTALLED_OK. Proceeding to ADaemon."
# Step 2: ADaemon.
if ! run_daemon_install; then
die "ADaemon did not return INSTALLED_OK after a successful ADPanel install."
fi
print_both_summary
}
do_uninstall() {
ui_section "Uninstall ADPanel infrastructure"
local panel_present="false"
local daemon_present="false"
adpanel_installed && panel_present="true"
adaemon_installed && daemon_present="true"
# Nothing installed at all.
if [ "$panel_present" != "true" ] && [ "$daemon_present" != "true" ]; then
ui_warn "ADPanel infrastructure isn't installed on your machine."
return 0
fi
local did_something="false"
# --- ADPanel question (only if ADPanel is present) -------------------------
if [ "$panel_present" == "true" ]; then
ui_info "Detected ADPanel installation at: ${PANEL_ROOT}"
local ans=""
# Tolerate EOF (e.g. non-interactive stdin): treat it as "no".
read -r -p "$(ui_prompt 'Do you want to uninstall ADPanel? (y/n): ')" ans || ans=""
case "$(printf '%s' "$ans" | tr '[:upper:]' '[:lower:]')" in
y|yes)
run_panel_uninstall || ui_warn "ADPanel uninstall encountered errors."
did_something="true"
;;
*)
ui_info "Keeping ADPanel."
;;
esac
fi
# --- ADaemon question (only if ADaemon is present) -------------------------
if [ "$daemon_present" == "true" ]; then
ui_info "Detected ADaemon installation."
local ans=""
# Tolerate EOF (e.g. non-interactive stdin): treat it as "no".
read -r -p "$(ui_prompt 'Do you want to uninstall ADaemon? (y/n): ')" ans || ans=""
case "$(printf '%s' "$ans" | tr '[:upper:]' '[:lower:]')" in
y|yes)
run_daemon_uninstall || ui_warn "ADaemon uninstall encountered errors."
did_something="true"
;;
*)
ui_info "Keeping ADaemon."
;;
esac
fi
printf '\n'
if [ "$did_something" == "true" ]; then
ui_success "Uninstall actions completed."
else
ui_info "No components were removed."
fi
}
# -----------------------------------------------------------------------------
# Fancy completion summaries
# -----------------------------------------------------------------------------
print_panel_summary() {
printf '\n'
ui_rule "${GREEN}${BOLD}"
printf "%b%s%b\n" "${GREEN}${BOLD}" " ADPanel was successfully installed and running." "$NC"
ui_rule "${GREEN}${BOLD}"
printf " %bRestart ADPanel:%b systemctl restart adpanel\n" "${CYAN}${BOLD}" "$NC"
printf '\n'
}
print_daemon_summary() {
printf '\n'
ui_rule "${GREEN}${BOLD}"
printf "%b%s%b\n" "${GREEN}${BOLD}" " ADaemon was successfully installed and running." "$NC"
ui_rule "${GREEN}${BOLD}"
printf " %bRestart ADaemon:%b systemctl restart adaemon\n" "${CYAN}${BOLD}" "$NC"
printf '\n'
}
print_both_summary() {
printf '\n'
ui_rule "${GREEN}${BOLD}"
printf "%b%s%b\n" "${GREEN}${BOLD}" " ADPanel and ADaemon were successfully installed and running." "$NC"
ui_rule "${GREEN}${BOLD}"
printf " %bTo restart one of these, try:%b\n" "${WHITE}${BOLD}" "$NC"
printf " %b•%b ADPanel: %bsystemctl restart adpanel%b\n" "${CYAN}${BOLD}" "$NC" "${WHITE}" "$NC"
printf " %b•%b ADaemon: %bsystemctl restart adaemon%b\n" "${CYAN}${BOLD}" "$NC" "${WHITE}" "$NC"
ui_rule "${GREEN}${BOLD}"
printf '\n'
}
# -----------------------------------------------------------------------------
# Menu / entrypoint
# -----------------------------------------------------------------------------
print_menu() {
ui_section "Choose an option:"
ui_menu "1)" "Install ADPanel"
ui_menu "2)" "Install ADaemon"
ui_menu "3)" "Install ADPanel and ADaemon"
ui_menu "4)" "Uninstall"
printf '\n'
}
main() {
ui_banner
# Allow a non-interactive choice via env var or first CLI arg.
local choice="${ADPANEL_INSTALL_CHOICE:-}"
if [ -z "$choice" ] && [ "${1:-}" != "" ]; then
case "${1:-}" in
--choice) choice="${2:-}";;
--choice=*) choice="${1#--choice=}";;
1|2|3|4) choice="$1";;
esac
fi
print_menu
if [ -n "$choice" ]; then
ui_info "Auto-selected option: ${choice}"
else
read -r -p "$(ui_prompt 'Enter choice (1, 2, 3 or 4): ')" choice || choice=""
fi
case "$choice" in
1) do_install_panel ;;
2) do_install_daemon ;;
3) do_install_both ;;
4) do_uninstall ;;
*) die "Invalid choice: '${choice}'. Expected 1, 2, 3 or 4." ;;
esac
}
main "$@"