2021-05-26 00:48:04 +08:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
2021-08-03 06:06:26 +08:00
|
|
|
"log"
|
2021-05-26 00:48:04 +08:00
|
|
|
"os"
|
2021-10-07 22:57:06 +08:00
|
|
|
"strconv"
|
2021-05-26 00:48:04 +08:00
|
|
|
"strings"
|
2021-08-31 03:58:23 +08:00
|
|
|
"time"
|
2021-10-07 22:57:06 +08:00
|
|
|
|
2021-08-03 06:06:26 +08:00
|
|
|
nodepb "github.com/gravitl/netmaker/grpc"
|
|
|
|
"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-08-03 06:06:26 +08:00
|
|
|
"golang.zx2c4.com/wireguard/wgctrl"
|
2021-05-26 00:48:04 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2021-08-03 06:06:26 +08:00
|
|
|
wgclient *wgctrl.Client
|
2021-05-26 00:48:04 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2021-08-03 06:06:26 +08:00
|
|
|
wcclient nodepb.NodeServiceClient
|
2021-05-26 00:48:04 +08:00
|
|
|
)
|
|
|
|
|
2021-08-03 06:06:26 +08:00
|
|
|
func Join(cfg config.ClientConfig, privateKey string) error {
|
2021-05-26 00:48:04 +08:00
|
|
|
|
2021-10-13 03:44:19 +08:00
|
|
|
var err error
|
|
|
|
err = functions.JoinNetwork(cfg, privateKey)
|
|
|
|
if err != nil && cfg.Node.IsServer != "yes" { // make sure server side is cleaned up
|
|
|
|
return err
|
|
|
|
}
|
2021-09-18 01:28:32 +08:00
|
|
|
if err != nil && !cfg.DebugJoin {
|
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
|
|
|
}
|
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-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)
|
2021-05-26 00:48:04 +08:00
|
|
|
if cfg.Daemon != "off" {
|
2021-09-20 02:03:47 +08:00
|
|
|
err = daemon.InstallDaemon(cfg)
|
2021-05-26 00:48:04 +08:00
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-10-03 00:28:17 +08:00
|
|
|
func getWindowsInterval() int {
|
|
|
|
interval := 15
|
2021-10-06 03:02:09 +08:00
|
|
|
networks, err := ncutils.GetSystemNetworks()
|
2021-10-03 00:28:17 +08:00
|
|
|
if err != nil {
|
|
|
|
return interval
|
|
|
|
}
|
|
|
|
cfg, err := config.ReadConfig(networks[0])
|
|
|
|
if err != nil {
|
|
|
|
return interval
|
|
|
|
}
|
|
|
|
netint, err := strconv.Atoi(cfg.Server.CheckinInterval)
|
2021-10-07 22:57:06 +08:00
|
|
|
if err == nil && netint != 0 {
|
2021-10-03 00:28:17 +08:00
|
|
|
interval = netint
|
|
|
|
}
|
|
|
|
return interval
|
|
|
|
}
|
|
|
|
|
2021-08-31 03:58:23 +08:00
|
|
|
func RunUserspaceDaemon() {
|
2021-10-07 22:57:06 +08:00
|
|
|
|
2021-08-31 03:58:23 +08:00
|
|
|
cfg := config.ClientConfig{
|
|
|
|
Network: "all",
|
|
|
|
}
|
2021-10-03 00:28:17 +08:00
|
|
|
interval := getWindowsInterval()
|
|
|
|
dur := time.Duration(interval) * time.Second
|
2021-08-31 03:58:23 +08:00
|
|
|
for {
|
|
|
|
if err := CheckIn(cfg); err != nil {
|
|
|
|
// pass
|
|
|
|
}
|
2021-10-03 00:28:17 +08:00
|
|
|
time.Sleep(dur)
|
2021-08-31 03:58:23 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-26 00:48:04 +08:00
|
|
|
func CheckIn(cfg config.ClientConfig) error {
|
2021-08-31 03:58:23 +08:00
|
|
|
var err error
|
2021-10-06 03:02:09 +08:00
|
|
|
|
2021-08-31 03:58:23 +08:00
|
|
|
if cfg.Network == "" {
|
2021-09-20 02:03:47 +08:00
|
|
|
ncutils.PrintLog("required, '-n', exiting", 0)
|
2021-06-01 08:36:08 +08:00
|
|
|
os.Exit(1)
|
2021-08-31 03:58:23 +08:00
|
|
|
} else if cfg.Network == "all" {
|
2021-09-20 02:03:47 +08:00
|
|
|
ncutils.PrintLog("running checkin for all networks", 1)
|
2021-10-06 03:02:09 +08:00
|
|
|
networks, err := ncutils.GetSystemNetworks()
|
2021-08-31 03:58:23 +08:00
|
|
|
if err != nil {
|
2021-09-20 02:03:47 +08:00
|
|
|
ncutils.PrintLog("error retrieving networks, exiting", 1)
|
2021-08-31 03:58:23 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, network := range networks {
|
|
|
|
currConf, err := config.ReadConfig(network)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
err = functions.CheckConfig(*currConf)
|
|
|
|
if err != nil {
|
2021-09-20 02:03:47 +08:00
|
|
|
ncutils.PrintLog("error checking in for "+network+" network: "+err.Error(), 1)
|
2021-08-31 03:58:23 +08:00
|
|
|
} else {
|
2021-10-07 23:17:40 +08:00
|
|
|
ncutils.PrintLog("checked in successfully for "+network, 1)
|
2021-08-31 03:58:23 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(networks) == 0 {
|
2021-09-20 02:03:47 +08:00
|
|
|
if ncutils.IsWindows() { // Windows specific - there are no netclients, so stop daemon process
|
|
|
|
daemon.StopWindowsDaemon()
|
2021-08-31 03:58:23 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
err = nil
|
|
|
|
} else {
|
|
|
|
err = functions.CheckConfig(cfg)
|
2021-06-01 08:36:08 +08:00
|
|
|
}
|
2021-08-03 06:06:26 +08:00
|
|
|
return err
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func Push(cfg config.ClientConfig) error {
|
2021-08-03 06:06:26 +08:00
|
|
|
var err error
|
2021-09-20 02:03:47 +08:00
|
|
|
if cfg.Network == "all" || ncutils.IsWindows() {
|
|
|
|
ncutils.PrintLog("pushing config to server for all networks.", 0)
|
2021-10-06 03:02:09 +08:00
|
|
|
networks, err := ncutils.GetSystemNetworks()
|
2021-08-03 06:06:26 +08:00
|
|
|
if err != nil {
|
2021-09-20 02:03:47 +08:00
|
|
|
ncutils.PrintLog("error retrieving networks, exiting.", 0)
|
2021-08-03 06:06:26 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, network := range networks {
|
|
|
|
err = functions.Push(network)
|
|
|
|
if err != nil {
|
2021-09-20 02:03:47 +08:00
|
|
|
log.Printf("error pushing network configs for "+network+" network: ", err)
|
2021-08-03 06:06:26 +08:00
|
|
|
} else {
|
2021-09-20 02:03:47 +08:00
|
|
|
ncutils.PrintLog("pushed network config for "+network, 1)
|
2021-08-03 06:06:26 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
err = nil
|
|
|
|
} else {
|
|
|
|
err = functions.Push(cfg.Network)
|
|
|
|
}
|
2021-09-20 02:03:47 +08:00
|
|
|
ncutils.PrintLog("completed pushing network configs to remote server", 1)
|
|
|
|
ncutils.PrintLog("success", 1)
|
2021-08-03 06:06:26 +08:00
|
|
|
return err
|
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 {
|
2021-08-03 06:06:26 +08:00
|
|
|
log.Printf("Error pulling network config for "+network+" network: ", err)
|
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)
|
|
|
|
ncutils.PrintLog("success", 1)
|
2021-08-03 06:06:26 +08:00
|
|
|
return err
|
2021-05-26 00:48:04 +08:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-08-10 05:42:32 +08:00
|
|
|
func Uninstall() error {
|
2021-09-20 02:03:47 +08:00
|
|
|
ncutils.PrintLog("uninstalling netclient", 0)
|
2021-05-30 01:22:18 +08:00
|
|
|
err := functions.Uninstall()
|
2021-08-03 06:06:26 +08:00
|
|
|
return err
|
2021-05-30 03:12:15 +08:00
|
|
|
}
|