Delete Cluster
Users can also delete a cluster on the Rafay Controller from an external system such as Jenkins. For clusters that are provisioned by Rafay on cloud infrastructure, the Rafay Controller will ensure that all associated infrastructre is also deprovisioned.
Automation Scripts¶
Rafay has developed high level, easy to use "bash scripts" that you can use for both provisioning and deprovisioning. You can embed these scripts "as is" into an existing CI system such as Jenkins for build out automation.¶
Requirements¶
- Linux OS
- Ensure you have cURL installed and available.
Deprovision Cluster¶
Use this script to deprovision a cluster managed by the Rafay Controller. The script expects the following items as input
- Access credentials for Rafay Controller
- Name of cluster to be deleted in the Rafay Controller
- Name of the Project in which the cluster exists
#!/bin/bash
SCRIPT='cluster_delete.sh'
function HELP {
echo -e "\nUsage: $SCRIPT [-u <Username>] [-p <Password>] [-n <Rafay Cluster Name>] [-o <Project name>]"
echo -e "\t-u Username to login to the Rafay Console"
echo -e "\t-p Password to login to the Rafay Console"
echo -e "\t-n Name of the Cluster to be deleted in Rafay Console"
echo -e "\t-o Name of the Project in which cluster needs to be deleted in Rafay Console"
echo -e "\t-h Displays this help message. No further functions are performed."
echo
echo -e "\nExample: $SCRIPT -u [email protected] -p test123 -n Dev-Cluster-1 -o project"
echo
exit 1
}
NUMARGS=$#
if [ $NUMARGS -eq 0 ]; then
HELP
fi
while getopts :u:p:n:h FLAG; do
case $FLAG in
u)
USERNAME=$OPTARG
;;
p)
PASSWORD=$OPTARG
;;
n)
CLUSTER_NAME=$OPTARG
;;
o)
PROJECT_NAME=$OPTARG
;;
h) #show help
HELP
;;
\?) #unrecognized option - show help
echo -e \\n"Option -$OPTARG not recognized."
HELP
;;
esac
done
USERDATA='{"username":"'"${USERNAME}"'","password":"'"${PASSWORD}"'"}'
OPS_HOST="console.rafay.dev"
GET_EDGE_URL="https://ops.stage.rafay.dev/edge/v1/edges/"
Providers_URL="https://ops.stage.rafay.dev/edge/v1/providers/"
GET_ALL_EDGES="https://ops.stage.rafay.dev/edge/v1/opsedges/"
PROVIDER_NAME="AWS1"
curl -k -vvvv -d ${USERDATA} -H "x-rafay-partner: rx28oml" -H "content-type: application/json;charset=UTF-8" -X POST https://${OPS_HOST}/auth/v1/login/ > /tmp/$$_curl 2>&1
csrf_token=`grep -inr "set-cookie: csrftoken" /tmp/$$_curl | cut -d'=' -f2 | cut -d';' -f1`
rsid=`grep -inr "set-cookie: rsid" /tmp/$$_curl | cut -d'=' -f2 | cut -d';' -f1`
rm /tmp/$$_curl
LookupCluster () {
local cluster match="$1"
shift
for cluster; do [[ "$cluster" == "$match" ]] && return 0; done
return 1
}
projects=`curl -k -s -H "x-rafay-partner: rx28oml" -H "content-type: application/json;charset=UTF-8" -H "x-csrftoken: ${csrf_token}" -H "cookie: partnerID=rx28oml; csrftoken=${csrf_token}; rsid=${rsid}" https://${OPS_HOST}/auth/v1/projects/ | jq '.results[]|.name,.id' |cut -d'"' -f2`
PROJECTS_ARRAY=( $projects )
LookupProvider $PROJECT "${PROJECTS_ARRAY[@]}"
[ $? -ne 0 ] && echo -e " !! Could not find the project with the name $PROJECT !! Exiting " && exit -1
for i in "${!PROJECTS_ARRAY[@]}";
do
if [ ${PROJECTS_ARRAY[$i]} == "null" ];
then
echo "No Projects found !! Exiting" && exit -1
elif [ ${PROJECTS_ARRAY[$i]} == $PROJECT ];
then
PROJECT_ID=${PROJECTS_ARRAY[$(( $i + 1))]}
break
fi
done
CLUSTERS=`curl -k -s -H "content-type: application/json;charset=UTF-8" -H "referer: https://${OPS_HOST}/" -H "x-rafay-partner: rx28oml" -H "x-csrftoken: ${csrf_token}" -H "cookie: partnerID=rx28oml; csrftoken=${csrf_token}; rsid=${rsid}" https://${OPS_HOST}/edge/v1/projects/${PROJECT_ID}/edges/?limit=1000 |jq '.results[]|.name,.id' |cut -d'"' -f2`
CLUSTERS_ARRAY=( $CLUSTERS )
CLUSTER_NAME=`echo $CLUSTER_NAME |tr '[:upper:]' '[:lower:]'`
LookupCluster $CLUSTER_NAME "${CLUSTERS_ARRAY[@]}"
[ $? -ne 0 ] && echo -e " !! Could not find the Cluster with the name $CLUSTER_NAME !! Exiting " && exit -1
for i in "${!CLUSTERS_ARRAY[@]}";
do
if [ ${CLUSTERS_ARRAY[$i]} == $CLUSTER_NAME ];
then
CLUSTER_TO_BE_DELTED=${CLUSTERS_ARRAY[$(( $i + 1))]}
break
fi
done
curl -k -vvv -H "x-rafay-partner: rx28oml" -H "content-type: application/json;charset=UTF-8" -H "x-csrftoken: ${csrf_token}" -H "cookie: partnerID=rx28oml; csrftoken=${csrf_token}; rsid=${rsid}" -X DELETE https://${OPS_HOST}/edge/v1/edges/${CLUSTER_TO_BE_DELTED}/ > /tmp/$$_curl 2>&1
grep 'HTTP/2 200' /tmp/$$_curl > /dev/null 2>&1
[ $? -ne 0 ] && DBG=`cat /tmp/$$_curl` && echo -e " !! Detected failure removing cluster ${DBG}!! Exiting " && exit -1
rm /tmp/$$_curl
echo "Deleted Cluster "${CLUSTER_TO_BE_DELTED}""
providers=`curl -k -s -H "x-rafay-partner: rx28oml" -H "content-type: application/json;charset=UTF-8" -H "x-csrftoken: ${csrf_token}" -H "cookie: partnerID=rx28oml; csrftoken=${csrf_token}; rsid=${rsid}" https://${OPS_HOST}/edge/v1/providers/ | jq '.results[]|.name,.ID' |cut -d'"' -f2`
PROVIDERS_ARRAY=( $providers )
LookupCluster $PROVIDER_NAME "${PROVIDERS_ARRAY[@]}"
[ $? -ne 0 ] && echo -e " !! Could not find the provider with the name $PROVIDER_NAME !! Exiting " && exit -1
for i in "${!PROVIDERS_ARRAY[@]}";
do
if [ ${PROVIDERS_ARRAY[$i]} == "null" ];
then
echo "No Cloud Providers found !! Exiting" && exit -1
elif [ ${PROVIDERS_ARRAY[$i]} == $PROVIDER_NAME ];
then
PROVIDER_TO_BE_DELTED=${PROVIDERS_ARRAY[$(( $i + 1))]}
break
fi
done
curl -k -vvv -H "x-rafay-partner: rx28oml" -H "content-type: application/json;charset=UTF-8" -H "x-csrftoken: ${csrf_token}" -H "cookie: partnerID=rx28oml; csrftoken=${csrf_token}; rsid=${rsid}" -X DELETE https://${OPS_HOST}/edge/v1/providers/${PROVIDER_TO_BE_DELTED}/ > /tmp/$$_curl 2>&1
grep 'HTTP/2 200' /tmp/$$_curl > /dev/null 2>&1
[ $? -ne 0 ] && DBG=`cat /tmp/$$_curl` && echo -e " !! Detected failure removing provider ${DBG}!! Exiting " && exit -1
rm /tmp/$$_curl
echo "Deleted Provider "${PROVIDER_TO_BE_DELTED}""
--