netmaker/netclient/command/commands.go

134 lines
3.1 KiB
Go
Raw Normal View History

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"
"strings"
2021-08-03 06:06:26 +08:00
nodepb "github.com/gravitl/netmaker/grpc"
"github.com/gravitl/netmaker/netclient/config"
"github.com/gravitl/netmaker/netclient/functions"
"github.com/gravitl/netmaker/netclient/local"
"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-08-03 06:06:26 +08:00
err := functions.JoinNetwork(cfg, privateKey)
2021-05-26 00:48:04 +08:00
if err != nil {
2021-06-02 00:23:05 +08:00
if !strings.Contains(err.Error(), "ALREADY_INSTALLED") {
log.Println("Error installing: ", err)
err = functions.LeaveNetwork(cfg.Network)
2021-05-26 00:48:04 +08:00
if err != nil {
2021-06-02 00:23:05 +08:00
err = local.WipeLocal(cfg.Network)
if err != nil {
log.Println("Error removing artifacts: ", err)
}
}
if cfg.Daemon != "off" {
2021-08-03 06:06:26 +08:00
err = local.RemoveSystemDServices(cfg.Network)
if err != nil {
log.Println("Error removing services: ", err)
}
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-08-03 06:06:26 +08:00
log.Println("joined " + cfg.Network)
2021-05-26 00:48:04 +08:00
if cfg.Daemon != "off" {
2021-06-02 00:23:05 +08:00
err = functions.InstallDaemon(cfg)
2021-05-26 00:48:04 +08:00
}
return err
}
func CheckIn(cfg config.ClientConfig) error {
2021-08-03 06:06:26 +08:00
if cfg.Network == "all" || cfg.Network == "" {
log.Println("Required, '-n'. No network provided. Exiting.")
os.Exit(1)
}
2021-08-03 06:06:26 +08:00
err := functions.CheckConfig(cfg)
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-05-26 00:48:04 +08:00
log.Println("Error attempting to leave network " + cfg.Network)
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
if cfg.Network == "all" {
log.Println("No network selected. Running Push for all networks.")
networks, err := functions.GetNetworks()
if err != nil {
log.Println("Error retrieving networks. Exiting.")
return err
}
for _, network := range networks {
err = functions.Push(network)
if err != nil {
log.Printf("Error pushing network configs for "+network+" network: ", err)
} else {
log.Println("pushed network config for " + network)
}
}
err = nil
} else {
err = functions.Push(cfg.Network)
}
log.Println("Completed pushing network configs to remote server.")
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
if cfg.Network == "all" {
2021-08-03 06:06:26 +08:00
log.Println("No network selected. Running Pull for all networks.")
networks, err := functions.GetNetworks()
if err != nil {
log.Println("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 {
2021-08-03 06:06:26 +08:00
log.Printf("Error pulling network config for "+network+" network: ", err)
} else {
log.Println("pulled network config for " + network)
}
}
err = nil
} else {
2021-08-03 06:06:26 +08:00
_, err = functions.Pull(cfg.Network, true)
}
log.Println("Completed pulling network and peer configs.")
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 {
err := functions.List()
return err
}
func Uninstall(cfg config.GlobalConfig) error {
log.Println("Uninstalling netclient")
err := functions.Uninstall()
err = functions.Unregister(cfg)
2021-08-03 06:06:26 +08:00
return err
2021-05-26 00:48:04 +08:00
}
2021-05-30 03:12:15 +08:00
func Unregister(cfg config.GlobalConfig) error {
2021-08-03 06:06:26 +08:00
err := functions.Unregister(cfg)
return err
2021-05-30 03:12:15 +08:00
}