diff --git a/.github/workflows/auto-start-status-check.yaml b/.github/workflows/auto-start-status-check.yaml index 146afc9a..ee6bdf37 100644 --- a/.github/workflows/auto-start-status-check.yaml +++ b/.github/workflows/auto-start-status-check.yaml @@ -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 github-platform-operations@hmcts.net + git pull + git checkout -b testing-json-file-updates + git add ./status + git commit -m "Update json files start status updates" + git push diff --git a/.github/workflows/auto-stop-status-check.yaml b/.github/workflows/auto-stop-status-check.yaml index e576fe18..2a003d55 100644 --- a/.github/workflows/auto-stop-status-check.yaml +++ b/.github/workflows/auto-stop-status-check.yaml @@ -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 github-platform-operations@hmcts.net + git pull + git checkout -b testing-json-file-updates-stop + git add ./status + git commit -m "Update json files stop status updates" + git push \ No newline at end of file diff --git a/.github/workflows/json-file-cleardown.yaml b/.github/workflows/json-file-cleardown.yaml new file mode 100644 index 00000000..30afb3b7 --- /dev/null +++ b/.github/workflows/json-file-cleardown.yaml @@ -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 github-platform-operations@hmcts.net + git add . + git commit -m "JSON Status files housekeeping" + git push \ No newline at end of file diff --git a/scripts/appgateway/appgw_status.sh b/scripts/appgateway/appgw_status.sh index e9654856..fe0ed4b6 100755 --- a/scripts/appgateway/appgw_status.sh +++ b/scripts/appgateway/appgw_status.sh @@ -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 \ No newline at end of file diff --git a/scripts/blob-storage/sftp-check-status.sh b/scripts/blob-storage/sftp-check-status.sh index e505d553..06b93251 100755 --- a/scripts/blob-storage/sftp-check-status.sh +++ b/scripts/blob-storage/sftp-check-status.sh @@ -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 diff --git a/scripts/common/common-functions.sh b/scripts/common/common-functions.sh index f84926a1..059fb727 100644 --- a/scripts/common/common-functions.sh +++ b/scripts/common/common-functions.sh @@ -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' } diff --git a/scripts/flexible-server/flexserverstatus.sh b/scripts/flexible-server/flexserverstatus.sh index 75127639..7a340323 100755 --- a/scripts/flexible-server/flexserverstatus.sh +++ b/scripts/flexible-server/flexserverstatus.sh @@ -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 \ No newline at end of file diff --git a/scripts/sqlmi/sqlmistatus.sh b/scripts/sqlmi/sqlmistatus.sh index 9ff956ea..2c67d04e 100755 --- a/scripts/sqlmi/sqlmistatus.sh +++ b/scripts/sqlmi/sqlmistatus.sh @@ -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 \ No newline at end of file diff --git a/scripts/vm/vmstatus.sh b/scripts/vm/vmstatus.sh index 68375141..fc756507 100755 --- a/scripts/vm/vmstatus.sh +++ b/scripts/vm/vmstatus.sh @@ -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