Files
PromdataPanel/install.sh
2026-04-06 15:14:49 +08:00

198 lines
6.1 KiB
Bash

#!/bin/bash
# PromdataPanel - Multi-Prometheus Monitoring Dashboard Installer
# This script handles OS detection, Node.js installation, project setup, and Systemd configuration.
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# 0. Configuration
VERSION=${VERSION:-"v0.1.0"}
DOWNLOAD_URL="https://git.littlediary.cn/CN-JS-HuiBai/PromdataPanel/archive/${VERSION}.zip"
MIN_NODE_VERSION=18
echo -e "${BLUE}================================================${NC}"
echo -e "${BLUE} PromdataPanel Auto-Installer ${NC}"
echo -e "${BLUE} Version: ${VERSION} ${NC}"
echo -e "${BLUE}================================================${NC}"
# 1. OS Detection
detect_os() {
if [ -f /etc/os-release ]; then
. /etc/os-release
OS_ID=$ID
OS_VER=$VERSION_ID
else
echo -e "${RED}Error: Cannot detect operating system type (/etc/os-release missing).${NC}"
exit 1
fi
echo -e "Detected OS: ${GREEN}${OS_ID} ${OS_VER}${NC}"
}
# 2. Node.js Installation/Verification
install_node() {
echo -e "${BLUE}Verifying Node.js environment...${NC}"
NODE_INSTALLED=false
if command -v node &> /dev/null; then
CURRENT_NODE_VER=$(node -v | cut -d'v' -f2 | cut -d'.' -f1)
if [ "$CURRENT_NODE_VER" -ge "$MIN_NODE_VERSION" ]; then
echo -e "${GREEN}Node.js v$(node -v) is already installed.${NC}"
NODE_INSTALLED=true
else
echo -e "${YELLOW}Existing Node.js version (v$(node -v)) is too old (Requires >= $MIN_NODE_VERSION).${NC}"
fi
fi
if [ "$NODE_INSTALLED" = false ]; then
echo -e "${BLUE}Installing Node.js 20.x...${NC}"
case "$OS_ID" in
ubuntu|debian|raspbian)
sudo apt-get update
sudo apt-get install -y ca-certificates curl gnupg
curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -
sudo apt-get install -y nodejs
;;
centos|rhel|almalinux|rocky)
curl -fsSL https://rpm.nodesource.com/setup_20.x | sudo bash -
sudo yum install -y nodejs
;;
fedora)
curl -fsSL https://rpm.nodesource.com/setup_20.x | sudo bash -
sudo dnf install -y nodejs
;;
*)
echo -e "${RED}Unsupported OS for automatic Node.js installation: $OS_ID${NC}"
echo -e "Please install Node.js >= 18 manually.${NC}"
exit 1
;;
esac
fi
}
# 3. Download and Extract (If needed)
if [ ! -f "server/index.js" ]; then
echo -e "${YELLOW}Project files not found. Starting download...${NC}"
if ! command -v curl &> /dev/null; then
echo -e "${BLUE}Installing curl...${NC}"
[ "$OS_ID" = "ubuntu" ] || [ "$OS_ID" = "debian" ] && sudo apt-get install -y curl
[ "$OS_ID" = "centos" ] || [ "$OS_ID" = "rhel" ] && sudo yum install -y curl
fi
if ! command -v unzip &> /dev/null; then
echo -e "${BLUE}Installing unzip...${NC}"
[ "$OS_ID" = "ubuntu" ] || [ "$OS_ID" = "debian" ] && sudo apt-get install -y unzip
[ "$OS_ID" = "centos" ] || [ "$OS_ID" = "rhel" ] && sudo yum install -y unzip
fi
TEMP_ZIP="promdatapanel_${VERSION}.zip"
echo -e "${BLUE}Downloading ${DOWNLOAD_URL}...${NC}"
curl -L "$DOWNLOAD_URL" -o "$TEMP_ZIP"
if [ $? -ne 0 ]; then
echo -e "${RED}Download failed.${NC}"
exit 1
fi
echo -e "${BLUE}Extracting files...${NC}"
unzip -q "$TEMP_ZIP"
EXTRACTED_DIR=$(ls -d */ | grep -E "^PromdataPanel" | head -n 1)
if [ -d "$EXTRACTED_DIR" ]; then
cd "$EXTRACTED_DIR" || exit 1
else
EXTRACTED_DIR=$(ls -d */ | head -n 1)
[ -d "$EXTRACTED_DIR" ] && cd "$EXTRACTED_DIR" || exit 1
fi
rm "../$TEMP_ZIP" 2>/dev/null || rm "$TEMP_ZIP" 2>/dev/null
fi
# 4. Initialize Setup
# Permission check
if [ "$EUID" -eq 0 ]; then
REAL_USER=${SUDO_USER:-$USER}
else
REAL_USER=$USER
fi
detect_os
install_node
PROJECT_DIR=$(pwd)
echo -e "Project Directory: ${GREEN}$PROJECT_DIR${NC}"
echo -e "Running User: ${GREEN}$REAL_USER${NC}"
# Check for .env file
if [ ! -f ".env" ]; then
if [ -f ".env.example" ]; then
echo -e "${BLUE}Creating .env from .env.example...${NC}"
cp .env.example .env
fi
fi
# 5. Install Dependencies
echo -e "${BLUE}Installing NPM dependencies...${NC}"
npm install --production
if [ $? -ne 0 ]; then
echo -e "${RED}NPM install failed.${NC}"
exit 1
fi
# 6. Create Systemd Service File
SERVICE_FILE="/etc/systemd/system/promdatapanel.service"
NODE_PATH=$(command -v node)
echo -e "${BLUE}Creating systemd service at $SERVICE_FILE...${NC}"
sudo bash -c "cat <<EOF > '$SERVICE_FILE'
[Unit]
Description=PromdataPanel Monitoring Dashboard
After=network.target mysql.service redis-server.service valkey-server.service
Wants=mysql.service
[Service]
Type=simple
User=$REAL_USER
WorkingDirectory=$PROJECT_DIR
ExecStart=$NODE_PATH server/index.js
Restart=always
RestartSec=10
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=promdatapanel
EnvironmentFile=-$PROJECT_DIR/.env
Environment=NODE_ENV=production
[Install]
WantedBy=multi-user.target
EOF"
# 7. Reload and Start
echo -e "${BLUE}Reloading systemd and restarting service...${NC}"
sudo systemctl daemon-reload
sudo systemctl enable promdatapanel
sudo systemctl restart promdatapanel
# 8. Check Status
echo -e "${BLUE}Checking service status...${NC}"
sleep 2
if sudo systemctl is-active --quiet promdatapanel; then
echo -e "${GREEN}SUCCESS: PromdataPanel is now running.${NC}"
PORT=$(grep "^PORT=" .env | cut -d'=' -f2)
PORT=${PORT:-3000}
IP_ADDR=$(hostname -I | awk '{print $1}')
echo -e "Dashboard URL: ${YELLOW}http://${IP_ADDR}:${PORT}${NC}"
else
echo -e "${RED}FAILED: Service failed to start.${NC}"
echo -e "Check logs with: ${BLUE}journalctl -u promdatapanel -xe${NC}"
fi
echo -e "${BLUE}================================================${NC}"
echo -e "${GREEN}Installation completed!${NC}"
echo -e "${BLUE}================================================${NC}"