#!/usr/bin/env bash # appctl - Unified, Manifest-Driven Container Orchestrator # Discovers individual application repositories located in ~/Sites/*/ # Injects global homelab secrets, per-app metadata from app.yaml, and monitors Git sync set -e SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" ENGINE="$SCRIPT_DIR/appctl_engine.py" ENV_FILE="/run/secrets/rendered/traefik-deployments.env" if [ ! -f "$ENV_FILE" ] && [ -f "/run/secrets/traefik-deployments.env" ]; then ENV_FILE="/run/secrets/traefik-deployments.env" fi export SITES_DIR="${SITES_DIR:-$HOME/Sites}" export CORE_DIR="${CORE_DIR:-$HOME/Core}" export ENV_FILE # Source global secrets/environment if available if [ -f "$ENV_FILE" ]; then set -a # shellcheck disable=SC1090 source "$ENV_FILE" set +a fi # Show help usage show_help() { echo "🚀 appctl - Homelab Container Orchestrator" echo "" echo "Usage: appctl [service] [options...]" echo "" echo "Commands:" echo " list, status [--fetch] [--core] List all applications with Git sync & container status" echo " info Display comprehensive application & Git diagnostics" echo " up Start/build a service stack" echo " down Stop and remove a service stack" echo " restart Restart a service stack" echo " update Full stack update (git pull -> compose pull -> recreate)" echo " pull Pull latest images and recreate service" echo " logs Stream service logs" echo " config Validate and view resolved compose configuration" echo " sync Synchronize Homepage dashboard services.yaml from app.yaml" } # Setup application environment based on resolved metadata setup_app_env() { local query="$1" local resolved_json resolved_json=$(python3 "$ENGINE" resolve "$query" 2>/dev/null || true) if [ -z "$resolved_json" ]; then echo "❌ Error: Application '$query' not found under $SITES_DIR" exit 1 fi # Extract fields via python json APP_DIR=$(python3 -c "import json, sys; d=json.loads(sys.argv[1]); print(d['dir_path'])" "$resolved_json") CANONICAL_NAME=$(python3 -c "import json, sys; d=json.loads(sys.argv[1]); print(d['name'])" "$resolved_json") RESOLVED_DOMAIN=$(python3 -c "import json, sys; d=json.loads(sys.argv[1]); print(d['domain'])" "$resolved_json") # 1. Base infrastructure defaults export PROXY_NETWORK="${PROXY_NETWORK:-proxy-net}" export NETWORK_NAME="${NETWORK_NAME:-$PROXY_NETWORK}" export CERT_RESOLVER="${CERT_RESOLVER:-myresolver}" export DOMAIN_SUFFIX="${DOMAIN_SUFFIX:-roadtotech.me}" # 2. Canonical application identity export SERVICE_NAME="$CANONICAL_NAME" export CONTAINER_NAME="$CANONICAL_NAME" export SERVICE_DOMAIN="$RESOLVED_DOMAIN" export DOMAIN="$RESOLVED_DOMAIN" # 3. Dynamic environment variables from app.yaml (e.g. PROJECT_PATH, MEDIA_PATH, etc.) eval "$(python3 -c " import json, sys d = json.loads(sys.argv[1]) env_dict = d.get('env', {}) for k, v in env_dict.items(): if v != '' and v is not None: print(f'export {k}=\"{v}\"') " "$resolved_json")" # 4. Source local .env if present if [ -f "$APP_DIR/.env" ]; then set -a # shellcheck disable=SC1090 source "$APP_DIR/.env" set +a fi # 5. Source local app.env if present if [ -f "$APP_DIR/app.env" ]; then set -a # shellcheck disable=SC1090 source "$APP_DIR/app.env" set +a fi } if [ $# -lt 1 ]; then show_help exit 1 fi COMMAND=$1 APP=$2 case "$COMMAND" in list|status) python3 "$ENGINE" list "${@:2}" ;; info) if [ -z "$APP" ]; then echo "❌ Error: Service name required"; exit 1; fi python3 "$ENGINE" info "$APP" ;; sync) python3 "$ENGINE" sync "${@:2}" ;; up) if [ -z "$APP" ]; then echo "❌ Error: Service name required"; exit 1; fi setup_app_env "$APP" echo "🚀 Starting $CANONICAL_NAME ($APP_DIR)..." cd "$APP_DIR" && docker compose up -d --remove-orphans "${@:3}" python3 "$ENGINE" sync >/dev/null 2>&1 || true ;; down) if [ -z "$APP" ]; then echo "❌ Error: Service name required"; exit 1; fi setup_app_env "$APP" echo "🛑 Stopping $CANONICAL_NAME ($APP_DIR)..." cd "$APP_DIR" && docker compose down "${@:3}" python3 "$ENGINE" sync >/dev/null 2>&1 || true ;; restart) if [ -z "$APP" ]; then echo "❌ Error: Service name required"; exit 1; fi setup_app_env "$APP" echo "🔄 Restarting $CANONICAL_NAME ($APP_DIR)..." cd "$APP_DIR" && docker compose down && docker compose up -d "${@:3}" python3 "$ENGINE" sync >/dev/null 2>&1 || true ;; update) if [ -z "$APP" ]; then echo "❌ Error: Service name required"; exit 1; fi setup_app_env "$APP" echo "🔄 Updating $CANONICAL_NAME ($APP_DIR)..." if [ -d "$APP_DIR/.git" ]; then if [ -n "$(git -C "$APP_DIR" status --porcelain)" ]; then echo "❌ Error: Cannot update. Working tree in $APP_DIR has uncommitted changes." echo "Commit or stash your changes before updating." exit 1 fi echo "📥 Pulling latest git changes..." git -C "$APP_DIR" pull --ff-only fi echo "🐳 Pulling latest container images..." cd "$APP_DIR" && docker compose pull echo "🚀 Recreating container stack..." cd "$APP_DIR" && docker compose up -d --remove-orphans "${@:3}" python3 "$ENGINE" sync >/dev/null 2>&1 || true echo "✨ $CANONICAL_NAME successfully updated!" ;; pull) if [ -z "$APP" ]; then echo "❌ Error: Service name required"; exit 1; fi setup_app_env "$APP" echo "📥 Pulling latest images for $CANONICAL_NAME..." cd "$APP_DIR" && docker compose pull && docker compose up -d --remove-orphans "${@:3}" ;; logs) if [ -z "$APP" ]; then echo "❌ Error: Service name required"; exit 1; fi setup_app_env "$APP" cd "$APP_DIR" && docker compose logs -f "${@:3}" ;; config) if [ -z "$APP" ]; then echo "❌ Error: Service name required"; exit 1; fi setup_app_env "$APP" cd "$APP_DIR" && docker compose config ;; *) show_help exit 1 ;; esac