#!/bin/bash # Deploy ontology and schema files to the GLAM SPARQL server # Usage: ./deploy-data.sh [server_ip_or_hostname] set -e # Configuration SERVER=${1:-$(cd terraform && terraform output -raw server_ip 2>/dev/null || echo "")} REMOTE_USER="root" REMOTE_DATA_DIR="/mnt/data" # Local directories (relative to project root) PROJECT_ROOT="$(cd "$(dirname "$0")/.." && pwd)" ONTOLOGY_DIR="$PROJECT_ROOT/data/ontology" RDF_DIR="$PROJECT_ROOT/schemas/20251121/rdf" LINKML_DIR="$PROJECT_ROOT/schemas/20251121/linkml" UML_DIR="$PROJECT_ROOT/schemas/20251121/uml/mermaid" FRONTEND_DIR="$PROJECT_ROOT/frontend/dist" if [ -z "$SERVER" ]; then echo "Error: Server IP not provided and couldn't be determined from Terraform" echo "Usage: $0 [server_ip_or_hostname]" exit 1 fi echo "═══════════════════════════════════════════════════════════════" echo " GLAM Data Deployment" echo " Target: $REMOTE_USER@$SERVER" echo "═══════════════════════════════════════════════════════════════" echo "" # Function to sync a directory sync_dir() { local LOCAL_DIR="$1" local REMOTE_SUBDIR="$2" local FILE_PATTERN="${3:-*}" if [ -d "$LOCAL_DIR" ]; then echo "📁 Syncing $REMOTE_SUBDIR..." rsync -avz --progress \ --include="$FILE_PATTERN" \ --exclude="*.pyc" \ --exclude="__pycache__" \ --exclude=".DS_Store" \ "$LOCAL_DIR/" \ "$REMOTE_USER@$SERVER:$REMOTE_DATA_DIR/$REMOTE_SUBDIR/" echo " ✅ Done" echo "" else echo "⚠️ Directory not found: $LOCAL_DIR" echo "" fi } # Check SSH connectivity echo "🔗 Testing SSH connection..." if ! ssh -o ConnectTimeout=5 "$REMOTE_USER@$SERVER" "echo 'Connected'" 2>/dev/null; then echo "❌ Cannot connect to $SERVER" echo " Make sure:" echo " - Server is running" echo " - SSH key is configured" echo " - IP address is correct" exit 1 fi echo " ✅ Connected" echo "" # Create remote directories echo "📂 Creating remote directories..." ssh "$REMOTE_USER@$SERVER" "mkdir -p $REMOTE_DATA_DIR/{ontologies,rdf,linkml,uml}" echo "" # Sync ontology files sync_dir "$ONTOLOGY_DIR" "ontologies" "*.ttl *.rdf *.owl *.jsonld" # Sync generated RDF files (only latest versions) if [ -d "$RDF_DIR" ]; then echo "📁 Syncing RDF schemas (latest versions only)..." # Get the latest timestamp from the filenames LATEST_TS=$(ls -1 "$RDF_DIR"/*.owl.ttl 2>/dev/null | sort -r | head -1 | grep -oE '[0-9]{8}_[0-9]{6}' | head -1) if [ -n "$LATEST_TS" ]; then rsync -avz --progress \ --include="*${LATEST_TS}*" \ --exclude="archive_*" \ "$RDF_DIR/" \ "$REMOTE_USER@$SERVER:$REMOTE_DATA_DIR/rdf/" echo " ✅ Synced files with timestamp $LATEST_TS" else rsync -avz --progress \ --exclude="archive_*" \ "$RDF_DIR/" \ "$REMOTE_USER@$SERVER:$REMOTE_DATA_DIR/rdf/" echo " ✅ Synced all RDF files" fi echo "" fi # Sync LinkML schemas sync_dir "$LINKML_DIR" "linkml" "*.yaml" # Sync UML diagrams sync_dir "$UML_DIR" "uml" "*.mmd" # Sync frontend build (if exists) if [ -d "$FRONTEND_DIR" ]; then echo "📁 Syncing frontend build..." rsync -avz --progress \ --delete \ "$FRONTEND_DIR/" \ "$REMOTE_USER@$SERVER:/var/www/glam-frontend/" echo " ✅ Done" echo "" fi # Load data into Oxigraph echo "═══════════════════════════════════════════════════════════════" echo " Loading data into Oxigraph" echo "═══════════════════════════════════════════════════════════════" echo "" read -p "Load ontologies into Oxigraph now? [y/N] " -n 1 -r echo "" if [[ $REPLY =~ ^[Yy]$ ]]; then echo "🔄 Loading ontologies..." ssh "$REMOTE_USER@$SERVER" "/var/lib/glam/scripts/load-ontologies.sh" echo "" fi # Show status echo "═══════════════════════════════════════════════════════════════" echo " Deployment Complete" echo "═══════════════════════════════════════════════════════════════" echo "" echo "Server: $SERVER" echo "SPARQL Query: https://$(ssh "$REMOTE_USER@$SERVER" "cat /etc/caddy/Caddyfile | head -1 | tr -d ' {'")/query" echo "" echo "Check status with:" echo " ssh $REMOTE_USER@$SERVER 'systemctl status oxigraph'" echo " ssh $REMOTE_USER@$SERVER 'systemctl status caddy'" echo ""