92e617da14
上一个离线包(696d4f02)打包时 rsync --delete 把 app/install- offline.sh 删掉了(仓库里它在 offline/ 下,包里要求在 app/ 下), 导致用户更新时跑不了安装脚本、frontend/dist 的 chmod 修复没执 行,后端读不到 index.html 报 500。移到根目录后仓库布局与打包 布局一致,rsync 不会再误删。
246 lines
9.5 KiB
Bash
Executable File
246 lines
9.5 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
# ============================================================
|
|
# IPMI Monitor - Offline Install Script
|
|
# ============================================================
|
|
# Installs IPMI Monitor on a host with NO internet access, using the
|
|
# bundled images, .debs and wheels in this package.
|
|
#
|
|
# Layout expected (relative to this script's directory, app/):
|
|
# ../images/influxdb-2.7.tar
|
|
# ../images/python-3.11-slim.tar
|
|
# ../debs/*.deb
|
|
# ../wheels/*.whl
|
|
# docker-compose.offline.yml
|
|
# .env.example
|
|
#
|
|
# Run from app/:
|
|
# bash install-offline.sh
|
|
# ============================================================
|
|
|
|
set -e
|
|
|
|
# Colors
|
|
RED='\033[0;31m'
|
|
GREEN='\033[0;32m'
|
|
YELLOW='\033[1;33m'
|
|
BLUE='\033[0;34m'
|
|
NC='\033[0m'
|
|
|
|
# Resolve the directory this script lives in (app/)
|
|
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
|
cd "$SCRIPT_DIR"
|
|
|
|
echo -e "${BLUE}"
|
|
echo "============================================"
|
|
echo " IPMI Monitor - Offline Install"
|
|
echo "============================================"
|
|
echo -e "${NC}"
|
|
|
|
# ----- Step 1: Check Docker ----
|
|
echo -e "${YELLOW}[1/6] Checking Docker installation...${NC}"
|
|
|
|
if ! command -v docker &> /dev/null; then
|
|
echo -e "${RED}Error: Docker is not installed.${NC}"
|
|
echo "Install Docker from an offline package or a local mirror first,"
|
|
echo "then re-run this script."
|
|
exit 1
|
|
fi
|
|
echo -e "${GREEN} ✓ Docker found: $(docker --version)${NC}"
|
|
|
|
# ----- Step 2: Check Docker Compose ----
|
|
echo -e "${YELLOW}[2/6] Checking Docker Compose installation...${NC}"
|
|
|
|
if docker compose version &> /dev/null 2>&1; then
|
|
COMPOSE_CMD="docker compose"
|
|
echo -e "${GREEN} ✓ Docker Compose (plugin) found: $(docker compose version)${NC}"
|
|
elif command -v docker-compose &> /dev/null; then
|
|
COMPOSE_CMD="docker-compose"
|
|
echo -e "${GREEN} ✓ Docker Compose (standalone) found: $(docker-compose --version)${NC}"
|
|
else
|
|
echo -e "${RED}Error: Docker Compose is not installed.${NC}"
|
|
exit 1
|
|
fi
|
|
|
|
# ----- Step 3: Load bundled images ----
|
|
echo -e "${YELLOW}[3/6] Loading bundled Docker images (offline)...${NC}"
|
|
|
|
for img in influxdb-2.7 python-3.11-slim; do
|
|
tar_file="../images/${img}.tar"
|
|
if [ ! -f "$tar_file" ]; then
|
|
echo -e "${RED}Error: missing $tar_file${NC}"
|
|
echo "The offline package is incomplete. Re-download the bundle."
|
|
exit 1
|
|
fi
|
|
echo " Loading $tar_file ..."
|
|
docker load -i "$tar_file"
|
|
done
|
|
echo -e "${GREEN} ✓ Images loaded${NC}"
|
|
|
|
# ----- Step 4: Check .env ----
|
|
echo -e "${YELLOW}[4/6] Checking environment configuration...${NC}"
|
|
|
|
if [ ! -f .env ]; then
|
|
if [ -f .env.example ]; then
|
|
echo -e "${YELLOW} .env not found, copying from .env.example...${NC}"
|
|
cp .env.example .env
|
|
echo -e "${GREEN} ✓ .env created from .env.example${NC}"
|
|
else
|
|
echo -e "${RED}Error: neither .env nor .env.example found.${NC}"
|
|
exit 1
|
|
fi
|
|
else
|
|
echo -e "${GREEN} ✓ .env file found${NC}"
|
|
fi
|
|
|
|
# ----- Step 4b: Validate required secrets ----
|
|
echo -e "${YELLOW}[4b/6] Checking required secrets in .env...${NC}"
|
|
|
|
get_env_value() {
|
|
grep -E "^$1=" .env 2>/dev/null | tail -n 1 | cut -d'=' -f2- | tr -d '\r' | sed -e 's/^["'\'']//' -e 's/["'\'']$//'
|
|
}
|
|
|
|
looks_like_placeholder() {
|
|
local value="$1"
|
|
local lowered
|
|
lowered=$(printf '%s' "$value" | tr '[:upper:]' '[:lower:]')
|
|
case "$lowered" in
|
|
*changeme*|*example*|*placeholder*|*default*|*todo*) return 0 ;;
|
|
esac
|
|
return 1
|
|
}
|
|
|
|
INFLUXDB_PASSWORD=$(get_env_value INFLUXDB_PASSWORD)
|
|
INFLUXDB_TOKEN=$(get_env_value INFLUXDB_TOKEN)
|
|
JWT_SECRET_KEY=$(get_env_value JWT_SECRET_KEY)
|
|
ADMIN_INITIAL_PASSWORD=$(get_env_value ADMIN_INITIAL_PASSWORD)
|
|
|
|
if [ -z "$INFLUXDB_PASSWORD" ] || looks_like_placeholder "$INFLUXDB_PASSWORD" || [ "${#INFLUXDB_PASSWORD}" -lt 8 ]; then
|
|
echo -e "${RED}Error: INFLUXDB_PASSWORD is missing, too short (< 8 chars), or still a placeholder.${NC}"
|
|
echo "Edit .env and set a strong password before running this script."
|
|
exit 1
|
|
fi
|
|
|
|
if [ -z "$INFLUXDB_TOKEN" ] || looks_like_placeholder "$INFLUXDB_TOKEN"; then
|
|
echo -e "${RED}Error: INFLUXDB_TOKEN is missing or still a placeholder.${NC}"
|
|
echo "Edit .env and set a strong random token before running this script."
|
|
echo "Generate one offline, e.g.: openssl rand -base64 32"
|
|
exit 1
|
|
fi
|
|
|
|
if [ -z "$JWT_SECRET_KEY" ] || looks_like_placeholder "$JWT_SECRET_KEY" || [ "${#JWT_SECRET_KEY}" -lt 32 ]; then
|
|
echo -e "${RED}Error: JWT_SECRET_KEY is missing, too short (< 32 chars), or still a placeholder.${NC}"
|
|
echo "Edit .env and set a secret of at least 32 characters."
|
|
echo "Generate one offline, e.g.: python3 -c \"import secrets; print(secrets.token_urlsafe(32))\""
|
|
exit 1
|
|
fi
|
|
|
|
# The backend refuses to start on a fresh (empty-user) database unless this
|
|
# is set to at least 12 characters. Catch it here instead of letting the
|
|
# backend crash-loop with a RuntimeError after the build.
|
|
if [ -z "$ADMIN_INITIAL_PASSWORD" ] || looks_like_placeholder "$ADMIN_INITIAL_PASSWORD" || [ "${#ADMIN_INITIAL_PASSWORD}" -lt 12 ]; then
|
|
echo -e "${RED}Error: ADMIN_INITIAL_PASSWORD is missing, too short (< 12 chars), or still a placeholder.${NC}"
|
|
echo "Set it in .env to a strong password (at least 12 characters)."
|
|
echo "It is only used to create the initial admin account on first boot;"
|
|
echo "change it right after the first login (see README-OFFLINE.md step 4)."
|
|
echo "Generate one offline, e.g.: openssl rand -base64 16"
|
|
exit 1
|
|
fi
|
|
|
|
echo -e "${GREEN} ✓ Required secrets are set${NC}"
|
|
|
|
# ----- Step 4c: Fix frontend/dist permissions ----
|
|
# The backend runs as non-root (appuser, UID 999) and reads the frontend
|
|
# through a read-only bind mount (./frontend/dist). Files extracted from
|
|
# the tarball often keep restrictive root-owned permissions, which makes
|
|
# the backend crash at startup with:
|
|
# PermissionError: [Errno 13] Permission denied: '/app/frontend/dist/assets'
|
|
# Make the tree world-readable (files) and traversable (directories).
|
|
echo -e "${YELLOW}[4c/6] Fixing frontend/dist permissions...${NC}"
|
|
|
|
if [ -d frontend/dist ]; then
|
|
chmod -R a+rX frontend/dist
|
|
echo -e "${GREEN} ✓ frontend/dist is readable by the backend user${NC}"
|
|
else
|
|
echo -e "${RED}Error: frontend/dist is missing.${NC}"
|
|
echo "The offline package is incomplete. Re-download the bundle."
|
|
exit 1
|
|
fi
|
|
|
|
# ----- Step 5: Build and start ----
|
|
echo -e "${YELLOW}[5/6] Building backend image (offline) and starting services...${NC}"
|
|
echo " (This uses only bundled .debs and wheels — no network.)"
|
|
|
|
$COMPOSE_CMD -f docker-compose.offline.yml down --remove-orphans 2>/dev/null || true
|
|
$COMPOSE_CMD -f docker-compose.offline.yml up -d --build
|
|
|
|
echo -e "${GREEN} ✓ Services started${NC}"
|
|
|
|
# ----- Step 6: Wait for InfluxDB ----
|
|
echo -e "${YELLOW}[6/6] Waiting for InfluxDB to be ready...${NC}"
|
|
|
|
MAX_RETRIES=30
|
|
RETRY_COUNT=0
|
|
INFLUXDB_READY=false
|
|
|
|
INFLUXDB_PORT=$(grep -E "^INFLUXDB_PORT=" .env 2>/dev/null | cut -d'=' -f2 || echo "8086")
|
|
INFLUXDB_PORT=${INFLUXDB_PORT:-8086}
|
|
|
|
while [ $RETRY_COUNT -lt $MAX_RETRIES ]; do
|
|
if curl -sf "http://localhost:${INFLUXDB_PORT}/health" > /dev/null 2>&1; then
|
|
INFLUXDB_READY=true
|
|
break
|
|
fi
|
|
RETRY_COUNT=$((RETRY_COUNT + 1))
|
|
echo " Waiting... ($RETRY_COUNT/$MAX_RETRIES)"
|
|
sleep 2
|
|
done
|
|
|
|
if [ "$INFLUXDB_READY" = true ]; then
|
|
echo -e "${GREEN} ✓ InfluxDB is ready${NC}"
|
|
else
|
|
echo -e "${YELLOW} ⚠ InfluxDB health check timed out, but the service may still be starting.${NC}"
|
|
echo -e "${YELLOW} Check status with: $COMPOSE_CMD -f docker-compose.offline.yml logs influxdb${NC}"
|
|
fi
|
|
|
|
# ----- Done ----
|
|
echo ""
|
|
echo -e "${GREEN}"
|
|
echo "============================================"
|
|
echo " IPMI Monitor is now running (offline)!"
|
|
echo "============================================"
|
|
echo -e "${NC}"
|
|
|
|
BACKEND_PORT=$(grep -E "^BACKEND_PORT=" .env 2>/dev/null | cut -d'=' -f2 || echo "8000")
|
|
BACKEND_PORT=${BACKEND_PORT:-8000}
|
|
|
|
HOST_IP=$(hostname -I 2>/dev/null | awk '{print $1}' || echo "localhost")
|
|
HOST_IP=${HOST_IP:-localhost}
|
|
|
|
echo -e "${BLUE} 📊 Web Interface:${NC} http://${HOST_IP}:${BACKEND_PORT}"
|
|
echo -e "${BLUE} 🗄️ InfluxDB UI:${NC} http://${HOST_IP}:${INFLUXDB_PORT}"
|
|
echo ""
|
|
echo -e "${YELLOW} First login:${NC}"
|
|
echo " Default admin account is created on first boot from"
|
|
echo " ADMIN_INITIAL_PASSWORD in .env (at least 12 characters)."
|
|
echo " Log in, then change the admin password immediately via the API"
|
|
echo " (there is no change-password UI in Settings — its only password"
|
|
echo " field is the SMTP password):"
|
|
echo " POST /api/auth/change-password (see README-OFFLINE.md step 4)."
|
|
echo ""
|
|
echo -e "${YELLOW} Useful commands:${NC}"
|
|
echo " View logs: $COMPOSE_CMD -f docker-compose.offline.yml logs -f"
|
|
echo " View backend: $COMPOSE_CMD -f docker-compose.offline.yml logs -f backend"
|
|
echo " Stop services: $COMPOSE_CMD -f docker-compose.offline.yml down"
|
|
echo " Restart: $COMPOSE_CMD -f docker-compose.offline.yml restart"
|
|
echo ""
|
|
echo -e "${YELLOW} Note:${NC} after editing .env, 'restart' does NOT pick up the new"
|
|
echo " values — the container keeps the environment it was created with."
|
|
echo " Use this instead (it recreates the container with the new .env):"
|
|
echo " $COMPOSE_CMD -f docker-compose.offline.yml up -d"
|
|
echo ""
|
|
echo -e "${YELLOW} Note:${NC} on old Docker (e.g. 23.0.x) the 'docker compose' plugin may"
|
|
echo " reject '-f' ('unknown shorthand flag'). Use the standalone binary:"
|
|
echo " docker-compose -f docker-compose.offline.yml <command>"
|
|
echo ""
|
|
echo -e "${GREEN} Done! Open the Web Interface URL to get started.${NC}"
|