From e23f46ff21a6c54349b11115922acf2b318fef38 Mon Sep 17 00:00:00 2001 From: Jordan Hoey Date: Mon, 14 Oct 2024 15:13:27 +0100 Subject: [PATCH] formatting and clean up --- .github/workflows/aks-auto-shutdown.yaml | 8 ++-- .github/workflows/aks-auto-start.yaml | 8 ++-- scripts/aks/auto-shutdown-status.sh | 8 ++-- scripts/aks/auto-start-stop.sh | 4 +- scripts/aks/common-functions.sh | 54 ++++++++++++------------ scripts/aks/set-subscription.sh | 37 ---------------- 6 files changed, 41 insertions(+), 78 deletions(-) delete mode 100644 scripts/aks/set-subscription.sh diff --git a/.github/workflows/aks-auto-shutdown.yaml b/.github/workflows/aks-auto-shutdown.yaml index 6faba592..debee598 100644 --- a/.github/workflows/aks-auto-shutdown.yaml +++ b/.github/workflows/aks-auto-shutdown.yaml @@ -2,7 +2,7 @@ name: aks-auto-shutdown on: workflow_dispatch: schedule: - - cron: '0 19,22 * * *' # Every day at 20:00 and 23:00 BST + - cron: "0 19,22 * * *" # Every day at 20:00 and 23:00 BST env: DEV_ENV: ${{ secrets.DEV_ENV }} permissions: @@ -14,13 +14,13 @@ jobs: - name: Checkout uses: actions/checkout@v4 - - name: 'Az CLI login' + - name: "Az CLI login" uses: azure/login@v2 with: client-id: 2b6fa9d7-7dba-4600-a58a-5e25554997aa # DTS AKS Auto-Shutdown tenant-id: 531ff96d-0ae9-462a-8d2d-bec7c0b42082 # HMCTS.NET allow-no-subscriptions: true - + - name: Staging - AKS Auto Stop run: ./scripts/aks/auto-start-stop.sh stop staging env: @@ -65,4 +65,4 @@ jobs: - name: AKS Auto Shutdown status check if: env.DEV_ENV != 'true' - run: ./scripts/aks/auto-shutdown-status.sh stop ${{ secrets.REGISTRYSLACKWEBHOOK }} \ No newline at end of file + run: ./scripts/aks/auto-shutdown-status.sh stop ${{ secrets.REGISTRYSLACKWEBHOOK }} diff --git a/.github/workflows/aks-auto-start.yaml b/.github/workflows/aks-auto-start.yaml index 31118681..053acc85 100644 --- a/.github/workflows/aks-auto-start.yaml +++ b/.github/workflows/aks-auto-start.yaml @@ -2,7 +2,7 @@ name: aks-auto-start on: workflow_dispatch: schedule: - - cron: '30 5 * * 1-5' # Every weekday at 6:30am BST + - cron: "30 5 * * 1-5" # Every weekday at 6:30am BST env: DEV_ENV: ${{ secrets.DEV_ENV }} permissions: @@ -14,13 +14,13 @@ jobs: - name: Checkout uses: actions/checkout@v4 - - name: 'Az CLI login' + - name: "Az CLI login" uses: azure/login@v2 with: client-id: 2b6fa9d7-7dba-4600-a58a-5e25554997aa # DTS AKS Auto-Shutdown tenant-id: 531ff96d-0ae9-462a-8d2d-bec7c0b42082 # HMCTS.NET allow-no-subscriptions: true - + - name: Staging - AKS Auto Start run: ./scripts/aks/auto-start-stop.sh start staging env: @@ -65,4 +65,4 @@ jobs: - name: AKS Auto Shutdown status check #if: env.DEV_ENV != 'true' - run: ./scripts/aks/auto-shutdown-status.sh start ${{ secrets.REGISTRYSLACKWEBHOOK }} \ No newline at end of file + run: ./scripts/aks/auto-shutdown-status.sh start ${{ secrets.REGISTRYSLACKWEBHOOK }} diff --git a/scripts/aks/auto-shutdown-status.sh b/scripts/aks/auto-shutdown-status.sh index 23530166..9f2e7cf3 100755 --- a/scripts/aks/auto-shutdown-status.sh +++ b/scripts/aks/auto-shutdown-status.sh @@ -11,11 +11,11 @@ MODE=${1:-start} registrySlackWebhook=$2 CLUSTERS=$(get_clusters) -clusters_count=$(jq -c -r '.count' <<< $CLUSTERS) +clusters_count=$(jq -c -r '.count' <<<$CLUSTERS) log "$clusters_count AKS Clusters found" log "----------------------------------------------" -jq -c '.data[]' <<< $CLUSTERS | while read cluster; do +jq -c '.data[]' <<<$CLUSTERS | while read cluster; do get_cluster_details if [[ $cluster_status == "Stopped" ]]; then @@ -24,6 +24,6 @@ jq -c '.data[]' <<< $CLUSTERS | while read cluster; do echo -e "${AMBER}$CLUSTER_NAME is $cluster_status" fi if [[ $MODE == "start" ]]; then - check_cluster_status + check_cluster_status fi -done \ No newline at end of file +done diff --git a/scripts/aks/auto-start-stop.sh b/scripts/aks/auto-start-stop.sh index 4e88e27d..8682b979 100755 --- a/scripts/aks/auto-start-stop.sh +++ b/scripts/aks/auto-start-stop.sh @@ -15,7 +15,7 @@ if [[ "$MODE" != "start" && "$MODE" != "stop" ]]; then fi CLUSTERS=$(get_clusters "$2") -clusters_count=$(jq -c -r '.count' <<< $CLUSTERS) +clusters_count=$(jq -c -r '.count' <<<$CLUSTERS) log "$clusters_count AKS Clusters found" log "----------------------------------------------" @@ -55,4 +55,4 @@ jq -c '.data[]' <<<$CLUSTERS | while read cluster; do else ts_echo_color AMBER "cluster $CLUSTER_NAME (rg:$RESOURCE_GROUP) has been skipped from today's $MODE operation schedule" fi -done \ No newline at end of file +done diff --git a/scripts/aks/common-functions.sh b/scripts/aks/common-functions.sh index a4f64d6d..eab3874f 100644 --- a/scripts/aks/common-functions.sh +++ b/scripts/aks/common-functions.sh @@ -1,18 +1,18 @@ #!/bin/bash function get_clusters() { - #MS az graph query to find and return a list of all AKS tagged to be included in the auto-shutdown process. - log "----------------------------------------------" - log "Running az graph query..." + #MS az graph query to find and return a list of all AKS tagged to be included in the auto-shutdown process. + log "----------------------------------------------" + log "Running az graph query..." - if [ -z $1 ]; then - env_selector="" - elif [ $1 == "untagged" ]; then - env_selector="| where isnull(tags.environment)" - else - env_selector="| where tags.environment == '$1'" - fi + if [ -z $1 ]; then + env_selector="" + elif [ $1 == "untagged" ]; then + env_selector="| where isnull(tags.environment)" + else + env_selector="| where tags.environment == '$1'" + fi - az graph query -q " + az graph query -q " resources | where type =~ 'Microsoft.ContainerService/managedClusters' | where tags.autoShutdown == 'true' @@ -20,25 +20,25 @@ function get_clusters() { | project name, resourceGroup, subscriptionId, ['tags'], properties, ['id'] " --first 1000 -o json - log "az graph query complete" + log "az graph query complete" } function get_cluster_details() { - RESOURCE_GROUP=$(jq -r '.resourceGroup' <<< $cluster) - CLUSTER_NAME=$(jq -r '.name' <<< $cluster) - STARTUP_MODE=$(jq -r '.tags.startupMode' <<< $cluster) - CLUSTER_STATUS=$(jq -r '.powerState.code' <<< $cluster) + RESOURCE_GROUP=$(jq -r '.resourceGroup' <<<$cluster) + CLUSTER_NAME=$(jq -r '.name' <<<$cluster) + STARTUP_MODE=$(jq -r '.tags.startupMode' <<<$cluster) + CLUSTER_STATUS=$(jq -r '.powerState.code' <<<$cluster) } function check_cluster_status() { - BUSINESS_AREA=$(jq -r '.tags.businessArea' <<< $cluster) + BUSINESS_AREA=$(jq -r '.tags.businessArea' <<<$cluster) if [[ "$BUSINESS_AREA" == "Cross-Cutting" ]]; then APP="toffee" elif [[ "$BUSINESS_AREA" == "CFT" ]]; then APP="plum" fi - ENVIRONMENT=$(jq -r '.tags.environment' <<< $cluster) + ENVIRONMENT=$(jq -r '.tags.environment' <<<$cluster) local env_variants=( "sandbox/Sandbox:sbox" @@ -49,15 +49,15 @@ function check_cluster_status() { ) local -A notify_channel_map=( - [sandbox]="sbox" - [testing]="perftest" - [staging]="aat" + [sandbox]="sbox" + [testing]="perftest" + [staging]="aat" ) if [ -n "${notify_channel_map[$ENVIRONMENT]}" ]; then - SLACK_CHANNEL_SUFFIX="${notify_channel_map[$ENVIRONMENT]}" + SLACK_CHANNEL_SUFFIX="${notify_channel_map[$ENVIRONMENT]}" else - SLACK_CHANNEL_SUFFIX="$ENVIRONMENT" + SLACK_CHANNEL_SUFFIX="$ENVIRONMENT" fi for variant in "${env_variants[@]}"; do @@ -72,7 +72,7 @@ function check_cluster_status() { ts_echo "Test that $APP works in $ENVIRONMENT after $CLUSTER_NAME start-up" - statuscode=$(curl --max-time 30 --retry 20 --retry-delay 15 -s -o /dev/null -w "%{http_code}" https://$APPLICATION.platform.hmcts.net) + statuscode=$(curl --max-time 30 --retry 20 --retry-delay 15 -s -o /dev/null -w "%{http_code}" https://$APPLICATION.platform.hmcts.net) if [[ ("$ENVIRONMENT" == "demo" || $statuscode -eq 200) ]]; then notification "#aks-monitor-$SLACK_CHANNEL_SUFFIX" "$APP works in $ENVIRONMENT after $CLUSTER_NAME start-up" @@ -85,6 +85,6 @@ function check_cluster_status() { } function aks_state_messages() { - ts_echo_color GREEN "Running $MODE operation on cluster $CLUSTER_NAME (rg:$RESOURCE_GROUP)" - ts_echo_color GREEN "az aks $MODE --resource-group $RESOURCE_GROUP --name $CLUSTER_NAME --no-wait || echo Ignoring any errors while $MODE operation on cluster" -} \ No newline at end of file + ts_echo_color GREEN "Running $MODE operation on cluster $CLUSTER_NAME (rg:$RESOURCE_GROUP)" + ts_echo_color GREEN "az aks $MODE --resource-group $RESOURCE_GROUP --name $CLUSTER_NAME --no-wait || echo Ignoring any errors while $MODE operation on cluster" +} diff --git a/scripts/aks/set-subscription.sh b/scripts/aks/set-subscription.sh deleted file mode 100644 index 72968c3c..00000000 --- a/scripts/aks/set-subscription.sh +++ /dev/null @@ -1,37 +0,0 @@ -#!/bin/bash - -# Define an associative array for subscription names, cluster name prefixes -declare -A SUBSCRIPTION_CLUSTERMAP - -SUBSCRIPTION_CLUSTERMAP["CFT,Sandbox"]="DCD-CFTAPPS-SBOX,CFT-SBOX" -SUBSCRIPTION_CLUSTERMAP["CFT,sbox"]="DCD-CFTAPPS-SBOX,CFT-SBOX" -SUBSCRIPTION_CLUSTERMAP["CFT,AAT / Staging"]="DCD-CFTAPPS-STG,CFT-AAT" -SUBSCRIPTION_CLUSTERMAP["CFT,Test / Perftest"]="DCD-CFTAPPS-TEST,CFT-PERFTEST" -SUBSCRIPTION_CLUSTERMAP["CFT,Preview / Dev"]="DCD-CFTAPPS-DEV,CFT-PREVIEW" -SUBSCRIPTION_CLUSTERMAP["CFT,Demo"]="DCD-CFTAPPS-DEMO,CFT-DEMO" -SUBSCRIPTION_CLUSTERMAP["CFT,ITHC"]="DCD-CFTAPPS-ITHC,CFT-ITHC" -SUBSCRIPTION_CLUSTERMAP["CFT,PTL"]="DTS-CFTPTL-INTSVC,CFT-PTL" -SUBSCRIPTION_CLUSTERMAP["CFT,PTLSBOX"]="DTS-CFTSBOX-INTSVC,CFT-PTLSBOX" - -SUBSCRIPTION_CLUSTERMAP["SDS,Sandbox"]="DTS-SHAREDSERVICES-SBOX,SS-SBOX" -SUBSCRIPTION_CLUSTERMAP["SDS,sbox"]="DTS-SHAREDSERVICES-SBOX,SS-SBOX" -SUBSCRIPTION_CLUSTERMAP["SDS,AAT / Staging"]="DTS-SHAREDSERVICES-STG,SS-STG" -SUBSCRIPTION_CLUSTERMAP["SDS,Test / Perftest"]="DTS-SHAREDSERVICES-TEST,SS-TEST" -SUBSCRIPTION_CLUSTERMAP["SDS,Preview / Dev"]="DTS-SHAREDSERVICES-DEV,SS-DEV" -SUBSCRIPTION_CLUSTERMAP["SDS,Demo"]="DTS-SHAREDSERVICES-DEMO,SS-DEMO" -SUBSCRIPTION_CLUSTERMAP["SDS,ITHC"]="DTS-SHAREDSERVICES-ITHC,SS-ITHC" -SUBSCRIPTION_CLUSTERMAP["SDS,PTL"]="DTS-SHAREDSERVICESPTL,SS-PTL" -SUBSCRIPTION_CLUSTERMAP["SDS,PTLSBOX"]="DTS-SHAREDSERVICESPTL-SBOX,SS-PTLSBOX" - -function subscription() { - key="${PROJECT},${SELECTED_ENV}" - SUBSCRIPTION=$(echo ${SUBSCRIPTION_CLUSTERMAP[$key]} | cut -d ',' -f 1) - CLUSTER_PREFIX=$(echo ${SUBSCRIPTION_CLUSTERMAP[$key]} | cut -d ',' -f 2) - - az account set -n $SUBSCRIPTION - ts_echo $SUBSCRIPTION selected -} - -function ts_echo() { - date +"%H:%M:%S $(printf "%s " "$@")" -}