2021-05-26 00:48:04 +08:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
2021-10-07 22:57:06 +08:00
|
|
|
|
2022-03-20 23:12:05 +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
|
|
|
)
|
|
|
|
|
2022-02-19 23:45:25 +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 {
|
2022-03-21 12:43:17 +08:00
|
|
|
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
|
|
|
}
|
2022-03-21 12:43:17 +08:00
|
|
|
commsCfg.ReadConfig()
|
|
|
|
|
|
|
|
if len(commsCfg.Node.Name) == 0 {
|
2022-02-19 23:45:25 +08:00
|
|
|
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
|
2022-03-21 12:43:17 +08:00
|
|
|
if err := functions.PingServer(commsCfg); err != nil {
|
2022-02-19 23:45:25 +08:00
|
|
|
if err = Pull(commsCfg); err != nil {
|
2022-02-19 09:21:03 +08:00
|
|
|
return err
|
|
|
|
}
|
2022-02-18 23:34:03 +08:00
|
|
|
}
|
|
|
|
}
|
2022-02-19 23:45:25 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Join - join command to run from cli
|
2022-03-21 12:43:17 +08:00
|
|
|
func Join(cfg *config.ClientConfig, privateKey string) error {
|
2022-02-19 23:45:25 +08:00
|
|
|
var err error
|
|
|
|
//check if comms network exists
|
2022-03-21 12:43:17 +08:00
|
|
|
if err = JoinComms(cfg); err != nil {
|
2022-02-19 23:45:25 +08:00
|
|
|
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)
|
2022-01-21 18:19:17 +08:00
|
|
|
if err != nil && !cfg.DebugOn {
|
2021-06-02 00:23:05 +08:00
|
|
|
if !strings.Contains(err.Error(), "ALREADY_INSTALLED") {
|
2022-03-20 23:12:05 +08:00
|
|
|
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 {
|
2022-03-20 23:12:05 +08:00
|
|
|
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 {
|
2022-03-20 23:12:05 +08:00
|
|
|
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 {
|
2022-03-20 23:12:05 +08:00
|
|
|
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") {
|
2022-03-20 23:12:05 +08:00
|
|
|
logger.Log(0, err.Error())
|
2021-11-29 17:34:11 +08:00
|
|
|
err = nil
|
|
|
|
}
|
2021-06-02 00:23:05 +08:00
|
|
|
return err
|
2021-05-26 00:48:04 +08:00
|
|
|
}
|
2022-03-20 23:12:05 +08:00
|
|
|
logger.Log(1, "joined ", cfg.Network)
|
2022-02-16 09:51:00 +08:00
|
|
|
/*
|
|
|
|
if ncutils.IsWindows() {
|
2022-03-20 23:12:05 +08:00
|
|
|
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
|
2022-03-21 12:43:17 +08:00
|
|
|
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 {
|
2022-03-20 23:12:05 +08:00
|
|
|
logger.Log(1, "error attempting to leave network "+cfg.Network)
|
2021-09-20 02:03:47 +08:00
|
|
|
} else {
|
2022-03-20 23:12:05 +08:00
|
|
|
logger.Log(0, "success")
|
2021-08-03 06:06:26 +08:00
|
|
|
}
|
2022-02-21 21:47:47 +08:00
|
|
|
nets, err := ncutils.GetSystemNetworks()
|
|
|
|
if err == nil && len(nets) == 1 {
|
|
|
|
if nets[0] == cfg.Node.CommID {
|
2022-03-20 23:12:05 +08:00
|
|
|
logger.Log(1, "detected comms as remaining network, removing...")
|
2022-02-21 21:47:47 +08:00
|
|
|
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
|
2022-03-21 12:43:17 +08:00
|
|
|
func Pull(cfg *config.ClientConfig) error {
|
2021-08-03 06:06:26 +08:00
|
|
|
var err error
|
2021-06-01 08:36:08 +08:00
|
|
|
if cfg.Network == "all" {
|
2022-03-20 23:12:05 +08:00
|
|
|
logger.Log(0, "No network selected. Running Pull for all networks.")
|
2021-10-06 03:02:09 +08:00
|
|
|
networks, err := ncutils.GetSystemNetworks()
|
2021-06-01 08:36:08 +08:00
|
|
|
if err != nil {
|
2022-03-20 23:12:05 +08:00
|
|
|
logger.Log(1, "Error retrieving networks. Exiting.")
|
2021-06-01 08:36:08 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, network := range networks {
|
2021-08-03 06:06:26 +08:00
|
|
|
_, err = functions.Pull(network, true)
|
2021-06-01 08:36:08 +08:00
|
|
|
if err != nil {
|
2022-03-20 23:12:05 +08:00
|
|
|
logger.Log(1, "Error pulling network config for network: ", network, "\n", err.Error())
|
2021-06-01 08:36:08 +08:00
|
|
|
} else {
|
2022-03-20 23:12:05 +08:00
|
|
|
logger.Log(1, "pulled network config for "+network)
|
2021-06-01 08:36:08 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
err = nil
|
|
|
|
} else {
|
2021-08-03 06:06:26 +08:00
|
|
|
_, err = functions.Pull(cfg.Network, true)
|
2021-06-01 08:36:08 +08:00
|
|
|
}
|
2022-03-20 23:12:05 +08:00
|
|
|
logger.Log(1, "reset network and peer configs")
|
2022-02-02 10:50:11 +08:00
|
|
|
if err == nil {
|
2022-03-20 23:12:05 +08:00
|
|
|
logger.Log(1, "reset network and peer configs")
|
|
|
|
logger.Log(1, "success")
|
2022-02-02 10:50:11 +08:00
|
|
|
} else {
|
2022-03-20 23:12:05 +08:00
|
|
|
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 {
|
2022-03-20 23:12:05 +08:00
|
|
|
logger.Log(0, "uninstalling netclient...")
|
2021-05-30 01:22:18 +08:00
|
|
|
err := functions.Uninstall()
|
2022-03-20 23:12:05 +08:00
|
|
|
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
|
|
|
|
2022-02-19 23:45:25 +08:00
|
|
|
// Daemon - runs the daemon
|
2022-01-03 00:02:59 +08:00
|
|
|
func Daemon() error {
|
|
|
|
err := functions.Daemon()
|
|
|
|
return err
|
|
|
|
}
|