From 36f141851b23469fa7ca7b0e16bcc34da67f3c08 Mon Sep 17 00:00:00 2001 From: jingzhao Date: Tue, 12 Nov 2019 21:47:04 +0800 Subject: [PATCH] Add uninstallation scripts In this patch, some un-install scripts were added into IEC project. It will be used for removing the iec infrastructure from K8s Master node and slave node. Signed-off-by: jingzhao Change-Id: I8a300a0b3f8a3121c62e881e722958c793945b7c --- src/foundation/scripts/nginx.sh | 2 +- src/foundation/scripts/uninstall.sh | 100 ++++++++++++++++++++++++++++++++++++ 2 files changed, 101 insertions(+), 1 deletion(-) create mode 100644 src/foundation/scripts/uninstall.sh diff --git a/src/foundation/scripts/nginx.sh b/src/foundation/scripts/nginx.sh index 3e419b9..96ee728 100755 --- a/src/foundation/scripts/nginx.sh +++ b/src/foundation/scripts/nginx.sh @@ -61,7 +61,7 @@ done [ $attempts -gt 0 ] || exit 1 svcip=$(kubectl get services nginx -o json | grep clusterIP | cut -f4 -d'"') -sleep 10 +sleep 20 wget -O /dev/null "http://$svcip" kubectl delete -f "${NGINX_APP}" rm -f "${NGINX_APP}" diff --git a/src/foundation/scripts/uninstall.sh b/src/foundation/scripts/uninstall.sh new file mode 100644 index 0000000..550cc5c --- /dev/null +++ b/src/foundation/scripts/uninstall.sh @@ -0,0 +1,100 @@ +#!/bin/bash +set -x +#Uninstall the k8s-master & k8s-worker node from Mgnt node +# + +# Get OS version +if grep -q -e rhel /etc/*-release; then + OS_ID_LIKE=${1:-rhel} +elif grep -q -e debian /etc/*-release; then + OS_ID_LIKE=${1:-debian} +fi + +# +# Destroy k8s. +# +Destroy_k8s(){ + + KUBEADM_DESTROY_CMD="kubeadm reset -f" + + for worker in "${K8S_WORKER_GROUP[@]}" + do + ip_addr="$(cut -d',' -f1 <<<${worker})" + passwd="$(cut -d',' -f2 <<<${worker})" + echo "Destroy k8s on ${ip_addr}. password:${passwd}" + + sshpass -p ${passwd} ssh -o StrictHostKeyChecking=no ${HOST_USER}@${ip_addr} ${KUBEADM_DESTROY_CMD} + sleep 2 + done + + #Destroy master k8s env + sshpass -p ${K8S_MASTERPW} ssh -o StrictHostKeyChecking=no ${HOST_USER}@${K8S_MASTER_IP} ${KUBEADM_DESTROY_CMD} +} + + +# +# Uninstall software. +# +Uninstall () { + OS_VERSION=$1 + case ${OS_VERSION:-} in + debian) + UNINSTALL_CMD="sudo apt purge -y kubernetes-cni kubeadm kubectl kubelet kube* docker-ce --allow-change-held-packages &&\ + sudo apt -y autoremove" + ;; + rhel) + UNINSTALL_CMD="sudo yum remove -y kubeadm kubectl kubelet kubernetes-cni kube* docker-ce &&\ + sudo yum -y autoremove" + ;; + *) + echo 'Unsupported distribution detected!' + exit 1 + ;; + esac + + for worker in "${K8S_WORKER_GROUP[@]}" + do + ip_addr="$(cut -d',' -f1 <<<${worker})" + passwd="$(cut -d',' -f2 <<<${worker})" + echo "Destroy k8s on ${ip_addr}. password:${passwd}" + + sshpass -p ${passwd} ssh -o StrictHostKeyChecking=no ${HOST_USER}@${ip_addr} "${UNINSTALL_CMD}" + sleep 2 + done + + #master k8s env + sshpass -p ${K8S_MASTERPW} ssh -o StrictHostKeyChecking=no ${HOST_USER}@${K8S_MASTER_IP} "${UNINSTALL_CMD}" +} + + +# +# Recover system configuration. +# +Recover_conf(){ + + REC_CMD="sudo swapon -a" + + for worker in "${K8S_WORKER_GROUP[@]}" + do + ip_addr="$(cut -d',' -f1 <<<${worker})" + passwd="$(cut -d',' -f2 <<<${worker})" + echo "Destroy k8s on ${ip_addr}. password:${passwd}" + + sshpass -p ${passwd} ssh -o StrictHostKeyChecking=no ${HOST_USER}@${ip_addr} "${REC_CMD}" + sleep 2 + done + + #master k8s env + sshpass -p ${K8S_MASTERPW} ssh -o StrictHostKeyChecking=no ${HOST_USER}@${K8S_MASTER_IP} "${REC_CMD}" +} + +# Read the configuration file +source config + +Destroy_k8s ${OS_ID_LIKE} + +Uninstall ${OS_ID_LIKE} +sleep 10 + +Recover_conf ${OS_ID_LIKE} +sleep 20 -- 2.16.6