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} "
6+ INSTATUS_API_KEY=" 1ff45b8a5008ae8d7a79619302563717"
7+ INSTATUS_PAGE_ID=" cm3inkn4o0004o3jhyy40laus"
8+ INSTATUS_COMPONENT_ID=" cm3invkvo003jzwwlx4pyquxn"
99
1010status=0
1111declare -a modules=()
1212declare -a failures=()
13+
14+ # Collect all module directories containing a main.tf file
1315for path in $( find . -not -path ' */.*' -type f -name main.tf -maxdepth 2 | cut -d ' /' -f 2 | sort -u) ; do
1416 modules+=(" ${path} " )
1517done
18+
1619echo " Checking modules: ${modules[*]} "
20+
21+ # Function to update the component status on Instatus
22+ update_component_status () {
23+ local component_status=$1
24+ # see https://instatus.com/help/api/components
25+ (curl -X PUT " https://api.instatus.com/v1/$INSTATUS_PAGE_ID /components/$INSTATUS_COMPONENT_ID " \
26+ -H " Authorization: Bearer $INSTATUS_API_KEY " \
27+ -H " Content-Type: application/json" \
28+ -d " {\" status\" : \" $component_status \" }" )
29+ }
30+
31+ # Function to create an incident
32+ create_incident () {
33+ local incident_name=" Testing Instatus"
34+ local message=" The following modules are experiencing issues:\n"
35+ for i in " ${! failures[@]} " ; do
36+ message+=" $(( $i + 1 )) . ${failures[$i]} \n"
37+ done
38+
39+ component_status=" PARTIALOUTAGE"
40+ if (( ${# failures[@]} == ${# modules[@]} )) ; then
41+ component_status=" MAJOROUTAGE"
42+ fi
43+ # see https://instatus.com/help/api/incidents
44+ response=$( curl -s -X POST " https://api.instatus.com/v1/$INSTATUS_PAGE_ID /incidents" \
45+ -H " Authorization: Bearer $INSTATUS_API_KEY " \
46+ -H " Content-Type: application/json" \
47+ -d " {
48+ \" name\" : \" $incident_name \" ,
49+ \" message\" : \" $message \" ,
50+ \" components\" : [\" $INSTATUS_COMPONENT_ID \" ],
51+ \" status\" : \" INVESTIGATING\" ,
52+ \" notify\" : true,
53+ \" statuses\" : [
54+ {
55+ \" id\" : \" $INSTATUS_COMPONENT_ID \" ,
56+ \" status\" : \" PARTIALOUTAGE\"
57+ }
58+ ]
59+ }" )
60+
61+ incident_id=$( echo " $response " | jq -r ' .id' )
62+ echo " $incident_id "
63+ }
64+
65+ # Check each module's accessibility
1766for module in " ${modules[@]} " ; do
1867 # Trim leading/trailing whitespace from module name
1968 module=$( echo " ${module} " | xargs)
2069 url=" ${REGISTRY_BASE_URL} /modules/${module} "
21- printf " === Check module %s at %s\n" " ${module} " " ${url} "
70+ printf " === Checking module %s at %s\n" " ${module} " " ${url} "
2271 status_code=$( curl --output /dev/null --head --silent --fail --location " ${url} " --retry 3 --write-out " %{http_code}" )
2372 # shellcheck disable=SC2181
2473 if (( status_code != 200 )) ; then
@@ -30,7 +79,23 @@ for module in "${modules[@]}"; do
3079 fi
3180done
3281
33- if (( status != 0 )) ; then
34- echo " The following modules appear to have issues: ${failures[*]} "
82+ # Determine overall status and update Instatus component
83+ if (( status == 0 )) ; then
84+ echo " All modules are operational."
85+ # set to
86+ update_component_status " OPERATIONAL"
87+ else
88+ echo " The following modules have issues: ${failures[*]} "
89+ # check if all modules are down
90+ if (( ${# failures[@]} == ${# modules[@]} )) ; then
91+ update_component_status " MAJOROUTAGE"
92+ else
93+ update_component_status " PARTIALOUTAGE"
94+ fi
95+
96+ # Create a new incident
97+ incident_id=$( create_incident)
98+ echo " Created incident with ID: $incident_id "
3599fi
100+
36101exit " ${status} "
0 commit comments