mnemo_cards/tools/deploy/admin/deploy.sh

105 lines
3.4 KiB
Bash
Raw Normal View History

2025-12-02 23:28:12 +00:00
#!/bin/bash
# Deployment script for Mnemo Cards Admin Panel
# Usage: ./deploy.sh
set -e
# Load configuration
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
source "$SCRIPT_DIR/config.sh"
echo "🚀 Starting deployment of $APP_TITLE..."
# Check if we're in the right directory
check_project_root
# Build the React app for production
build_react_app
print_status "Uploading files to server using rsync..."
# Upload files directly using rsync (much faster and more reliable)
rsync -avz --delete $BUILD_DIR/ "$SERVER_USER@$SERVER_IP:$WEB_ROOT/"
print_status "Uploading nginx configuration..."
# Upload nginx config separately
scp nginx.conf "$SERVER_USER@$SERVER_IP:/tmp/admin_nginx.conf"
print_status "Deploying on server..."
# Execute deployment commands on server
ssh "$SERVER_USER@$SERVER_IP" << EOF
set -e
# Create admin web directory if it doesn't exist
mkdir -p $WEB_ROOT
# Backup existing deployment
if [ -d "$WEB_ROOT" ] && [ "\$(ls -A $WEB_ROOT)" ]; then
echo "Creating backup of existing admin deployment..."
cp -r $WEB_ROOT $BACKUP_DIR.\$(date +%Y%m%d_%H%M%S)
fi
# Set proper permissions
chown -R $WEB_USER:$WEB_GROUP $WEB_ROOT
chmod -R $WEB_PERMISSIONS $WEB_ROOT
# Check SSL certificate status
if [ -d "/etc/letsencrypt/live/admin.mnemo-cards.online" ]; then
echo "✅ Let's Encrypt certificate exists for admin.mnemo-cards.online"
else
echo "⚠️ SSL certificate not found for admin.mnemo-cards.online"
echo " This may cause HTTPS warnings. Certificate should be obtained separately:"
echo " sudo certbot certonly --standalone -d admin.mnemo-cards.online --email $LETSENCRYPT_EMAIL --agree-tos"
echo " Note: This requires stopping nginx temporarily"
# Generate self-signed certificate as fallback for immediate functionality
if [ ! -f "$SSL_SELF_CERT" ]; then
echo " Generating self-signed certificate as fallback..."
openssl req -x509 -nodes -days 365 -newkey rsa:2048 \
-keyout $SSL_SELF_KEY \
-out $SSL_SELF_CERT \
-subj "/C=RU/ST=Moscow/L=Moscow/O=MnemoCards/OU=Admin/CN=admin.mnemo-cards.online"
echo " ✅ Self-signed certificate generated"
fi
fi
# Configure nginx
echo "Configuring nginx for admin panel..."
# Install nginx if not installed
if ! command -v nginx &> /dev/null; then
apt update
apt install -y nginx
fi
# Copy nginx configuration
cp /tmp/admin_nginx.conf $NGINX_CONFIG
# Enable site
ln -sf $NGINX_CONFIG $NGINX_ENABLED
# Remove default nginx site if it exists (only if no other sites exist)
if [ ! -L "/etc/nginx/sites-enabled/mnemo_cards" ]; then
rm -f /etc/nginx/sites-enabled/default
fi
# Test nginx configuration
nginx -t
# Restart nginx
systemctl restart nginx
systemctl enable nginx
# Configure firewall (ports should already be open)
ufw allow '$FIREWALL_ALLOW_NGINX'
ufw allow $FIREWALL_ALLOW_SSH
ufw --force enable
echo "Admin panel deployment completed successfully!"
echo "Application is available at: https://admin.mnemo-cards.online"
EOF
print_success "Deployment completed successfully! 🎉"
print_success "Admin panel is now available at: https://admin.mnemo-cards.online"
print_info "SSL certificate: Let's Encrypt (preferred) or self-signed (fallback)"