ServerKit is a lightweight, modern server control panel for managing web applications, databases, and services on your VPS or dedicated server. Built with Python/Flask backend and React frontend.
Python astracloudflarecontrol-panelcyberpaneldnsfablehosting-control-panelhosting-panelkimi-k3server-managementserverkitubuntu-web-panelwebserver
fix(cli): guard start/restart/logs/add-site, loud --branch error, RHEL nginx layout (L4-L7,L9,L12)
Juan Denis62300b429962
diff --git a/scripts/test/test_cli.sh b/scripts/test/test_cli.shnew file mode 100644index 00000000..75053c2a--- /dev/null+++ b/scripts/test/test_cli.sh@@ -0,0 +1,562 @@+#!/usr/bin/env bash+#+# Unit tests for the `serverkit` CLI (repo root).+#+# The CLI is source-able: when sourced it defines every function and returns+# *before* the command dispatch (the BASH_SOURCE guard), exactly like+# scripts/update.sh. Each unit under test runs in a subshell that re-arms+# `set -Eeuo pipefail`, so a "benign non-zero aborts the script" regression+# (the class the shell-surface audit found: L5/L6/L7/L9/L12) fails here as an+# assertion instead of on a real box.+#+# External tools (systemctl/docker/nginx/journalctl/curl/...) are PATH-stubbed,+# so this runs deterministically on any host, including Windows/Git-Bash.+#+# Run: bash scripts/test/test_cli.sh+set -uo pipefail++SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"+CLI="$SCRIPT_DIR/../../serverkit"++PASS=0+FAIL=0+SKIP=0+ok() { PASS=$((PASS + 1)); printf ' \033[32m✔\033[0m %s\n' "$1"; }+bad() { FAIL=$((FAIL + 1)); printf ' \033[31m✘\033[0m %s\n' "$1"; }+skip() { SKIP=$((SKIP + 1)); printf ' \033[33m∼\033[0m %s (skipped)\n' "$1"; }++WORK="$(mktemp -d)"+trap 'rm -rf "$WORK" 2>/dev/null || true' EXIT # teardown is best-effort++# --------------------------------------------------------------------------+# Global stubs: harmless defaults for every external tool the CLI may shell+# out to, so nothing ever touches the host's systemd/docker/nginx. Per-test+# stub dirs are PATH-prepended when a test needs a failing/recording variant.+# --------------------------------------------------------------------------+STUB_BIN="$WORK/bin"+mkdir -p "$STUB_BIN"+for cmd in systemctl docker nginx journalctl curl sleep ss; do+ printf '#!/usr/bin/env bash\nexit 0\n' > "$STUB_BIN/$cmd"+done+# Healthy system probes for doctor (free does not exist on Git-Bash/macOS).+cat > "$STUB_BIN/df" <<'EOF'+#!/usr/bin/env bash+printf 'Filesystem 1K-blocks Used Available Use%% Mounted\n'+printf '/dev/sda1 1000000 420000 580000 42%% /\n'+EOF+cat > "$STUB_BIN/free" <<'EOF'+#!/usr/bin/env bash+printf ' total used free shared buff/cache available\n'+printf 'Mem: 2000 400 600 10 1000 1500\n'+printf 'Swap: 1024 0 1024\n'+EOF+chmod +x "$STUB_BIN"/*+export PATH="$STUB_BIN:$PATH"++# --------------------------------------------------------------------------+# Source the CLI (functions only — the BASH_SOURCE guard skips the dispatch).+# Point the install/nginx dirs into the sandbox so nothing touches the host.+# --------------------------------------------------------------------------+export SERVERKIT_DIR="$WORK/opt/serverkit"+export SERVERKIT_NGINX_DIR="$WORK/etc/nginx"+# shellcheck disable=SC1090+source "$CLI"+set +e +u # hand control back to the harness; tests re-arm per subshell++printf '\nserverkit CLI unit tests\n\n'++# --------------------------------------------------------------------------+# T1 — L5: `serverkit update --branch` with NO value used to die on `shift 2`+# under set -e with zero output. It must fail loudly instead.+# --------------------------------------------------------------------------+out="$( set -Eeuo pipefail; cmd_update --branch 2>&1 )"+rc=$?+if [ "$rc" -ne 0 ] && printf '%s' "$out" | grep -q -- '--branch requires'; then+ ok "update --branch with no value fails loudly (rc=$rc, message printed)"+else+ bad "update --branch with no value: rc=$rc out=[$out] — silent or passing"+fi++# T1b — a valid --branch still forwards to the canonical updater unchanged.+t="$WORK/t1b"; mkdir -p "$t/scripts"+printf '#!/usr/bin/env bash\nprintf "ARGS:%%s\\n" "$*"\n' > "$t/scripts/update.sh"+chmod +x "$t/scripts/update.sh"+out="$(+ set -Eeuo pipefail+ check_root() { :; }+ check_installed() { :; }+ INSTALL_DIR="$t"; VENV_DIR="$t/venv"+ cmd_update --branch dev --force 2>&1+)"+if printf '%s' "$out" | grep -q 'ARGS:--branch dev --force'; then+ ok "update --branch <name> still forwards args to scripts/update.sh"+else+ bad "update --branch dev forwarding broken: [$out]"+fi++# --------------------------------------------------------------------------+# T2 — L6: cmd_start must warn (not raw-abort) when systemctl/docker fail+# (partial installs, non-systemd boxes). cmd_stop already had these guards.+# --------------------------------------------------------------------------+t="$WORK/t2"; mkdir -p "$t/bin" "$t/install"+CALL_LOG="$t/calls.log"; : > "$CALL_LOG"+cat > "$t/bin/systemctl" <<EOF+#!/usr/bin/env bash+printf 'systemctl %s\n' "\$*" >> "$CALL_LOG"+exit 1+EOF+cat > "$t/bin/docker" <<EOF+#!/usr/bin/env bash+printf 'docker %s\n' "\$*" >> "$CALL_LOG"+exit 1+EOF+chmod +x "$t/bin"/*+out="$(+ set -Eeuo pipefail+ export PATH="$t/bin:$PATH"+ check_root() { :; }+ INSTALL_DIR="$t/install"+ cmd_start 2>&1+)"+rc=$?+if [ "$rc" -eq 0 ] \+ && printf '%s' "$out" | grep -q 'Could not start backend' \+ && printf '%s' "$out" | grep -q 'Could not start frontend' \+ && grep -q 'docker compose up -d' "$CALL_LOG"; then+ ok "start survives failing systemctl/docker with warnings (no raw abort)"+else+ bad "start aborted or lost a warning: rc=$rc out=[$out]"+fi++# --------------------------------------------------------------------------+# T3 — L6: cmd_restart, same shape.+# --------------------------------------------------------------------------+t="$WORK/t3"; mkdir -p "$t/bin" "$t/install"+CALL_LOG="$t/calls.log"; : > "$CALL_LOG"+cat > "$t/bin/systemctl" <<EOF+#!/usr/bin/env bash+printf 'systemctl %s\n' "\$*" >> "$CALL_LOG"+exit 1+EOF+cat > "$t/bin/docker" <<EOF+#!/usr/bin/env bash+printf 'docker %s\n' "\$*" >> "$CALL_LOG"+exit 1+EOF+chmod +x "$t/bin"/*+out="$(+ set -Eeuo pipefail+ export PATH="$t/bin:$PATH"+ check_root() { :; }+ INSTALL_DIR="$t/install"+ cmd_restart 2>&1+)"+rc=$?+if [ "$rc" -eq 0 ] \+ && printf '%s' "$out" | grep -q 'Could not restart backend' \+ && printf '%s' "$out" | grep -q 'ServerKit restarted' \+ && grep -q 'systemctl restart serverkit' "$CALL_LOG"; then+ ok "restart survives failing systemctl/docker with warnings (no raw abort)"+else+ bad "restart aborted or lost a warning: rc=$rc out=[$out]"+fi++# --------------------------------------------------------------------------+# T4 — L7: `serverkit logs` on a journal-less box must not die before showing+# the Docker logs that would have worked.+# --------------------------------------------------------------------------+t="$WORK/t4"; mkdir -p "$t/bin" "$t/install"+CALL_LOG="$t/calls.log"; : > "$CALL_LOG"+cat > "$t/bin/journalctl" <<EOF+#!/usr/bin/env bash+printf 'journalctl %s\n' "\$*" >> "$CALL_LOG"+exit 1+EOF+cat > "$t/bin/docker" <<EOF+#!/usr/bin/env bash+printf 'docker %s\n' "\$*" >> "$CALL_LOG"+exit 0+EOF+chmod +x "$t/bin"/*+out="$(+ set -Eeuo pipefail+ export PATH="$t/bin:$PATH"+ INSTALL_DIR="$t/install"+ cmd_logs all 2>&1+)"+rc=$?+if [ "$rc" -eq 0 ] \+ && printf '%s' "$out" | grep -q 'Could not read the backend journal' \+ && grep -q 'docker compose logs --tail=50' "$CALL_LOG"; then+ ok "logs falls through a failing journalctl to the Docker-logs half"+else+ bad "logs died before the Docker half: rc=$rc out=[$out]"+fi++# --------------------------------------------------------------------------+# T5 — L9: add-site on a Debian layout (sites-available present) writes the+# vhost, enables it, and reloads.+# --------------------------------------------------------------------------+t="$WORK/t5"; mkdir -p "$t/nginx/sites-available" "$t/nginx/sites-enabled"+out="$(+ set -Eeuo pipefail+ check_root() { :; }+ NGINX_DIR="$t/nginx"+ cmd_add_site app.example.com 3100 2>&1+)"+rc=$?+if [ "$rc" -eq 0 ] \+ && [ -f "$t/nginx/sites-available/app.example.com.conf" ] \+ && [ -e "$t/nginx/sites-enabled/app.example.com.conf" ] \+ && grep -q 'proxy_pass http://127.0.0.1:3100;' "$t/nginx/sites-available/app.example.com.conf" \+ && printf '%s' "$out" | grep -q 'Site added'; then+ ok "add-site (Debian layout) writes, enables, and reports the site"+else+ bad "add-site Debian layout broken: rc=$rc out=[$out]"+fi++# --------------------------------------------------------------------------+# T6 — L9: add-site on a conf.d-only layout (RHEL family) must not assume+# sites-available/sites-enabled exist.+# --------------------------------------------------------------------------+t="$WORK/t6"; mkdir -p "$t/nginx"+out="$(+ set -Eeuo pipefail+ check_root() { :; }+ NGINX_DIR="$t/nginx"+ cmd_add_site rhel.example.com 3200 2>&1+)"+rc=$?+if [ "$rc" -eq 0 ] \+ && [ -f "$t/nginx/conf.d/rhel.example.com.conf" ] \+ && [ ! -d "$t/nginx/sites-enabled" ]; then+ ok "add-site (conf.d layout) writes into conf.d and skips the symlink"+else+ bad "add-site conf.d layout broken: rc=$rc out=[$out]"+fi++# --------------------------------------------------------------------------+# T7 — L9: a stopped nginx / non-systemd box must not raw-abort AFTER the+# site file is written — warn with a reload-manually hint instead.+# --------------------------------------------------------------------------+t="$WORK/t7"; mkdir -p "$t/bin" "$t/nginx/sites-available" "$t/nginx/sites-enabled"+printf '#!/usr/bin/env bash\nexit 1\n' > "$t/bin/systemctl"+cat > "$t/bin/nginx" <<'EOF'+#!/usr/bin/env bash+[ "${1:-}" = "-t" ] && exit 0+exit 1+EOF+chmod +x "$t/bin"/*+out="$(+ set -Eeuo pipefail+ export PATH="$t/bin:$PATH"+ check_root() { :; }+ NGINX_DIR="$t/nginx"+ cmd_add_site down.example.com 3300 2>&1+)"+rc=$?+if [ "$rc" -eq 0 ] \+ && [ -f "$t/nginx/sites-available/down.example.com.conf" ] \+ && printf '%s' "$out" | grep -q 'Could not reload nginx' \+ && printf '%s' "$out" | grep -q 'Site added'; then+ ok "add-site with a stopped nginx warns 'reload manually' instead of aborting"+else+ bad "add-site stopped-nginx path broken: rc=$rc out=[$out]"+fi++# --------------------------------------------------------------------------+# T8 — add-site with a failing `nginx -t` still errors out loudly and+# disables the broken vhost: Debian → drop the symlink; conf.d → park the+# live file as .disabled so it can't poison an unrelated reload.+# --------------------------------------------------------------------------+t="$WORK/t8"; mkdir -p "$t/bin" "$t/nginx/sites-available" "$t/nginx/sites-enabled" "$t/nginx2"+printf '#!/usr/bin/env bash\nexit 1\n' > "$t/bin/nginx"+chmod +x "$t/bin/nginx"+out="$(+ set -Eeuo pipefail+ export PATH="$t/bin:$PATH"+ check_root() { :; }+ NGINX_DIR="$t/nginx"+ cmd_add_site bad.example.com 3400 2>&1+)"+rc=$?+out2="$(+ set -Eeuo pipefail+ export PATH="$t/bin:$PATH"+ check_root() { :; }+ NGINX_DIR="$t/nginx2"+ cmd_add_site bad2.example.com 3500 2>&1+)"+rc2=$?+if [ "$rc" -ne 0 ] && printf '%s' "$out" | grep -q 'Nginx config error' \+ && [ ! -e "$t/nginx/sites-enabled/bad.example.com.conf" ] \+ && [ -f "$t/nginx/sites-available/bad.example.com.conf" ] \+ && [ "$rc2" -ne 0 ] \+ && [ ! -f "$t/nginx2/conf.d/bad2.example.com.conf" ] \+ && [ -f "$t/nginx2/conf.d/bad2.example.com.conf.disabled" ]; then+ ok "add-site on nginx -t failure disables the vhost in both layouts"+else+ bad "add-site nginx -t failure handling broken: rc=$rc/$rc2 out=[$out] out2=[$out2]"+fi++# --------------------------------------------------------------------------+# T9 — L4-CLI: on a remnants-only box (shared uninstall lib gone), the CLI+# must print exact recovery instructions instead of a bare refusal —+# the local standalone uninstall.sh when it exists, curl|bash otherwise.+# --------------------------------------------------------------------------+t="$WORK/t9"; mkdir -p "$t/install"+: > "$t/install/uninstall.sh"+out="$(+ set -Eeuo pipefail+ check_root() { :; }+ INSTALL_DIR="$t/install"+ cmd_uninstall --yes 2>&1+)"+rc=$?+if [ "$rc" -ne 0 ] && printf '%s' "$out" | grep -qF "sudo bash $t/install/uninstall.sh"; then+ ok "uninstall (lib missing, local uninstall.sh present) points at the local script"+else+ bad "uninstall recovery hint (local) missing: rc=$rc out=[$out]"+fi+rm -f "$t/install/uninstall.sh"+out="$(+ set -Eeuo pipefail+ check_root() { :; }+ INSTALL_DIR="$t/install"+ cmd_uninstall --yes 2>&1+)"+rc=$?+if [ "$rc" -ne 0 ] && printf '%s' "$out" | grep -q 'curl -fsSL' \+ && printf '%s' "$out" | grep -q 'uninstall.sh | sudo bash'; then+ ok "uninstall (nothing left locally) points at the curl | sudo bash fallback"+else+ bad "uninstall recovery hint (curl) missing: rc=$rc out=[$out]"+fi++# --------------------------------------------------------------------------+# T10 — L12: compare_versions must treat unparsable versions as "update+# available" (rc 2), never as a silent "up to date"; numeric compares intact.+# --------------------------------------------------------------------------+cv() { ( set -Eeuo pipefail; compare_versions "$1" "$2" >/dev/null 2>&1 ); printf '%s' "$?"; }+if [ "$(cv unknown 1.7.7)" = "2" ] && [ "$(cv 1.7.7 '<html>')" = "2" ] \+ && [ "$(cv 1.7.6 1.7.7)" = "2" ] && [ "$(cv 2.0.0 1.9.9)" = "1" ] \+ && [ "$(cv 1.7.7 1.7.7)" = "0" ] && [ "$(cv 1.8 1.8.1)" = "2" ]; then+ ok "compare_versions: unparsable → 'older' (update available); numeric compare intact"+else+ bad "compare_versions matrix wrong: unknown=$(cv unknown 1.7.7) html=$(cv 1.7.7 '<html>') lt=$(cv 1.7.6 1.7.7) gt=$(cv 2.0.0 1.9.9) eq=$(cv 1.7.7 1.7.7) xy=$(cv 1.8 1.8.1)"+fi++# T10b — end-to-end: a box with a missing/corrupt VERSION file must report+# "Update available", not "latest version".+t="$WORK/t10b"; mkdir -p "$t/bin"+printf '#!/usr/bin/env bash\nprintf "9.9.9"\n' > "$t/bin/curl"+chmod +x "$t/bin/curl"+out="$(+ set -Eeuo pipefail+ export PATH="$t/bin:$PATH"+ VERSION_FILE="$t/no-version-file"+ cmd_check_update 2>&1+)"+rc=$?+if [ "$rc" -eq 0 ] && printf '%s' "$out" | grep -q 'Update available'; then+ ok "check-update with an unreadable local VERSION reports 'Update available'"+else+ bad "check-update unknown-local path wrong: rc=$rc out=[$out]"+fi++# --------------------------------------------------------------------------+# T11 — L12: doctor must exit non-zero when checks fail (it always exited 0)+# and still run every section to the end despite the failures.+# --------------------------------------------------------------------------+t="$WORK/t11"; mkdir -p "$t/bin" "$t/install" "$t/nginx"+for c in systemctl docker nginx curl; do+ printf '#!/usr/bin/env bash\nexit 1\n' > "$t/bin/$c"+done+chmod +x "$t/bin"/*+out="$(+ set -Eeuo pipefail+ export PATH="$t/bin:$PATH"+ INSTALL_DIR="$t/install"+ NGINX_DIR="$t/nginx"+ cmd_doctor 2>&1+)"+rc=$?+if [ "$rc" -ne 0 ] \+ && printf '%s' "$out" | grep -q 'Found .* issue' \+ && printf '%s' "$out" | grep -q 'Connectivity'; then+ ok "doctor exits non-zero on a broken box and still runs every section"+else+ bad "doctor gate broken: rc=$rc (expected non-zero) or it aborted early"+fi++# --------------------------------------------------------------------------+# T12 — doctor exits 0 on a fully healthy box, and the portable port+# extraction (sed, not GNU grep -oP) still matches ports correctly.+# --------------------------------------------------------------------------+t="$WORK/t12"+mkdir -p "$t/bin" "$t/install/backend/instance" \+ "$t/nginx/sites-available" "$t/nginx/sites-enabled" "$t/nginx/serverkit-locations"+printf 'x\n' > "$t/install/backend/instance/serverkit.db"+cat > "$t/nginx/sites-available/serverkit.conf" <<'EOF'+server {+ listen 80;+ location / {+ proxy_pass http://127.0.0.1:8080;+ }+ location /api {+ proxy_pass http://127.0.0.1:5000;+ }+}+EOF+cat > "$t/bin/docker" <<'EOF'+#!/usr/bin/env bash+case "${1:-}" in+ ps) printf 'serverkit-frontend\n' ;;+ port) printf '0.0.0.0:8080\n' ;;+esac+exit 0+EOF+cat > "$t/bin/ss" <<'EOF'+#!/usr/bin/env bash+printf 'LISTEN 0 128 127.0.0.1:5000 users:(("gunicorn",pid=1,fd=1))\n'+exit 0+EOF+chmod +x "$t/bin"/*+out="$(+ set -Eeuo pipefail+ export PATH="$t/bin:$PATH"+ INSTALL_DIR="$t/install"+ NGINX_DIR="$t/nginx"+ cmd_doctor 2>&1+)"+rc=$?+if [ "$rc" -eq 0 ] \+ && printf '%s' "$out" | grep -q 'All checks passed' \+ && printf '%s' "$out" | grep -q 'matches frontend container (8080)' \+ && printf '%s' "$out" | grep -q '(5000) has backend listening'; then+ ok "doctor exits 0 when healthy; portable port extraction matches 8080/5000"+else+ bad "doctor healthy path broken: rc=$rc out=[$out]"+fi++# --------------------------------------------------------------------------+# T13 — L12: list-sites port extraction without GNU grep -oP; conf.d layouts+# are listed too.+# --------------------------------------------------------------------------+t="$WORK/t13"; mkdir -p "$t/nginx/sites-enabled" "$t/rhel/conf.d"+cat > "$t/nginx/sites-enabled/proxied.conf" <<'EOF'+server {+ server_name proxied.example.com;+ location / {+ proxy_pass http://127.0.0.1:4100;+ }+}+EOF+cat > "$t/nginx/sites-enabled/static-site.conf" <<'EOF'+server {+ server_name static.example.com;+ root /var/www/static;+}+EOF+cat > "$t/rhel/conf.d/confd.conf" <<'EOF'+server {+ server_name confd.example.com;+ location / {+ proxy_pass http://127.0.0.1:4200;+ }+}+EOF+out="$( set -Eeuo pipefail; NGINX_DIR="$t/nginx"; cmd_list_sites 2>&1 )"+rc=$?+out2="$( set -Eeuo pipefail; NGINX_DIR="$t/rhel"; cmd_list_sites 2>&1 )"+rc2=$?+if [ "$rc" -eq 0 ] && [ "$rc2" -eq 0 ] \+ && printf '%s' "$out" | grep 'proxied.example.com' | grep -q 'port 4100' \+ && printf '%s' "$out" | grep 'static.example.com' | grep -q 'port static' \+ && printf '%s' "$out2" | grep 'confd.example.com' | grep -q 'port 4200'; then+ ok "list-sites extracts ports portably and lists conf.d layouts"+else+ bad "list-sites broken: rc=$rc/$rc2 out=[$out] out2=[$out2]"+fi++# --------------------------------------------------------------------------+# T14 — L12: `read -p` at EOF (no tty) must not abort under set -e; the empty+# answer takes the safe "No" path. Then a piped "y" actually removes the site+# and a failing reload only warns.+# --------------------------------------------------------------------------+t="$WORK/t14"; mkdir -p "$t/bin" "$t/nginx/sites-available" "$t/nginx/sites-enabled"+printf 'server { server_name gone.example.com; }\n' > "$t/nginx/sites-available/gone.example.com.conf"+: > "$t/nginx/sites-enabled/gone.example.com.conf"+printf '#!/usr/bin/env bash\nexit 1\n' > "$t/bin/systemctl"+printf '#!/usr/bin/env bash\nexit 1\n' > "$t/bin/nginx"+chmod +x "$t/bin"/*+out="$(+ set -Eeuo pipefail+ check_root() { :; }+ NGINX_DIR="$t/nginx"+ cmd_remove_site gone.example.com </dev/null 2>&1+)"+rc=$?+if [ "$rc" -eq 0 ] && [ -f "$t/nginx/sites-available/gone.example.com.conf" ]; then+ ok "remove-site read at EOF defaults to 'No' (no silent set -e abort)"+else+ bad "remove-site EOF handling broken: rc=$rc out=[$out]"+fi+out="$(+ printf 'y' | {+ set -Eeuo pipefail+ export PATH="$t/bin:$PATH"+ check_root() { :; }+ NGINX_DIR="$t/nginx"+ cmd_remove_site gone.example.com+ } 2>&1+)"+rc=$?+if [ "$rc" -eq 0 ] \+ && [ ! -f "$t/nginx/sites-available/gone.example.com.conf" ] \+ && [ ! -e "$t/nginx/sites-enabled/gone.example.com.conf" ] \+ && printf '%s' "$out" | grep -q 'Site removed' \+ && printf '%s' "$out" | grep -q 'Could not reload nginx'; then+ ok "remove-site with 'y' removes the vhost and only warns on a dead nginx"+else+ bad "remove-site confirm path broken: rc=$rc out=[$out]"+fi++# --------------------------------------------------------------------------+# T15 — regression guards baked into the source (same spirit as+# test_update.sh T13/T26): the audited failure shapes must never creep back.+# --------------------------------------------------------------------------+if grep -qE '^[^#]*grep -oP' "$CLI"; then+ bad "GNU-only 'grep -oP' is back in the CLI (breaks busybox doctor/list-sites)"+else+ ok "CLI contains no GNU-only 'grep -oP' on code lines"+fi+if grep -n 'read -p' "$CLI" | grep -v '|| true' | grep -q .; then+ bad "a 'read -p' without '|| true' crept back (EOF aborts the CLI under set -e)"+else+ ok "every interactive 'read -p' tolerates EOF (|| true)"+fi+if grep -qF '[ "${BASH_SOURCE[0]}" = "${0}" ] || return 0' "$CLI"; then+ ok "CLI keeps the sourceability guard (functions testable without dispatch)"+else+ bad "the BASH_SOURCE sourceability guard is missing from the CLI"+fi++# --------------------------------------------------------------------------+# T16 — executed-mode sanity: the sourceability guard must not change direct+# execution (help/version work, unknown commands still fail loudly).+# --------------------------------------------------------------------------+out="$(bash "$CLI" version 2>&1)"; rc=$?+out_help="$(bash "$CLI" help 2>&1)"; rc_help=$?+out_bogus="$(bash "$CLI" no-such-command 2>&1)"; rc_bogus=$?+if [ "$rc" -eq 0 ] && printf '%s' "$out" | grep -q 'ServerKit version' \+ && [ "$rc_help" -eq 0 ] && printf '%s' "$out_help" | grep -q 'Usage: serverkit' \+ && [ "$rc_bogus" -ne 0 ] && printf '%s' "$out_bogus" | grep -q 'Unknown command'; then+ ok "executed mode intact: version/help succeed, unknown command fails loudly"+else+ bad "executed-mode dispatch broken: version=$rc help=$rc_help bogus=$rc_bogus"+fi++# --------------------------------------------------------------------------+printf '\n%d passed, %d failed, %d skipped\n\n' "$PASS" "$FAIL" "$SKIP"+[ "$FAIL" -eq 0 ]diff --git a/serverkit b/serverkitindex 7d883a78..fd7dcf7f 100644--- a/serverkit+++ b/serverkit@@ -36,6 +36,7 @@ HUE_OK_TC="$(paint 52 211 153)"; HUE_WARN_TC="$(paint 250 204 21)"; HUE_ERR_TC="# ConfigurationINSTALL_DIR="${SERVERKIT_DIR:-/opt/serverkit}"VENV_DIR="${SERVERKIT_VENV_DIR:-$INSTALL_DIR/venv}"+NGINX_DIR="${SERVERKIT_NGINX_DIR:-/etc/nginx}"BACKEND_SERVICE="serverkit"FRONTEND_CONTAINER="serverkit-frontend"@@ -132,6 +133,15 @@ compare_versions() {return 0fi+ # Unparsable versions used to compare as "equal" (every [ -gt ] probe below+ # failed silently under 2>/dev/null), reporting "up to date" on a box with+ # a missing or corrupt VERSION file. Treat them as "older" instead so+ # check-update points the operator at `serverkit update`, which re-validates.+ local vre='^[0-9]+\.[0-9]+(\.[0-9]+)?$'+ if ! [[ "$v1" =~ $vre ]] || ! [[ "$v2" =~ $vre ]]; then+ return 2+ fi+# Compare version componentslocal v1_major=$(echo "$v1" | cut -d. -f1)local v1_minor=$(echo "$v1" | cut -d. -f2)@@ -141,6 +151,10 @@ compare_versions() {local v2_minor=$(echo "$v2" | cut -d. -f2)local v2_patch=$(echo "$v2" | cut -d. -f3)+ # X.Y versions carry no patch component — compare it as 0.+ v1_patch="${v1_patch:-0}"+ v2_patch="${v2_patch:-0}"+if [ "$v1_major" -gt "$v2_major" ] 2>/dev/null; then return 1; fiif [ "$v1_major" -lt "$v2_major" ] 2>/dev/null; then return 2; fiif [ "$v1_minor" -gt "$v2_minor" ] 2>/dev/null; then return 1; fi@@ -273,12 +287,13 @@ cmd_start() {check_installedprint_info "Starting ServerKit..."- # Start backend (systemd)- systemctl start $BACKEND_SERVICE+ # Start backend (systemd) — a partial install / non-systemd box must get a+ # readable warning, not a raw set -e abort.+ systemctl start $BACKEND_SERVICE || print_warning "Could not start backend service '$BACKEND_SERVICE' (is systemd available?)"# Start frontend (Docker)cd "$INSTALL_DIR"- docker compose up -d+ docker compose up -d || print_warning "Could not start frontend container (is Docker running?)"sleep 3print_success "ServerKit started"@@ -305,12 +320,13 @@ cmd_restart() {check_installedprint_info "Restarting ServerKit..."- # Restart backend (systemd)- systemctl restart $BACKEND_SERVICE+ # Restart backend (systemd) — mirror cmd_stop's guards: a failing service+ # manager is reported, never a raw abort.+ systemctl restart $BACKEND_SERVICE || print_warning "Could not restart backend service '$BACKEND_SERVICE' (is systemd available?)"# Restart frontend (Docker)cd "$INSTALL_DIR"- docker compose restart+ docker compose restart || print_warning "Could not restart frontend container (is Docker running?)"sleep 3print_success "ServerKit restarted"@@ -368,22 +384,32 @@ cmd_logs() {case "$service" inbackend)- journalctl -u $BACKEND_SERVICE -f+ if command -v journalctl >/dev/null 2>&1; then+ journalctl -u $BACKEND_SERVICE -f || print_warning "Could not read the backend journal (is journald running?)"+ else+ print_warning "journalctl not found — cannot stream backend logs on this system"+ fi;;frontend)cd "$INSTALL_DIR"- docker compose logs -f+ docker compose logs -f || print_warning "Could not read frontend container logs (is Docker running?)";;all|*)print_info "Showing backend logs (Ctrl+C to switch to frontend)..."echo ""- # Show both in split view isn't easy in bash, so show backend first- journalctl -u $BACKEND_SERVICE -n 50 --no-pager+ # Show both in split view isn't easy in bash, so show backend first.+ # A journal-less box must still fall through to the Docker logs+ # below instead of dying here.+ if command -v journalctl >/dev/null 2>&1; then+ journalctl -u $BACKEND_SERVICE -n 50 --no-pager || print_warning "Could not read the backend journal"+ else+ print_warning "journalctl not found — skipping backend logs"+ fiecho ""print_info "Showing frontend logs..."echo ""cd "$INSTALL_DIR"- docker compose logs --tail=50+ docker compose logs --tail=50 || print_warning "Could not read frontend container logs (is Docker running?)"echo ""print_info "For live logs, use: serverkit logs backend OR serverkit logs frontend";;@@ -420,8 +446,15 @@ cmd_update() {shift;;--branch|-b)- update_args+=("--branch" "$2")- shift 2+ # A missing value used to make `shift 2` fail and exit the CLI+ # silently under set -e. Guard it like --release below.+ if [[ -n "${2:-}" ]] && [[ ! "$2" =~ ^- ]]; then+ update_args+=("--branch" "$2")+ shift 2+ else+ print_error "--branch requires a branch name (e.g. --branch dev)"+ exit 1+ fi;;--release|-r)update_args+=("--release")@@ -483,7 +516,9 @@ cmd_uninstall() {echo ""if [ "$assume_yes" != "1" ]; then- read -p "Are you sure? (y/N) " -n 1 -r+ # EOF (no tty / piped input) must not abort under set -e; an empty+ # REPLY falls through to the safe "No" path below.+ read -p "Are you sure? (y/N) " -n 1 -r || trueechoif [[ ! $REPLY =~ ^[Yy]$ ]]; thenprint_warning "Uninstall cancelled"@@ -493,7 +528,19 @@ cmd_uninstall() {local lib="$INSTALL_DIR/scripts/lib/uninstall.sh"if [ ! -f "$lib" ]; then+ # Remnants-only box: the install tree (and the shared lib) is gone but+ # traces remain. Don't duplicate the teardown here — point at the+ # standalone uninstaller, which carries its own inline fallback.print_error "Canonical uninstall routine not found at $lib"+ echo ""+ if [ -f "$INSTALL_DIR/uninstall.sh" ]; then+ print_info "Run the standalone uninstaller instead:"+ echo " sudo bash $INSTALL_DIR/uninstall.sh"+ else+ print_info "Fetch and run the standalone uninstaller instead:"+ echo " curl -fsSL https://raw.githubusercontent.com/jhd3197/serverkit/main/uninstall.sh | sudo bash"+ echo " (pass flags with: ... | sudo bash -s -- --purge --yes)"+ fiexit 1fi@@ -625,7 +672,7 @@ cmd_restore_db() {echo "Available backups:"ls -la "$BACKUP_DIR"/*.db 2>/dev/null || echo "No backups found"echo ""- read -p "Enter backup file path: " BACKUP_FILE+ read -p "Enter backup file path: " BACKUP_FILE || trueelseBACKUP_FILE="$1"fi@@ -636,7 +683,7 @@ cmd_restore_db() {fiprint_warning "This will overwrite the current database!"- read -p "Continue? (y/N) " -n 1 -r+ read -p "Continue? (y/N) " -n 1 -r || trueechoif [[ $REPLY =~ ^[Yy]$ ]]; then@@ -798,6 +845,30 @@ cmd_app_logs() {docker compose logs -f}+# Resolve the nginx vhost layout: Debian-style sites-available/sites-enabled+# when present, otherwise the conf.d layout used by the RHEL family. Sets+# SITE_FILE (where the vhost lives) and SITE_LINK (the sites-enabled symlink;+# empty in conf.d layouts, where the vhost file itself is live).+resolve_site_paths() {+ local domain="$1"+ if [ -d "$NGINX_DIR/sites-available" ]; then+ SITE_FILE="$NGINX_DIR/sites-available/${domain}.conf"+ SITE_LINK="$NGINX_DIR/sites-enabled/${domain}.conf"+ else+ SITE_FILE="$NGINX_DIR/conf.d/${domain}.conf"+ SITE_LINK=""+ fi+}++# Reload nginx if we can; on non-systemd or stopped-nginx boxes this is an+# observation, not a fatal error — the config is already on disk either way.+reload_nginx_or_warn() {+ if systemctl reload nginx 2>/dev/null || nginx -s reload 2>/dev/null; then+ return 0+ fi+ print_warning "Could not reload nginx — apply the change manually: systemctl reload nginx (or nginx -s reload)"+}+cmd_add_site() {check_root@@ -813,11 +884,11 @@ cmd_add_site() {exit 1fi- SITE_FILE="/etc/nginx/sites-available/${DOMAIN}.conf"+ resolve_site_paths "$DOMAIN"if [ -f "$SITE_FILE" ]; thenprint_warning "Site config already exists: $SITE_FILE"- read -p "Overwrite? (y/N) " -n 1 -r+ read -p "Overwrite? (y/N) " -n 1 -r || trueechoif [[ ! $REPLY =~ ^[Yy]$ ]]; thenexit 0@@ -825,6 +896,7 @@ cmd_add_site() {fiprint_info "Creating site config for $DOMAIN..."+ mkdir -p "$(dirname "$SITE_FILE")"cat > "$SITE_FILE" << EOF# Site: $DOMAIN@@ -847,12 +919,16 @@ server {}EOF- # Enable the site- ln -sf "$SITE_FILE" /etc/nginx/sites-enabled/+ # Enable the site (Debian layout only — conf.d configs are live as written)+ if [ -n "$SITE_LINK" ]; then+ mkdir -p "$(dirname "$SITE_LINK")"+ ln -sf "$SITE_FILE" "$SITE_LINK"+ fi- # Test and reload nginx+ # Test and reload nginx. A stopped nginx (or a non-systemd box) must not+ # raw-abort here — the site file is already written and valid.if nginx -t 2>/dev/null; then- systemctl reload nginx+ reload_nginx_or_warnprint_success "Site added: $DOMAIN → localhost:$PORT"echo ""echo "Next steps:"@@ -862,7 +938,15 @@ EOFecho ""elseprint_error "Nginx config error. Check: $SITE_FILE"- rm -f "/etc/nginx/sites-enabled/${DOMAIN}.conf"+ if [ -n "$SITE_LINK" ]; then+ rm -f "$SITE_LINK"+ elif mv -f "$SITE_FILE" "${SITE_FILE}.disabled" 2>/dev/null; then+ # conf.d layouts have no enabled/available split — park the broken+ # file so it can't poison an unrelated nginx reload.+ print_info "Broken config parked at: ${SITE_FILE}.disabled"+ else+ print_warning "Remove or fix $SITE_FILE before reloading nginx"+ fiexit 1fi}@@ -873,11 +957,23 @@ cmd_list_sites() {echo "Configured Sites:"echo "─────────────────────────────────────────"- if [ -d /etc/nginx/sites-enabled ]; then- for site in /etc/nginx/sites-enabled/*.conf; do+ # Debian layout lists sites-enabled; conf.d layouts (RHEL family) list conf.d.+ local sites_dir="$NGINX_DIR/sites-enabled"+ if [ ! -d "$sites_dir" ]; then+ sites_dir="$NGINX_DIR/conf.d"+ fi++ if [ -d "$sites_dir" ]; then+ for site in "$sites_dir"/*.conf; doif [ -f "$site" ]; then- DOMAIN=$(grep -m1 "server_name" "$site" | awk '{print $2}' | tr -d ';')- PORT=$(grep -m1 "proxy_pass" "$site" | grep -oP ':\K[0-9]+' || echo "static")+ # `|| true`: a vhost without server_name/proxy_pass is data,+ # not an error — never abort the listing under set -e/pipefail.+ DOMAIN=$(grep -m1 "server_name" "$site" | awk '{print $2}' | tr -d ';' || true)+ # Portable port extraction (GNU-only `grep -oP` broke busybox)+ PORT=$(grep -m1 "proxy_pass" "$site" | sed -n 's/.*:\([0-9][0-9]*\).*/\1/p' || true)+ if [ -z "$PORT" ]; then+ PORT="static"+ fiENABLED="✓"printf " %s %-35s → port %s\n" "$ENABLED" "$DOMAIN" "$PORT"fi@@ -960,7 +1056,7 @@ cmd_doctor() {if docker ps --format '{{.Names}}' 2>/dev/null | grep -q "$FRONTEND_CONTAINER"; thenFRONTEND_RUNNING=true# Get the actual host port the container is mapped to- FRONTEND_PORT=$(docker port "$FRONTEND_CONTAINER" 80 2>/dev/null | head -1 | grep -oP ':\K[0-9]+' || echo "")+ FRONTEND_PORT=$(docker port "$FRONTEND_CONTAINER" 80 2>/dev/null | head -1 | sed -n 's/.*:\([0-9][0-9]*\).*/\1/p' || true)doctor_ok "Container running (port $FRONTEND_PORT)"elsedoctor_issue "Container '$FRONTEND_CONTAINER' is not running"@@ -968,7 +1064,7 @@ cmd_doctor() {cd "$INSTALL_DIR"docker compose up -d 2>/dev/null && {sleep 3- FRONTEND_PORT=$(docker port "$FRONTEND_CONTAINER" 80 2>/dev/null | head -1 | grep -oP ':\K[0-9]+' || echo "")+ FRONTEND_PORT=$(docker port "$FRONTEND_CONTAINER" 80 2>/dev/null | head -1 | sed -n 's/.*:\([0-9][0-9]*\).*/\1/p' || true)FRONTEND_RUNNING=truedoctor_fixed "Started frontend container (port $FRONTEND_PORT)"} || print_error " Could not start container"@@ -1016,7 +1112,7 @@ cmd_doctor() {# Scan ALL enabled nginx configs for competing server_name _# Only serverkit.conf should use server_name _ (the catch-all panel block)- for SITE_CONF in /etc/nginx/sites-enabled/*; do+ for SITE_CONF in "$NGINX_DIR/sites-enabled"/*; do[ -f "$SITE_CONF" ] || continueSITE_NAME=$(basename "$SITE_CONF")# Skip all ServerKit-managed configs@@ -1025,8 +1121,8 @@ cmd_doctor() {if grep -Eq 'server_name[[:space:]]+_;' "$SITE_CONF" 2>/dev/null; thendoctor_issue "'$SITE_NAME' uses server_name _ (conflicts with panel, can hijack all traffic)"if [ "$FIX_MODE" = "--fix" ]; then- rm -f "/etc/nginx/sites-enabled/$SITE_NAME"- rm -f "/etc/nginx/sites-available/$SITE_NAME"+ rm -f "$NGINX_DIR/sites-enabled/$SITE_NAME"+ rm -f "$NGINX_DIR/sites-available/$SITE_NAME"doctor_fixed "Removed conflicting '$SITE_NAME'"elsedoctor_skip "Remove conflicting '$SITE_NAME' from sites-enabled"@@ -1035,20 +1131,20 @@ cmd_doctor() {done# Ensure serverkit-locations directory exists- if [ ! -d /etc/nginx/serverkit-locations ]; then- print_warning "Missing /etc/nginx/serverkit-locations directory"+ if [ ! -d "$NGINX_DIR/serverkit-locations" ]; then+ print_warning "Missing $NGINX_DIR/serverkit-locations directory"if [ "$FIX_MODE" = "--fix" ]; then- mkdir -p /etc/nginx/serverkit-locations- doctor_fixed "Created /etc/nginx/serverkit-locations"+ mkdir -p "$NGINX_DIR/serverkit-locations"+ doctor_fixed "Created $NGINX_DIR/serverkit-locations"else- doctor_skip "mkdir -p /etc/nginx/serverkit-locations"+ doctor_skip "mkdir -p $NGINX_DIR/serverkit-locations"fifi# Check for port mismatch between nginx and frontend container- NGINX_CONF="/etc/nginx/sites-available/serverkit.conf"+ NGINX_CONF="$NGINX_DIR/sites-available/serverkit.conf"if [ -f "$NGINX_CONF" ]; then- NGINX_FRONTEND_PORT=$(grep -m1 'proxy_pass.*127.0.0.1' "$NGINX_CONF" | head -1 | grep -oP ':\K[0-9]+' || echo "")+ NGINX_FRONTEND_PORT=$(grep -m1 'proxy_pass.*127.0.0.1' "$NGINX_CONF" | head -1 | sed -n 's/.*:\([0-9][0-9]*\).*/\1/p' || true)if [ -n "$FRONTEND_PORT" ] && [ -n "$NGINX_FRONTEND_PORT" ]; thenif [ "$NGINX_FRONTEND_PORT" = "$FRONTEND_PORT" ]; then@@ -1071,7 +1167,7 @@ cmd_doctor() {fi# Check API proxy port- NGINX_API_PORT=$(grep -A1 'location /api' "$NGINX_CONF" | grep proxy_pass | grep -oP ':\K[0-9]+' || echo "")+ NGINX_API_PORT=$(grep -A1 'location /api' "$NGINX_CONF" | grep proxy_pass | sed -n 's/.*:\([0-9][0-9]*\).*/\1/p' | head -1 || true)if [ -n "$NGINX_API_PORT" ]; thenif ss -tlnp 2>/dev/null | grep -q ":${NGINX_API_PORT}.*gunicorn\|:${NGINX_API_PORT}.*python"; thendoctor_ok "nginx API proxy port ($NGINX_API_PORT) has backend listening"@@ -1187,6 +1283,17 @@ cmd_doctor() {fifiecho ""++ # Meaningful exit code so doctor can gate scripts: non-zero while issues+ # remain (with --fix, only the ones that could not be auto-fixed count).+ local remaining="$ISSUES"+ if [ "$FIX_MODE" = "--fix" ]; then+ remaining=$((ISSUES - FIXED))+ fi+ if [ "$remaining" -gt 0 ]; then+ return 1+ fi+ return 0}cmd_remove_site() {@@ -1200,8 +1307,7 @@ cmd_remove_site() {exit 1fi- SITE_FILE="/etc/nginx/sites-available/${DOMAIN}.conf"- ENABLED_FILE="/etc/nginx/sites-enabled/${DOMAIN}.conf"+ resolve_site_paths "$DOMAIN"if [ ! -f "$SITE_FILE" ]; thenprint_error "Site not found: $DOMAIN"@@ -1209,18 +1315,26 @@ cmd_remove_site() {fiprint_warning "This will remove the site config for $DOMAIN"- read -p "Continue? (y/N) " -n 1 -r+ read -p "Continue? (y/N) " -n 1 -r || trueechoif [[ $REPLY =~ ^[Yy]$ ]]; then- rm -f "$ENABLED_FILE"+ if [ -n "$SITE_LINK" ]; then+ rm -f "$SITE_LINK"+ firm -f "$SITE_FILE"- systemctl reload nginx+ reload_nginx_or_warnprint_success "Site removed: $DOMAIN"fi}# Main+#+# Sourcing this file (e.g. from scripts/test/test_cli.sh) exposes every+# function above for unit testing without dispatching a command — the same+# pattern as scripts/update.sh. Only direct execution runs the dispatch below.+[ "${BASH_SOURCE[0]}" = "${0}" ] || return 0+case "${1:-help}" inhelp|--help|-h)cmd_help