#!/bin/bash # Oliver Metadata Tool — Deployment Script # Usage: ./deploy.sh [--first-run] set -euo pipefail APP_DIR="/var/www/oliver" SERVICE_NAME="oliver-metadata" VENV_DIR="$APP_DIR/venv" REPO_BRANCH="${DEPLOY_BRANCH:-main}" echo "=== Oliver Metadata Tool Deployment ===" echo "Directory: $APP_DIR" echo "Service: $SERVICE_NAME" echo "" # Check we're running as root or with sudo if [ "$EUID" -ne 0 ]; then echo "Please run with sudo" exit 1 fi cd "$APP_DIR" # First run setup if [ "${1:-}" = "--first-run" ]; then echo ">>> First-run setup..." # System dependencies apt-get update apt-get install -y python3.11 python3.11-venv python3.11-dev \ libimage-exiftool-perl tesseract-ocr tesseract-ocr-eng \ tesseract-ocr-chi-sim tesseract-ocr-chi-tra tesseract-ocr-jpn tesseract-ocr-kor \ poppler-utils ffmpeg gcc # Create venv python3.11 -m venv "$VENV_DIR" # Create directories mkdir -p "$APP_DIR/uploads" "$APP_DIR/data" "$APP_DIR/templates_saved" # Set permissions chown -R www-data:www-data "$APP_DIR" # Install systemd service cp "$APP_DIR/deploy/oliver-metadata.service" /etc/systemd/system/ systemctl daemon-reload systemctl enable "$SERVICE_NAME" # Install Apache config (if Apache is installed) if command -v apache2 &> /dev/null; then cp "$APP_DIR/deploy/oliver-metadata.conf" /etc/apache2/sites-available/ a2enmod proxy proxy_http headers rewrite ssl expires a2ensite oliver-metadata echo ">>> Apache config installed. Update SSL paths and restart Apache." fi echo ">>> First-run setup complete." echo ">>> Edit $APP_DIR/.env before starting the service." echo "" fi # Pull latest code echo ">>> Pulling latest code..." sudo -u www-data git pull origin "$REPO_BRANCH" # Install/update Python deps echo ">>> Installing Python dependencies..." "$VENV_DIR/bin/pip" install --upgrade pip "$VENV_DIR/bin/pip" install -r requirements.txt # Restart service echo ">>> Restarting service..." systemctl restart "$SERVICE_NAME" # Wait for health echo ">>> Waiting for service to start..." sleep 3 # Health check for i in {1..10}; do if curl -sf http://127.0.0.1:5001/login > /dev/null 2>&1; then echo ">>> Service is healthy!" systemctl status "$SERVICE_NAME" --no-pager -l echo "" echo "=== Deployment complete ===" exit 0 fi echo " Waiting... ($i/10)" sleep 2 done echo ">>> WARNING: Service may not be healthy. Check logs:" echo " journalctl -u $SERVICE_NAME -n 50 --no-pager" exit 1