289 lines
9 KiB
Bash
289 lines
9 KiB
Bash
#!/bin/bash
|
||
|
||
################################################################################
|
||
# Video Optimizer - Deployment Script
|
||
################################################################################
|
||
# This script deploys the backend application to /opt/video-optimizer-back
|
||
# Run this script AFTER copying files to /opt/video-optimizer-back
|
||
#
|
||
# Usage:
|
||
# sudo bash deploy.sh [--initial]
|
||
#
|
||
# Options:
|
||
# --initial First-time deployment (creates venv, installs dependencies)
|
||
# On subsequent runs without this flag, script will:
|
||
# - Update code
|
||
# - Restart the systemd service
|
||
################################################################################
|
||
|
||
set -e # Exit on any error
|
||
|
||
# Colors for output
|
||
RED='\033[0;31m'
|
||
GREEN='\033[0;32m'
|
||
YELLOW='\033[1;33m'
|
||
BLUE='\033[0;34m'
|
||
NC='\033[0m' # No Color
|
||
|
||
# Configuration
|
||
APP_DIR="/opt/video-optimizer-back"
|
||
VENV_DIR="$APP_DIR/venv"
|
||
BACKEND_DIR="$APP_DIR/backend"
|
||
SERVICE_NAME="video-optimizer-backend"
|
||
APP_USER="www-data"
|
||
APP_GROUP="www-data"
|
||
|
||
# Check if running as root
|
||
if [ "$EUID" -ne 0 ]; then
|
||
echo -e "${RED}ERROR: This script must be run as root (use sudo)${NC}"
|
||
exit 1
|
||
fi
|
||
|
||
# Check if initial deployment flag is set
|
||
INITIAL_DEPLOY=false
|
||
if [ "$1" == "--initial" ]; then
|
||
INITIAL_DEPLOY=true
|
||
fi
|
||
|
||
################################################################################
|
||
# Functions
|
||
################################################################################
|
||
|
||
print_header() {
|
||
echo ""
|
||
echo -e "${BLUE}========================================${NC}"
|
||
echo -e "${BLUE}$1${NC}"
|
||
echo -e "${BLUE}========================================${NC}"
|
||
echo ""
|
||
}
|
||
|
||
print_success() {
|
||
echo -e "${GREEN}✓ $1${NC}"
|
||
}
|
||
|
||
print_error() {
|
||
echo -e "${RED}✗ $1${NC}"
|
||
}
|
||
|
||
print_warning() {
|
||
echo -e "${YELLOW}⚠ $1${NC}"
|
||
}
|
||
|
||
print_info() {
|
||
echo -e "${BLUE}ℹ $1${NC}"
|
||
}
|
||
|
||
################################################################################
|
||
# Main Deployment
|
||
################################################################################
|
||
|
||
print_header "Video Optimizer - Backend Deployment"
|
||
|
||
# Check if app directory exists
|
||
if [ ! -d "$APP_DIR" ]; then
|
||
print_error "Application directory not found: $APP_DIR"
|
||
print_info "Please copy application files to $APP_DIR first"
|
||
exit 1
|
||
fi
|
||
|
||
print_success "Application directory found: $APP_DIR"
|
||
|
||
# Change to app directory
|
||
cd "$APP_DIR"
|
||
|
||
################################################################################
|
||
# Initial Setup (only on first deployment)
|
||
################################################################################
|
||
|
||
if [ "$INITIAL_DEPLOY" = true ]; then
|
||
print_header "Initial Deployment Setup"
|
||
|
||
# Check if Python 3 is installed
|
||
if ! command -v python3 &> /dev/null; then
|
||
print_error "Python 3 is not installed"
|
||
print_info "Install with: sudo apt-get install python3 python3-venv python3-pip"
|
||
exit 1
|
||
fi
|
||
print_success "Python 3 found: $(python3 --version)"
|
||
|
||
# Check if FFmpeg is installed
|
||
if ! command -v ffmpeg &> /dev/null; then
|
||
print_warning "FFmpeg is not installed - video conversion will not work!"
|
||
print_info "Install with: sudo apt-get install ffmpeg"
|
||
read -p "Continue anyway? (y/n) " -n 1 -r
|
||
echo
|
||
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
|
||
exit 1
|
||
fi
|
||
else
|
||
print_success "FFmpeg found: $(ffmpeg -version | head -n1)"
|
||
fi
|
||
|
||
# Create virtual environment
|
||
print_info "Creating Python virtual environment..."
|
||
if [ -d "$VENV_DIR" ]; then
|
||
print_warning "Virtual environment already exists, removing old one..."
|
||
rm -rf "$VENV_DIR"
|
||
fi
|
||
python3 -m venv "$VENV_DIR"
|
||
print_success "Virtual environment created"
|
||
|
||
# Activate virtual environment and install dependencies
|
||
print_info "Installing Python dependencies..."
|
||
source "$VENV_DIR/bin/activate"
|
||
pip install --upgrade pip
|
||
pip install -r "$BACKEND_DIR/requirements.txt"
|
||
deactivate
|
||
print_success "Dependencies installed"
|
||
|
||
# Check if .env file exists
|
||
if [ ! -f "$APP_DIR/.env" ]; then
|
||
print_error ".env file not found!"
|
||
print_info "Please create $APP_DIR/.env from .env.example"
|
||
print_info "Required settings:"
|
||
print_info " - FLASK_ENV=production"
|
||
print_info " - AZURE_CLIENT_ID=<your-client-id>"
|
||
print_info " - AZURE_TENANT_ID=<your-tenant-id>"
|
||
print_info " - REDIRECT_URI=https://ai-sandbox.oliver.solutions/video-optimizer"
|
||
print_info " - FRONTEND_URL=https://ai-sandbox.oliver.solutions/video-optimizer"
|
||
print_info " - SECRET_KEY=<generate-random-key>"
|
||
exit 1
|
||
fi
|
||
print_success ".env file found"
|
||
|
||
else
|
||
print_header "Updating Existing Deployment"
|
||
|
||
# Check if venv exists
|
||
if [ ! -d "$VENV_DIR" ]; then
|
||
print_error "Virtual environment not found!"
|
||
print_info "Run with --initial flag for first-time setup: sudo bash deploy.sh --initial"
|
||
exit 1
|
||
fi
|
||
print_success "Virtual environment found"
|
||
|
||
# Update dependencies (in case requirements.txt changed)
|
||
print_info "Updating Python dependencies..."
|
||
source "$VENV_DIR/bin/activate"
|
||
pip install --upgrade pip
|
||
pip install -r "$BACKEND_DIR/requirements.txt"
|
||
deactivate
|
||
print_success "Dependencies updated"
|
||
fi
|
||
|
||
################################################################################
|
||
# Create Required Directories
|
||
################################################################################
|
||
|
||
print_info "Creating required directories..."
|
||
mkdir -p "$BACKEND_DIR/uploads"
|
||
mkdir -p "$BACKEND_DIR/outputs"
|
||
mkdir -p "$BACKEND_DIR/logs"
|
||
mkdir -p "$BACKEND_DIR/logs/conversions"
|
||
print_success "Directories created"
|
||
|
||
################################################################################
|
||
# Set Permissions
|
||
################################################################################
|
||
|
||
print_info "Setting file permissions..."
|
||
|
||
# Set ownership to www-data for entire app directory
|
||
chown -R $APP_USER:$APP_GROUP "$APP_DIR"
|
||
|
||
# Ensure www-data can write to working directories
|
||
chmod -R 755 "$APP_DIR"
|
||
chmod -R 775 "$BACKEND_DIR/uploads"
|
||
chmod -R 775 "$BACKEND_DIR/outputs"
|
||
chmod -R 775 "$BACKEND_DIR/logs"
|
||
|
||
# Protect .env file (readable only by owner)
|
||
if [ -f "$APP_DIR/.env" ]; then
|
||
chmod 600 "$APP_DIR/.env"
|
||
chown $APP_USER:$APP_GROUP "$APP_DIR/.env"
|
||
fi
|
||
|
||
print_success "Permissions set"
|
||
|
||
################################################################################
|
||
# Service Management
|
||
################################################################################
|
||
|
||
print_header "Service Management"
|
||
|
||
# Check if service is running
|
||
if systemctl is-active --quiet $SERVICE_NAME; then
|
||
print_info "Stopping $SERVICE_NAME service..."
|
||
systemctl stop $SERVICE_NAME
|
||
print_success "Service stopped"
|
||
fi
|
||
|
||
# Reload systemd daemon
|
||
print_info "Reloading systemd daemon..."
|
||
systemctl daemon-reload
|
||
print_success "Systemd daemon reloaded"
|
||
|
||
# Enable service (if not already enabled)
|
||
if ! systemctl is-enabled --quiet $SERVICE_NAME; then
|
||
print_info "Enabling $SERVICE_NAME service..."
|
||
systemctl enable $SERVICE_NAME
|
||
print_success "Service enabled"
|
||
else
|
||
print_success "Service already enabled"
|
||
fi
|
||
|
||
# Start service
|
||
print_info "Starting $SERVICE_NAME service..."
|
||
systemctl start $SERVICE_NAME
|
||
sleep 2
|
||
|
||
# Check if service started successfully
|
||
if systemctl is-active --quiet $SERVICE_NAME; then
|
||
print_success "Service started successfully"
|
||
else
|
||
print_error "Service failed to start!"
|
||
print_info "Check logs with: sudo journalctl -u $SERVICE_NAME -n 50 --no-pager"
|
||
exit 1
|
||
fi
|
||
|
||
################################################################################
|
||
# Verification
|
||
################################################################################
|
||
|
||
print_header "Deployment Verification"
|
||
|
||
# Check service status
|
||
print_info "Service status:"
|
||
systemctl status $SERVICE_NAME --no-pager -l | head -n 10
|
||
|
||
# Test health endpoint
|
||
print_info "Testing health endpoint..."
|
||
sleep 3
|
||
if curl -f http://localhost:5000/api/health > /dev/null 2>&1; then
|
||
print_success "Backend API is responding"
|
||
else
|
||
print_warning "Backend API is not responding (may still be starting up)"
|
||
print_info "Check logs with: sudo journalctl -u $SERVICE_NAME -f"
|
||
fi
|
||
|
||
################################################################################
|
||
# Summary
|
||
################################################################################
|
||
|
||
print_header "Deployment Complete!"
|
||
|
||
echo -e "Backend Service: ${GREEN}Running${NC}"
|
||
echo -e "Service Name: $SERVICE_NAME"
|
||
echo -e "Application Dir: $APP_DIR"
|
||
echo ""
|
||
echo "Useful Commands:"
|
||
echo " View logs: sudo journalctl -u $SERVICE_NAME -f"
|
||
echo " Restart service: sudo systemctl restart $SERVICE_NAME"
|
||
echo " Stop service: sudo systemctl stop $SERVICE_NAME"
|
||
echo " Service status: sudo systemctl status $SERVICE_NAME"
|
||
echo ""
|
||
echo "Next Steps:"
|
||
echo " 1. Copy frontend files to /var/www/html/video-optimizer"
|
||
echo " 2. Configure Apache (see DEPLOYMENT.md)"
|
||
echo " 3. Test application: https://ai-sandbox.oliver.solutions/video-optimizer"
|
||
echo ""
|