Skip to content

Commit d15b3e5

Browse files
committed
Refactor windows update method
1 parent 3492013 commit d15b3e5

File tree

1 file changed

+48
-17
lines changed

1 file changed

+48
-17
lines changed

client/internal/updatemanager/update_windows.go

Lines changed: 48 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
package updatemanager
44

55
import (
6+
"fmt"
67
"os/exec"
8+
"runtime"
79
"strings"
810

911
log "github.com/sirupsen/logrus"
@@ -12,31 +14,49 @@ import (
1214
)
1315

1416
const (
15-
msiDownloadURL = "https://github.com/netbirdio/netbird/releases/download/v%s/netbird_installer_%s_windows_amd64.msi"
16-
exeDownloadURL = "https://github.com/netbirdio/netbird/releases/download/v%s/netbird_installer_%s_windows_amd64.exe"
17+
msiDownloadURL = "https://github.com/netbirdio/netbird/releases/download/v%version/netbird_installer_%version_windows_%arch.msi"
18+
exeDownloadURL = "https://github.com/netbirdio/netbird/releases/download/v%version/netbird_installer_%version_windows_%arch.exe"
19+
uninstallKeyPath64 = `SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\Netbird`
20+
uninstallKeyPath32 = `SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Netbird`
1721
)
1822

19-
func (u *UpdateManager) triggerUpdate(targetVersion string) error {
20-
k, err := registry.OpenKey(registry.LOCAL_MACHINE, `SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\Netbird`, registry.QUERY_VALUE)
21-
if err != nil && strings.Contains(err.Error(), "system cannot find the file specified") {
22-
// Installed using MSI installer
23-
path, err := downloadFileToTemporaryDir(u.ctx, strings.ReplaceAll(msiDownloadURL, "%s", targetVersion))
23+
func installationMethod() string {
24+
k, err := registry.OpenKey(registry.LOCAL_MACHINE, uninstallKeyPath64, registry.QUERY_VALUE)
25+
if err != nil {
26+
k, err = registry.OpenKey(registry.LOCAL_MACHINE, uninstallKeyPath32, registry.QUERY_VALUE)
2427
if err != nil {
25-
return err
28+
return "MSI"
29+
} else {
30+
err = k.Close()
31+
if err != nil {
32+
log.Warnf("Error closing registry key: %v", err)
33+
}
34+
}
35+
} else {
36+
err = k.Close()
37+
if err != nil {
38+
log.Warnf("Error closing registry key: %v", err)
2639
}
27-
cmd := exec.Command("msiexec", "/quiet", "/i", path)
28-
err = cmd.Run()
29-
return err
30-
} else if err != nil {
31-
return err
3240
}
33-
err = k.Close()
41+
return "EXE"
42+
}
43+
44+
func (u *UpdateManager) updateMSI(targetVersion string) error {
45+
url := strings.ReplaceAll(msiDownloadURL, "%version", targetVersion)
46+
url = strings.ReplaceAll(url, "%arch", runtime.GOARCH)
47+
path, err := downloadFileToTemporaryDir(u.ctx, url)
3448
if err != nil {
35-
log.Warnf("Error closing registry key: %v", err)
49+
return err
3650
}
51+
cmd := exec.Command("msiexec", "/quiet", "/i", path)
52+
err = cmd.Run()
53+
return err
54+
}
3755

38-
// Installed using EXE installer
39-
path, err := downloadFileToTemporaryDir(u.ctx, strings.ReplaceAll(exeDownloadURL, "%s", targetVersion))
56+
func (u *UpdateManager) updateEXE(targetVersion string) error {
57+
url := strings.ReplaceAll(exeDownloadURL, "%version", targetVersion)
58+
url = strings.ReplaceAll(url, "%arch", runtime.GOARCH)
59+
path, err := downloadFileToTemporaryDir(u.ctx, url)
4060
if err != nil {
4161
return err
4262
}
@@ -49,3 +69,14 @@ func (u *UpdateManager) triggerUpdate(targetVersion string) error {
4969

5070
return err
5171
}
72+
73+
func (u *UpdateManager) triggerUpdate(targetVersion string) error {
74+
switch installationMethod() {
75+
case "EXE":
76+
return u.updateEXE(targetVersion)
77+
case "MSI":
78+
return u.updateMSI(targetVersion)
79+
default:
80+
return fmt.Errorf("unsupported installation method: %s", installationMethod())
81+
}
82+
}

0 commit comments

Comments
 (0)