#!/usr/bin/env bash
# Run ONCE on EACH worker node (vps1, and the desktop — not the manager,
# vps2). Joins the Swarm initialized by init-swarm.sh, over the WireGuard
# tunnel. The join token is the same for both workers.
set -euo pipefail

MANAGER_WG_IP="${1:?usage: $0 <manager_wg_ip> <join_token>}"
JOIN_TOKEN="${2:?}"

if docker info 2>/dev/null | grep -q "Swarm: active"; then
  echo "This node is already part of a swarm:"
  docker info 2>/dev/null | grep -A2 "Swarm:"
  exit 0
fi

docker swarm join --token "$JOIN_TOKEN" "${MANAGER_WG_IP}:2377"

echo
echo "Joined. Verify from the manager: docker node ls"
