#!/usr/bin/env bash
# TODO: it might be good to enhance this script to also update the confured "join" values for node configurations.
if [[ ! -f terraform.tfstate ]]; then
echo "no state file found, are you in the right directory?"
exit 1
fi
client_ips="$(terraform output -json nomad_client_ips | jq -r '.[]')"
for from_ip in ${client_ips}; do
echo "clearing existing values for ${from_ip}"
# TODO: is there a firewalld command we could run to do this instead?
ssh "${from_ip}" "sudo sed -i '/<source address/d' /etc/firewalld/zones/nomad-clients.xml"
ssh "${from_ip}" "sudo firewall-cmd --reload"
for to_ip in ${client_ips}; do
if [[ "${from_ip}" = "${to_ip}" ]]; then
continue
fi
echo "from ${from_ip} trust ${to_ip}"
ssh "${from_ip}" "sudo firewall-cmd --zone=nomad-clients --add-source='${to_ip}' --permanent"
done
echo "reloading for ${from_ip}"
ssh "${from_ip}" "sudo firewall-cmd --reload"
done