update.go 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. package network
  2. import (
  3. "encoding/json"
  4. "log"
  5. "os"
  6. "github.com/gravitl/netmaker/cli/functions"
  7. "github.com/gravitl/netmaker/models"
  8. "github.com/spf13/cobra"
  9. )
  10. var networkUpdateCmd = &cobra.Command{
  11. Use: "update [NETWORK NAME]",
  12. Short: "Update a Network",
  13. Long: `Update a Network`,
  14. Args: cobra.ExactArgs(1),
  15. Run: func(cmd *cobra.Command, args []string) {
  16. var (
  17. networkName = args[0]
  18. network = &models.Network{}
  19. )
  20. if networkDefinitionFilePath != "" {
  21. content, err := os.ReadFile(networkDefinitionFilePath)
  22. if err != nil {
  23. log.Fatal("Error when opening file: ", err)
  24. }
  25. if err := json.Unmarshal(content, network); err != nil {
  26. log.Fatal(err)
  27. }
  28. } else {
  29. network.NetID = networkName
  30. network.AddressRange = address
  31. if address6 != "" {
  32. network.AddressRange6 = address6
  33. network.IsIPv6 = "yes"
  34. }
  35. if udpHolePunch {
  36. network.DefaultUDPHolePunch = "yes"
  37. }
  38. if localNetwork {
  39. network.IsLocal = "yes"
  40. }
  41. if defaultACL {
  42. network.DefaultACL = "yes"
  43. }
  44. network.DefaultInterface = defaultInterface
  45. network.DefaultListenPort = int32(defaultListenPort)
  46. network.NodeLimit = int32(nodeLimit)
  47. network.DefaultPostUp = defaultPostUp
  48. network.DefaultPostDown = defaultPostDown
  49. network.DefaultKeepalive = int32(defaultKeepalive)
  50. if allowManualSignUp {
  51. network.AllowManualSignUp = "yes"
  52. }
  53. network.DefaultExtClientDNS = defaultExtClientDNS
  54. network.DefaultMTU = int32(defaultMTU)
  55. }
  56. functions.PrettyPrint(functions.UpdateNetwork(networkName, network))
  57. },
  58. }
  59. func init() {
  60. networkUpdateCmd.Flags().StringVar(&networkDefinitionFilePath, "file", "", "Path to network_definition.json")
  61. networkUpdateCmd.Flags().StringVar(&address, "ipv4_addr", "", "IPv4 address of the network")
  62. networkUpdateCmd.Flags().StringVar(&address6, "ipv6_addr", "", "IPv6 address of the network")
  63. networkUpdateCmd.Flags().BoolVar(&udpHolePunch, "udp_hole_punch", false, "Enable UDP Hole Punching ?")
  64. networkUpdateCmd.Flags().BoolVar(&localNetwork, "local", false, "Is the network local (LAN) ?")
  65. networkUpdateCmd.Flags().BoolVar(&defaultACL, "default_acl", false, "Enable default Access Control List ?")
  66. networkUpdateCmd.Flags().StringVar(&defaultInterface, "interface", "", "Name of the network interface")
  67. networkUpdateCmd.Flags().StringVar(&defaultPostUp, "post_up", "", "Commands to run after server is up `;` separated")
  68. networkUpdateCmd.Flags().StringVar(&defaultPostDown, "post_down", "", "Commands to run after server is down `;` separated")
  69. networkUpdateCmd.Flags().StringVar(&defaultExtClientDNS, "ext_client_dns", "", "IPv4 address of DNS server to be used by external clients")
  70. networkUpdateCmd.Flags().IntVar(&defaultListenPort, "listen_port", 0, "Default wireguard port each node will attempt to use")
  71. networkUpdateCmd.Flags().IntVar(&nodeLimit, "node_limit", 0, "Maximum number of nodes that can be associated with this network")
  72. networkUpdateCmd.Flags().IntVar(&defaultKeepalive, "keep_alive", 0, "Keep Alive in seconds")
  73. networkUpdateCmd.Flags().IntVar(&defaultMTU, "mtu", 0, "MTU size")
  74. networkUpdateCmd.Flags().BoolVar(&allowManualSignUp, "manual_signup", false, "Allow manual signup ?")
  75. rootCmd.AddCommand(networkUpdateCmd)
  76. }