Skip to content

Commit

Permalink
Add healthy and supported states (#53)
Browse files Browse the repository at this point in the history
* Add healthy and supported states

* Update supervisor.go

Co-authored-by: Pascal Vizeli <[email protected]>

Co-authored-by: Pascal Vizeli <[email protected]>
  • Loading branch information
ludeeus and pvizeli authored Sep 29, 2021
1 parent 61bb3c1 commit 6945f53
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 24 deletions.
20 changes: 15 additions & 5 deletions http.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
36 changes: 29 additions & 7 deletions rootfs/usr/share/www/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,35 @@
<body>
<div class="container">
<h1>Home Assistant observer</h1>
<div class="info">
<span class="title">Supervisor:</span>
<span class="description {{if .On}}connected{{else}}disconnected{{end}}">
{{if .On}}Connected{{else}}Disconnected{{end}}
</span>
</div>
{{ if not .On }}
<table>
<tr>
<td>
Supervisor:
</td>
<td class="{{if .SupervisorConnected}}connected{{else}}disconnected{{end}}">
{{if .SupervisorConnected}}Connected{{else}}Disconnected{{end}}
</td>
</tr>
{{if .SupervisorConnected}}
<tr>
<td>
Supported:
</td>
<td class="{{if .Supported}}connected{{else}}disconnected{{end}}">
{{if .Supported}}Supported{{else}}Unsupported{{end}}
</td>
</tr>
<tr>
<td>
Healthy:
</td>
<td class="{{if .Healthy}}connected{{else}}disconnected{{end}}">
{{if .Healthy}}Healthy{{else}}Unhealthy{{end}}
</td>
</tr>
{{end}}
</table>
{{ if not .SupervisorConnected }}
<pre class="log">{{.Logs}}</pre>
{{ end }}
</div>
Expand Down
9 changes: 3 additions & 6 deletions rootfs/usr/share/www/observer.css
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -31,7 +28,7 @@ body {
.disconnected {
color: red;
}
.title {
table tr td:first-of-type {
font-weight: bold;
}
h1 {
Expand Down
64 changes: 58 additions & 6 deletions supervisor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 6945f53

Please sign in to comment.