diff --git a/api/api.go b/api/api.go index bd2da6b..5b7fec7 100644 --- a/api/api.go +++ b/api/api.go @@ -45,7 +45,7 @@ const ( cptvGlob = "*.cptv" failedUploadsFolder = "failed-uploads" rebootDelay = time.Second * 5 - apiVersion = 3 + apiVersion = 4 ) type ManagementAPI struct { @@ -460,6 +460,22 @@ func (api *ManagementAPI) DeleteEvents(w http.ResponseWriter, r *http.Request) { } } +// CheckSaltConnection will try to ping the salt server and return the response +func (api *ManagementAPI) CheckSaltConnection(w http.ResponseWriter, r *http.Request) { + log.Println("pinging salt server") + out, err := exec.Command("salt-call", "test.ping").Output() + + output := map[string]interface{}{ + "output": string(out), + "success": true, + } + if err != nil { + output["error"] = err.Error() + output["success"] = false + } + json.NewEncoder(w).Encode(output) +} + func isEventKey(key uint64) (bool, error) { keys, err := eventclient.GetEventKeys() if err != nil { diff --git a/cmd/managementd/main.go b/cmd/managementd/main.go index f2551f2..21eb2c1 100644 --- a/cmd/managementd/main.go +++ b/cmd/managementd/main.go @@ -101,6 +101,7 @@ func main() { apiRouter.HandleFunc("/event-keys", apiObj.GetEventKeys).Methods("GET") apiRouter.HandleFunc("/events", apiObj.GetEvents).Methods("GET") apiRouter.HandleFunc("/events", apiObj.DeleteEvents).Methods("DELETE") + apiRouter.HandleFunc("/check-salt-connection", apiObj.CheckSaltConnection).Methods("GET") apiRouter.Use(basicAuth) listenAddr := fmt.Sprintf(":%d", config.Port) diff --git a/html/about.html b/html/about.html index d258f1c..3fa8e45 100644 --- a/html/about.html +++ b/html/about.html @@ -62,6 +62,7 @@

Device

+
@@ -94,6 +95,7 @@

Installed Packages

+ diff --git a/static/js/about.js b/static/js/about.js new file mode 100644 index 0000000..3cd2b19 --- /dev/null +++ b/static/js/about.js @@ -0,0 +1,31 @@ + function checkSaltConnection() { + $("#check-salt-button").attr('disabled', true); + var xmlHttp = new XMLHttpRequest(); + xmlHttp.open("GET", "/api/check-salt-connection", true); + xmlHttp.setRequestHeader("Authorization", "Basic "+btoa("admin:feathers")) + + xmlHttp.timeout = 20000; // Set timeout for 20 seconds + xmlHttp.onload = async function() { + if (xmlHttp.status == 200) { + var response = JSON.parse(xmlHttp.response); + console.log(response); + if (response.success) { + alert("Salt connected and accepted") + } else { + alert(response.output) + } + } else { + console.log("error with checking salt connection"); + } + $("#check-salt-button").attr('disabled', false); + } + xmlHttp.onerror = async function() { + console.log("error with checking salt connection"); + $("#check-salt-button").attr('disabled', false); + } + xmlHttp.ontimeout = async function() { + alert("connection timeout") + $("#check-salt-button").attr('disabled', false); + } + xmlHttp.send(null) + } \ No newline at end of file