#!/usr/bin/env bash set -euo pipefail BASE_DOMAIN="${NETMAKER_BASE_DOMAIN:-nm.101-96-214-81.nip.io}" ADMIN_EMAIL="${NETMAKER_ADMIN_EMAIL:-admin@abyssinfo.local}" ASSET_PORT="${NETMAKER_ASSET_PORT:-18080}" ASSET_DIR="/tmp/netmaker-assets" QUICK_SCRIPT="/tmp/nm-quick.sh" echo "[AbyssInfo] Deploying Netmaker CE" echo "[AbyssInfo] Base domain: ${BASE_DOMAIN}" if [[ "$(id -u)" != "0" ]]; then echo "[AbyssInfo] Please run as root." >&2 exit 1 fi if [[ ! -f "${QUICK_SCRIPT}" ]]; then echo "[AbyssInfo] Missing ${QUICK_SCRIPT}" >&2 exit 1 fi if [[ ! -d "${ASSET_DIR}" ]]; then echo "[AbyssInfo] Missing ${ASSET_DIR}" >&2 exit 1 fi echo "[AbyssInfo] Installing Netmaker quick-install dependencies ..." dnf install -y wget jq bind-utils gawk wireguard-tools python3 echo "[AbyssInfo] Ensuring Docker is running ..." mkdir -p /etc/docker cat >/etc/docker/daemon.json <<'JSON' { "registry-mirrors": [ "https://docker.1ms.run", "https://docker.m.daocloud.io", "https://hub-mirror.c.163.com" ] } JSON systemctl daemon-reload systemctl restart docker systemctl enable --now docker echo "[AbyssInfo] Stopping Nginx to free ports 80/443 for Netmaker Caddy ..." systemctl disable --now nginx 2>/dev/null || true health_check() { local dashboard_url="https://dashboard.${BASE_DOMAIN}" if [[ ! -f /root/netmaker/docker-compose.yml ]]; then echo "[AbyssInfo] Missing /root/netmaker/docker-compose.yml" >&2 return 1 fi echo "[AbyssInfo] Netmaker containers:" (cd /root/netmaker && docker compose ps) echo "[AbyssInfo] Checking dashboard endpoint: ${dashboard_url}" curl -kfsSI --connect-timeout 15 "${dashboard_url}" >/dev/null } apply_internal_only_defaults() { local env_file="/root/netmaker/netmaker.env" if [[ ! -f "${env_file}" ]]; then return 0 fi echo "[AbyssInfo] Applying internal-only Netmaker client defaults ..." python3 - <<'PY' from pathlib import Path env_path = Path("/root/netmaker/netmaker.env") values = {} order = [] for line in env_path.read_text(encoding="utf-8").splitlines(): if not line or line.startswith("#") or "=" not in line: continue key, value = line.split("=", 1) if key not in values: order.append(key) values[key] = value for key, value in { "DNS_MODE": "off", "MANAGE_DNS": "false", }.items(): if key not in values: order.append(key) values[key] = value env_path.write_text("\n".join(f"{key}={values[key]}" for key in order) + "\n", encoding="utf-8") PY } if [[ "${NETMAKER_FORCE_REINSTALL:-0}" == "1" ]]; then echo "[AbyssInfo] Force reinstall requested. Removing previous Netmaker deployment ..." if [[ -f /root/netmaker/docker-compose.yml ]]; then (cd /root/netmaker && docker compose down --volumes || true) fi rm -rf /root/netmaker /etc/netmaker /etc/netclient elif [[ -f /root/netmaker/docker-compose.yml ]]; then echo "[AbyssInfo] Existing Netmaker deployment found. Starting services and running health check ..." apply_internal_only_defaults (cd /root/netmaker && docker compose up -d) if health_check; then echo "[AbyssInfo] Existing Netmaker deployment is healthy." echo "[AbyssInfo] URLs:" echo " Dashboard: https://dashboard.${BASE_DOMAIN}" echo " API: https://api.${BASE_DOMAIN}" echo " Broker: wss://broker.${BASE_DOMAIN}" exit 0 fi echo "[AbyssInfo] Existing deployment is not healthy. Set NETMAKER_FORCE_REINSTALL=1 to rebuild it." >&2 exit 1 fi echo "[AbyssInfo] Preparing quick installer ..." mkdir -p /root cp "${QUICK_SCRIPT}" /root/nm-quick.sh chmod +x /root/nm-quick.sh # Use uploaded cached assets instead of pulling compose files from GitHub on the server. sed -i "s|local BASE_URL=\"https://raw.githubusercontent.com/gravitl/netmaker/\$BRANCH\"|local BASE_URL=\"http://127.0.0.1:${ASSET_PORT}\"|g" /root/nm-quick.sh echo "[AbyssInfo] Starting temporary local asset server ..." pkill -f "python3 -m http.server ${ASSET_PORT}" 2>/dev/null || true cd "${ASSET_DIR}" python3 -m http.server "${ASSET_PORT}" --bind 127.0.0.1 >/tmp/abyssinfo-netmaker-assets.log 2>&1 & ASSET_PID=$! trap 'kill ${ASSET_PID} 2>/dev/null || true' EXIT sleep 2 curl -fsS "http://127.0.0.1:${ASSET_PORT}/compose/docker-compose.yml" >/dev/null echo "[AbyssInfo] Running Netmaker quick installer ..." cd /root export NM_DOMAIN="${BASE_DOMAIN}" export NM_EMAIL="${ADMIN_EMAIL}" export NM_SKIP_DEPS=1 # Input sequence for nm-quick CE: # 1 = use auto/generated domain, email, y = confirm settings. set +e printf "1\n%s\ny\n" "${ADMIN_EMAIL}" | /root/nm-quick.sh -c QUICK_EXIT=$? set -e echo "[AbyssInfo] Netmaker files:" ls -la /root/netmaker if ! health_check; then echo "[AbyssInfo] Netmaker health check failed after quick installer exit code ${QUICK_EXIT}." >&2 exit 1 fi if [[ "${QUICK_EXIT}" != "0" ]]; then echo "[AbyssInfo] Quick installer exited with ${QUICK_EXIT}, but Netmaker services are healthy. Continuing." fi apply_internal_only_defaults (cd /root/netmaker && docker compose up -d) echo "[AbyssInfo] URLs:" echo " Dashboard: https://dashboard.${BASE_DOMAIN}" echo " API: https://api.${BASE_DOMAIN}" echo " Broker: wss://broker.${BASE_DOMAIN}"