From 156683beeb82ce82c4a8834b109c5eeb318f3735 Mon Sep 17 00:00:00 2001 From: cam Date: Tue, 9 Jul 2024 14:13:52 +1200 Subject: [PATCH] Update API to add option to force an update. --- api/api.go | 36 ++++++++++++++++++++++++++++++++++-- go.mod | 2 +- go.sum | 4 ++-- static/js/about.js | 6 ++++-- 4 files changed, 41 insertions(+), 7 deletions(-) diff --git a/api/api.go b/api/api.go index 6313c5c..3979599 100644 --- a/api/api.go +++ b/api/api.go @@ -666,18 +666,50 @@ func (api *ManagementAPI) CheckSaltConnection(w http.ResponseWriter, r *http.Req // StartSaltUpdate will start a salt update process if not already running func (api *ManagementAPI) StartSaltUpdate(w http.ResponseWriter, r *http.Request) { + var requestBody struct { + Force bool `json:"force"` + } + + // Decode the JSON request body if there is one. + if r.ContentLength >= 0 { + if err := json.NewDecoder(r.Body).Decode(&requestBody); err != nil { + serverError(&w, errors.New("failed to parse request body")) + return + } + } + state, err := saltrequester.State() if err != nil { serverError(&w, errors.New("failed to check salt state")) + return } + + // Check if the update is already running if state.RunningUpdate { - w.Write([]byte("already runing salt update")) + w.Write([]byte("already running salt update")) return } - if err := saltrequester.RunUpdate(); err != nil { + + // Check if we should force the update + if requestBody.Force { + err := saltrequester.ForceUpdate() + if err != nil { + log.Printf("error forcing salt update: %v", err) + serverError(&w, errors.New("failed to force salt update")) + return + } + w.Write([]byte("force salt update started")) + return + } + + // Run the update, this will only run an update if one is required. + err = saltrequester.RunUpdate() + if err != nil { log.Printf("error calling a salt update: %v", err) serverError(&w, errors.New("failed to call a salt update")) + return } + w.Write([]byte("salt update started")) } // GetSaltUpdateState will get the salt update status diff --git a/go.mod b/go.mod index 2f464bf..2ec0d15 100644 --- a/go.mod +++ b/go.mod @@ -10,7 +10,7 @@ require ( github.com/TheCacophonyProject/go-cptv v0.0.0-20201215230510-ae7134e91a71 github.com/TheCacophonyProject/lepton3 v0.0.0-20211005194419-22311c15d6ee github.com/TheCacophonyProject/rtc-utils v1.2.0 - github.com/TheCacophonyProject/salt-updater v0.8.0 + github.com/TheCacophonyProject/salt-updater v0.8.1 github.com/gobuffalo/packr v1.30.1 github.com/godbus/dbus v4.1.0+incompatible github.com/gorilla/mux v1.8.0 diff --git a/go.sum b/go.sum index c64b6f4..d6b1f56 100644 --- a/go.sum +++ b/go.sum @@ -82,8 +82,8 @@ github.com/TheCacophonyProject/rpi-net-manager v0.4.0-deb12 h1:0EowolSaXYxt8DOlV github.com/TheCacophonyProject/rpi-net-manager v0.4.0-deb12/go.mod h1:6Xl1Dp7F8IyvufOv0O5EfoXQjqoA/qNLeoWh3Rv1Y0c= github.com/TheCacophonyProject/rtc-utils v1.2.0 h1:570sPJE/s0b21NrP9VVeSI/gBh/dLK+G5Ne/ZFwkNv0= github.com/TheCacophonyProject/rtc-utils v1.2.0/go.mod h1:uV1SIy93TLZrrBcqDczUNFUz2G/Pk6pZNUdTRglmANU= -github.com/TheCacophonyProject/salt-updater v0.8.0 h1:boiKcKXTtR4+VxADKza9DGD8zj15AfZOB4DbkqNphUM= -github.com/TheCacophonyProject/salt-updater v0.8.0/go.mod h1:jzmJe2yOF4Vme6Mj7RL9S4VyTTceY7dsAVdSMW/91UE= +github.com/TheCacophonyProject/salt-updater v0.8.1 h1:F2k0ooGOoMQ5iA71QqaJ6vh0f6GVXhftp/6j/QdruSU= +github.com/TheCacophonyProject/salt-updater v0.8.1/go.mod h1:jzmJe2yOF4Vme6Mj7RL9S4VyTTceY7dsAVdSMW/91UE= github.com/TheCacophonyProject/trap-controller v0.0.0-20230227002937-262a1adfaa47 h1:QSQnyDIk04eLq1FcegZbA4nF3QtBU+co0VX/g94u8I8= github.com/TheCacophonyProject/trap-controller v0.0.0-20230227002937-262a1adfaa47/go.mod h1:tGi6Qpp0vY9ycT9AM+a0/5DMW5kkvS2ofe7pdRMFqoU= github.com/TheCacophonyProject/window v0.0.0-20190821235241-ab92c2ee24b6/go.mod h1:Vww417iimOb0s46Ndsm8U/vYtwc0dZUet4uW8QzBo4M= diff --git a/static/js/about.js b/static/js/about.js index dc642d9..429627f 100644 --- a/static/js/about.js +++ b/static/js/about.js @@ -64,20 +64,22 @@ function runSaltUpdate() { var xmlHttp = new XMLHttpRequest(); xmlHttp.open("POST", "/api/salt-update", true); xmlHttp.setRequestHeader("Authorization", "Basic " + btoa("admin:feathers")); + xmlHttp.setRequestHeader("Content-Type", "application/json"); xmlHttp.onload = async function () { if (xmlHttp.status == 200) { $("#salt-update-button").attr("disabled", true); $("#salt-update-button").html("Running Salt Update..."); setTimeout(updateSaltState, 2000); } else { - console.log(response); + console.log(xmlHttp.responseText); } }; xmlHttp.onerror = async function () { console.log("error with running salt update"); }; - xmlHttp.send(null); + var jsonPayload = JSON.stringify({ force: true }); + xmlHttp.send(jsonPayload); } async function uploadLogs() {