diff --git a/.github/workflows/aks-auto-shutdown.yaml b/.github/workflows/aks-auto-shutdown.yaml index 8f3f2609..015a037a 100644 --- a/.github/workflows/aks-auto-shutdown.yaml +++ b/.github/workflows/aks-auto-shutdown.yaml @@ -18,6 +18,6 @@ jobs: tenant-id: 531ff96d-0ae9-462a-8d2d-bec7c0b42082 # HMCTS.NET allow-no-subscriptions: true - name: AKS Auto Shutdown - run: ./scripts/aks/auto-shutdown.sh + run: ./scripts/aks/auto-start-stop.sh stop - name: AKS Auto Shutdown status check run: ./scripts/aks/auto-shutdown-status.sh diff --git a/.github/workflows/aks-auto-start.yaml b/.github/workflows/aks-auto-start.yaml index 70442068..f18dd9d0 100644 --- a/.github/workflows/aks-auto-start.yaml +++ b/.github/workflows/aks-auto-start.yaml @@ -18,4 +18,4 @@ jobs: tenant-id: 531ff96d-0ae9-462a-8d2d-bec7c0b42082 # HMCTS.NET allow-no-subscriptions: true - name: AKS Auto Start - run: ./scripts/aks/auto-start.sh ${{ secrets.REGISTRYSLACKWEBHOOK }} + run: ./scripts/aks/auto-start-stop.sh start ${{ secrets.REGISTRYSLACKWEBHOOK }} diff --git a/scripts/aks/auto-shutdown.sh b/scripts/aks/auto-shutdown.sh deleted file mode 100755 index 1f387c6b..00000000 --- a/scripts/aks/auto-shutdown.sh +++ /dev/null @@ -1,28 +0,0 @@ -#!/usr/bin/env bash - -shopt -s nocasematch -AMBER='\033[1;33m' -GREEN='\033[0;32m' -source scripts/aks/common-functions.sh - -SUBSCRIPTIONS=$(az account list -o json) -jq -c '.[]' <<< $SUBSCRIPTIONS | while read subscription; do - subscription - jq -c '.[]' <<< $CLUSTERS | while read cluster; do - cluster - cluster_env=$(echo $CLUSTER_NAME | cut -d'-' -f2) - cluster_env=${cluster_env/#sbox/Sandbox} - cluster_env=${cluster_env/stg/Staging} - cluster_business_area=$(echo $CLUSTER_NAME | cut -d'-' -f1) - cluster_business_area=${cluster_business_area/ss/cross-cutting} - SKIP=$(should_skip_shutdown $cluster_env $cluster_business_area) - - if [[ $SKIP == "false" ]]; then - echo -e "${GREEN}About to shutdown cluster $CLUSTER_NAME (rg:$RESOURCE_GROUP)" - echo az aks stop --resource-group $RESOURCE_GROUP --name $CLUSTER_NAME --no-wait || echo Ignoring any errors stopping cluster - az aks stop --resource-group $RESOURCE_GROUP --name $CLUSTER_NAME --no-wait || echo Ignoring any errors stopping cluster - else - echo -e "${AMBER}cluster $CLUSTER_NAME (rg:$RESOURCE_GROUP) has been skipped from today's shutdown schedule" - fi - done -done diff --git a/scripts/aks/auto-start-stop.sh b/scripts/aks/auto-start-stop.sh new file mode 100755 index 00000000..a7bdaa03 --- /dev/null +++ b/scripts/aks/auto-start-stop.sh @@ -0,0 +1,53 @@ +#!/usr/bin/env bash + +shopt -s nocasematch +AMBER='\033[1;33m' +GREEN='\033[0;32m' +source scripts/aks/common-functions.sh + +MODE=${1:-start} +registrySlackWebhook=$2 +SKIP="false" + +if [[ "$MODE" != "start" && "$MODE" != "stop" ]]; then + echo "Invalid MODE. Please use 'start' or 'stop'." + exit 1 +fi + +SUBSCRIPTIONS=$(az account list -o json) +jq -c '.[]' <<< $SUBSCRIPTIONS | while read subscription; do + subscription + jq -c '.[]' <<< $CLUSTERS | while read cluster; do + cluster + cluster_env=$(echo $CLUSTER_NAME | cut -d'-' -f2) + cluster_env=${cluster_env/#sbox/Sandbox} + cluster_env=${cluster_env/stg/Staging} + cluster_business_area=$(echo $CLUSTER_NAME | cut -d'-' -f1) + cluster_business_area=${cluster_business_area/ss/cross-cutting} + if [[ $MODE == "stop" ]]; then + SKIP=$(should_skip_shutdown $cluster_env $cluster_business_area) + fi + if [[ $SKIP == "false" ]]; then + echo -e "${GREEN}About to run $MODE operation on cluster $CLUSTER_NAME (rg:$RESOURCE_GROUP)" + echo az aks $MODE --resource-group $RESOURCE_GROUP --name $CLUSTER_NAME --no-wait || echo Ignoring any errors while $MODE operation on cluster + az aks $MODE --resource-group $RESOURCE_GROUP --name $CLUSTER_NAME --no-wait || echo Ignoring any errors while $MODE operation on cluster + else + echo -e "${AMBER}cluster $CLUSTER_NAME (rg:$RESOURCE_GROUP) has been skipped from today's $MODE operation schedule" + fi + done +done + +if [[ $MODE == "start" ]]; then + echo "Waiting 10 mins to give clusters time to start before testing pods" + sleep 600 + + jq -c '.[]' <<< $SUBSCRIPTIONS | while read subscription; do + subscription + jq -c '.[]' <<< $CLUSTERS | while read cluster; do + cluster + check_cluster_status + POWER_STATE=$(az aks show --name $CLUSTER_NAME -g $RESOURCE_GROUP | jq -r .powerState.code) + ts_echo "cluster: $CLUSTER_NAME, Power State : ${POWER_STATE}" + done + done +fi \ No newline at end of file diff --git a/scripts/aks/auto-start.sh b/scripts/aks/auto-start.sh deleted file mode 100755 index e4dbd7ab..00000000 --- a/scripts/aks/auto-start.sh +++ /dev/null @@ -1,32 +0,0 @@ -#!/usr/bin/env bash - -registrySlackWebhook=$1 - -source scripts/aks/common-functions.sh - -function process_clusters() { - jq -c '.[]' <<< $SUBSCRIPTIONS | while read subscription; do - subscription - jq -c '.[]' <<< $CLUSTERS | while read cluster; do - cluster - ts_echo "About to start cluster $CLUSTER_NAME (rg:$RESOURCE_GROUP)" - az aks start --resource-group $RESOURCE_GROUP --name $CLUSTER_NAME --no-wait || ts_echo Ignoring any errors starting cluster $CLUSTER_NAME - done - done - - echo "Waiting 10 mins to give clusters time to start before testing pods" - sleep 600 - - jq -c '.[]' <<< $SUBSCRIPTIONS | while read subscription; do - subscription - jq -c '.[]' <<< $CLUSTERS | while read cluster; do - cluster - check_cluster_status - POWER_STATE=$(az aks show --name $CLUSTER_NAME -g $RESOURCE_GROUP | jq -r .powerState.code) - ts_echo "cluster: $CLUSTER_NAME, Power State : ${RESULT}" - done - done -} - -SUBSCRIPTIONS=$(az account list -o json) -process_clusters