2021-05-26 00:48:04 +08:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
2021-10-07 22:57:06 +08:00
|
|
|
|
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 00:34:18 +08:00
|
|
|
// JoinCommsNetwork -- Join the message queue comms network
|
2022-02-18 23:34:03 +08:00
|
|
|
func JoinCommsNetwork(cfg config.ClientConfig) error {
|
2022-02-19 09:21:03 +08:00
|
|
|
if err := functions.JoinNetwork(cfg, "", true); err != nil {
|
2022-02-18 23:34:03 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-01-02 23:30:07 +08:00
|
|
|
// Join - join command to run from cli
|
2021-08-03 06:06:26 +08:00
|
|
|
func Join(cfg config.ClientConfig, privateKey string) error {
|
2021-10-13 03:44:19 +08:00
|
|
|
var err error
|
2022-02-18 23:34:03 +08:00
|
|
|
//check if comms network exists
|
|
|
|
var 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-02-18 23:34:03 +08:00
|
|
|
commsCfg.ReadConfig()
|
|
|
|
if commsCfg.Node.Name == "" {
|
|
|
|
if err := JoinCommsNetwork(commsCfg); err != nil {
|
|
|
|
ncutils.Log("could not join comms network " + err.Error())
|
|
|
|
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 := functions.LeaveNetwork(commsCfg.Network); err != nil {
|
|
|
|
ncutils.Log("could not leave comms network " + err.Error())
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := JoinCommsNetwork(commsCfg); err != nil {
|
|
|
|
ncutils.Log("could not join comms network " + err.Error())
|
|
|
|
return err
|
|
|
|
}
|
2022-02-18 23:34:03 +08:00
|
|
|
}
|
|
|
|
}
|
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") {
|
2021-09-20 02:03:47 +08:00
|
|
|
ncutils.PrintLog("error installing: "+err.Error(), 1)
|
2021-06-02 00:23:05 +08:00
|
|
|
err = functions.LeaveNetwork(cfg.Network)
|
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 {
|
2021-09-20 02:03:47 +08:00
|
|
|
ncutils.PrintLog("error removing artifacts: "+err.Error(), 1)
|
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 {
|
2021-09-20 02:03:47 +08:00
|
|
|
ncutils.PrintLog("error removing services: "+err.Error(), 1)
|
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 {
|
|
|
|
ncutils.PrintLog("success", 0)
|
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") {
|
2021-11-29 17:34:11 +08:00
|
|
|
ncutils.PrintLog(err.Error(), 0)
|
|
|
|
err = nil
|
|
|
|
}
|
2021-06-02 00:23:05 +08:00
|
|
|
return err
|
2021-05-26 00:48:04 +08:00
|
|
|
}
|
2021-09-20 02:03:47 +08:00
|
|
|
ncutils.PrintLog("joined "+cfg.Network, 1)
|
2022-02-16 09:51:00 +08:00
|
|
|
/*
|
|
|
|
if ncutils.IsWindows() {
|
|
|
|
ncutils.PrintLog("setting up WireGuard app", 0)
|
|
|
|
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
|
2021-05-26 00:48:04 +08:00
|
|
|
func Leave(cfg config.ClientConfig) error {
|
|
|
|
err := functions.LeaveNetwork(cfg.Network)
|
2021-08-03 06:06:26 +08:00
|
|
|
if err != nil {
|
2021-09-20 02:03:47 +08:00
|
|
|
ncutils.PrintLog("error attempting to leave network "+cfg.Network, 1)
|
|
|
|
} else {
|
|
|
|
ncutils.PrintLog("success", 0)
|
2021-08-03 06:06:26 +08:00
|
|
|
}
|
2021-05-26 00:48:04 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-01-02 23:30:07 +08:00
|
|
|
// Pull - runs pull command from cli
|
2021-05-26 00:48:04 +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" {
|
2021-09-20 02:03:47 +08:00
|
|
|
ncutils.PrintLog("No network selected. Running Pull for all networks.", 0)
|
2021-10-06 03:02:09 +08:00
|
|
|
networks, err := ncutils.GetSystemNetworks()
|
2021-06-01 08:36:08 +08:00
|
|
|
if err != nil {
|
2021-09-20 02:03:47 +08:00
|
|
|
ncutils.PrintLog("Error retrieving networks. Exiting.", 1)
|
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-02-04 05:34:34 +08:00
|
|
|
ncutils.PrintLog("Error pulling network config for network: "+network+"\n"+err.Error(), 1)
|
2021-06-01 08:36:08 +08:00
|
|
|
} else {
|
2021-09-20 02:03:47 +08:00
|
|
|
ncutils.PrintLog("pulled network config for "+network, 1)
|
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
|
|
|
}
|
2021-09-20 02:03:47 +08:00
|
|
|
ncutils.PrintLog("reset network and peer configs", 1)
|
2022-02-02 10:50:11 +08:00
|
|
|
if err == nil {
|
|
|
|
ncutils.PrintLog("reset network and peer configs", 1)
|
|
|
|
ncutils.PrintLog("success", 1)
|
|
|
|
} else {
|
|
|
|
ncutils.PrintLog("error occurred pulling configs from server", 1)
|
|
|
|
}
|
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-01-05 02:13:12 +08:00
|
|
|
ncutils.PrintLog("uninstalling netclient...", 0)
|
2021-05-30 01:22:18 +08:00
|
|
|
err := functions.Uninstall()
|
2022-01-05 02:13:12 +08:00
|
|
|
ncutils.PrintLog("uninstalled netclient", 0)
|
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
|
|
|
|
|
|
|
func Daemon() error {
|
|
|
|
err := functions.Daemon()
|
|
|
|
return err
|
|
|
|
}
|