Browse Source

Merge pull request #1272 from gravitl/feature_v0.14.5_client_upgrades

Feature v0.14.5 client upgrades
dcarns 3 years ago
parent
commit
a02a63c5a6

+ 74 - 0
netclient/functions/clientconfig.go

@@ -0,0 +1,74 @@
+package functions
+
+import (
+	"strconv"
+	"strings"
+
+	"github.com/gravitl/netmaker/logger"
+	"github.com/gravitl/netmaker/netclient/config"
+	"github.com/gravitl/netmaker/netclient/functions/upgrades"
+	"github.com/gravitl/netmaker/netclient/ncutils"
+)
+
+// UpdateClientConfig - function is called on daemon start to update clientConfig if required
+// Usage :  set update required to true and and update logic to function
+func UpdateClientConfig() {
+	defer upgrades.ReleaseUpgrades()
+
+	networks, _ := ncutils.GetSystemNetworks()
+	if len(networks) == 0 {
+		return
+	}
+	logger.Log(0, "checking for netclient updates...")
+	for _, network := range networks {
+		cfg := config.ClientConfig{}
+		cfg.Network = network
+		cfg.ReadConfig()
+		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
+		for _, u := range upgrades.Upgrades {
+			if ncutils.StringSliceContains(u.RequiredVersions, cfg.Node.Version) {
+				logger.Log(0, "upgrading node", cfg.Node.Name, "on network", cfg.Node.Network, "from", cfg.Node.Version, "to", u.NewVersion)
+				upgrades.UpgradeFunction(u.OP)(&cfg)
+				cfg.Node.Version = u.NewVersion
+				configChanged = true
+			}
+		}
+		if configChanged {
+			//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")
+}
+
+// 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
+}

+ 1 - 0
netclient/functions/daemon.go

@@ -42,6 +42,7 @@ type cachedMessage struct {
 
 
 // Daemon runs netclient daemon from command line
 // Daemon runs netclient daemon from command line
 func Daemon() error {
 func Daemon() error {
+	UpdateClientConfig()
 	serverSet := make(map[string]bool)
 	serverSet := make(map[string]bool)
 	// == initial pull of all networks ==
 	// == initial pull of all networks ==
 	networks, _ := ncutils.GetSystemNetworks()
 	networks, _ := ncutils.GetSystemNetworks()

+ 13 - 0
netclient/functions/upgrades/types.go

@@ -0,0 +1,13 @@
+package upgrades
+
+import "github.com/gravitl/netmaker/netclient/config"
+
+// UpgradeFunction - logic for upgrade
+type UpgradeFunction func(*config.ClientConfig)
+
+// UpgradeInfo - struct for holding upgrade info
+type UpgradeInfo struct {
+	RequiredVersions []string
+	NewVersion       string
+	OP               UpgradeFunction
+}

+ 20 - 0
netclient/functions/upgrades/upgrades.go

@@ -0,0 +1,20 @@
+package upgrades
+
+func init() {
+	addUpgrades([]UpgradeInfo{
+		upgrade0145,
+	})
+}
+
+// Upgrades - holds all upgrade funcs
+var Upgrades = []UpgradeInfo{}
+
+// addUpgrades - Adds upgrades to make to client
+func addUpgrades(upgrades []UpgradeInfo) {
+	Upgrades = append(Upgrades, upgrades...)
+}
+
+// ReleaseUpgrades - releases upgrade funcs from memory
+func ReleaseUpgrades() {
+	Upgrades = nil
+}

+ 24 - 0
netclient/functions/upgrades/v0-14-5.go

@@ -0,0 +1,24 @@
+package upgrades
+
+import (
+	"github.com/gravitl/netmaker/logger"
+	"github.com/gravitl/netmaker/netclient/config"
+)
+
+var upgrade0145 = UpgradeInfo{
+	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) {
+	// do stuff for 14.X -> 14.5
+	// No-op
+	logger.Log(0, "updating schema for 0.14.5")
+}