clientconfig.go 2.2 KB

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