#!/bin/bash

get_env_info() {
    find_path=`find / -name "auto_install.sh" -type f | awk -F "auto_install.sh" '{print $1}'`
    install_path="${find_path%%/}"
    echo "==============================================="
    echo `"${install_path}"/scripts/remote_env.sh`
    source "${install_path}"/scripts/remote_env.sh
}

get_node_list_name() {
    MasterNodes="${K8S_MASTER01} ${K8S_MASTER02} ${K8S_MASTER03}"
    WorkNodes="${K8S_NODE01} ${K8S_NODE02} ${K8S_NODE03}"
}

get_node_ip() {
    k8s_lb_ip=$K8S_MASTER_LB_IP
    master01_ip=$K8S_MASTER_IP01
    master02_ip=$K8S_MASTER_IP02
    master03_ip=$K8S_MASTER_IP03
    node01_ip=$K8S_NODE_IP01
    node02_ip=$K8S_NODE_IP02
    node03_ip=$K8S_NODE_IP03
    k8s_service_net=$K8S_CLUSTER_NET
    k8s_service_ip=$K8S_SERVICE_IP
    k8s_cluster_dns=$K8S_CLUSTER_DNS
    ssh_port=$SSH_PORT
    master_eth01=$MASTER_ETH01
    master_eth02=$MASTER_ETH02
    master_eth03=$MASTER_ETH03
    cluster_cidr=$DOCKER0_NETWORK
}


destroy_k8s_bin() {
    get_env_info
    get_node_list_name
    get_node_ip
    cd $install_path/soft/k8s1.23/k8s-ha-install/dashboard/ && kubectl delete -f . &
    cd $install_path/soft/k8s1.23/k8s-ha-install/metrics-server/ && kubectl delete -f . &
    cd $install_path/soft/k8s1.23/k8s-ha-install/bootstrap/ && kubectl delete -f . &
    cd $install_path/soft/k8s1.23/k8s-ha-install/calico/ && kubectl delete -f . &
    cd $install_path/soft/k8s1.23/k8s-ha-install/CoreDNS/ && kubectl delete -f . &
    cd $install_path/soft/k8s1.23/k8s-ha-install/pki/ && kubectl delete -f . &
    sleep 15
    systemctl disable etcd
    #source /tmp/scripts/remote_env.sh
    for NODE in $MasterNodes; do
        echo $NODE;
        sshpass ssh -p $ssh_port root@${NODE} 'source /tmp/scripts/remote_env.sh && systemctl stop haproxy && systemctl stop keepalived && systemctl stop kubelet && systemctl stop kube-proxy && systemctl stop kube-scheduler && systemctl stop kube-controller-manager && systemctl stop kube-apiserver && systemctl stop etcd && systemctl stop docker && systemctl disable keepalived && systemctl disable haproxy && systemctl disable etcd && systemctl disable kube-scheduler && systemctl disable kube-controller-manager && systemctl disable kube-proxy && systemctl disable kubelet && systemctl disable kube-apiserver';
        sleep 15;
	sshpass ssh -p $ssh_port root@${NODE} "rm -rf /etc/systemd/system/docker.service &";
	sleep 1
	sshpass ssh -p $ssh_port root@${NODE} "rm -rf /lib/systemd/system/nfs-*";
	sshpass ssh -p $ssh_port root@${NODE} "rm -rf /etc/systemd/system/nfs-*";
	sshpass ssh -p $ssh_port root@${NODE} "rm -rf /lib/systemd/system/rpcbind*";
	sshpass ssh -p $ssh_port root@${NODE} "rm -rf /etc/systemd/system/rpcbind*";
	sshpass ssh -p $ssh_port root@${NODE} "rm -rf /etc/systemd/system/keepalived.service &";
        sshpass ssh -p $ssh_port root@${NODE} 'source /tmp/scripts/remote_env.sh && systemctl stop kubelet && systemctl stop kube-proxy && systemctl stop kube-scheduler && systemctl stop kube-controller-manager && systemctl stop kube-apiserver && systemctl stop etcd && systemctl stop docker';
        sleep 15;
        sshpass ssh -p $ssh_port root@${NODE} "source /tmp/scripts/remote_env.sh && rm -rf /opt/cni/* && rm -rf /etc/kubernetes && rm -rf /var/lib/etcd && rm -rf /var/lib/kubelet/* && rm -rf /var/lib/kubelet  && rm -rf /etc/etcd && rm -rf /etc/systemd/system/kubelet.service.d && rm -rf /var/lib/kubelet && rm -rf /var/lib/calico* && rm -rf /var/log/kubernetes && rm -rf /etc/cni && rm -rf /root/.kube && rm -rf /etc/calico && rm -rf /var/lib/dockershim && rm -rf /var/lib/docker && rm -rf /usr/local/bin/kube* && rm -rf /usr/local/bin/etcd* && rm -rf /usr/bin/containerd* && rm -rf /usr/bin/ctr* && rm -rf /usr/bin/docker* && rm -rf /usr/bin/dockerd*";
        sleep 5;
        sshpass ssh -p $ssh_port root@${NODE} "rm -rf $DOCKER_DATA_ROOT";
        sleep 5;
    done

    for NODE in $WorkNodes; do
        echo $NODE;
        sshpass ssh -p $ssh_port root@${NODE} 'systemctl stop haproxy && systemctl stop keepalived && systemctl stop kubelet && systemctl stop kube-proxy && systemctl stop kube-scheduler && systemctl stop kube-controller-manager && systemctl stop kube-apiserver && systemctl stop etcd && systemctl stop docker';
        sleep 10;
        sshpass ssh -p $ssh_port root@${NODE} 'systemctl stop kubelet && systemctl stop kube-proxy && systemctl stop kube-scheduler && systemctl stop kube-controller-manager && systemctl stop kube-apiserver && systemctl stop etcd && systemctl stop docker';
        sleep 10;
        sshpass ssh -p $ssh_port root@${NODE} "rm -rf /var/lib/kubelet && rm -rf /etc/systemd/system/kubelet.service.d && rm -rf /var/lib/kubelet && rm -rf /etc/calico && rm -rf /var/lib/dockershim && rm -rf /var/lib/docker && rm -rf /usr/local/bin/kube* && rm -rf /usr/local/bin/etcd* && rm -rf /usr/bin/containerd* && rm -rf /usr/bin/ctr* && rm -rf /usr/bin/docker* && rm -rf /usr/bin/dockerd* && rm -rf /var/lib/calico*";
        sleep 5;
        sshpass ssh -p $ssh_port root@${NODE} "rm -rf $DOCKER_DATA_ROOT";
        sleep 5;
    done
    sshpass ssh -p $ssh_port root@${K8S_MASTER02} 'reboot &';
    sleep 1
    sshpass ssh -p $ssh_port root@${K8S_MASTER03} 'reboot &';
    sleep 1
    sshpass ssh -p $ssh_port root@${K8S_NODE02} 'reboot &';
    sleep 1
    sshpass ssh -p $ssh_port root@${K8S_NODE03} 'reboot &';
    sleep 1
    sshpass ssh -p $ssh_port root@${K8S_NODE01} 'reboot &';
    sleep 1
    reboot
}

destroy_k8s_bin
