#!/usr/bin/env bash
# orchestrate-deploy.sh - To be run on SATURN
# Triggers deployment on remote execution nodes via inventory.

set -e

REPO_PATH="${HOME}/homelab-codex-ws"
USER="oskar"

while IFS=' ' read -r HOST TAG; do
    echo ">>> Triggering deployment on ${HOST}..."
    if [[ "$TAG" == "lte" ]]; then
        ssh -o ConnectTimeout=30 "${USER}@${HOST}" "bash ~/homelab-codex-ws/scripts/deploy/deploy-node.sh" || \
            echo "WARNING: Deployment on ${HOST} failed or timed out (LTE/intermittent node, skipping)"
    else
        ssh "${USER}@${HOST}" "bash ~/homelab-codex-ws/scripts/deploy/deploy-node.sh"
    fi
done < <(python3 -c "
import yaml, sys
with open('${REPO_PATH}/inventory/topology.yaml') as f:
    data = yaml.safe_load(f)
skip = {'saturn', 'solaria'}
for name, info in (data.get('nodes') or {}).items():
    if name in skip:
        continue
    uplink = ((info or {}).get('connectivity') or {}).get('uplink', '')
    print(name, 'lte' if uplink == 'lte' else 'standard')
")

echo ">>> All deployments triggered."
