#!/bin/bash

all_ip_list=(172.18.10.8 172.18.10.239 172.18.10.231 172.18.10.198)

clean_octopus_agent() {
  # 无法运行
  local server
  for server in "${all_ip_list[@]}"; do
    echo "the current server is ${server}"
    ssh root@"${server}" "echo yes"
    ssh root@"${server}" "docker container stop $(docker ps -aq) && docker prune -y"
    ssh root@"${server}" "for mount in $(mount | grep tmpfs | grep '/var/lib/kubelet' | awk '{ print $3 }') /var/lib/kubelet /var/lib/rancher; do umount $mount; done"
    ssh root@"${server}" "rm -rf /etc/ceph  /etc/cni  /etc/kubernetes  /etc/rancher  /opt/cni  /opt/rke  /run/secrets/kubernetes.io  /run/calico  /run/flannel  /var/lib/calico  /var/lib/etcd  /var/lib/cni  /var/lib/kubelet  /var/lib/rancher /var/log/containers  /var/log/kube-audit  /var/log/pods  /var/run/calico"
    ssh root@"${server}" "ip link delete flannel.1"
    ssh root@"${server}" "ip link delete cni0"
    ssh root@"${server}" "ip link delete tunl0"
    ssh root@"${server}" "rmmod ipip"
    ssh root@"${server}" "iptables -F && iptables -t nat -F && iptables -t mangle -F && iptables -t raw -F"
  done
}

clean_rke_cluster() {

  k8s_componet=(kubelet kube-proxy kube-apiserver kube-controller-manager kube-scheduler etcd)
  for componet in ${k8s_componet[@]}; do docker container stop $componet && docker container rm $componet; done


  for mount in $(mount | grep tmpfs | grep '/var/lib/kubelet' | awk  '{ print $3 }') /var/lib/kubelet /var/lib/rancher; do umount $mount; done

  rm -rf /etc/ceph \
    /etc/cni \
    /etc/kubernetes \
    /etc/rancher \
    /opt/cni \
    /opt/rke \
    /run/secrets/kubernetes.io \
    /run/calico \
    /run/flannel \
    /var/lib/calico \
    /var/lib/etcd \
    /var/lib/cni \
    /var/lib/kubelet \
    /var/lib/rancher /var/log/containers \
    /var/log/kube-audit \
    /var/log/pods \
    /var/run/calico

  ip link delete flannel.1
  ip link delete cni0
  ip link delete tunl0
  rmmod ipip

  iptables -F && iptables -t nat -F && iptables -t mangle -F && iptables -t raw -F
  ip6tables -F && ip6tables -t nat -F && ip6tables -t mangle -F && ip6tables -t raw -F


  rke remove --ignore-docker-version --force
  printf "y" | docker container prune


  rke -d up

}


clean_rke_cluster
