ARM=$(kubectl get nodes  -o wide --show-labels | grep -v STATUS | grep -v master | grep arm64 | awk '{print $1}')
X86=$(kubectl get nodes  -o wide --show-labels | grep -v STATUS | grep -v control-plane | grep amd64 | awk '{print $1}')
CONTROL=$(kubectl get nodes  -o wide --show-labels | grep -v STATUS | grep control-plane| awk '{print $1}')
DATA=$(kubectl get nodes  -o wide --show-labels | grep -v STATUS | grep -v master| awk '{print $1}')

for i in $DATA ; do
  ./bin/wipe-ceph $i /var/lib/ceph
done

for i in $CONTROL ; do
  ./bin/wipe-ceph $i /var/lib/ceph
done

for i in $DATA ; do
  ./bin/wipe-node $i
done

NODES=()
for i in $DATA ; do
  echo mindwipe $i
  NODES+=(" -n $i ")
done

for i in $CONTROL ; do
  echo mindwipe $i
  NODES+=(" -n $i ")
done

#exit

talosctl reset --system-labels-to-wipe STATE --system-labels-to-wipe EPHEMERAL --graceful=false --reboot ${NODES[@]}

echo "Waiting for master node to come online"
j=0
while ! nc -z 192.168.0.11 50000 ; do
  j=$((j+1))
  echo $j
done

sleep 10

echo "Applying config to control plane nodes"
for i in $CONTROL; do
  while ! nc -z 192.168.0.11 50000 ; do
    j=$((j+1))
    echo $j
  done

  echo "Applying controlplane config to $i"
  talosctl apply --insecure -f nodeconfig/controlplane.yaml -n $i
done

sleep 10

j=0
while ! nc -z 192.168.0.11 50000 ; do
  j=$((j+1))
  echo $j
done

echo "bootstrap master"
talosctl bootstrap -n 192.168.0.11
sleep 20

j=0
while ! nc -z 192.168.0.11 6443 ; do
  j=$((j+1))
  echo $j
done

#echo "applying configs for x86 workers"
#for i in $X86 ; do
#  echo "talosctl apply --insecure -f nodeconfig/worker.yaml -n $i"
#  talosctl apply --insecure -f nodeconfig/worker.yaml -n $i
#done

echo "sleeping 30"
sleep 30

echo "applying configs for arm workers"
for i in $ARM ; do
  echo "talosctl apply --insecure -f nodeconfig/pi.yaml -n $i"
  talosctl apply --insecure -f nodeconfig/pi.yaml -n $i
done



