133 lines
4.4 KiB
Bash
133 lines
4.4 KiB
Bash
#!/bin/bash
|
|
|
|
# Update Script for PromdataPanel
|
|
# -------------------------------
|
|
|
|
set -e
|
|
|
|
# Config
|
|
SERVICE_NAME="promdatapanel"
|
|
DEFAULT_APP_DIR="/opt/promdata-panel"
|
|
|
|
# 1. Colors & Banner
|
|
GREEN='\033[0;32m'
|
|
BLUE='\033[0;34m'
|
|
RED='\033[0;31m'
|
|
NC='\033[0m' # No Color
|
|
|
|
echo -e "${BLUE}=== Starting PromdataPanel Update ===${NC}"
|
|
|
|
# 2. Detect APP_DIR automatically from systemd service
|
|
if systemctl list-unit-files | grep -q "^$SERVICE_NAME.service"; then
|
|
echo "Detecting application directory from systemd service..."
|
|
# Get WorkingDirectory from systemd (using env to handle it safely)
|
|
SERVICE_DIR=$(systemctl show -p WorkingDirectory "$SERVICE_NAME" | cut -d= -f2-)
|
|
if [ -n "$SERVICE_DIR" ] && [ -d "$SERVICE_DIR" ]; then
|
|
APP_DIR="$SERVICE_DIR"
|
|
fi
|
|
fi
|
|
|
|
# Fallback 1: Current directory if it contains package.json
|
|
if [ -z "$APP_DIR" ]; then
|
|
if [ -f "package.json" ]; then
|
|
APP_DIR=$(pwd)
|
|
else
|
|
APP_DIR="$DEFAULT_APP_DIR"
|
|
fi
|
|
fi
|
|
|
|
echo -e "${BLUE}Application directory: $APP_DIR${NC}"
|
|
|
|
if [ ! -d "$APP_DIR" ]; then
|
|
echo -e "${RED}Error: Could not find application directory at $APP_DIR. Check if you installed it correctly.${NC}"
|
|
exit 1
|
|
fi
|
|
|
|
cd "$APP_DIR"
|
|
|
|
# 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}"
|