Browse Source

support upgrades from multiple version

Matthew R. Kasun 3 years ago
parent
commit
f59a3c860e

+ 42 - 8
netclient/functions/clientconfig.go

@@ -1,7 +1,11 @@
 package functions
 package functions
 
 
 import (
 import (
+	"strconv"
+	"strings"
+
 	"github.com/gravitl/netmaker/logger"
 	"github.com/gravitl/netmaker/logger"
+	"github.com/gravitl/netmaker/logic"
 	"github.com/gravitl/netmaker/netclient/config"
 	"github.com/gravitl/netmaker/netclient/config"
 	"github.com/gravitl/netmaker/netclient/functions/upgrades"
 	"github.com/gravitl/netmaker/netclient/functions/upgrades"
 	"github.com/gravitl/netmaker/netclient/ncutils"
 	"github.com/gravitl/netmaker/netclient/ncutils"
@@ -16,27 +20,57 @@ func UpdateClientConfig() {
 	if len(networks) == 0 {
 	if len(networks) == 0 {
 		return
 		return
 	}
 	}
-	logger.Log(0, "updating netclient...")
+	logger.Log(0, "checking for netclient updates...")
 	for _, network := range networks {
 	for _, network := range networks {
 		cfg := config.ClientConfig{}
 		cfg := config.ClientConfig{}
 		cfg.Network = network
 		cfg.Network = network
 		cfg.ReadConfig()
 		cfg.ReadConfig()
-		//update any new fields
+		major, minor, _ := Version(cfg.Node.Version)
+		if major == 0 && minor < 14 {
+			logger.Log(0, "schema of network", cfg.Network, "is out of date and cannot be updated\n Correct behaviour of netclient cannot be guaranteed")
+			continue
+		}
 		configChanged := false
 		configChanged := false
 		for _, u := range upgrades.Upgrades {
 		for _, u := range upgrades.Upgrades {
-			if cfg.Node.Version == u.RequiredVersion {
-				upgrades.UpgradeFunction(u.OP)(&cfg)
+			configChanged = false
+			if logic.StringSliceContains(u.RequiredVersions, cfg.Node.Version) {
+				logger.Log(0, "updating ", cfg.Network)
+				u.OP(&cfg)
 				cfg.Node.Version = u.NewVersion
 				cfg.Node.Version = u.NewVersion
 				configChanged = true
 				configChanged = true
 			}
 			}
 		}
 		}
-		//insert update logic here
 		if configChanged {
 		if configChanged {
-			logger.Log(0, "updating clientConfig for network", cfg.Network)
-			if err := config.Write(&cfg, cfg.Network); err != nil {
-				logger.Log(0, "failed to update clientConfig for ", cfg.Network, err.Error())
+			//save and publish
+			if err := PublishNodeUpdate(&cfg); err != nil {
+				logger.Log(0, "error publishing node update during schema change", err.Error())
 			}
 			}
 		}
 		}
 	}
 	}
 	logger.Log(0, "finished updates")
 	logger.Log(0, "finished updates")
 }
 }
+
+// Version - parse version string into component parts
+// version string must be semantic version of form 1.2.3 or v1.2.3
+// otherwise 0, 0, 0 will be returned.
+func Version(version string) (int, int, int) {
+	var major, minor, patch int
+	var errMajor, errMinor, errPatch error
+	parts := strings.Split(version, ".")
+	//ensure semantic version
+	if len(parts) < 3 {
+		return major, minor, patch
+	}
+	if strings.Contains(parts[0], "v") {
+		majors := strings.Split(parts[0], "v")
+		major, errMajor = strconv.Atoi(majors[1])
+	} else {
+		major, errMajor = strconv.Atoi(parts[0])
+	}
+	minor, errMinor = strconv.Atoi(parts[1])
+	patch, errPatch = strconv.Atoi(parts[2])
+	if errMajor != nil || errMinor != nil || errPatch != nil {
+		return 0, 0, 0
+	}
+	return major, minor, patch
+}

+ 3 - 3
netclient/functions/upgrades/types.go

@@ -7,7 +7,7 @@ type UpgradeFunction func(*config.ClientConfig)
 
 
 // UpgradeInfo - struct for holding upgrade info
 // UpgradeInfo - struct for holding upgrade info
 type UpgradeInfo struct {
 type UpgradeInfo struct {
-	RequiredVersion string
-	NewVersion      string
-	OP              UpgradeFunction
+	RequiredVersions []string
+	NewVersion       string
+	OP               UpgradeFunction
 }
 }

+ 15 - 5
netclient/functions/upgrades/v0-14-5.go

@@ -1,14 +1,24 @@
 package upgrades
 package upgrades
 
 
-import "github.com/gravitl/netmaker/netclient/config"
+import (
+	"github.com/gravitl/netmaker/logger"
+	"github.com/gravitl/netmaker/netclient/config"
+)
 
 
 var upgrade0145 = UpgradeInfo{
 var upgrade0145 = UpgradeInfo{
-	RequiredVersion: "0.14.4",
-	NewVersion:      "0.14.5",
-	OP:              update0145,
+	RequiredVersions: []string{
+		"v0.14.0",
+		"v0.14.1",
+		"v0.14.2",
+		"v0.14.3",
+		"v0.14.4",
+	},
+	NewVersion: "v0.14.5",
+	OP:         update0145,
 }
 }
 
 
 func update0145(cfg *config.ClientConfig) {
 func update0145(cfg *config.ClientConfig) {
-	// do stuff for 14.4 -> 14.5
+	// do stuff for 14.X -> 14.5
 	// No-op
 	// No-op
+	logger.Log(0, "updating schema for 0.14.5")
 }
 }