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+ # List of required environment variables
6+ required_vars=(
7+ " INSTATUS_API_KEY"
8+ " INSTATUS_PAGE_ID"
9+ " INSTATUS_COMPONENT_ID"
10+ )
11+
12+ # Check if each required variable is set
13+ for var in " ${required_vars[@]} " ; do
14+ if [[ -z " ${! var:- } " ]]; then
15+ echo " Error: Environment variable '$var ' is not set."
16+ exit 1
17+ fi
18+ done
19+
20+ REGISTRY_BASE_URL=" ${REGISTRY_BASE_URL:- https:// registry.coder.com} "
921
1022status=0
1123declare -a modules=()
1224declare -a failures=()
25+
26+ # Collect all module directories containing a main.tf file
1327for path in $( find . -not -path ' */.*' -type f -name main.tf -maxdepth 2 | cut -d ' /' -f 2 | sort -u) ; do
1428 modules+=(" ${path} " )
1529done
30+
1631echo " Checking modules: ${modules[*]} "
32+
33+ # Function to update the component status on Instatus
34+ update_component_status () {
35+ local component_status=$1
36+ # see https://instatus.com/help/api/components
37+ (curl -X PUT " https://api.instatus.com/v1/$INSTATUS_PAGE_ID /components/$INSTATUS_COMPONENT_ID " \
38+ -H " Authorization: Bearer $INSTATUS_API_KEY " \
39+ -H " Content-Type: application/json" \
40+ -d " {\" status\" : \" $component_status \" }" )
41+ }
42+
43+ # Function to create an incident
44+ create_incident () {
45+ local incident_name=" Testing Instatus"
46+ local message=" The following modules are experiencing issues:\n"
47+ for i in " ${! failures[@]} " ; do
48+ message+=" $(( $i + 1 )) . ${failures[$i]} \n"
49+ done
50+
51+ component_status=" PARTIALOUTAGE"
52+ if (( ${# failures[@]} == ${# modules[@]} )) ; then
53+ component_status=" MAJOROUTAGE"
54+ fi
55+ # see https://instatus.com/help/api/incidents
56+ response=$( curl -s -X POST " https://api.instatus.com/v1/$INSTATUS_PAGE_ID /incidents" \
57+ -H " Authorization: Bearer $INSTATUS_API_KEY " \
58+ -H " Content-Type: application/json" \
59+ -d " {
60+ \" name\" : \" $incident_name \" ,
61+ \" message\" : \" $message \" ,
62+ \" components\" : [\" $INSTATUS_COMPONENT_ID \" ],
63+ \" status\" : \" INVESTIGATING\" ,
64+ \" notify\" : true,
65+ \" statuses\" : [
66+ {
67+ \" id\" : \" $INSTATUS_COMPONENT_ID \" ,
68+ \" status\" : \" PARTIALOUTAGE\"
69+ }
70+ ]
71+ }" )
72+
73+ incident_id=$( echo " $response " | jq -r ' .id' )
74+ echo " $incident_id "
75+ }
76+
77+ # Check each module's accessibility
1778for module in " ${modules[@]} " ; do
1879 # Trim leading/trailing whitespace from module name
1980 module=$( echo " ${module} " | xargs)
2081 url=" ${REGISTRY_BASE_URL} /modules/${module} "
21- printf " === Check module %s at %s\n" " ${module} " " ${url} "
82+ printf " === Checking module %s at %s\n" " ${module} " " ${url} "
2283 status_code=$( curl --output /dev/null --head --silent --fail --location " ${url} " --retry 3 --write-out " %{http_code}" )
2384 # shellcheck disable=SC2181
2485 if (( status_code != 200 )) ; then
@@ -30,7 +91,23 @@ for module in "${modules[@]}"; do
3091 fi
3192done
3293
33- if (( status != 0 )) ; then
34- echo " The following modules appear to have issues: ${failures[*]} "
94+ # Determine overall status and update Instatus component
95+ if (( status == 0 )) ; then
96+ echo " All modules are operational."
97+ # set to
98+ update_component_status " OPERATIONAL"
99+ else
100+ echo " The following modules have issues: ${failures[*]} "
101+ # check if all modules are down
102+ if (( ${# failures[@]} == ${# modules[@]} )) ; then
103+ update_component_status " MAJOROUTAGE"
104+ else
105+ update_component_status " PARTIALOUTAGE"
106+ fi
107+
108+ # Create a new incident
109+ incident_id=$( create_incident)
110+ echo " Created incident with ID: $incident_id "
35111fi
112+
36113exit " ${status} "
0 commit comments