Skip to content

Commit

Permalink
Update API to add option to force an update.
Browse files Browse the repository at this point in the history
  • Loading branch information
CameronRP committed Jul 9, 2024
1 parent ef5cf20 commit 156683b
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 7 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.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
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
6 changes: 4 additions & 2 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...");
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

0 comments on commit 156683b

Please sign in to comment.