Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[NET-553] Improve host upgrade message handling #583

Merged
merged 7 commits into from
Oct 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 31 additions & 1 deletion functions/mqhandlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,12 @@ func HostPeerUpdate(client mqtt.Client, msg mqtt.Message) {
}
if peerUpdate.ServerVersion != config.Version {
slog.Warn("server/client version mismatch", "server", peerUpdate.ServerVersion, "client", config.Version)
if versionLessThan(config.Version, peerUpdate.ServerVersion) && config.Netclient().Host.AutoUpdate {
vlt, err := versionLessThan(config.Version, peerUpdate.ServerVersion)
if err != nil {
slog.Error("error checking version less than", "error", err)
return
}
if vlt && config.Netclient().Host.AutoUpdate {
slog.Info("updating client to server's version", "version", peerUpdate.ServerVersion)
if err := UseVersion(peerUpdate.ServerVersion, false); err != nil {
slog.Error("error updating client to server's version", "error", err)
Expand Down Expand Up @@ -202,6 +207,31 @@ func HostUpdate(client mqtt.Client, msg mqtt.Message) {
slog.Info("processing host update", "server", serverName, "action", hostUpdate.Action)
var resetInterface, restartDaemon, sendHostUpdate, clearMsg bool
switch hostUpdate.Action {
case models.Upgrade:
cv, sv := config.Version, server.Version
slog.Info("checking if need to upgrade client to server's version", "", config.Version, "version", server.Version)
vlt, err := versionLessThan(cv, sv)
if err == nil {
// if we have an assertive result, and it's that
// the client is up-to-date, nothing else to do
if !vlt {
slog.Info("no need to upgrade client, version is up-to-date")
break
}
} else {
// if we have a dubious result, assume that we need to upgrade client,
// this can occur when using custom client versions not following semver
slog.Warn("error checking version less than, but will proceed with upgrade", "error", err)
}
slog.Info("upgrading client to server's version", "version", sv)
if err := UseVersion(sv, false); err != nil {
slog.Error("error upgrading client to server's version", "error", err)
break
}
slog.Info("upgraded client to server's version, restarting", "version", sv)
if err := daemon.HardRestart(); err != nil {
slog.Error("failed to hard restart daemon", "error", err)
}
case models.JoinHostToNetwork:
commonNode := hostUpdate.Node.CommonNode
nodeCfg := config.Node{
Expand Down
16 changes: 12 additions & 4 deletions functions/use_version.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,20 +61,28 @@ func downloadVersion(version string) error {

// versionLessThan checks if v1 < v2 semantically
// dev is the latest version
func versionLessThan(v1, v2 string) bool {
func versionLessThan(v1, v2 string) (bool, error) {
if v1 == "dev" {
return false
return false, nil
}
if v2 == "dev" {
return true
return true, nil
}
semVer1 := strings.TrimFunc(v1, func(r rune) bool {
return !unicode.IsNumber(r)
})
semVer2 := strings.TrimFunc(v2, func(r rune) bool {
return !unicode.IsNumber(r)
})
return semver.MustParse(semVer1).LT(semver.MustParse(semVer2))
sv1, err := semver.Parse(semVer1)
if err != nil {
return false, fmt.Errorf("failed to parse semver1 (%s): %w", semVer1, err)
}
sv2, err := semver.Parse(semVer2)
if err != nil {
return false, fmt.Errorf("failed to parse semver2 (%s): %w", semVer2, err)
}
return sv1.LT(sv2), nil
}

// UseVersion switches the current netclient version to the one specified if available in the github releases page
Expand Down
24 changes: 21 additions & 3 deletions functions/version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,33 @@ func TestIsVersionCompatible(t *testing.T) {
func TestVersionLessThan(t *testing.T) {
is := is.New(t)
t.Run("lower version", func(t *testing.T) {
ok := versionLessThan("v0.15.0", "v0.16.0")
ok, _ := versionLessThan("v0.15.0", "v0.16.0")
is.Equal(ok, true)
})
t.Run("equal version", func(t *testing.T) {
ok := versionLessThan("v0.18.0", "v0.18.0")
ok, _ := versionLessThan("v0.18.0", "v0.18.0")
is.Equal(ok, false)
})
t.Run("higher version", func(t *testing.T) {
ok := versionLessThan("v0.19.0", "v0.18.0")
ok, _ := versionLessThan("v0.19.0", "v0.18.0")
is.Equal(ok, false)
})
}

func TestVersionUnknown(t *testing.T) {
is := is.New(t)
t.Run("no semver 1", func(t *testing.T) {
ok, err := versionLessThan("not-sem-ver", "v0.16.0")
is.Equal(ok, false)
if err == nil {
t.Fatalf("error should be non-nil")
}
})
t.Run("no semver 2", func(t *testing.T) {
ok, err := versionLessThan("v0.16.0", "not-sem-ver")
is.Equal(ok, false)
if err == nil {
t.Fatalf("error should be non-nil")
}
})
}
Loading