update.go 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. package node
  2. import (
  3. "encoding/json"
  4. "log"
  5. "net"
  6. "os"
  7. "strings"
  8. "time"
  9. "github.com/gravitl/netmaker/cli/functions"
  10. "github.com/gravitl/netmaker/models"
  11. "github.com/spf13/cobra"
  12. )
  13. var nodeUpdateCmd = &cobra.Command{
  14. Use: "update [NETWORK NAME] [NODE ID]",
  15. Args: cobra.ExactArgs(2),
  16. Short: "Update a Node",
  17. Long: `Update a Node`,
  18. Run: func(cmd *cobra.Command, args []string) {
  19. var (
  20. node = &models.Node{}
  21. networkName = args[0]
  22. nodeID = args[1]
  23. )
  24. if nodeDefinitionFilePath != "" {
  25. content, err := os.ReadFile(nodeDefinitionFilePath)
  26. if err != nil {
  27. log.Fatal("Error when opening file: ", err)
  28. }
  29. if err := json.Unmarshal(content, node); err != nil {
  30. log.Fatal(err)
  31. }
  32. } else {
  33. if address != "" {
  34. if _, addr, err := net.ParseCIDR(address); err != nil {
  35. log.Fatal(err)
  36. } else {
  37. node.Address = *addr
  38. }
  39. }
  40. if address6 != "" {
  41. if _, addr6, err := net.ParseCIDR(address6); err != nil {
  42. log.Fatal(err)
  43. } else {
  44. node.Address6 = *addr6
  45. }
  46. }
  47. if localAddress != "" {
  48. if _, localAddr, err := net.ParseCIDR(localAddress); err != nil {
  49. log.Fatal(err)
  50. } else {
  51. node.LocalAddress = *localAddr
  52. node.IsLocal = true
  53. }
  54. }
  55. node.PostUp = postUp
  56. node.PostDown = postDown
  57. node.PersistentKeepalive = time.Duration(time.Second * time.Duration(keepAlive))
  58. if relayAddrs != "" {
  59. node.RelayAddrs = strings.Split(relayAddrs, ",")
  60. }
  61. if egressGatewayRanges != "" {
  62. node.EgressGatewayRanges = strings.Split(egressGatewayRanges, ",")
  63. }
  64. node.ExpirationDateTime = time.Unix(int64(expirationDateTime), 0)
  65. if defaultACL {
  66. node.DefaultACL = "yes"
  67. }
  68. node.DNSOn = dnsOn
  69. node.Connected = !disconnect
  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(&address, "ipv4_addr", "", "IPv4 address of the node")
  77. nodeUpdateCmd.Flags().StringVar(&address6, "ipv6_addr", "", "IPv6 address of the node")
  78. nodeUpdateCmd.Flags().StringVar(&localAddress, "local_addr", "", "Locally reachable address of the node")
  79. nodeUpdateCmd.Flags().StringVar(&name, "name", "", "Node name")
  80. nodeUpdateCmd.Flags().StringVar(&postUp, "post_up", "", "Commands to run after node is up `;` separated")
  81. nodeUpdateCmd.Flags().StringVar(&postDown, "post_down", "", "Commands to run after node is down `;` separated")
  82. nodeUpdateCmd.Flags().IntVar(&keepAlive, "keep_alive", 0, "Interval in which packets are sent to keep connections open with peers")
  83. nodeUpdateCmd.Flags().StringVar(&relayAddrs, "relay_addrs", "", "Addresses for relaying connections if node acts as a relay")
  84. nodeUpdateCmd.Flags().StringVar(&egressGatewayRanges, "egress_addrs", "", "Addresses for egressing traffic if node acts as an egress")
  85. nodeUpdateCmd.Flags().IntVar(&expirationDateTime, "expiry", 0, "UNIX timestamp after which node will lose access to the network")
  86. nodeUpdateCmd.Flags().BoolVar(&defaultACL, "acl", false, "Enable default ACL ?")
  87. nodeUpdateCmd.Flags().BoolVar(&dnsOn, "dns", false, "Setup DNS entries for peers locally ?")
  88. nodeUpdateCmd.Flags().BoolVar(&disconnect, "disconnect", false, "Disconnect from the network ?")
  89. rootCmd.AddCommand(nodeUpdateCmd)
  90. }