Files
digiserver-v2/migrate_network.sh
Deployment System ae3b82862d rrrrr
2026-01-21 21:33:32 +02:00

154 lines
5.2 KiB
Bash
Executable File

#!/bin/bash
# Network Migration Script for DigiServer
# Use this when moving the server to a new network with a different IP address
# Example: ./migrate_network.sh 10.55.150.160
set -e
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'
# Check arguments
if [ $# -lt 1 ]; then
echo -e "${RED}❌ Usage: ./migrate_network.sh <new_ip_address> [hostname]${NC}"
echo ""
echo " Example: ./migrate_network.sh 10.55.150.160"
echo " Example: ./migrate_network.sh 10.55.150.160 digiserver-secured"
echo ""
exit 1
fi
NEW_IP="$1"
HOSTNAME="${2:-digiserver}"
EMAIL="${EMAIL:-admin@example.com}"
PORT="${PORT:-443}"
# Validate IP format
if ! [[ "$NEW_IP" =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
echo -e "${RED}❌ Invalid IP address format: $NEW_IP${NC}"
exit 1
fi
echo -e "${BLUE}╔════════════════════════════════════════════════════════════════╗${NC}"
echo -e "${BLUE}║ DigiServer Network Migration ║${NC}"
echo -e "${BLUE}╚════════════════════════════════════════════════════════════════╝${NC}"
echo ""
echo -e "${BLUE}Migration Settings:${NC}"
echo " New IP Address: $NEW_IP"
echo " Hostname: $HOSTNAME"
echo " Email: $EMAIL"
echo " Port: $PORT"
echo ""
# Check if containers are running
echo -e "${YELLOW}🔍 [1/4] Checking containers...${NC}"
if ! docker compose ps | grep -q "digiserver-app"; then
echo -e "${RED}❌ digiserver-app container not running!${NC}"
echo "Please start containers with: docker compose up -d"
exit 1
fi
echo -e "${GREEN}✅ Containers are running${NC}"
echo ""
# Step 1: Regenerate SSL certificates for new IP
echo -e "${YELLOW}🔐 [2/4] Regenerating SSL certificates for new IP...${NC}"
echo " Generating self-signed certificate for $NEW_IP..."
CERT_DIR="./data/nginx-ssl"
mkdir -p "$CERT_DIR"
openssl req -x509 -nodes -days 365 \
-newkey rsa:2048 \
-keyout "$CERT_DIR/key.pem" \
-out "$CERT_DIR/cert.pem" \
-subj "/CN=$NEW_IP/O=DigiServer/C=US" >/dev/null 2>&1
chmod 644 "$CERT_DIR/cert.pem"
chmod 600 "$CERT_DIR/key.pem"
echo -e " ${GREEN}${NC} Certificates regenerated for $NEW_IP"
echo -e "${GREEN}✅ SSL certificates updated${NC}"
echo ""
# Step 2: Update HTTPS configuration in database
echo -e "${YELLOW}🔧 [3/4] Updating HTTPS configuration in database...${NC}"
docker compose exec -T digiserver-app python << EOF
from app.app import create_app
from app.models.https_config import HTTPSConfig
from app.extensions import db
app = create_app('production')
with app.app_context():
# Update or create HTTPS config for the new IP
https_config = HTTPSConfig.query.first()
if https_config:
https_config.hostname = '$HOSTNAME'
https_config.ip_address = '$NEW_IP'
https_config.email = '$EMAIL'
https_config.port = $PORT
https_config.enabled = True
db.session.commit()
print(f" ✓ HTTPS configuration updated")
print(f" Hostname: {https_config.hostname}")
print(f" IP: {https_config.ip_address}")
print(f" Port: {https_config.port}")
else:
print(" ⚠️ No existing HTTPS config found")
print(" This will be created on next app startup")
EOF
echo -e "${GREEN}✅ Database configuration updated${NC}"
echo ""
# Step 3: Restart containers
echo -e "${YELLOW}🔄 [4/4] Restarting containers...${NC}"
docker compose restart nginx digiserver-app
sleep 3
if ! docker compose ps | grep -q "Up"; then
echo -e "${RED}❌ Containers failed to restart!${NC}"
docker compose logs | tail -20
exit 1
fi
echo -e "${GREEN}✅ Containers restarted successfully${NC}"
echo ""
# Verification
echo -e "${YELLOW}🔍 Verifying HTTPS connectivity...${NC}"
sleep 2
if curl -s -k -I https://$NEW_IP 2>/dev/null | grep -q "HTTP"; then
echo -e "${GREEN}✅ HTTPS connection verified${NC}"
else
echo -e "${YELLOW}⚠️ HTTPS verification pending (containers warming up)${NC}"
fi
echo ""
echo -e "${GREEN}╔════════════════════════════════════════════════════════════════╗${NC}"
echo -e "${GREEN}║ ✅ Network Migration Complete! ║${NC}"
echo -e "${GREEN}╚════════════════════════════════════════════════════════════════╝${NC}"
echo ""
echo -e "${BLUE}📍 New Access Points:${NC}"
echo " 🔒 https://$NEW_IP"
echo " 🔒 https://$HOSTNAME.local (if mDNS enabled)"
echo ""
echo -e "${BLUE}📋 Changes Made:${NC}"
echo " ✓ SSL certificates regenerated for $NEW_IP"
echo " ✓ Database HTTPS config updated"
echo " ✓ Nginx and app containers restarted"
echo ""
echo -e "${YELLOW}⏳ Allow 30 seconds for containers to become fully healthy${NC}"
echo ""