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

Feature v0.14.5 client upgrades
This commit is contained in:
dcarns 2022-07-07 13:41:19 -04:00 committed by GitHub
commit a02a63c5a6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 132 additions and 0 deletions

View file

@ -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
}

View file

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

View file

@ -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
}

View file

@ -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
}

View file

@ -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")
}