From 35b7f94d22385429eadf188d4766ae7d00e18a0a Mon Sep 17 00:00:00 2001 From: salim abdulkareem Date: Wed, 20 Sep 2023 15:49:01 +0100 Subject: [PATCH 1/3] added sqlmi --- scripts/sqlmi/auto-shutdown.sh | 65 ++++++++++++++++++++++++++++++++++ scripts/sqlmi/auto-start.sh | 18 ++++++++++ 2 files changed, 83 insertions(+) create mode 100644 scripts/sqlmi/auto-shutdown.sh create mode 100644 scripts/sqlmi/auto-start.sh diff --git a/scripts/sqlmi/auto-shutdown.sh b/scripts/sqlmi/auto-shutdown.sh new file mode 100644 index 00000000..299ff0a9 --- /dev/null +++ b/scripts/sqlmi/auto-shutdown.sh @@ -0,0 +1,65 @@ +#!/usr/bin/env bash +#set -x +shopt -s nocasematch +AMBER='\033[1;33m' +GREEN='\033[0;32m' +RED='\033[0;31m' +SUBSCRIPTIONS=$(az account list -o json) +jq -c '.[]' <<<$SUBSCRIPTIONS | while read subcription; do + SUBSCRIPTION_ID=$(jq -r '.id' <<<$subcription) + SUBSCRIPTION_NAME=$(jq -r '.name' <<<$subcription) + az account set -s $SUBSCRIPTION_ID + APPGS=$(az resource list --resource-type Microsoft.Sql/managedInstances --query "[?tags.autoShutdown == 'true']" -o json) + + jq -c '.[]' <<<$APPGS | while read app; do + + SKIP="false" + app_id=$(jq -r '.id' <<<$app) + name=$(jq -r '.name' <<<$app) + rg=$(jq -r '.resourceGroup' <<<$app) + app_env=$(echo $name | awk -F "-" '{ print $(NF) }') + app_env=${app_env/stg/Staging} + app_env=${app_env/sbox/Sandbox} + if [[ $SUBSCRIPTION_NAME =~ "SHAREDSERVICES" ]]; then + business_area="Cross-Cutting" + else + business_area="CFT" + fi + while read id; do + business_area_entry=$(jq -r '."business_area"' <<<$id) + env_entry=$(jq -r '."environment"' <<<$id) + start_date=$(jq -r '."skip_start_date"' <<<$id) + end_date=$(jq -r '."skip_end_date"' <<<$id) + #start date business_area_entry formatting + start_date_formatting=$(awk -F'-' '{printf("%04d-%02d-%02d\n",$3,$2,$1)}' <<<$start_date) + start_date_seconds=$(date -d "$start_date_formatting 00:00:00" +%s) + #end date formatting + end_date_formatting=$(awk -F'-' '{printf("%04d-%02d-%02d\n",$3,$2,$1)}' <<<$end_date) + end_date_seconds=$(date -d "$end_date_formatting 00:00:00" +%s) + #current date formatting + current_date=$(date +'%d-%m-%Y') + current_date_formatting=$(awk -F'-' '{printf("%04d-%02d-%02d\n",$3,$2,$1)}' <<<$current_date) + current_date_seconds=$(date -d "$current_date_formatting 00:00:00" +%s) + #Skip logic + #if start date is equal to current date: skip shutdown on that cluster + if [[ ${env_entry} =~ ${app_env} ]] && [[ $business_area == $business_area_entry ]] && [[ $start_date_seconds -eq $current_date_seconds ]]; then + echo "Match: $id" + SKIP="true" + continue + #if current date is less than skip end date: skip shutdown on that cluster + elif [[ ${env_entry} =~ ${app_env} ]] && [[ $business_area == $business_area_entry ]] && [[ $current_date_seconds -ge $start_date_seconds ]] && [[ $current_date_seconds -le $end_date_seconds ]]; then + echo "Match : $id" + SKIP="true" + continue + fi + done < <(jq -c '.[]' issues_list.json) + if [[ $SKIP == "false" ]]; then + echo -e "${GREEN}About to shutdown sqlmi server $name (rg:$rg) sub:$SUBSCRIPTION_NAME" + echo -e "${GREEN}az sql managed-instance stop -g $rg -n $name --no-wait" + az sql managed-instance stop -g $rg -n $name --no-wait || echo Ignoring errors stopping $name + else + echo -e "${AMBER}sql managed-instance $name (rg:$rg) sub:$SUBSCRIPTION_NAME has been skipped from todays shutdown schedule" + fi + + done +done diff --git a/scripts/sqlmi/auto-start.sh b/scripts/sqlmi/auto-start.sh new file mode 100644 index 00000000..68aac3b9 --- /dev/null +++ b/scripts/sqlmi/auto-start.sh @@ -0,0 +1,18 @@ +#!/usr/bin/env bash +SUBSCRIPTIONS=$(az account list -o json) +jq -c '.[]' <<< $SUBSCRIPTIONS | while read subcription +do + SUBSCRIPTION_ID=$(jq -r '.id' <<< $subcription) + az account set -s $SUBSCRIPTION_ID + SERVERS=$(az resource list --resource-type Microsoft.Sql/managedInstances --query "[?tags.autoShutdown == 'true']" -o json) + jq -c '.[]'<<< $SERVERS | while read server + do + ID=$(jq -r '.id' <<< $server) + NAME=$(jq -r '.name' <<< $server) + status=$(az sql managed-instance show --ids $ID --query "state") + if [[ "$status" != *"Ready"* ]]; then + echo "Starting sql managed-instance show in Subscription: $(az account show --query name) ResourceGroup: $(jq -r '.resourceGroup' <<< $server) Name: $NAME" + az sql managed-instance start --ids $ID --no-wait || echo Ignoring error starting $NAME + fi + done +done \ No newline at end of file From 00c2c9c7bfa8bfb57c15965409037fffc7d99b13 Mon Sep 17 00:00:00 2001 From: salim abdulkareem Date: Fri, 22 Sep 2023 12:06:53 +0100 Subject: [PATCH 2/3] Added sqlmi auto shutdown --- .github/workflows/sqlmi-auto-shutdown.yaml | 23 +++++++++++ .github/workflows/sqlmi-auto-start.yaml | 23 +++++++++++ scripts/sqlmi/auto-shutdown.sh | 6 ++- scripts/sqlmi/auto-start.sh | 4 +- scripts/sqlmi/maual-start.sh | 46 ++++++++++++++++++++++ scripts/sqlmi/sqlmistatus.sh | 25 ++++++++++++ 6 files changed, 123 insertions(+), 4 deletions(-) create mode 100644 .github/workflows/sqlmi-auto-shutdown.yaml create mode 100644 .github/workflows/sqlmi-auto-start.yaml create mode 100644 scripts/sqlmi/maual-start.sh create mode 100644 scripts/sqlmi/sqlmistatus.sh diff --git a/.github/workflows/sqlmi-auto-shutdown.yaml b/.github/workflows/sqlmi-auto-shutdown.yaml new file mode 100644 index 00000000..8eec01f1 --- /dev/null +++ b/.github/workflows/sqlmi-auto-shutdown.yaml @@ -0,0 +1,23 @@ +name: sql-managed-instance-auto-shutdown +on: + workflow_dispatch: + schedule: + - cron: '0 19 * * *' # Every day at 8pm BST +permissions: + id-token: write +jobs: + build: + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v3 + - name: 'Az CLI login' + uses: azure/login@v1 + 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: Sql Managed Instance Auto Shutdown + run: ./scripts/sqlmi/auto-shutdown.sh + - name: Sql Managed Instance status check + run: ./scripts/sqlmi/sqlmistatus.sh \ No newline at end of file diff --git a/.github/workflows/sqlmi-auto-start.yaml b/.github/workflows/sqlmi-auto-start.yaml new file mode 100644 index 00000000..05f78b56 --- /dev/null +++ b/.github/workflows/sqlmi-auto-start.yaml @@ -0,0 +1,23 @@ +name: sql-managed-instance-auto-start +on: + workflow_dispatch: + schedule: + - cron: '30 5 * * 1-5' # Every weekday at 6:30am BST +permissions: + id-token: write +jobs: + build: + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v3 + - name: 'Az CLI login' + uses: azure/login@v1 + 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: Sql Managed Instance Auto Start + run: ./scripts/sqlmi/auto-start.sh + - name: Sql Managed Instance status check + run: ./scripts/sqlmi/sqlmistatus.sh \ No newline at end of file diff --git a/scripts/sqlmi/auto-shutdown.sh b/scripts/sqlmi/auto-shutdown.sh index 299ff0a9..31d5e0ef 100644 --- a/scripts/sqlmi/auto-shutdown.sh +++ b/scripts/sqlmi/auto-shutdown.sh @@ -20,6 +20,8 @@ jq -c '.[]' <<<$SUBSCRIPTIONS | while read subcription; do app_env=$(echo $name | awk -F "-" '{ print $(NF) }') app_env=${app_env/stg/Staging} app_env=${app_env/sbox/Sandbox} + app_env=${app_env/dev/Development} + if [[ $SUBSCRIPTION_NAME =~ "SHAREDSERVICES" ]]; then business_area="Cross-Cutting" else @@ -55,8 +57,8 @@ jq -c '.[]' <<<$SUBSCRIPTIONS | while read subcription; do done < <(jq -c '.[]' issues_list.json) if [[ $SKIP == "false" ]]; then echo -e "${GREEN}About to shutdown sqlmi server $name (rg:$rg) sub:$SUBSCRIPTION_NAME" - echo -e "${GREEN}az sql managed-instance stop -g $rg -n $name --no-wait" - az sql managed-instance stop -g $rg -n $name --no-wait || echo Ignoring errors stopping $name + echo -e "${GREEN}az sql mi stop --mi $name -g $rg --subscription $SUBSCRIPTION_NAME --no-wait" + az sql mi stop --mi $name -g $rg --subscription $SUBSCRIPTION_NAME --no-wait || echo Ignoring errors stopping $name else echo -e "${AMBER}sql managed-instance $name (rg:$rg) sub:$SUBSCRIPTION_NAME has been skipped from todays shutdown schedule" fi diff --git a/scripts/sqlmi/auto-start.sh b/scripts/sqlmi/auto-start.sh index 68aac3b9..27a413b4 100644 --- a/scripts/sqlmi/auto-start.sh +++ b/scripts/sqlmi/auto-start.sh @@ -9,10 +9,10 @@ do do ID=$(jq -r '.id' <<< $server) NAME=$(jq -r '.name' <<< $server) - status=$(az sql managed-instance show --ids $ID --query "state") + status=$(az sql mi show --ids $ID --query "state") if [[ "$status" != *"Ready"* ]]; then echo "Starting sql managed-instance show in Subscription: $(az account show --query name) ResourceGroup: $(jq -r '.resourceGroup' <<< $server) Name: $NAME" - az sql managed-instance start --ids $ID --no-wait || echo Ignoring error starting $NAME + az sql mi start --ids $ID --no-wait || echo Ignoring error starting $NAME fi done done \ No newline at end of file diff --git a/scripts/sqlmi/maual-start.sh b/scripts/sqlmi/maual-start.sh new file mode 100644 index 00000000..8bcab08d --- /dev/null +++ b/scripts/sqlmi/maual-start.sh @@ -0,0 +1,46 @@ +#!/usr/bin/env bash + +function ts_echo() { + date +"%H:%M:%S $(printf "%s " "$@")" +} +if [[ $SELECTED_ENV == "sbox" ]]; then + SELECTED_ENV="box" +fi +if [[ $SELECTED_ENV == "test/perftest" ]] && [[ $PROJECT == "CFT" ]]; then + SELECTED_ENV="perftest" +elif [[ $SELECTED_ENV == "test/perftest" ]] && [[ $PROJECT == "SDS" ]]; then + SELECTED_ENV="test" +elif [[ $SELECTED_ENV == "preview/dev" ]] && [[ $PROJECT == "SDS" ]]; then + SELECTED_ENV="dev" +elif [[ $SELECTED_ENV == "preview/dev" ]] && [[ $PROJECT == "CFT" ]]; then + SELECTED_ENV="preview" +elif [[ $SELECTED_ENV == "aat/staging" ]] && [[ $PROJECT == "SDS" ]]; then + SELECTED_ENV="stg" +elif [[ $SELECTED_ENV == "aat/staging" ]] && [[ $PROJECT == "CFT" ]]; then + SELECTED_ENV="aat" +fi +SUBSCRIPTIONS=$(az account list -o json) +jq -c '.[]' <<<$SUBSCRIPTIONS | while read subcription; do + SUBSCRIPTION_ID=$(jq -r '.id' <<<$subcription) + SUBSCRIPTION_NAME=$(jq -r '.name' <<<$subcription) + if [[ $PROJECT == "SDS" ]] && [[ $SUBSCRIPTION_NAME =~ "DCD-" ]]; then + continue + fi + if [[ $PROJECT == "CFT" ]] && [[ $SUBSCRIPTION_NAME =~ "SHAREDSERVICES" ]]; then + continue + fi + az account set -s $SUBSCRIPTION_ID + + SERVERS=$(az resource list --resource-type Microsoft.Sql/managedInstances --query "[?tags.autoShutdown == 'true']" -o json) + jq -c '.[]' <<<$SERVERS | while read server; do + ID=$(jq -r '.id' <<<$server) + NAME=$(jq -r '.name' <<<$server) + if [[ $NAME =~ $SELECTED_ENV ]]; then + status=$(az sql mi show --ids $ID --query "state") + if [[ "$status" != *"Ready"* ]]; then + ts_echo "Starting sql managed-instance show in Subscription: $(az account show --query name) ResourceGroup: $(jq -r '.resourceGroup' <<<$server) Name: $NAME" + az sql mi start --ids $ID --no-wait || echo Ignoring error starting $NAME + fi + fi + done +done diff --git a/scripts/sqlmi/sqlmistatus.sh b/scripts/sqlmi/sqlmistatus.sh new file mode 100644 index 00000000..885658c7 --- /dev/null +++ b/scripts/sqlmi/sqlmistatus.sh @@ -0,0 +1,25 @@ +#!/usr/bin/env bash +#set -x +AMBER='\033[1;33m' +GREEN='\033[0;32m' +RED='\033[0;31m' +SUBSCRIPTIONS=$(az account list -o json) +jq -c '.[]' <<< $SUBSCRIPTIONS | while read subcription +do + SUBSCRIPTION_ID=$(jq -r '.id' <<< $subcription) + az account set -s $SUBSCRIPTION_ID + SERVERS=$(az resource list --resource-type Microsoft.Sql/managedInstances --query "[?tags.autoShutdown == 'true']" -o json) + + jq -c '.[]'<<< $SERVERS | while read server + do + ID=$(jq -r '.id' <<< $server) + status=$(az sql mi show --ids $ID --query "state") + if [[ "$status" =~ .*"Stopped".* ]]; then + echo -e "${RED}status of sql managed-instance Name: $(jq -r '.name' <<< $server) in Subscription: $(az account show --query name) ResourceGroup: $(jq -r '.resourceGroup' <<< $server) is $status" + elif [[ "$status" =~ .*"Ready".* ]]; then + echo -e "${GREEN}status of sql managed-instance Name: $(jq -r '.name' <<< $server) in Subscription: $(az account show --query name) ResourceGroup: $(jq -r '.resourceGroup' <<< $server) is $status" + else + echo -e "${AMBER}status of sql managed-instance Name: $(jq -r '.name' <<< $server) in Subscription: $(az account show --query name) ResourceGroup: $(jq -r '.resourceGroup' <<< $server) is $status" + fi + done +done \ No newline at end of file From 7e38796d09bc2879120983b4a0a475fe90302edd Mon Sep 17 00:00:00 2001 From: salim abdulkareem Date: Fri, 22 Sep 2023 22:43:10 +0100 Subject: [PATCH 3/3] fix typo --- scripts/sqlmi/{maual-start.sh => manual-start.sh} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename scripts/sqlmi/{maual-start.sh => manual-start.sh} (100%) diff --git a/scripts/sqlmi/maual-start.sh b/scripts/sqlmi/manual-start.sh similarity index 100% rename from scripts/sqlmi/maual-start.sh rename to scripts/sqlmi/manual-start.sh