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} "
6
+ INSTATUS_API_KEY=" 1ff45b8a5008ae8d7a79619302563717"
7
+ INSTATUS_PAGE_ID=" cm3inkn4o0004o3jhyy40laus"
8
+ INSTATUS_COMPONENT_ID=" cm3invkvo003jzwwlx4pyquxn"
9
9
10
10
status=0
11
11
declare -a modules=()
12
12
declare -a failures=()
13
+
14
+ # Collect all module directories containing a main.tf file
13
15
for path in $( find . -not -path ' */.*' -type f -name main.tf -maxdepth 2 | cut -d ' /' -f 2 | sort -u) ; do
14
16
modules+=(" ${path} " )
15
17
done
18
+
16
19
echo " 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
17
66
for module in " ${modules[@]} " ; do
18
67
# Trim leading/trailing whitespace from module name
19
68
module=$( echo " ${module} " | xargs)
20
69
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} "
22
71
status_code=$( curl --output /dev/null --head --silent --fail --location " ${url} " --retry 3 --write-out " %{http_code}" )
23
72
# shellcheck disable=SC2181
24
73
if (( status_code != 200 )) ; then
@@ -30,7 +79,23 @@ for module in "${modules[@]}"; do
30
79
fi
31
80
done
32
81
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 "
35
99
fi
100
+
36
101
exit " ${status} "
0 commit comments