diff --git a/netclient/cli_options/cmds.go b/netclient/cli_options/cmds.go index 9e6a0624..f0e542a0 100644 --- a/netclient/cli_options/cmds.go +++ b/netclient/cli_options/cmds.go @@ -104,6 +104,32 @@ func GetCommands(cliFlags []cli.Flag) []*cli.Command { return command.Install() }, }, + { + Name: "connect", + Usage: "connect netclient to a given network if disconnected", + Flags: cliFlags, + Action: func(c *cli.Context) error { + parseVerbosity(c) + cfg, _, err := config.GetCLIConfig(c) + if err != nil { + return err + } + return command.Connect(cfg) + }, + }, + { + Name: "disconnect", + Usage: "disconnect netclient from a given network if connected", + Flags: cliFlags, + Action: func(c *cli.Context) error { + parseVerbosity(c) + cfg, _, err := config.GetCLIConfig(c) + if err != nil { + return err + } + return command.Disconnect(cfg) + }, + }, } } diff --git a/netclient/command/commands.go b/netclient/command/commands.go index b8246036..75569556 100644 --- a/netclient/command/commands.go +++ b/netclient/command/commands.go @@ -142,3 +142,13 @@ func Daemon() error { func Install() error { return functions.Install() } + +// Connect - re-instates a connection of a node +func Connect(cfg config.ClientConfig) error { + return nil +} + +// Disconnect - disconnects a connection of a node +func Disconnect(cfg config.ClientConfig) error { + return nil +} diff --git a/netclient/functions/connection.go b/netclient/functions/connection.go new file mode 100644 index 00000000..b727b0ef --- /dev/null +++ b/netclient/functions/connection.go @@ -0,0 +1,54 @@ +package functions + +import ( + "fmt" + + "github.com/gravitl/netmaker/logger" + "github.com/gravitl/netmaker/netclient/config" + "github.com/gravitl/netmaker/netclient/ncutils" + "github.com/gravitl/netmaker/netclient/wireguard" +) + +// Connect - will attempt to connect a node on given network +func Connect(network string) error { + cfg, err := config.ReadConfig(network) + if err != nil { + return err + } + if cfg.Node.Connected == "yes" { + return fmt.Errorf("node already connected") + } + cfg.Node.Connected = "yes" + filePath := ncutils.GetNetclientPathSpecific() + cfg.Node.Interface + ".conf" + + if err = wireguard.ApplyConf(&cfg.Node, cfg.Node.Interface, filePath); err != nil { + return err + } + if err := PublishNodeUpdate(cfg); err != nil { + logger.Log(0, "network:", cfg.Node.Network, "could not publish connection change, it will likely get reverted") + } + + return config.ModNodeConfig(&cfg.Node) +} + +// Disconnect - attempts to disconnect a node on given network +func Disconnect(network string) error { + cfg, err := config.ReadConfig(network) + if err != nil { + return err + } + if cfg.Node.Connected == "no" { + return fmt.Errorf("node already disconnected") + } + cfg.Node.Connected = "no" + filePath := ncutils.GetNetclientPathSpecific() + cfg.Node.Interface + ".conf" + + if err = wireguard.ApplyConf(&cfg.Node, cfg.Node.Interface, filePath); err != nil { + return err + } + if err := PublishNodeUpdate(cfg); err != nil { + logger.Log(0, "network:", cfg.Node.Network, "could not publish connection change, it will likely get reverted") + } + + return config.ModNodeConfig(&cfg.Node) +}