2021-04-07 00:33:01 +08:00
|
|
|
package serverctl
|
|
|
|
|
|
|
|
import (
|
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"
|
2022-03-14 08:58:30 +08:00
|
|
|
"github.com/gravitl/netmaker/logic/acls"
|
|
|
|
"github.com/gravitl/netmaker/logic/acls/nodeacls"
|
2021-09-20 02:03:47 +08:00
|
|
|
"github.com/gravitl/netmaker/netclient/ncutils"
|
2022-02-19 00:11:21 +08:00
|
|
|
"github.com/gravitl/netmaker/servercfg"
|
2021-04-07 00:33:01 +08:00
|
|
|
)
|
|
|
|
|
2022-02-19 00:11:21 +08:00
|
|
|
const (
|
|
|
|
// NETMAKER_BINARY_NAME - name of netmaker binary
|
|
|
|
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 {
|
2022-03-29 05:26:03 +08:00
|
|
|
if currentServerNode.Version != servercfg.Version {
|
|
|
|
currentServerNode.Version = servercfg.Version
|
|
|
|
logic.UpdateNode(¤tServerNode, ¤tServerNode)
|
|
|
|
}
|
2022-02-06 02:12:07 +08:00
|
|
|
if err = logic.ServerPull(¤tServerNode, true); err != nil {
|
2022-02-11 00:07:21 +08:00
|
|
|
logger.Log(1, "failed pull for network", network.NetID, ", on server node", currentServerNode.ID)
|
2022-02-06 02:12:07 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 {
|
2022-06-17 03:42:32 +08:00
|
|
|
logger.Log(0, "network add failed for "+serverNetworkSettings.NetID)
|
2021-09-28 05:51:20 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2022-03-14 08:58:30 +08:00
|
|
|
|
|
|
|
// SetDefaultACLS - runs through each network to see if ACL's are set. If not, goes through each node in network and adds the default ACL
|
|
|
|
func SetDefaultACLS() error {
|
|
|
|
// upgraded systems will not have ACL's set, which is why we need this function
|
2022-03-14 09:35:22 +08:00
|
|
|
nodes, err := logic.GetAllNodes()
|
2022-03-14 08:58:30 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-03-14 09:35:22 +08:00
|
|
|
for i := range nodes {
|
|
|
|
currentNodeACL, err := nodeacls.FetchNodeACL(nodeacls.NetworkID(nodes[i].Network), nodeacls.NodeID(nodes[i].ID))
|
|
|
|
if (err != nil && (database.IsEmptyRecord(err) || strings.Contains(err.Error(), "no node ACL present"))) || currentNodeACL == nil {
|
|
|
|
if _, err = nodeacls.CreateNodeACL(nodeacls.NetworkID(nodes[i].Network), nodeacls.NodeID(nodes[i].ID), acls.Allowed); err != nil {
|
|
|
|
logger.Log(1, "could not create a default ACL for node", nodes[i].ID)
|
2022-03-14 08:58:30 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-03-14 09:35:22 +08:00
|
|
|
return nil
|
2022-03-14 08:58:30 +08:00
|
|
|
}
|