#!/usr/bin/env bash
# 노드 하나에서 지정한 상대 IP 로 가는 트래픽을 blackhole route 로 버린다(비대칭 네트워크 분리).
# app(172.28.0.20)은 막지 않으므로 클라이언트는 계속 그 노드에 접속할 수 있다.
# 사용: ./scripts/partition.sh on  <node> <상대 IP ...>
#       ./scripts/partition.sh off <node> <상대 IP ...>
#   예: ./scripts/partition.sh on node1 172.28.0.10 172.28.0.12   # node1 에서 monitor, node2 차단
set -euo pipefail
cd "$(dirname "$0")/.."
DC="docker compose"; $DC version >/dev/null 2>&1 || DC=docker-compose
mode="$1"; node="$2"; shift 2
for ip in "$@"; do
  if [ "$mode" = on ]; then $DC exec -T -u root "$node" ip route add blackhole "$ip/32"
  else $DC exec -T -u root "$node" ip route del blackhole "$ip/32"; fi
done
$DC exec -T -u root "$node" ip route show | grep blackhole || echo "$node: no blackhole routes"
