From ea0f619854a56b07bde9c1f77e654e0b3b59eb72 Mon Sep 17 00:00:00 2001 From: cam Date: Mon, 29 Jan 2024 19:46:32 +1300 Subject: [PATCH] Added upload logs button on About page --- api/api.go | 22 ++++++++++++++++++++++ cmd/managementd/main.go | 1 + html/about.html | 4 ++++ static/js/about.js | 26 ++++++++++++++++++++++++++ 4 files changed, 53 insertions(+) diff --git a/api/api.go b/api/api.go index 212cb3d..7173e85 100644 --- a/api/api.go +++ b/api/api.go @@ -1031,3 +1031,25 @@ func (api *ManagementAPI) GetConnectionStatus(w http.ResponseWriter, r *http.Req w.WriteHeader(http.StatusOK) json.NewEncoder(w).Encode(data) } + +func (api *ManagementAPI) UploadLogs(w http.ResponseWriter, r *http.Request) { + if err := exec.Command("cp", "/var/log/syslog", "/tmp/syslog").Run(); err != nil { + log.Printf("Error copying syslog: %v", err) + serverError(&w, err) + return + } + + if err := exec.Command("gzip", "/tmp/syslog", "-f").Run(); err != nil { + log.Printf("Error compressing syslog: %v", err) + serverError(&w, err) + return + } + + if err := exec.Command("salt-call", "cp.push", "/tmp/syslog.gz").Run(); err != nil { + log.Printf("Error pushing syslog with salt: %v", err) + serverError(&w, err) + return + } + + w.WriteHeader(http.StatusOK) +} diff --git a/cmd/managementd/main.go b/cmd/managementd/main.go index dd70912..22f5dc5 100644 --- a/cmd/managementd/main.go +++ b/cmd/managementd/main.go @@ -159,6 +159,7 @@ func main() { apiRouter.HandleFunc("/enable-wifi", apiObj.EnableWifi).Methods("POST") apiRouter.HandleFunc("/enable-hotspot", apiObj.EnableHotspot).Methods("POST") apiRouter.HandleFunc("/wifi-status", apiObj.GetConnectionStatus).Methods("GET") + apiRouter.HandleFunc("/upload-logs", apiObj.UploadLogs).Methods("PUT") apiRouter.Use(basicAuth) diff --git a/html/about.html b/html/about.html index c1d1814..8bf0e72 100644 --- a/html/about.html +++ b/html/about.html @@ -78,6 +78,10 @@

Device

+
+ +
+

Installed Packages

diff --git a/static/js/about.js b/static/js/about.js index cfb4201..084ba53 100644 --- a/static/js/about.js +++ b/static/js/about.js @@ -84,6 +84,32 @@ function runSaltUpdate() { xmlHttp.send(null); } +async function uploadLogs() { + $("#upload-logs-button").attr("disabled", true); + $("#upload-logs-button").html("Uploading logs..."); + try { + const response = await fetch("/api/upload-logs", { + method: "PUT", + headers: { + "Authorization": "Basic " + btoa("admin:feathers"), + "Content-Type": "application/json" + } + }); + + if (response.ok) { + alert("Logs uploaded"); + } else { + alert("Error uploading logs"); + console.error("Error with response:", await response.text()); + } + } catch (error) { + alert("Error uploading logs"); + console.error("Error with uploading logs:", error); + } + $("#upload-logs-button").attr("disabled", false); + $("#upload-logs-button").html("Upload logs"); +} + var runningSaltUpdate = true; // Check salt update state. Returns true if it is no longer running function checkSaltUpdateState() {