From 1768308331cf30ed2569f87885208307d03d01ca Mon Sep 17 00:00:00 2001 From: Jesus Date: Sun, 25 May 2025 18:44:47 +0200 Subject: [PATCH 1/4] updater not found --- backend/cmd/main.go | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/backend/cmd/main.go b/backend/cmd/main.go index 860259acc..0a9ff32f7 100644 --- a/backend/cmd/main.go +++ b/backend/cmd/main.go @@ -33,8 +33,8 @@ import ( vehicle_models "github.com/HyperloopUPV-H8/h9-backend/internal/vehicle/models" "github.com/HyperloopUPV-H8/h9-backend/pkg/abstraction" "github.com/HyperloopUPV-H8/h9-backend/pkg/broker" - connection_topic "github.com/HyperloopUPV-H8/h9-backend/pkg/broker/topics/connection" blcu_topics "github.com/HyperloopUPV-H8/h9-backend/pkg/broker/topics/blcu" + connection_topic "github.com/HyperloopUPV-H8/h9-backend/pkg/broker/topics/connection" data_topic "github.com/HyperloopUPV-H8/h9-backend/pkg/broker/topics/data" logger_topic "github.com/HyperloopUPV-H8/h9-backend/pkg/broker/topics/logger" message_topic "github.com/HyperloopUPV-H8/h9-backend/pkg/broker/topics/message" @@ -206,16 +206,23 @@ func main() { updaterExe = filepath.Join(updatersDir, "updater-macos-64") default: fmt.Fprintf(os.Stderr, "Unsupported updater: %s\n", osType) - os.Exit(1) + fmt.Println("Skipping update. Proceeding with the current version.") + // break } - cmd := exec.Command(updaterExe) - cmd.Dir = updatersDir - cmd.Stdout = os.Stdout - cmd.Stderr = os.Stderr - if err := cmd.Run(); err != nil { - fmt.Fprintf(os.Stderr, "Error launching updater: %v\n", err) - os.Exit(1) + if _, err := os.Stat(updaterExe); err == nil { + cmd := exec.Command(updaterExe) + cmd.Dir = updatersDir + cmd.Stdout = os.Stdout + cmd.Stderr = os.Stderr + if err := cmd.Run(); err != nil { + fmt.Fprintf(os.Stderr, "Error launching updater: %v\n", err) + os.Exit(1) + } + os.Exit(0) + } else { + fmt.Fprintf(os.Stderr, "Updater not found: %s\n", updaterExe) + fmt.Println("Skipping update. Proceeding with the current version.") } } From b2e079005ba5dd4ee7925c757cac22d4b5b14a4d Mon Sep 17 00:00:00 2001 From: Jesus Date: Sun, 25 May 2025 19:07:59 +0200 Subject: [PATCH 2/4] exit unnecesary --- backend/cmd/main.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/backend/cmd/main.go b/backend/cmd/main.go index 0a9ff32f7..4cbb90453 100644 --- a/backend/cmd/main.go +++ b/backend/cmd/main.go @@ -219,14 +219,12 @@ func main() { fmt.Fprintf(os.Stderr, "Error launching updater: %v\n", err) os.Exit(1) } - os.Exit(0) } else { fmt.Fprintf(os.Stderr, "Updater not found: %s\n", updaterExe) fmt.Println("Skipping update. Proceeding with the current version.") } } - os.Exit(0) } else { fmt.Println("Skipping update. Proceeding with the current version.") } From 09a62d48c715bbdffb61636193b77d55dc351717 Mon Sep 17 00:00:00 2001 From: Jesus Date: Wed, 28 May 2025 14:41:03 +0200 Subject: [PATCH 3/4] updater draft --- updater/main.go | 86 ++++++++++++++++++++++++------------------------- 1 file changed, 42 insertions(+), 44 deletions(-) diff --git a/updater/main.go b/updater/main.go index ba08f9e4a..8e456b5c5 100644 --- a/updater/main.go +++ b/updater/main.go @@ -21,7 +21,7 @@ const ( func main() { // Detect the operating system - osType := detectOS() + zipName := getZipName() // Check if the `../backend/` folder exists if _, err := os.Stat("../backend"); err == nil { @@ -29,21 +29,21 @@ func main() { updateFromGit() } else { fmt.Println("Directory '../backend' not found. Checking binaries...") - updateFromBinaries(osType) + updateFromBinaries(zipName) } } -func detectOS() string { +func getZipName() string { switch runtime.GOOS { case "windows": - return "backend-windows-64.exe" // Incluye la extensión .exe para Windows + return "windows" case "darwin": if strings.Contains(runtime.GOARCH, "arm") { - return "backend-macos-m1-64" + return "macos-arm64" } - return "backend-macos-64" + return "macos-intel" case "linux": - return "backend-linux-64" + return "linux" default: fmt.Fprintf(os.Stderr, "Unsupported operating system: %s\n", runtime.GOOS) os.Exit(1) @@ -104,42 +104,41 @@ func stopProcess(processName string) error { return nil } -func updateFromBinaries(osType string) { - binaries := []string{"backend-windows-64.exe", "backend-linux-64", "backend-macos-64", "backend-macos-m1-64"} - for _, binary := range binaries { - if _, err := os.Stat("./" + binary); err == nil { - // Check if the backend process is running - isRunning, err := isProcessRunning(binary) - if err != nil { - fmt.Fprintf(os.Stderr, "Error checking if process is running: %v\n", err) - os.Exit(1) - } - - // Stop the process if it's running - if isRunning { - fmt.Printf("Process %s is running. Stopping it...\n", binary) - if err := stopProcess(binary); err != nil { - fmt.Fprintf(os.Stderr, "Error stopping process %s: %v\n", binary, err) - os.Exit(1) - } - time.Sleep(500 * time.Millisecond) // waits 1/2 second to ensure the process is stopped - } +func updateFromBinaries(zipBase string) { + binary := "backend" + if runtime.GOOS == "windows" { + binary += ".exe" + } - fmt.Printf("Deleting old binary: %s\n", binary) - deleted := false - for i := 0; i < 5; i++ { - if err := os.Remove("./" + binary); err == nil { - deleted = true - break - } else { - fmt.Printf("Retrying delete (%d/5)...\n", i+1) - time.Sleep(300 * time.Millisecond) - } - } - if !deleted { - fmt.Fprintf(os.Stderr, "Error deleting old binary after multiple attempts.\n") + // Stop and remove old binary if exists + if _, err := os.Stat("./" + binary); err == nil { + isRunning, err := isProcessRunning(binary) + if err != nil { + fmt.Fprintf(os.Stderr, "Error checking if process is running: %v\n", err) + os.Exit(1) + } + if isRunning { + fmt.Printf("Process %s is running. Stopping it...\n", binary) + if err := stopProcess(binary); err != nil { + fmt.Fprintf(os.Stderr, "Error stopping process %s: %v\n", binary, err) os.Exit(1) } + time.Sleep(500 * time.Millisecond) + } + fmt.Printf("Deleting old binary: %s\n", binary) + deleted := false + for i := 0; i < 5; i++ { + if err := os.Remove("./" + binary); err == nil { + deleted = true + break + } else { + fmt.Printf("Retrying delete (%d/5)...\n", i+1) + time.Sleep(300 * time.Millisecond) + } + } + if !deleted { + fmt.Fprintf(os.Stderr, "Error deleting old binary after multiple attempts.\n") + os.Exit(1) } } @@ -150,8 +149,7 @@ func updateFromBinaries(osType string) { os.Exit(1) } - // Construct the ZIP file URL - zipFileName := fmt.Sprintf("control-station-v%s.zip", strings.ReplaceAll(latestVersion, ".", "-")) + zipFileName := fmt.Sprintf("%s-%s.zip", zipBase, latestVersion) url := fmt.Sprintf("https://github.com/%s/%s/releases/download/v%s/%s", repoOwner, repoName, latestVersion, zipFileName) fmt.Printf("Downloading ZIP from: %s\n", url) @@ -162,8 +160,8 @@ func updateFromBinaries(osType string) { os.Exit(1) } - // Extract the binary from the ZIP file - binaryPath, err := extractBinaryFromZip("./"+zipFileName, osType) + // Extract the binary and VERSION.md from the ZIP file + binaryPath, err := extractBinaryFromZip("./"+zipFileName, binary) if err != nil { fmt.Fprintf(os.Stderr, "Error extracting the binary: %v\n", err) os.Exit(1) From 4acb3204f2500bca8c712723699a63af1fdef6a6 Mon Sep 17 00:00:00 2001 From: Jesus Date: Wed, 28 May 2025 14:42:03 +0200 Subject: [PATCH 4/4] main changes for new structure release --- backend/cmd/main.go | 25 +++++++------------------ 1 file changed, 7 insertions(+), 18 deletions(-) diff --git a/backend/cmd/main.go b/backend/cmd/main.go index 4cbb90453..4739093da 100644 --- a/backend/cmd/main.go +++ b/backend/cmd/main.go @@ -185,34 +185,23 @@ func main() { } else { fmt.Println("Backend folder not detected. Launching existing updater...") - osType := detectOS() execPath, err := os.Executable() if err != nil { fmt.Fprintf(os.Stderr, "Error getting executable path: %v\n", err) os.Exit(1) } - updatersDir := filepath.Join(filepath.Dir(execPath), "updaters") - - var updaterExe string - switch osType { - case "updaters/updater-windows-64.exe": - updaterExe = filepath.Join(updatersDir, "updater-windows-64") - case "updaters/updater-linux": - updaterExe = filepath.Join(updatersDir, "updater-linux") - case "updaters/updater-macos-m1": - updaterExe = filepath.Join(updatersDir, "updater-macos-m1") - case "updaters/updater-macos-64": - updaterExe = filepath.Join(updatersDir, "updater-macos-64") - default: - fmt.Fprintf(os.Stderr, "Unsupported updater: %s\n", osType) - fmt.Println("Skipping update. Proceeding with the current version.") - // break + execDir := filepath.Dir(execPath) + + updaterExe := filepath.Join(execDir, "updater") + // En Windows el ejecutable lleva extensión .exe + if runtime.GOOS == "windows" { + updaterExe += ".exe" } if _, err := os.Stat(updaterExe); err == nil { cmd := exec.Command(updaterExe) - cmd.Dir = updatersDir + cmd.Dir = execDir cmd.Stdout = os.Stdout cmd.Stderr = os.Stderr if err := cmd.Run(); err != nil {