Files
PromdataPanel/update.sh
2026-04-06 16:39:19 +08:00

115 lines
3.8 KiB
Bash

#!/bin/bash
# Update Script for PromdataPanel
# -------------------------------
set -e
# Config
APP_DIR="/opt/promdata-panel"
SERVICE_NAME="promdatapanel"
# 1. Colors for logs
GREEN='\033[0;32m'
BLUE='\033[0;34m'
RED='\033[0;31m'
NC='\033[0m' # No Color
echo -e "${BLUE}=== Starting PromdataPanel Update ===${NC}"
# Detect if we're in the app directory
if [ ! -d ".git" ]; then
if [ -d "$APP_DIR/.git" ]; then
cd "$APP_DIR"
else
echo -e "${RED}Error: Not in a git repository or $APP_DIR. Please run this from the app root.${NC}"
exit 1
fi
fi
# 2. Update logic
if [ -d ".git" ]; then
echo -e "${BLUE}Git repository detected. Pulling latest code...${NC}"
git pull
else
echo -e "${BLUE}No git repository found. Updating via ZIP archive...${NC}"
# URL for zip download
ZIP_URL="https://git.littlediary.cn/CN-JS-HuiBai/PromdataPanel/archive/main.zip"
TEMP_DIR="/tmp/promdata_update_$(date +%s)"
mkdir -p "$TEMP_DIR"
echo "Downloading latest version (main branch)..."
curl -L "$ZIP_URL" -o "$TEMP_DIR/latest.zip"
# Ensure unzip is available
if ! command -v unzip &> /dev/null; then
echo -e "${BLUE}unzip is not installed. Attempting to install it...${NC}"
if command -v apt-get &> /dev/null; then
sudo apt-get update && sudo apt-get install -y unzip
elif command -v dnf &> /dev/null; then
sudo dnf install -y unzip
elif command -v yum &> /dev/null; then
sudo yum install -y yum-utils && sudo yum install -y unzip
elif command -v apk &> /dev/null; then
sudo apk add unzip
fi
fi
echo "Extracting archive..."
unzip -q "$TEMP_DIR/latest.zip" -d "$TEMP_DIR"
# The extracted folder is usually named project-main or similar
EXTRACTED_FOLDER=$(ls -d "$TEMP_DIR"/*/ | head -n 1)
# Ensure rsync is available (Auto-install if missing)
if ! command -v rsync &> /dev/null; then
echo -e "${BLUE}rsync is not installed. Attempting to install it...${NC}"
if command -v apt-get &> /dev/null; then
sudo apt-get update && sudo apt-get install -y rsync
elif command -v dnf &> /dev/null; then
sudo dnf install -y rsync
elif command -v yum &> /dev/null; then
sudo yum install -y rsync
elif command -v apk &> /dev/null; then
sudo apk add rsync
else
echo -e "${RED}Error: 'rsync' is not installed and could not auto-install. Please install it manually.${NC}"
rm -rf "$TEMP_DIR"
exit 1
fi
fi
if [ -n "$EXTRACTED_FOLDER" ]; then
echo "Applying updates (preserving .env)..."
# Copy everything except .env
rsync -av --exclude '.env' --exclude 'node_modules' "$EXTRACTED_FOLDER" ./
else
echo -e "${RED}Extraction failed. Please check the archive structure.${NC}"
exit 1
fi
# Cleanup
rm -rf "$TEMP_DIR"
fi
# 3. Update dependencies
echo -e "${BLUE}Updating npm dependencies...${NC}"
npm install --production
# 4. Restart service
if systemctl is-active --quiet "$SERVICE_NAME"; then
echo -e "${BLUE}Restarting systemd service: $SERVICE_NAME...${NC}"
sudo systemctl restart "$SERVICE_NAME"
echo -e "${GREEN}Update completed and service restarted!${NC}"
elif command -v pm2 &> /dev/null && pm2 list | grep -q "$SERVICE_NAME"; then
echo -e "${BLUE}Restarting with PM2...${NC}"
pm2 restart "$SERVICE_NAME"
echo -e "${GREEN}Update completed and PM2 restarted!${NC}"
else
echo -e "${RED}Warning: Could not detect an active systemd service or PM2 process named '$SERVICE_NAME'.${NC}"
echo -e "${RED}Please restart the application manually.${NC}"
fi
echo -e "${GREEN}=== Update successfully finished ===${NC}"