solventum-image-metadata/deploy.sh
SamoilenkoVadym bcb2c49e43 Fix deploy.sh: auto-detect sudo for docker commands
git pull runs as current user (needs SSH key), docker commands
use sudo automatically if the user doesn't have docker socket access.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-02-09 21:29:20 +00:00

84 lines
2.3 KiB
Bash
Executable file

#!/bin/bash
# Solventum Image Metadata — Idempotent Deployment Script
# Usage: ./deploy.sh
#
# First run:
# cd /opt/oliver-metadata-tool
# cp .env.example .env # edit with your secrets
# chmod +x deploy.sh
# ./deploy.sh
#
# Subsequent updates:
# cd /opt/oliver-metadata-tool && ./deploy.sh
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
COMPOSE_PROJECT="solventum-image-metadata"
# Use sudo for docker if current user can't access docker socket
DOCKER_CMD="docker"
if ! docker info > /dev/null 2>&1; then
DOCKER_CMD="sudo docker"
fi
cd "$SCRIPT_DIR"
echo "=== Solventum Image Metadata — Deploy ==="
echo "Directory: $SCRIPT_DIR"
echo ""
# 1. Pull latest code from Bitbucket (runs as current user — needs SSH key)
echo ">>> Pulling latest code..."
git pull
# 2. Check .env exists (first-run guard)
if [ ! -f .env ]; then
echo ""
echo "ERROR: .env file not found!"
echo ""
echo " cp .env.example .env"
echo " Then edit .env with your secrets (AZURE_CLIENT_SECRET, SECRET_KEY, etc.)"
echo ""
exit 1
fi
# 3. Build Docker image (uses layer cache, picks up code changes via COPY . .)
echo ">>> Building Docker image..."
$DOCKER_CMD compose -p "$COMPOSE_PROJECT" build
# 4. Start or restart containers (idempotent — creates if missing, restarts if running)
echo ">>> Starting containers..."
$DOCKER_CMD compose -p "$COMPOSE_PROJECT" up -d
# 5. Wait for health check
# Database auto-initializes on first container startup:
# - Tables created via CREATE TABLE IF NOT EXISTS
# - Migrations run in-code (check-before-act pattern)
# - Superadmin created if SUPERADMIN_EMAIL is set
echo ">>> Waiting for app to be healthy..."
HEALTHY=false
for i in $(seq 1 20); do
if curl -sf http://127.0.0.1:5001/login > /dev/null 2>&1; then
echo ">>> App is healthy!"
HEALTHY=true
break
fi
echo " Waiting... ($i/20)"
sleep 3
done
if [ "$HEALTHY" = false ]; then
echo ""
echo "WARNING: App may not be healthy after 60 seconds."
echo "Check logs:"
echo " $DOCKER_CMD compose -p $COMPOSE_PROJECT logs --tail 50"
echo ""
exit 1
fi
echo ""
echo "=== Deploy complete ==="
echo "URL: https://ai-sandbox.oliver.solutions/solventum-image-metadata/"
echo ""
$DOCKER_CMD compose -p "$COMPOSE_PROJECT" ps