From 90d83fbe29880cb8f30ab90d64c408deb37932b6 Mon Sep 17 00:00:00 2001 From: Drew Minnear Date: Mon, 17 Mar 2025 16:31:39 -0400 Subject: [PATCH] add shell script for deleting hypershift workshop student clusters --- scripts/hcp_destroy_cluster_loop.sh | 46 +++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100755 scripts/hcp_destroy_cluster_loop.sh diff --git a/scripts/hcp_destroy_cluster_loop.sh b/scripts/hcp_destroy_cluster_loop.sh new file mode 100755 index 0000000..71cedf7 --- /dev/null +++ b/scripts/hcp_destroy_cluster_loop.sh @@ -0,0 +1,46 @@ +#!/bin/bash + +REGION="us-west-2" +BASE_DOMAIN="aws.validatedpatterns.io" +ROLE_ARN="arn:aws:iam::296267305927:role/hypershift_cli_role" +STS_CREDS="$HOME/sts-creds/sts-creds.json" + +for i in {1..10}; do + CLUSTER="studentcluster-$i" + + echo "Refreshing STS credentials..." + aws sts get-session-token --output json > "$STS_CREDS" + + if [ $? -ne 0 ]; then + echo "Failed to refresh STS credentials. Aborting..." + exit 1 + fi + + echo "Destroying cluster: $CLUSTER in region: $REGION" + + trap "echo 'Aborting script...'; exit 1" SIGINT + + RETRIES=3 + while [ $RETRIES -gt 0 ]; do + hcp destroy cluster aws --destroy-cloud-resources \ + --name "$CLUSTER" \ + --infra-id "$CLUSTER" \ + --region="$REGION" \ + --sts-creds "$STS_CREDS" \ + --base-domain "$BASE_DOMAIN" \ + --role-arn "$ROLE_ARN" + + if [ $? -eq 0 ]; then + echo "Successfully destroyed $CLUSTER" + break + else + echo "Failed to destroy $CLUSTER, retrying... ($((RETRIES-1)) attempts left)" + ((RETRIES--)) + fi + done + + if [ $RETRIES -eq 0 ]; then + echo "Failed to destroy $CLUSTER after multiple attempts. Moving to the next cluster..." + fi +done +