Skip to content

Commit

Permalink
Merge pull request #96 from CameronRP/add-salt-ping-test
Browse files Browse the repository at this point in the history
Add salt ping test
  • Loading branch information
CameronRP authored Nov 29, 2020
2 parents 4784405 + bcb6e96 commit 9852351
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 1 deletion.
18 changes: 17 additions & 1 deletion api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ const (
cptvGlob = "*.cptv"
failedUploadsFolder = "failed-uploads"
rebootDelay = time.Second * 5
apiVersion = 3
apiVersion = 4
)

type ManagementAPI struct {
Expand Down Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions cmd/managementd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions html/about.html
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ <h3>Device<br></h3>
</div>
</div>
</div>
<button id="check-salt-button" type="button" onclick="checkSaltConnection()" class="btn btn-primary">Check Salt Connection</button>


<div class="container pt-5 pb-4 pl-0">
Expand Down Expand Up @@ -94,6 +95,7 @@ <h3>Installed Packages<br></h3>

</div>

<script type="text/javascript" src="/static/js/about.js"></script>
<script src="/static/js/jquery-3.3.1.slim.min.js"></script>
<script src="/static/js/bootstrap.min.js"></script>

Expand Down
31 changes: 31 additions & 0 deletions static/js/about.js
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit 9852351

Please sign in to comment.