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
+ # 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} "
9
21
10
22
status=0
11
23
declare -a modules=()
12
24
declare -a failures=()
25
+
26
+ # Collect all module directories containing a main.tf file
13
27
for path in $( find . -not -path ' */.*' -type f -name main.tf -maxdepth 2 | cut -d ' /' -f 2 | sort -u) ; do
14
28
modules+=(" ${path} " )
15
29
done
30
+
16
31
echo " 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
17
78
for module in " ${modules[@]} " ; do
18
79
# Trim leading/trailing whitespace from module name
19
80
module=$( echo " ${module} " | xargs)
20
81
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} "
22
83
status_code=$( curl --output /dev/null --head --silent --fail --location " ${url} " --retry 3 --write-out " %{http_code}" )
23
84
# shellcheck disable=SC2181
24
85
if (( status_code != 200 )) ; then
@@ -30,7 +91,23 @@ for module in "${modules[@]}"; do
30
91
fi
31
92
done
32
93
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 "
35
111
fi
112
+
36
113
exit " ${status} "
0 commit comments