Skip to content

Commit

Permalink
DTSPO-18443: Add JSON status updates files (#694)
Browse files Browse the repository at this point in the history
* DTSPO-18443: Initial json function add
  • Loading branch information
reespozzi authored Oct 23, 2024
1 parent e7a5214 commit b3dea7d
Show file tree
Hide file tree
Showing 9 changed files with 115 additions and 17 deletions.
11 changes: 11 additions & 0 deletions .github/workflows/auto-start-status-check.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,14 @@ jobs:

- name: Output log file
run: ./scripts/common/log-output.sh

- name: Commit status update json files
continue-on-error: true
run: |
git config user.name hmcts-platform-operations
git config user.email [email protected]
git pull
git checkout -b testing-json-file-updates
git add ./status
git commit -m "Update json files start status updates"
git push
11 changes: 11 additions & 0 deletions .github/workflows/auto-stop-status-check.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,14 @@ jobs:

- name: Output log file
run: ./scripts/common/log-output.sh

- name: Commit status update json files
continue-on-error: true
run: |
git config user.name hmcts-platform-operations
git config user.email [email protected]
git pull
git checkout -b testing-json-file-updates-stop
git add ./status
git commit -m "Update json files stop status updates"
git push
26 changes: 26 additions & 0 deletions .github/workflows/json-file-cleardown.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: json-file-cleardown
on:
workflow_dispatch:
schedule:
- cron: '0 19 * * *' # Every day at 20:00 BST
permissions:
id-token: write
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4

- name: Remove json status files
run: |
rm -f status/*
- name: Commit empty json status folder
continue-on-error: true
run: |
git config user.name hmcts-platform-operations
git config user.email [email protected]
git add .
git commit -m "JSON Status files housekeeping"
git push
5 changes: 4 additions & 1 deletion scripts/appgateway/appgw_status.sh
Original file line number Diff line number Diff line change
Expand Up @@ -56,20 +56,23 @@ jq -c '.data[]' <<<$APPLICATION_GATEWAYS | while read application_gateway; do
ts_echo_color $([[ $MODE == "start" ]] && echo GREEN || echo RED) "$logMessage"
if [[ $MODE == "stop" ]]; then
auto_shutdown_notification ":red_circle: $slackMessage"
add_to_json "$APPLICATION_GATEWAY_ID" "$APPLICATION_GATEWAY_NAME" "$slackMessage" "appgateway" "$MODE"
fi
;;
*"Stopped"*)
ts_echo_color $([[ $MODE == "start" ]] && echo RED || echo GREEN) "$logMessage"
if [[ $MODE == "start" ]]; then
auto_shutdown_notification ":red_circle: $slackMessage"
add_to_json "$APPLICATION_GATEWAY_ID" "$APPLICATION_GATEWAY_NAME" "$slackMessage" "appgateway" "$MODE"
fi
;;
*)
ts_echo_color AMBER "$logMessage"
auto_shutdown_notification ":yellow_circle: $slackMessage"
add_to_json "$APPLICATION_GATEWAY_ID" "$APPLICATION_GATEWAY_NAME" "$slackMessage" "appgateway" "$MODE"
;;
esac
else
ts_echo_color AMBER "Application Gateway: $APPLICATION_GATEWAY_NAME in ResourceGroup: $RESOURCE_GROUP has been skipped from today's $MODE operation schedule"
fi
done
done
2 changes: 2 additions & 0 deletions scripts/blob-storage/sftp-check-status.sh
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,13 @@ do
ts_echo_color $( [[ $MODE == "start" ]] && echo GREEN || echo RED ) "$logMessage"
if [[ $MODE == "stop" ]]; then
auto_shutdown_notification "$slackMessage"
add_to_json "$STORAGE_ACCOUNT_ID" "$STORAGE_ACCOUNT_NAME" "$slackMessage" "blob-storage" "$MODE"
fi
elif [[ "$SFTP_SERVER_ENABLED" =~ "false" ]]; then
ts_echo_color $( [[ $MODE == "start" ]] && echo RED || echo GREEN ) "$logMessage"
if [[ $MODE == "start" ]]; then
auto_shutdown_notification "$slackMessage"
add_to_json "$STORAGE_ACCOUNT_ID" "$STORAGE_ACCOUNT_NAME" "$slackMessage" "blob-storage" "$MODE"
fi
fi
done
Expand Down
39 changes: 38 additions & 1 deletion scripts/common/common-functions.sh
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,48 @@ function auto_shutdown_notification() {
# This silences the slack response message in logs.
# Comment this line out if you are having issues with slack delivery and want to see responses in your terminal
local silentResponse="-s -o /dev/null"

curl $silentResponse -X POST --data-urlencode "payload={\"username\": \"Auto Shutdown Notifications\", \"text\": \"$message\", \"icon_emoji\": \":tim-webster:\"}" \
${notificationSlackWebhook}
}

# Saves to JSON file in this repo which is to be used by another repo for daily-monitoring
function add_to_json() {
local id="$1"
local resource="$2"
local statusMessage="$3"
local resourceType="$4"
local mode="$5"
# Send to json file dependent on resource type
local pathToJson="status/${resourceType}_status_updates_${mode}.json"

# Create JSON file if it does not exist or is empty
if [[ ! -f "$pathToJson" || ! -s "$pathToJson" ]]; then
echo "[]" > "$pathToJson"
fi

# Update the existing object if the ID is found, else add a new object
# Saves us duplicates if there is another individual pipeline run during the day, whilst still allowing for potential status updates
jq --arg id "$id" --arg resource "$resource" --arg statusMessage "$statusMessage" --arg resourceType "$resourceType" \
'map(if .id == $id then
.resource = $resource |
.statusMessage = $statusMessage |
.resourceType = $resourceType
else
.
end)
+ (if any(.id == $id) then [] else
[{
"id": $id,
"resource": $resource,
"statusMessage": $statusMessage,
"resourceType": $resourceType
}]
end)' "$pathToJson" \
> "json_file.tmp" && mv "json_file.tmp" "$pathToJson"
echo "JSON file updated successfully."
}


function get_current_date() {
$date_command +'%d-%m-%Y %H:%M'
}
Expand Down
5 changes: 4 additions & 1 deletion scripts/flexible-server/flexserverstatus.sh
Original file line number Diff line number Diff line change
Expand Up @@ -56,20 +56,23 @@ jq -c '.data[]' <<<$FLEXIBLE_SERVERS | while read flexibleserver; do
ts_echo_color $([[ $MODE == "start" ]] && echo GREEN || echo RED) "$logMessage"
if [[ $MODE == "stop" ]]; then
auto_shutdown_notification ":red_circle: $slackMessage"
add_to_json "$SERVER_ID" "$SERVER_NAME" "$slackMessage" "flexible-server" "$MODE"
fi
;;
*"Stopped"*)
ts_echo_color $([[ $MODE == "start" ]] && echo RED || echo GREEN) "$logMessage"
if [[ $MODE == "start" ]]; then
auto_shutdown_notification ":red_circle: $slackMessage"
add_to_json "$SERVER_ID" "$SERVER_NAME" "$slackMessage" "flexible-server" "$MODE"
fi
;;
*)
ts_echo_color AMBER "$logMessage"
auto_shutdown_notification ":yellow_circle: $slackMessage"
add_to_json "$SERVER_ID" "$SERVER_NAME" "$slackMessage" "flexible-server" "$MODE"
;;
esac
else
ts_echo_color AMBER "Flexible SQL Server: $SERVER_NAME in ResourceGroup: $RESOURCE_GROUP has been skipped from today's $MODE operation schedule"
fi
done
done
29 changes: 16 additions & 13 deletions scripts/sqlmi/sqlmistatus.sh
Original file line number Diff line number Diff line change
Expand Up @@ -53,20 +53,23 @@ jq -c '.[]' <<<$MI_SQL_SERVERS | while read server; do
# - If MODE = Stop then a running App Gateway is incorrect and we should notify
# - If neither Running or Stopped is found then something else is going on and we should notify
case "$SERVER_STATE" in
*"Ready"*)
ts_echo_color $([[ $MODE == "start" ]] && echo GREEN || echo RED) "$logMessage"
[[ $MODE == "stop" ]] && auto_shutdown_notification ":red_circle: $slackMessage"
;;
*"Stopped"*)
ts_echo_color $([[ $MODE == "start" ]] && echo RED || echo GREEN) "$logMessage"
[[ $MODE == "start" ]] && auto_shutdown_notification ":red_circle: $slackMessage"
;;
*)
ts_echo_color AMBER "$logMessage"
auto_shutdown_notification ":yellow_circle: $slackMessage"
;;
*"Ready"*)
ts_echo_color $( [[ $MODE == "start" ]] && echo GREEN || echo RED ) "$logMessage"
[[ $MODE == "stop" ]] && auto_shutdown_notification ":red_circle: $slackMessage"
add_to_json "$SERVER_ID" "$SERVER_NAME" "$slackMessage" "sql" "$MODE"
;;
*"Stopped"*)
ts_echo_color $( [[ $MODE == "start" ]] && echo RED || echo GREEN ) "$logMessage"
[[ $MODE == "start" ]] && auto_shutdown_notification ":red_circle: $slackMessage"
add_to_json "$SERVER_ID" "$SERVER_NAME" "$slackMessage" "sql" "$MODE"
;;
*)
ts_echo_color AMBER "$logMessage"
auto_shutdown_notification ":yellow_circle: $slackMessage"
add_to_json "$SERVER_ID" "$SERVER_NAME" "$slackMessage" "sql" "$MODE"
;;
esac
else
ts_echo_color AMBER "SQL managed-instance: $SERVER_NAME in ResourceGroup: $RESOURCE_GROUP has been skipped from today's $MODE operation schedule"
fi
done
done
4 changes: 3 additions & 1 deletion scripts/vm/vmstatus.sh
Original file line number Diff line number Diff line change
Expand Up @@ -76,22 +76,24 @@ do
ts_echo_color $( [[ $MODE == "start" ]] && echo GREEN || echo RED ) "$logMessage"
if [[ $MODE == "deallocate" ]]; then
auto_shutdown_notification ":red_circle: $slackMessage"
add_to_json "$VM_ID" "$VM_NAME" "$slackMessage" "vm" "$MODE"
fi
;;
*"deallocated"*)
ts_echo_color $( [[ $MODE == "start" ]] && echo RED || echo GREEN ) "$logMessage"
if [[ $MODE == "start" ]]; then
auto_shutdown_notification ":red_circle: $slackMessage"
add_to_json "$VM_ID" "$VM_NAME" "$slackMessage" "vm" "$MODE"
fi
;;
*)
ts_echo_color AMBER "$logMessage"
auto_shutdown_notification ":yellow_circle: $slackMessage"
add_to_json "$VM_ID" "$VM_NAME" "$slackMessage" "vm" "$MODE"
;;
esac
else
ts_echo_color AMBER "VM: $VM_NAME in Resource Group: $RESOURCE_GROUP has been skipped from today's $MODE operation schedule"
fi

done
done

0 comments on commit b3dea7d

Please sign in to comment.