修复安装脚本的潜在问题
This commit is contained in:
333
install.sh
333
install.sh
@@ -1,155 +1,196 @@
|
||||
#!/bin/bash
|
||||
|
||||
# PromdataPanel - Multi-Prometheus Monitoring Dashboard Installer
|
||||
# This script handles OS detection, Node.js installation, project setup, and Systemd configuration.
|
||||
set -euo pipefail
|
||||
|
||||
# Colors for output
|
||||
RED='\033[0;31m'
|
||||
GREEN='\033[0;32m'
|
||||
YELLOW='\033[1;33m'
|
||||
BLUE='\033[0;34m'
|
||||
NC='\033[0m' # No Color
|
||||
NC='\033[0m'
|
||||
|
||||
# 0. Configuration
|
||||
VERSION=${VERSION:-"v0.1.0"}
|
||||
DOWNLOAD_URL="https://git.littlediary.cn/CN-JS-HuiBai/PromdataPanel/archive/${VERSION}.zip"
|
||||
MIN_NODE_VERSION=18
|
||||
SERVICE_NAME="promdatapanel"
|
||||
SERVICE_FILE="/etc/systemd/system/${SERVICE_NAME}.service"
|
||||
|
||||
OS_ID=""
|
||||
OS_VER=""
|
||||
PROJECT_DIR=""
|
||||
REAL_USER=""
|
||||
|
||||
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
|
||||
# shellcheck disable=SC1091
|
||||
. /etc/os-release
|
||||
OS_ID=$ID
|
||||
OS_VER=$VERSION_ID
|
||||
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}"
|
||||
if [ -z "$OS_ID" ]; then
|
||||
echo -e "${RED}Error: Unable to determine operating system ID.${NC}"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
echo -e "Detected OS: ${GREEN}${OS_ID} ${OS_VER}${NC}"
|
||||
}
|
||||
|
||||
require_cmd() {
|
||||
local cmd="$1"
|
||||
local hint="${2:-}"
|
||||
if ! command -v "$cmd" >/dev/null 2>&1; then
|
||||
echo -e "${RED}Missing required command: ${cmd}.${NC}"
|
||||
if [ -n "$hint" ]; then
|
||||
echo -e "${YELLOW}${hint}${NC}"
|
||||
fi
|
||||
exit 1
|
||||
fi
|
||||
}
|
||||
|
||||
install_packages() {
|
||||
case "$OS_ID" in
|
||||
ubuntu|debian|raspbian)
|
||||
sudo apt-get update
|
||||
sudo apt-get install -y "$@"
|
||||
;;
|
||||
centos|rhel|almalinux|rocky)
|
||||
sudo yum install -y "$@"
|
||||
;;
|
||||
fedora)
|
||||
sudo dnf install -y "$@"
|
||||
;;
|
||||
*)
|
||||
echo -e "${RED}Unsupported OS for automatic package installation: ${OS_ID}${NC}"
|
||||
echo -e "${YELLOW}Please install the following packages manually: $*${NC}"
|
||||
exit 1
|
||||
;;
|
||||
esac
|
||||
}
|
||||
|
||||
ensure_tooling() {
|
||||
if ! command -v curl >/dev/null 2>&1; then
|
||||
echo -e "${BLUE}Installing curl...${NC}"
|
||||
install_packages curl
|
||||
fi
|
||||
|
||||
if ! command -v unzip >/dev/null 2>&1; then
|
||||
echo -e "${BLUE}Installing unzip...${NC}"
|
||||
install_packages unzip
|
||||
fi
|
||||
}
|
||||
|
||||
configure_nodesource_apt_repo() {
|
||||
sudo install -d -m 0755 /etc/apt/keyrings
|
||||
curl -fsSL https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key | sudo gpg --dearmor -o /etc/apt/keyrings/nodesource.gpg
|
||||
echo "deb [signed-by=/etc/apt/keyrings/nodesource.gpg] https://deb.nodesource.com/node_20.x nodistro main" | sudo tee /etc/apt/sources.list.d/nodesource.list >/dev/null
|
||||
}
|
||||
|
||||
install_node() {
|
||||
echo -e "${BLUE}Verifying Node.js environment...${NC}"
|
||||
|
||||
local node_installed=false
|
||||
if command -v node >/dev/null 2>&1; then
|
||||
local current_node_ver
|
||||
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 $(node -v) is already installed.${NC}"
|
||||
node_installed=true
|
||||
else
|
||||
echo -e "${YELLOW}Existing Node.js $(node -v) is too old (requires >= ${MIN_NODE_VERSION}).${NC}"
|
||||
fi
|
||||
fi
|
||||
|
||||
if [ "$node_installed" = true ]; then
|
||||
return
|
||||
fi
|
||||
|
||||
echo -e "${BLUE}Installing Node.js 20.x...${NC}"
|
||||
case "$OS_ID" in
|
||||
ubuntu|debian|raspbian)
|
||||
install_packages ca-certificates curl gnupg
|
||||
configure_nodesource_apt_repo
|
||||
sudo apt-get update
|
||||
sudo apt-get install -y nodejs
|
||||
;;
|
||||
centos|rhel|almalinux|rocky)
|
||||
install_packages nodejs
|
||||
;;
|
||||
fedora)
|
||||
install_packages nodejs
|
||||
;;
|
||||
*)
|
||||
echo -e "${RED}Unsupported OS for automatic Node.js installation: ${OS_ID}${NC}"
|
||||
echo -e "${YELLOW}Please install Node.js >= ${MIN_NODE_VERSION} manually.${NC}"
|
||||
exit 1
|
||||
;;
|
||||
esac
|
||||
|
||||
require_cmd node "Please install Node.js >= ${MIN_NODE_VERSION} manually and rerun the installer."
|
||||
local installed_major
|
||||
installed_major=$(node -v | cut -d'v' -f2 | cut -d'.' -f1)
|
||||
if [ "$installed_major" -lt "$MIN_NODE_VERSION" ]; then
|
||||
echo -e "${RED}Installed Node.js $(node -v) is still below the required version.${NC}"
|
||||
echo -e "${YELLOW}Please upgrade Node.js manually to >= ${MIN_NODE_VERSION}.${NC}"
|
||||
exit 1
|
||||
fi
|
||||
}
|
||||
|
||||
download_project_if_needed() {
|
||||
if [ -f "server/index.js" ]; then
|
||||
return
|
||||
fi
|
||||
|
||||
echo -e "${YELLOW}Project files not found. Starting download...${NC}"
|
||||
ensure_tooling
|
||||
|
||||
local temp_dir
|
||||
temp_dir=$(mktemp -d "${TMPDIR:-/tmp}/promdatapanel-install-XXXXXX")
|
||||
local temp_zip="${temp_dir}/promdatapanel_${VERSION}.zip"
|
||||
|
||||
echo -e "${BLUE}Downloading ${DOWNLOAD_URL}...${NC}"
|
||||
curl -fL "$DOWNLOAD_URL" -o "$temp_zip"
|
||||
|
||||
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
|
||||
unzip -q "$temp_zip" -d "$temp_dir"
|
||||
|
||||
local extracted_dir
|
||||
extracted_dir=$(find "$temp_dir" -mindepth 1 -maxdepth 1 -type d | head -n 1)
|
||||
if [ -z "$extracted_dir" ] || [ ! -f "$extracted_dir/server/index.js" ]; then
|
||||
echo -e "${RED}Download succeeded, but archive structure is invalid.${NC}"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
cd "$extracted_dir"
|
||||
}
|
||||
|
||||
detect_runtime_user() {
|
||||
if [ "$EUID" -eq 0 ]; then
|
||||
REAL_USER="${SUDO_USER:-${USER:-root}}"
|
||||
else
|
||||
EXTRACTED_DIR=$(ls -d */ | head -n 1)
|
||||
[ -d "$EXTRACTED_DIR" ] && cd "$EXTRACTED_DIR" || exit 1
|
||||
REAL_USER="${USER}"
|
||||
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
|
||||
write_service_file() {
|
||||
local node_path
|
||||
node_path=$(command -v node)
|
||||
if [ -z "$node_path" ]; then
|
||||
echo -e "${RED}Unable to locate node executable after installation.${NC}"
|
||||
exit 1
|
||||
fi
|
||||
fi
|
||||
|
||||
# 5. Install Dependencies
|
||||
echo -e "${BLUE}Installing NPM dependencies...${NC}"
|
||||
npm install --production
|
||||
local tmp_service
|
||||
tmp_service=$(mktemp "${TMPDIR:-/tmp}/${SERVICE_NAME}.service.XXXXXX")
|
||||
|
||||
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'
|
||||
cat > "$tmp_service" <<EOF
|
||||
[Unit]
|
||||
Description=PromdataPanel Monitoring Dashboard
|
||||
After=network.target mysql.service redis-server.service valkey-server.service
|
||||
@@ -157,39 +198,63 @@ Wants=mysql.service
|
||||
|
||||
[Service]
|
||||
Type=simple
|
||||
User=$REAL_USER
|
||||
WorkingDirectory=$PROJECT_DIR
|
||||
ExecStart=$NODE_PATH server/index.js
|
||||
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
|
||||
StandardOutput=journal
|
||||
StandardError=journal
|
||||
SyslogIdentifier=${SERVICE_NAME}
|
||||
EnvironmentFile=-${PROJECT_DIR}/.env
|
||||
Environment=NODE_ENV=production
|
||||
|
||||
[Install]
|
||||
WantedBy=multi-user.target
|
||||
EOF"
|
||||
EOF
|
||||
|
||||
echo -e "${BLUE}Creating systemd service at ${SERVICE_FILE}...${NC}"
|
||||
sudo install -m 0644 "$tmp_service" "$SERVICE_FILE"
|
||||
rm -f "$tmp_service"
|
||||
}
|
||||
|
||||
detect_os
|
||||
download_project_if_needed
|
||||
detect_runtime_user
|
||||
install_node
|
||||
|
||||
PROJECT_DIR=$(pwd)
|
||||
echo -e "Project Directory: ${GREEN}${PROJECT_DIR}${NC}"
|
||||
echo -e "Running User: ${GREEN}${REAL_USER}${NC}"
|
||||
|
||||
if [ ! -f ".env" ] && [ -f ".env.example" ]; then
|
||||
echo -e "${BLUE}Creating .env from .env.example...${NC}"
|
||||
cp .env.example .env
|
||||
fi
|
||||
|
||||
echo -e "${BLUE}Installing NPM dependencies...${NC}"
|
||||
npm install --production
|
||||
|
||||
write_service_file
|
||||
|
||||
# 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
|
||||
sudo systemctl enable "$SERVICE_NAME"
|
||||
sudo systemctl restart "$SERVICE_NAME"
|
||||
|
||||
# 8. Check Status
|
||||
echo -e "${BLUE}Checking service status...${NC}"
|
||||
sleep 2
|
||||
if sudo systemctl is-active --quiet promdatapanel; then
|
||||
if sudo systemctl is-active --quiet "$SERVICE_NAME"; then
|
||||
echo -e "${GREEN}SUCCESS: PromdataPanel is now running.${NC}"
|
||||
PORT=$(grep "^PORT=" .env | cut -d'=' -f2)
|
||||
PORT=$(grep "^PORT=" .env 2>/dev/null | cut -d'=' -f2 || true)
|
||||
PORT=${PORT:-3000}
|
||||
IP_ADDR=$(hostname -I | awk '{print $1}')
|
||||
echo -e "Dashboard URL: ${YELLOW}http://${IP_ADDR}:${PORT}${NC}"
|
||||
IP_ADDR=$(hostname -I 2>/dev/null | awk '{print $1}')
|
||||
if [ -n "${IP_ADDR:-}" ]; then
|
||||
echo -e "Dashboard URL: ${YELLOW}http://${IP_ADDR}:${PORT}${NC}"
|
||||
fi
|
||||
else
|
||||
echo -e "${RED}FAILED: Service failed to start.${NC}"
|
||||
echo -e "Check logs with: ${BLUE}journalctl -u promdatapanel -xe${NC}"
|
||||
echo -e "Check logs with: ${BLUE}journalctl -u ${SERVICE_NAME} -xe${NC}"
|
||||
fi
|
||||
|
||||
echo -e "${BLUE}================================================${NC}"
|
||||
|
||||
Reference in New Issue
Block a user