clientconfig.go 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. if err := config.ModNodeConfig(&cfg.Node); err != nil {
  43. logger.Log(0, "error saving local config for node,", cfg.Node.Name, ", on network,", cfg.Node.Network)
  44. }
  45. }
  46. }
  47. logger.Log(0, "finished updates")
  48. }
  49. // Version - parse version string into component parts
  50. // version string must be semantic version of form 1.2.3 or v1.2.3
  51. // otherwise 0, 0, 0 will be returned.
  52. func Version(version string) (int, int, int) {
  53. var major, minor, patch int
  54. var errMajor, errMinor, errPatch error
  55. parts := strings.Split(version, ".")
  56. //ensure semantic version
  57. if len(parts) < 3 {
  58. return major, minor, patch
  59. }
  60. if strings.Contains(parts[0], "v") {
  61. majors := strings.Split(parts[0], "v")
  62. major, errMajor = strconv.Atoi(majors[1])
  63. } else {
  64. major, errMajor = strconv.Atoi(parts[0])
  65. }
  66. minor, errMinor = strconv.Atoi(parts[1])
  67. patch, errPatch = strconv.Atoi(parts[2])
  68. if errMajor != nil || errMinor != nil || errPatch != nil {
  69. return 0, 0, 0
  70. }
  71. return major, minor, patch
  72. }