11#! /usr/bin/env bash
22set -o pipefail
3- REGISTRY_BASE_URL=" ${REGISTRY_BASE_URL:- https:// registry.coder.com} "
43set -u
54
6- if [[ -n " ${VERBOSE:- } " ]]; then
7- set -x
8- fi
5+ REGISTRY_BASE_URL=" ${REGISTRY_BASE_URL:- https:// registry.coder.com} "
96
107status=0
118declare -a modules=()
129declare -a failures=()
10+
11+ # Collect all module directories containing a main.tf file
1312for path in $( find . -not -path ' */.*' -type f -name main.tf -maxdepth 2 | cut -d ' /' -f 2 | sort -u) ; do
1413 modules+=(" ${path} " )
1514done
15+
1616echo " Checking modules: ${modules[*]} "
17+
18+ # Function to update the component status on Instatus
19+ update_component_status () {
20+ local component_status=$1
21+ # see https://instatus.com/help/api/components
22+ (curl -X PUT " https://api.instatus.com/v1/$INSTATUS_PAGE_ID /components/$INSTATUS_COMPONENT_ID " \
23+ -H " Authorization: Bearer $INSTATUS_API_KEY " \
24+ -H " Content-Type: application/json" \
25+ -d " {\" status\" : \" $component_status \" }" )
26+ }
27+
28+ # Function to create an incident
29+ create_incident () {
30+ local incident_name=" Testing Instatus"
31+ local message=" The following modules are experiencing issues:\n"
32+ for i in " ${! failures[@]} " ; do
33+ message+=" $(( $i + 1 )) . ${failures[$i]} \n"
34+ done
35+
36+ component_status=" PARTIALOUTAGE"
37+ if (( ${# failures[@]} == ${# modules[@]} )) ; then
38+ component_status=" MAJOROUTAGE"
39+ fi
40+ # see https://instatus.com/help/api/incidents
41+ response=$( curl -s -X POST " https://api.instatus.com/v1/$INSTATUS_PAGE_ID /incidents" \
42+ -H " Authorization: Bearer $INSTATUS_API_KEY " \
43+ -H " Content-Type: application/json" \
44+ -d " {
45+ \" name\" : \" $incident_name \" ,
46+ \" message\" : \" $message \" ,
47+ \" components\" : [\" $INSTATUS_COMPONENT_ID \" ],
48+ \" status\" : \" INVESTIGATING\" ,
49+ \" notify\" : true,
50+ \" statuses\" : [
51+ {
52+ \" id\" : \" $INSTATUS_COMPONENT_ID \" ,
53+ \" status\" : \" PARTIALOUTAGE\"
54+ }
55+ ]
56+ }" )
57+
58+ incident_id=$( echo " $response " | jq -r ' .id' )
59+ echo " $incident_id "
60+ }
61+
62+ # Check each module's accessibility
1763for module in " ${modules[@]} " ; do
1864 # Trim leading/trailing whitespace from module name
1965 module=$( echo " ${module} " | xargs)
2066 url=" ${REGISTRY_BASE_URL} /modules/${module} "
21- printf " === Check module %s at %s\n" " ${module} " " ${url} "
67+ printf " === Checking module %s at %s\n" " ${module} " " ${url} "
2268 status_code=$( curl --output /dev/null --head --silent --fail --location " ${url} " --retry 3 --write-out " %{http_code}" )
2369 # shellcheck disable=SC2181
2470 if (( status_code != 200 )) ; then
@@ -30,7 +76,23 @@ for module in "${modules[@]}"; do
3076 fi
3177done
3278
33- if (( status != 0 )) ; then
34- echo " The following modules appear to have issues: ${failures[*]} "
79+ # Determine overall status and update Instatus component
80+ if (( status == 0 )) ; then
81+ echo " All modules are operational."
82+ # set to
83+ update_component_status " OPERATIONAL"
84+ else
85+ echo " The following modules have issues: ${failures[*]} "
86+ # check if all modules are down
87+ if (( ${# failures[@]} == ${# modules[@]} )) ; then
88+ update_component_status " MAJOROUTAGE"
89+ else
90+ update_component_status " PARTIALOUTAGE"
91+ fi
92+
93+ # Create a new incident
94+ incident_id=$( create_incident)
95+ echo " Created incident with ID: $incident_id "
3596fi
97+
3698exit " ${status} "
0 commit comments