netmaker/netclient/command/commands.go

162 lines
4.2 KiB
Go
Raw Normal View History

2021-05-26 00:48:04 +08:00
package command
import (
"strings"
2021-10-07 22:57:06 +08:00
"github.com/gravitl/netmaker/logger"
2021-08-03 06:06:26 +08:00
"github.com/gravitl/netmaker/netclient/config"
2021-09-20 02:03:47 +08:00
"github.com/gravitl/netmaker/netclient/daemon"
2021-08-03 06:06:26 +08:00
"github.com/gravitl/netmaker/netclient/functions"
2021-09-20 02:03:47 +08:00
"github.com/gravitl/netmaker/netclient/ncutils"
2021-05-26 00:48:04 +08:00
)
// JoinComms -- Join the message queue comms network if it doesn't have it
// tries to ping if already found locally, if fail ping pull for best effort for communication
func JoinComms(cfg *config.ClientConfig) error {
commsCfg := &config.ClientConfig{}
2022-02-19 05:27:54 +08:00
commsCfg.Network = cfg.Server.CommsNetwork
2022-02-19 09:21:03 +08:00
commsCfg.Node.Network = cfg.Server.CommsNetwork
commsCfg.Server.AccessKey = cfg.Server.AccessKey
commsCfg.Server.GRPCAddress = cfg.Server.GRPCAddress
commsCfg.Server.GRPCSSL = cfg.Server.GRPCSSL
commsCfg.Server.CoreDNSAddr = cfg.Server.CoreDNSAddr
2022-03-22 00:13:27 +08:00
if commsCfg.ConfigFileExists() {
return nil
2022-02-21 06:47:03 +08:00
}
commsCfg.ReadConfig()
if len(commsCfg.Node.Name) == 0 {
if err := functions.JoinNetwork(commsCfg, "", true); err != nil {
2022-02-18 23:34:03 +08:00
return err
}
2022-02-19 09:21:03 +08:00
} else { // check if comms is currently reachable
if err := functions.PingServer(commsCfg); err != nil {
if err = Pull(commsCfg); err != nil {
2022-02-19 09:21:03 +08:00
return err
}
2022-02-18 23:34:03 +08:00
}
}
return nil
}
// Join - join command to run from cli
func Join(cfg *config.ClientConfig, privateKey string) error {
var err error
//check if comms network exists
if err = JoinComms(cfg); err != nil {
return err
}
2022-02-19 09:21:03 +08:00
2022-02-18 23:34:03 +08:00
//join network
2022-02-19 09:21:03 +08:00
err = functions.JoinNetwork(cfg, privateKey, false)
if err != nil && !cfg.DebugOn {
2021-06-02 00:23:05 +08:00
if !strings.Contains(err.Error(), "ALREADY_INSTALLED") {
logger.Log(1, "error installing: ", err.Error())
2022-02-21 07:09:32 +08:00
err = functions.LeaveNetwork(cfg.Network, true)
2021-05-26 00:48:04 +08:00
if err != nil {
2021-09-20 02:03:47 +08:00
err = functions.WipeLocal(cfg.Network)
2021-06-02 00:23:05 +08:00
if err != nil {
logger.Log(1, "error removing artifacts: ", err.Error())
2021-06-02 00:23:05 +08:00
}
}
if cfg.Daemon != "off" {
2021-09-20 02:03:47 +08:00
if ncutils.IsLinux() {
2021-10-18 03:31:37 +08:00
err = daemon.RemoveSystemDServices()
2021-08-31 03:58:23 +08:00
}
2021-08-03 06:06:26 +08:00
if err != nil {
logger.Log(1, "error removing services: ", err.Error())
2021-08-03 06:06:26 +08:00
}
2022-02-09 11:31:17 +08:00
if ncutils.IsFreeBSD() {
daemon.RemoveFreebsdDaemon()
2022-02-09 11:06:41 +08:00
}
2021-05-26 00:48:04 +08:00
}
2021-09-20 02:03:47 +08:00
} else {
logger.Log(0, "success")
2021-05-26 00:48:04 +08:00
}
2021-12-01 10:11:10 +08:00
if err != nil && strings.Contains(err.Error(), "ALREADY_INSTALLED") {
logger.Log(0, err.Error())
err = nil
}
2021-06-02 00:23:05 +08:00
return err
2021-05-26 00:48:04 +08:00
}
logger.Log(1, "joined ", cfg.Network)
2022-02-16 09:51:00 +08:00
/*
if ncutils.IsWindows() {
logger.Log("setting up WireGuard app", 0)
2022-02-16 09:51:00 +08:00
time.Sleep(time.Second >> 1)
functions.Pull(cfg.Network, true)
}
*/
2021-05-26 00:48:04 +08:00
return err
}
2022-01-02 23:30:07 +08:00
// Leave - runs the leave command from cli
func Leave(cfg *config.ClientConfig, force bool) error {
2022-02-21 07:09:32 +08:00
err := functions.LeaveNetwork(cfg.Network, force)
2021-08-03 06:06:26 +08:00
if err != nil {
logger.Log(1, "error attempting to leave network "+cfg.Network)
2021-09-20 02:03:47 +08:00
} else {
logger.Log(0, "success")
2021-08-03 06:06:26 +08:00
}
nets, err := ncutils.GetSystemNetworks()
if err == nil && len(nets) == 1 {
if nets[0] == cfg.Node.CommID {
logger.Log(1, "detected comms as remaining network, removing...")
err = functions.LeaveNetwork(nets[0], true)
}
}
2021-05-26 00:48:04 +08:00
return err
}
2022-01-02 23:30:07 +08:00
// Pull - runs pull command from cli
func Pull(cfg *config.ClientConfig) error {
2021-08-03 06:06:26 +08:00
var err error
if cfg.Network == "all" {
logger.Log(0, "No network selected. Running Pull for all networks.")
networks, err := ncutils.GetSystemNetworks()
if err != nil {
logger.Log(1, "Error retrieving networks. Exiting.")
return err
}
for _, network := range networks {
2021-08-03 06:06:26 +08:00
_, err = functions.Pull(network, true)
if err != nil {
logger.Log(1, "Error pulling network config for network: ", network, "\n", err.Error())
} else {
logger.Log(1, "pulled network config for "+network)
}
}
err = nil
} else {
2021-08-03 06:06:26 +08:00
_, err = functions.Pull(cfg.Network, true)
}
logger.Log(1, "reset network and peer configs")
2022-02-02 10:50:11 +08:00
if err == nil {
logger.Log(1, "reset network and peer configs")
logger.Log(1, "success")
2022-02-02 10:50:11 +08:00
} else {
logger.Log(0, "error occurred pulling configs from server")
2022-02-02 10:50:11 +08:00
}
2021-08-03 06:06:26 +08:00
return err
2021-05-26 00:48:04 +08:00
}
2022-01-02 23:30:07 +08:00
// List - runs list command from cli
2021-05-30 01:22:18 +08:00
func List(cfg config.ClientConfig) error {
2021-10-20 03:53:01 +08:00
err := functions.List(cfg.Network)
2021-05-30 01:22:18 +08:00
return err
}
2022-01-02 23:30:07 +08:00
// Uninstall - runs uninstall command from cli
2021-08-10 05:42:32 +08:00
func Uninstall() error {
logger.Log(0, "uninstalling netclient...")
2021-05-30 01:22:18 +08:00
err := functions.Uninstall()
logger.Log(0, "uninstalled netclient")
2021-08-03 06:06:26 +08:00
return err
2021-05-30 03:12:15 +08:00
}
2022-01-03 00:02:59 +08:00
// Daemon - runs the daemon
2022-01-03 00:02:59 +08:00
func Daemon() error {
err := functions.Daemon()
return err
}