update.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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.ApiNode{}
  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. node.Address = address
  32. node.Address6 = address6
  33. node.LocalAddress = localAddress
  34. node.PersistentKeepalive = int32(keepAlive)
  35. if relayAddrs != "" {
  36. node.RelayAddrs = strings.Split(relayAddrs, ",")
  37. }
  38. if egressGatewayRanges != "" {
  39. node.EgressGatewayRanges = strings.Split(egressGatewayRanges, ",")
  40. }
  41. node.ExpirationDateTime = int64(expirationDateTime)
  42. if defaultACL {
  43. node.DefaultACL = "yes"
  44. }
  45. node.DNSOn = dnsOn
  46. node.Connected = !disconnect
  47. }
  48. node.HostID = functions.GetNodeByID(networkName, nodeID).Host.ID.String()
  49. functions.PrettyPrint(functions.UpdateNode(networkName, nodeID, node))
  50. },
  51. }
  52. func init() {
  53. nodeUpdateCmd.Flags().StringVar(&nodeDefinitionFilePath, "file", "", "Filepath of updated node definition in JSON")
  54. nodeUpdateCmd.Flags().StringVar(&address, "ipv4_addr", "", "IPv4 address of the node")
  55. nodeUpdateCmd.Flags().StringVar(&address6, "ipv6_addr", "", "IPv6 address of the node")
  56. nodeUpdateCmd.Flags().StringVar(&localAddress, "local_addr", "", "Locally reachable address of the node")
  57. nodeUpdateCmd.Flags().StringVar(&name, "name", "", "Node name")
  58. nodeUpdateCmd.Flags().StringVar(&postUp, "post_up", "", "Commands to run after node is up `;` separated")
  59. nodeUpdateCmd.Flags().StringVar(&postDown, "post_down", "", "Commands to run after node is down `;` separated")
  60. nodeUpdateCmd.Flags().IntVar(&keepAlive, "keep_alive", 0, "Interval in which packets are sent to keep connections open with peers")
  61. nodeUpdateCmd.Flags().StringVar(&relayAddrs, "relay_addrs", "", "Addresses for relaying connections if node acts as a relay")
  62. nodeUpdateCmd.Flags().StringVar(&egressGatewayRanges, "egress_addrs", "", "Addresses for egressing traffic if node acts as an egress")
  63. nodeUpdateCmd.Flags().IntVar(&expirationDateTime, "expiry", 0, "UNIX timestamp after which node will lose access to the network")
  64. nodeUpdateCmd.Flags().BoolVar(&defaultACL, "acl", false, "Enable default ACL ?")
  65. nodeUpdateCmd.Flags().BoolVar(&dnsOn, "dns", false, "Setup DNS entries for peers locally ?")
  66. nodeUpdateCmd.Flags().BoolVar(&disconnect, "disconnect", false, "Disconnect from the network ?")
  67. rootCmd.AddCommand(nodeUpdateCmd)
  68. }