From 6945f53412ce986c62106fe260aa9ab87210022f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joakim=20S=C3=B8rensen?= Date: Wed, 29 Sep 2021 15:37:45 +0200 Subject: [PATCH] Add healthy and supported states (#53) * Add healthy and supported states * Update supervisor.go Co-authored-by: Pascal Vizeli Co-authored-by: Pascal Vizeli --- http.go | 20 +++++++--- rootfs/usr/share/www/index.html | 36 +++++++++++++---- rootfs/usr/share/www/observer.css | 9 ++--- supervisor.go | 64 ++++++++++++++++++++++++++++--- 4 files changed, 105 insertions(+), 24 deletions(-) diff --git a/http.go b/http.go index a5dec61..902717a 100644 --- a/http.go +++ b/http.go @@ -75,18 +75,28 @@ func apiRestart(w http.ResponseWriter, r *http.Request) { } type statusData struct { - On bool - Logs string + SupervisorConnected bool + Supported bool + Healthy bool + Logs string } func statusIndex(w http.ResponseWriter, r *http.Request) { data := statusData{ - On: supervisorPing(), - Logs: "", + SupervisorConnected: supervisorPing(), + } + + if data.SupervisorConnected { + supervisorInfo, err := getSupervisorInfo() + if err == nil { + data.Healthy = supervisorInfo.Healthy + data.Supported = supervisorInfo.Supported + } + } // Set logs - if !data.On { + if !data.SupervisorConnected { var buf bytes.Buffer var re = regexp.MustCompile(`\[\d+m`) logWriter := bufio.NewWriter(&buf) diff --git a/rootfs/usr/share/www/index.html b/rootfs/usr/share/www/index.html index c5a7e84..54a3d41 100644 --- a/rootfs/usr/share/www/index.html +++ b/rootfs/usr/share/www/index.html @@ -9,13 +9,35 @@

Home Assistant observer

-
- Supervisor: - - {{if .On}}Connected{{else}}Disconnected{{end}} - -
- {{ if not .On }} + + + + + + {{if .SupervisorConnected}} + + + + + + + + + {{end}} +
+ Supervisor: + + {{if .SupervisorConnected}}Connected{{else}}Disconnected{{end}} +
+ Supported: + + {{if .Supported}}Supported{{else}}Unsupported{{end}} +
+ Healthy: + + {{if .Healthy}}Healthy{{else}}Unhealthy{{end}} +
+ {{ if not .SupervisorConnected }}
{{.Logs}}
{{ end }}
diff --git a/rootfs/usr/share/www/observer.css b/rootfs/usr/share/www/observer.css index e753b29..e65a50b 100644 --- a/rootfs/usr/share/www/observer.css +++ b/rootfs/usr/share/www/observer.css @@ -18,11 +18,8 @@ body { 0px 1px 1px 0px rgba(0, 0, 0, 0.14), 0px 1px 3px 0px rgba(0, 0, 0, 0.12); } -.info { - display: flex; - align-items: center; -} -.info .title { + +.title { margin-right: 8px; } .connected { @@ -31,7 +28,7 @@ body { .disconnected { color: red; } -.title { +table tr td:first-of-type { font-weight: bold; } h1 { diff --git a/supervisor.go b/supervisor.go index b0421fa..6700181 100644 --- a/supervisor.go +++ b/supervisor.go @@ -2,27 +2,79 @@ package main import ( "context" + "encoding/json" + "fmt" "io" + "io/ioutil" "log" + "net/http" + "os" "github.com/docker/docker/api/types" "github.com/docker/docker/pkg/stdcopy" ) +type SupervisorResponse struct { + Result string `json:"result"` + Message string `json:"message,omitempty"` + Data map[string]interface{} `json:"data,omitempty"` +} + +type SupervisorInfo struct { + Healthy bool `json:"healthy"` + Supported bool `json:"supported"` +} + +func supervisorApiProxy(path string) (SupervisorResponse, error) { + var jsonResponse SupervisorResponse + request, _ := http.NewRequest("GET", fmt.Sprintf("http://supervisor/%s", path), nil) + request.Header = http.Header{ + "Authorization": []string{fmt.Sprintf("Bearer %s", os.Getenv("SUPERVISOR_TOKEN"))}, + } + + response, err := httpClient.Do(request) + if err != nil { + log.Printf("Supervisor API call failed with error %s", err) + return jsonResponse, err + } + + if response.StatusCode >= 300 { + log.Printf("Supervisor API call failed with status code %v", response.StatusCode) + return jsonResponse, err + } + + bodyBytes, err := ioutil.ReadAll(response.Body) + if err != nil { + return jsonResponse, err + } + + defer response.Body.Close() + + json.Unmarshal([]byte(bodyBytes), &jsonResponse) + return jsonResponse, err +} + func supervisorPing() bool { - response, err := httpClient.Get("http://supervisor/supervisor/ping") + _, err := supervisorApiProxy("supervisor/ping") if err != nil { log.Printf("Supervisor ping failed with error %s", err) return false } + return true +} - // Check response - if response.StatusCode < 300 { - return true +func getSupervisorInfo() (SupervisorInfo, error) { + var supervisorInfo SupervisorInfo + response, err := supervisorApiProxy("supervisor/info") + if err != nil { + log.Printf("Supervisor API call failed with error %s", err) + return supervisorInfo, err } - log.Printf("Supervisor ping failed with %d", response.StatusCode) - return false + jsonData, _ := json.Marshal(response.Data) + json.Unmarshal(jsonData, &supervisorInfo) + + return supervisorInfo, nil } func supervisorLogs(w io.Writer) error {