update.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. package node
  2. import (
  3. "encoding/json"
  4. "log"
  5. "os"
  6. "strings"
  7. "github.com/gravitl/netmaker/cli/functions"
  8. "github.com/gravitl/netmaker/models"
  9. "github.com/spf13/cobra"
  10. )
  11. var nodeUpdateCmd = &cobra.Command{
  12. Use: "update [NETWORK NAME] [NODE ID]",
  13. Args: cobra.ExactArgs(2),
  14. Short: "Update a Node",
  15. Long: `Update a Node`,
  16. Run: func(cmd *cobra.Command, args []string) {
  17. var (
  18. node = &models.Node{}
  19. networkName = args[0]
  20. nodeID = args[1]
  21. )
  22. if nodeDefinitionFilePath != "" {
  23. content, err := os.ReadFile(nodeDefinitionFilePath)
  24. if err != nil {
  25. log.Fatal("Error when opening file: ", err)
  26. }
  27. if err := json.Unmarshal(content, node); err != nil {
  28. log.Fatal(err)
  29. }
  30. } else {
  31. if endpoint != "" {
  32. node.Endpoint = endpoint
  33. node.IsStatic = "no"
  34. }
  35. node.ListenPort = int32(listenPort)
  36. node.Address = address
  37. node.Address6 = address6
  38. node.LocalAddress = localAddress
  39. node.Name = name
  40. node.PostUp = postUp
  41. node.PostDown = postDown
  42. if allowedIPs != "" {
  43. node.AllowedIPs = strings.Split(allowedIPs, ",")
  44. }
  45. node.PersistentKeepalive = int32(keepAlive)
  46. if relayAddrs != "" {
  47. node.RelayAddrs = strings.Split(relayAddrs, ",")
  48. }
  49. if egressGatewayRanges != "" {
  50. node.EgressGatewayRanges = strings.Split(egressGatewayRanges, ",")
  51. }
  52. if localRange != "" {
  53. node.LocalRange = localRange
  54. node.IsLocal = "yes"
  55. }
  56. node.MTU = int32(mtu)
  57. node.ExpirationDateTime = int64(expirationDateTime)
  58. if defaultACL {
  59. node.DefaultACL = "yes"
  60. }
  61. if dnsOn {
  62. node.DNSOn = "yes"
  63. }
  64. if disconnect {
  65. node.Connected = "no"
  66. }
  67. if networkHub {
  68. node.IsHub = "yes"
  69. }
  70. }
  71. functions.PrettyPrint(functions.UpdateNode(networkName, nodeID, node))
  72. },
  73. }
  74. func init() {
  75. nodeUpdateCmd.Flags().StringVar(&nodeDefinitionFilePath, "file", "", "Filepath of updated node definition in JSON")
  76. nodeUpdateCmd.Flags().StringVar(&endpoint, "endpoint", "", "Public endpoint of the node")
  77. nodeUpdateCmd.Flags().IntVar(&listenPort, "listen_port", 0, "Default wireguard port for the node")
  78. nodeUpdateCmd.Flags().StringVar(&address, "ipv4_addr", "", "IPv4 address of the node")
  79. nodeUpdateCmd.Flags().StringVar(&address6, "ipv6_addr", "", "IPv6 address of the node")
  80. nodeUpdateCmd.Flags().StringVar(&localAddress, "local_addr", "", "Locally reachable address of the node")
  81. nodeUpdateCmd.Flags().StringVar(&name, "name", "", "Node name")
  82. nodeUpdateCmd.Flags().StringVar(&postUp, "post_up", "", "Commands to run after node is up `;` separated")
  83. nodeUpdateCmd.Flags().StringVar(&postDown, "post_down", "", "Commands to run after node is down `;` separated")
  84. nodeUpdateCmd.Flags().StringVar(&allowedIPs, "allowed_addrs", "", "Additional private addresses given to the node (comma separated)")
  85. nodeUpdateCmd.Flags().IntVar(&keepAlive, "keep_alive", 0, "Interval in which packets are sent to keep connections open with peers")
  86. nodeUpdateCmd.Flags().StringVar(&relayAddrs, "relay_addrs", "", "Addresses for relaying connections if node acts as a relay")
  87. nodeUpdateCmd.Flags().StringVar(&egressGatewayRanges, "egress_addrs", "", "Addresses for egressing traffic if node acts as an egress")
  88. nodeUpdateCmd.Flags().StringVar(&localRange, "local_range", "", "Local range in which the node will look for private addresses to use as an endpoint if `LocalNetwork` is enabled")
  89. nodeUpdateCmd.Flags().IntVar(&mtu, "mtu", 0, "MTU size")
  90. nodeUpdateCmd.Flags().IntVar(&expirationDateTime, "expiry", 0, "UNIX timestamp after which node will lose access to the network")
  91. nodeUpdateCmd.Flags().BoolVar(&defaultACL, "acl", false, "Enable default ACL ?")
  92. nodeUpdateCmd.Flags().BoolVar(&dnsOn, "dns", false, "Setup DNS entries for peers locally ?")
  93. nodeUpdateCmd.Flags().BoolVar(&disconnect, "disconnect", false, "Disconnect from the network ?")
  94. nodeUpdateCmd.Flags().BoolVar(&networkHub, "hub", false, "On a point to site network, this node is the only one which all peers connect to ?")
  95. rootCmd.AddCommand(nodeUpdateCmd)
  96. }