Skip to content

Commit

Permalink
Merge pull request #139 from TheCacophonyProject/add-update-progess
Browse files Browse the repository at this point in the history
Add progress bar for salt updates
  • Loading branch information
CameronRP authored Jul 9, 2024
2 parents 880ebd4 + 59b4a9f commit 590f598
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 12 deletions.
36 changes: 34 additions & 2 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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.4.0
github.com/TheCacophonyProject/salt-updater v0.8.2
github.com/gobuffalo/packr v1.30.1
github.com/godbus/dbus v4.1.0+incompatible
github.com/gorilla/mux v1.8.0
Expand Down
6 changes: 2 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ github.com/TheCacophonyProject/audiobait/v3 v3.0.1 h1:7MGtBVqwuxLs4Tuw3VUQy1z/vc
github.com/TheCacophonyProject/audiobait/v3 v3.0.1/go.mod h1:EDM0fyF6oHPUmtOfY4QsLmOL4mfpLOtsngaQfHbttKk=
github.com/TheCacophonyProject/event-reporter v1.3.2-0.20200210010421-ca3fcb76a231 h1:nLqfSx3zDBzghlP/S8X2j/WRtW0e3BZvSkMm1J/Fa8U=
github.com/TheCacophonyProject/event-reporter v1.3.2-0.20200210010421-ca3fcb76a231/go.mod h1:kei6S/4x+VHp5yiwYPqvcNnhuRpZ8iLsh95Sue6kPvc=
github.com/TheCacophonyProject/event-reporter/v3 v3.2.1/go.mod h1:0ejh8kTFM9iC3/Q+etu7Hf4XivPu35PDH5oQ1XY3KNU=
github.com/TheCacophonyProject/event-reporter/v3 v3.3.0/go.mod h1:dGIYfhABsJHKjcsxtftDwpdcfLOWTYKeIyCYxCOIMrc=
github.com/TheCacophonyProject/event-reporter/v3 v3.7.0 h1:B+a5PXqBh7Bkf2HDo+j94Ascqw6SCTlOhSN9TXZfjY8=
github.com/TheCacophonyProject/event-reporter/v3 v3.7.0/go.mod h1:WTppJtTBxduasM1Or5SAh4Mm0YrTDnprOChjnGYgyEI=
Expand Down Expand Up @@ -83,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.4.0 h1:2RCgdI4wr/ZoeWOvfGlZ/Epgz3GTC+Vc7/wS6EI90c0=
github.com/TheCacophonyProject/salt-updater v0.4.0/go.mod h1:fVlMqJZpvT8HeT4GcmQlvMq8+AtvF4NZ/LuEVSoA5E8=
github.com/TheCacophonyProject/salt-updater v0.8.2 h1:QEGJDRKGWxJJcITD0Lrl7bsQU2DzBFdk8R46nR9w6Rg=
github.com/TheCacophonyProject/salt-updater v0.8.2/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=
Expand All @@ -96,7 +95,6 @@ github.com/alexflint/go-arg v0.0.0-20180516182405-f7c0423bd11e/go.mod h1:PHxo6ZW
github.com/alexflint/go-arg v1.0.0/go.mod h1:Cto8k5VtkP4pp0EXiWD4ZJMFOOinZ38ggVcQ/6CGuRI=
github.com/alexflint/go-arg v1.1.0/go.mod h1:3Rj4baqzWaGGmZA2+bVTV8zQOZEjBQAPBnL5xLT+ftY=
github.com/alexflint/go-arg v1.2.0/go.mod h1:3Rj4baqzWaGGmZA2+bVTV8zQOZEjBQAPBnL5xLT+ftY=
github.com/alexflint/go-arg v1.3.0/go.mod h1:9iRbDxne7LcR/GSvEr7ma++GLpdIU1zrghf2y2768kM=
github.com/alexflint/go-arg v1.4.2/go.mod h1:9iRbDxne7LcR/GSvEr7ma++GLpdIU1zrghf2y2768kM=
github.com/alexflint/go-scalar v1.0.0/go.mod h1:GpHzbCOZXEKMEcygYQ5n/aa4Aq84zbxjy3MxYW0gjYw=
github.com/alexflint/go-scalar v1.1.0/go.mod h1:LoFvNMqS1CPrMVltza4LvnGKhaSpc3oyLEBUZVhhS2o=
Expand Down
9 changes: 9 additions & 0 deletions html/about.html
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,15 @@ <h3>Device<br></h3>
<label class="form-check-label" for="auto-update-checkbox">AutoUpdate</label>
</div>

<div class="row">
<p class="col-2 text-left">Salt update progress:</p>
<p id="salt-update-progress" class="col-9 monospace"></p>
</div>
<div class="row">
<p class="col-2 text-left">Salt update state:</p>
<p id="salt-update-progress-text" class="col-9 monospace"></p>
</div>

<div class="row">
<p class="col-2 text-left">Running Salt Command:</p>
<p id="running-salt-command" class="col-9 monospace"></p>
Expand Down
27 changes: 22 additions & 5 deletions static/js/about.js
Original file line number Diff line number Diff line change
Expand Up @@ -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...");
pollSaltUpdateState();
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() {
Expand Down Expand Up @@ -117,10 +119,18 @@ async function updateSaltState() {
});

if (response.ok) {
var jsonString = await response.text();
var data = JSON.parse(await response.text());

var data = JSON.parse(jsonString);
if (data.RunningUpdate) {
document.getElementById("salt-update-button").setAttribute("disabled", true);
document.getElementById("salt-update-button").textContent = "Running Salt Update...";
setTimeout(updateSaltState, 2000);
} else {
enableSaltButton();
}

document.getElementById("salt-update-progress").textContent = data.UpdateProgressPercentage;
document.getElementById("salt-update-progress-text").textContent = data.UpdateProgressStr;
document.getElementById("running-salt-command").textContent =
data.RunningUpdate ? "Yes" : "No";
document.getElementById("running-salt-arguements").textContent =
Expand All @@ -135,13 +145,20 @@ async function updateSaltState() {
} else {
alert("Error updating salt");
console.error("Error with response:", await response.text());
enableSaltButton();
}
} catch (error) {
alert("Error updating salt");
console.error("Error with fetching salt update:", error);
enableSaltButton();
}
}

function enableSaltButton() {
document.getElementById("salt-update-button").removeAttribute("disabled");
document.getElementById("salt-update-button").textContent = "Run Salt Update...";
}

var runningSaltUpdate = true;
// Check salt update state. Returns true if it is no longer running
function checkSaltUpdateState() {
Expand Down

0 comments on commit 590f598

Please sign in to comment.