Files
2026-04-20 12:48:24 +08:00

136 lines
4.6 KiB
Bash

#!/usr/bin/env bash
# ============================================================
# IPMI Monitor - Start Script
# ============================================================
# This script simplifies the deployment of IPMI Monitor
# using Docker Compose.
# ============================================================
set -e
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Get the directory where this script is located
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
cd "$SCRIPT_DIR"
echo -e "${BLUE}"
echo "============================================"
echo " IPMI Monitor - Deployment Script"
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 "Please install Docker first: https://docs.docker.com/get-docker/"
exit 1
fi
DOCKER_VERSION=$(docker --version 2>/dev/null)
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"
COMPOSE_VERSION=$(docker compose version 2>/dev/null)
echo -e "${GREEN} ✓ Docker Compose (plugin) found: $COMPOSE_VERSION${NC}"
elif command -v docker-compose &> /dev/null; then
COMPOSE_CMD="docker-compose"
COMPOSE_VERSION=$(docker-compose --version 2>/dev/null)
echo -e "${GREEN} ✓ Docker Compose (standalone) found: $COMPOSE_VERSION${NC}"
else
echo -e "${RED}Error: Docker Compose is not installed.${NC}"
echo "Please install Docker Compose: https://docs.docker.com/compose/install/"
exit 1
fi
# ----- Step 3: Check .env file -----
echo -e "${YELLOW}[3/6] Checking environment configuration...${NC}"
if [ ! -f .env ]; then
if [ -f .env.example ]; then
echo -e "${YELLOW} .env file not found, copying from .env.example...${NC}"
cp .env.example .env
echo -e "${GREEN} ✓ .env file created from .env.example${NC}"
echo -e "${YELLOW} ⚠ Please review and update .env with your configuration!${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 4: Start Docker Compose services -----
echo -e "${YELLOW}[4/6] Starting Docker Compose services...${NC}"
$COMPOSE_CMD down --remove-orphans 2>/dev/null || true
$COMPOSE_CMD up -d --build
echo -e "${GREEN} ✓ Services started${NC}"
# ----- Step 5: Wait for InfluxDB -----
echo -e "${YELLOW}[5/6] Waiting for InfluxDB to be ready...${NC}"
MAX_RETRIES=30
RETRY_COUNT=0
INFLUXDB_READY=false
# Read InfluxDB port from .env or use default
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 service may still be starting.${NC}"
echo -e "${YELLOW} You can check status with: docker compose logs influxdb${NC}"
fi
# ----- Step 6: Display access information -----
echo ""
echo -e "${GREEN}"
echo "============================================"
echo " IPMI Monitor is now running!"
echo "============================================"
echo -e "${NC}"
# Read ports from .env or use defaults
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} 📖 API Docs:${NC} http://${HOST_IP}:${BACKEND_PORT}/docs"
echo -e "${BLUE} 🗄️ InfluxDB UI:${NC} http://${HOST_IP}:${INFLUXDB_PORT}"
echo ""
echo -e "${YELLOW} Useful commands:${NC}"
echo " View logs: $COMPOSE_CMD logs -f"
echo " View backend: $COMPOSE_CMD logs -f backend"
echo " Stop services: $COMPOSE_CMD down"
echo " Restart: $COMPOSE_CMD restart"
echo ""
echo -e "${GREEN} Done! Open the Web Interface URL to get started.${NC}"