netmaker/netclient/command/commands.go

152 lines
4 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
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 {
2022-02-18 23:34:03 +08:00
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-21 06:47:03 +08:00
if commsCfg.ConfigFileExists() {
commsCfg.ReadConfig()
}
2022-02-18 23:34:03 +08:00
if commsCfg.Node.Name == "" {
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") {
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") {
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
if cfg.Network == "all" {
2021-09-20 02:03:47 +08:00
ncutils.PrintLog("No network selected. Running Pull for all networks.", 0)
networks, err := ncutils.GetSystemNetworks()
if err != nil {
2021-09-20 02:03:47 +08:00
ncutils.PrintLog("Error retrieving networks. Exiting.", 1)
return err
}
for _, network := range networks {
2021-08-03 06:06:26 +08:00
_, err = functions.Pull(network, true)
if err != nil {
2022-02-04 05:34:34 +08:00
ncutils.PrintLog("Error pulling network config for network: "+network+"\n"+err.Error(), 1)
} else {
2021-09-20 02:03:47 +08:00
ncutils.PrintLog("pulled network config for "+network, 1)
}
}
err = nil
} else {
2021-08-03 06:06:26 +08:00
_, err = functions.Pull(cfg.Network, true)
}
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
// Daemon - runs the daemon
2022-01-03 00:02:59 +08:00
func Daemon() error {
err := functions.Daemon()
return err
}