2021-04-07 00:33:01 +08:00
|
|
|
package serverctl
|
|
|
|
|
|
|
|
import (
|
2021-07-21 05:18:45 +08:00
|
|
|
"errors"
|
2022-02-06 02:12:07 +08:00
|
|
|
"fmt"
|
2021-10-29 04:59:16 +08:00
|
|
|
"net"
|
2021-07-21 05:18:45 +08:00
|
|
|
"os"
|
2021-10-14 03:15:20 +08:00
|
|
|
"strings"
|
2021-09-07 03:53:16 +08:00
|
|
|
|
2022-02-06 02:12:07 +08:00
|
|
|
"github.com/gravitl/netmaker/database"
|
2021-12-07 04:31:08 +08:00
|
|
|
"github.com/gravitl/netmaker/logger"
|
2021-10-13 22:02:04 +08:00
|
|
|
"github.com/gravitl/netmaker/logic"
|
2021-09-20 02:03:47 +08:00
|
|
|
"github.com/gravitl/netmaker/netclient/ncutils"
|
2021-04-07 00:33:01 +08:00
|
|
|
)
|
|
|
|
|
2022-01-26 00:58:51 +08:00
|
|
|
const NETMAKER_BINARY_NAME = "netmaker"
|
|
|
|
|
2021-10-09 03:07:12 +08:00
|
|
|
// InitServerNetclient - intializes the server netclient
|
2022-02-06 02:12:07 +08:00
|
|
|
// 1. Check if config directory exists, if not attempt to make
|
|
|
|
// 2. Check current networks and run pull to get interface up to date in case of restart
|
2021-09-29 02:08:16 +08:00
|
|
|
func InitServerNetclient() error {
|
2021-09-28 05:51:20 +08:00
|
|
|
netclientDir := ncutils.GetNetclientPath()
|
2021-10-05 04:56:27 +08:00
|
|
|
_, err := os.Stat(netclientDir + "/config")
|
2021-09-28 05:51:20 +08:00
|
|
|
if os.IsNotExist(err) {
|
2022-02-06 02:12:07 +08:00
|
|
|
os.MkdirAll(netclientDir+"/config", 0700)
|
2021-09-28 05:51:20 +08:00
|
|
|
} else if err != nil {
|
2021-12-07 04:31:08 +08:00
|
|
|
logger.Log(1, "could not find or create", netclientDir)
|
2021-09-28 05:51:20 +08:00
|
|
|
return err
|
|
|
|
}
|
2022-02-06 02:12:07 +08:00
|
|
|
|
|
|
|
var networks, netsErr = logic.GetNetworks()
|
|
|
|
if netsErr == nil || database.IsEmptyRecord(netsErr) {
|
|
|
|
for _, network := range networks {
|
|
|
|
var currentServerNode, nodeErr = logic.GetNetworkServerLocal(network.NetID)
|
|
|
|
if nodeErr == nil {
|
|
|
|
if err = logic.ServerPull(¤tServerNode, true); err != nil {
|
|
|
|
logger.Log(1, fmt.Sprintf("failed pull for network %s, on server node %s",
|
|
|
|
network.NetID,
|
|
|
|
currentServerNode.ID))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-06 03:02:09 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-01-18 06:44:12 +08:00
|
|
|
// SyncServerNetwork - ensures a wg interface and node exists for server
|
2022-02-01 22:37:16 +08:00
|
|
|
func SyncServerNetwork(network string) error {
|
|
|
|
serverNetworkSettings, err := logic.GetNetwork(network)
|
2022-01-18 06:44:12 +08:00
|
|
|
if err != nil {
|
2021-10-06 03:02:09 +08:00
|
|
|
return err
|
|
|
|
}
|
2021-10-29 04:59:16 +08:00
|
|
|
localnets, err := net.Interfaces()
|
2021-09-28 05:51:20 +08:00
|
|
|
if err != nil {
|
2021-10-06 03:02:09 +08:00
|
|
|
return err
|
2021-09-28 05:51:20 +08:00
|
|
|
}
|
2022-02-01 22:59:23 +08:00
|
|
|
|
|
|
|
ifaceExists := false
|
2022-01-18 06:44:12 +08:00
|
|
|
for _, localnet := range localnets {
|
|
|
|
if serverNetworkSettings.DefaultInterface == localnet.Name {
|
2022-02-01 22:59:23 +08:00
|
|
|
ifaceExists = true
|
2021-10-06 03:02:09 +08:00
|
|
|
}
|
2022-01-18 06:44:12 +08:00
|
|
|
}
|
2022-02-01 22:59:23 +08:00
|
|
|
|
2022-02-03 00:56:39 +08:00
|
|
|
serverNode, err := logic.GetNetworkServerLocal(network)
|
|
|
|
if !ifaceExists && (err == nil && serverNode.ID != "") {
|
2022-02-01 22:59:23 +08:00
|
|
|
return logic.ServerUpdate(&serverNode, true)
|
|
|
|
} else if !ifaceExists {
|
2022-02-01 23:08:44 +08:00
|
|
|
_, err := logic.ServerJoin(&serverNetworkSettings)
|
2022-01-18 06:44:12 +08:00
|
|
|
if err != nil {
|
|
|
|
if err == nil {
|
|
|
|
err = errors.New("network add failed for " + serverNetworkSettings.NetID)
|
|
|
|
}
|
|
|
|
if !strings.Contains(err.Error(), "macaddress_unique") { // ignore macaddress unique error throws
|
|
|
|
logger.Log(1, "error adding network", serverNetworkSettings.NetID, "during sync:", err.Error())
|
2021-10-06 03:02:09 +08:00
|
|
|
}
|
2021-09-28 05:51:20 +08:00
|
|
|
}
|
|
|
|
}
|
2022-02-01 22:37:16 +08:00
|
|
|
|
|
|
|
// remove networks locally that do not exist in database
|
|
|
|
/*
|
|
|
|
for _, localnet := range localnets {
|
|
|
|
if strings.Contains(localnet.Name, "nm-") {
|
|
|
|
var exists = ""
|
|
|
|
if serverNetworkSettings.DefaultInterface == localnet.Name {
|
|
|
|
exists = serverNetworkSettings.NetID
|
|
|
|
}
|
|
|
|
if exists == "" {
|
|
|
|
err := logic.DeleteNodeByID(serverNode, true)
|
|
|
|
if err != nil {
|
|
|
|
if err == nil {
|
|
|
|
err = errors.New("network delete failed for " + exists)
|
|
|
|
}
|
|
|
|
logger.Log(1, "error removing network", exists, "during sync", err.Error())
|
2021-10-29 05:29:44 +08:00
|
|
|
}
|
2021-10-14 03:15:20 +08:00
|
|
|
}
|
2021-10-06 03:02:09 +08:00
|
|
|
}
|
|
|
|
}
|
2022-02-01 22:37:16 +08:00
|
|
|
*/
|
2021-09-28 05:51:20 +08:00
|
|
|
return nil
|
|
|
|
}
|