netmaker/serverctl/serverctl.go

101 lines
3.1 KiB
Go
Raw Normal View History

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
"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
// 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 {
netclientDir := ncutils.GetNetclientPath()
2021-10-05 04:56:27 +08:00
_, err := os.Stat(netclientDir + "/config")
if os.IsNotExist(err) {
os.MkdirAll(netclientDir+"/config", 0700)
} else if err != nil {
2021-12-07 04:31:08 +08:00
logger.Log(1, "could not find or create", netclientDir)
return err
}
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 currentServerNode.Version != servercfg.Version {
currentServerNode.Version = servercfg.Version
logic.UpdateNode(&currentServerNode, &currentServerNode)
}
if err = logic.ServerPull(&currentServerNode, 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)
}
}
}
}
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 {
return err
}
2021-10-29 04:59:16 +08:00
localnets, err := net.Interfaces()
if err != nil {
return err
}
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
}
2022-01-18 06:44:12 +08:00
}
2022-02-01 22:59:23 +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)
}
}
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
}