Skip to content

Commit

Permalink
Merge pull request #151 from hmcts/DTSPO-15269
Browse files Browse the repository at this point in the history
Dtspo 15269
  • Loading branch information
SalimKainos authored Sep 27, 2023
2 parents d25396a + 9cfb027 commit 2a6c244
Show file tree
Hide file tree
Showing 6 changed files with 202 additions and 0 deletions.
23 changes: 23 additions & 0 deletions .github/workflows/sqlmi-auto-shutdown.yaml
Original file line number Diff line number Diff line change
@@ -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
23 changes: 23 additions & 0 deletions .github/workflows/sqlmi-auto-start.yaml
Original file line number Diff line number Diff line change
@@ -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
67 changes: 67 additions & 0 deletions scripts/sqlmi/auto-shutdown.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#!/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}
app_env=${app_env/dev/Development}

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 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

done
done
18 changes: 18 additions & 0 deletions scripts/sqlmi/auto-start.sh
Original file line number Diff line number Diff line change
@@ -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 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 mi start --ids $ID --no-wait || echo Ignoring error starting $NAME
fi
done
done
46 changes: 46 additions & 0 deletions scripts/sqlmi/manual-start.sh
Original file line number Diff line number Diff line change
@@ -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
25 changes: 25 additions & 0 deletions scripts/sqlmi/sqlmistatus.sh
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 2a6c244

Please sign in to comment.