clientconfig.go 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. package functions
  2. import (
  3. "strconv"
  4. "strings"
  5. "github.com/gravitl/netmaker/logger"
  6. "github.com/gravitl/netmaker/netclient/config"
  7. "github.com/gravitl/netmaker/netclient/functions/upgrades"
  8. "github.com/gravitl/netmaker/netclient/ncutils"
  9. )
  10. // UpdateClientConfig - function is called on daemon start to update clientConfig if required
  11. // Usage : set update required to true and and update logic to function
  12. func UpdateClientConfig() {
  13. defer upgrades.ReleaseUpgrades()
  14. networks, _ := ncutils.GetSystemNetworks()
  15. if len(networks) == 0 {
  16. return
  17. }
  18. logger.Log(0, "checking for netclient updates...")
  19. for _, network := range networks {
  20. cfg := config.ClientConfig{}
  21. cfg.Network = network
  22. cfg.ReadConfig()
  23. major, minor, _ := Version(cfg.Node.Version)
  24. if major == 0 && minor < 14 {
  25. logger.Log(0, "schema of network", cfg.Network, "is out of date and cannot be updated\n Correct behaviour of netclient cannot be guaranteed")
  26. continue
  27. }
  28. configChanged := false
  29. for _, u := range upgrades.Upgrades {
  30. if ncutils.StringSliceContains(u.RequiredVersions, cfg.Node.Version) {
  31. logger.Log(0, "upgrading node", cfg.Node.Name, "on network", cfg.Node.Network, "from", cfg.Node.Version, "to", u.NewVersion)
  32. upgrades.UpgradeFunction(u.OP)(&cfg)
  33. cfg.Node.Version = u.NewVersion
  34. configChanged = true
  35. }
  36. }
  37. if configChanged {
  38. //save and publish
  39. if err := PublishNodeUpdate(&cfg); err != nil {
  40. logger.Log(0, "error publishing node update during schema change", err.Error())
  41. }
  42. }
  43. }
  44. logger.Log(0, "finished updates")
  45. }
  46. // Version - parse version string into component parts
  47. // version string must be semantic version of form 1.2.3 or v1.2.3
  48. // otherwise 0, 0, 0 will be returned.
  49. func Version(version string) (int, int, int) {
  50. var major, minor, patch int
  51. var errMajor, errMinor, errPatch error
  52. parts := strings.Split(version, ".")
  53. //ensure semantic version
  54. if len(parts) < 3 {
  55. return major, minor, patch
  56. }
  57. if strings.Contains(parts[0], "v") {
  58. majors := strings.Split(parts[0], "v")
  59. major, errMajor = strconv.Atoi(majors[1])
  60. } else {
  61. major, errMajor = strconv.Atoi(parts[0])
  62. }
  63. minor, errMinor = strconv.Atoi(parts[1])
  64. patch, errPatch = strconv.Atoi(parts[2])
  65. if errMajor != nil || errMinor != nil || errPatch != nil {
  66. return 0, 0, 0
  67. }
  68. return major, minor, patch
  69. }