1
1
#! /usr/bin/env bash
2
2
set -o pipefail
3
- REGISTRY_BASE_URL=" ${REGISTRY_BASE_URL:- https:// registry.coder.com} "
4
3
set -u
5
4
6
- if [[ -n " ${VERBOSE:- } " ]]; then
7
- set -x
8
- fi
5
+ REGISTRY_BASE_URL=" ${REGISTRY_BASE_URL:- https:// registry.coder.com} "
9
6
10
7
status=0
11
8
declare -a modules=()
12
9
declare -a failures=()
10
+
11
+ # Collect all module directories containing a main.tf file
13
12
for path in $( find . -not -path ' */.*' -type f -name main.tf -maxdepth 2 | cut -d ' /' -f 2 | sort -u) ; do
14
13
modules+=(" ${path} " )
15
14
done
15
+
16
16
echo " 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
17
63
for module in " ${modules[@]} " ; do
18
64
# Trim leading/trailing whitespace from module name
19
65
module=$( echo " ${module} " | xargs)
20
66
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} "
22
68
status_code=$( curl --output /dev/null --head --silent --fail --location " ${url} " --retry 3 --write-out " %{http_code}" )
23
69
# shellcheck disable=SC2181
24
70
if (( status_code != 200 )) ; then
@@ -30,7 +76,23 @@ for module in "${modules[@]}"; do
30
76
fi
31
77
done
32
78
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 "
35
96
fi
97
+
36
98
exit " ${status} "
0 commit comments