netmaker/serverctl/serverctl.go

146 lines
3.8 KiB
Go
Raw Normal View History

2021-04-07 00:33:01 +08:00
package serverctl
import (
2021-07-21 05:18:45 +08:00
"encoding/json"
"errors"
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
2021-07-21 05:18:45 +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"
"github.com/gravitl/netmaker/models"
2021-09-20 02:03:47 +08:00
"github.com/gravitl/netmaker/netclient/ncutils"
"github.com/gravitl/netmaker/servercfg"
2021-04-07 00:33:01 +08:00
)
2021-10-09 03:07:12 +08:00
// GetServerWGConf - gets the server WG configuration
func GetServerWGConf() (models.IntClient, error) {
2021-07-21 05:18:45 +08:00
var server models.IntClient
2021-07-22 06:55:19 +08:00
collection, err := database.FetchRecords(database.INT_CLIENTS_TABLE_NAME)
if err != nil {
return models.IntClient{}, errors.New("could not find comms server")
}
for _, value := range collection {
json.Unmarshal([]byte(value), &server)
if server.Network == "comms" && server.IsServer == "yes" {
return server, nil
}
}
return models.IntClient{}, errors.New("could not find comms server")
2021-05-26 00:48:04 +08:00
}
2021-10-09 03:07:12 +08:00
// FileExists - checks if local file exists
2021-05-05 05:36:55 +08:00
func FileExists(f string) bool {
2021-07-21 05:18:45 +08:00
info, err := os.Stat(f)
if os.IsNotExist(err) {
return false
}
return !info.IsDir()
2021-04-07 00:33:01 +08:00
}
2021-05-05 05:36:55 +08:00
2021-10-09 03:07:12 +08:00
// RemoveNetwork - removes a network locally on server
func RemoveNetwork(network string) (bool, error) {
2021-10-13 22:02:04 +08:00
err := logic.ServerLeave(servercfg.GetNodeID(), network)
2021-07-21 05:18:45 +08:00
return true, err
}
2021-04-07 00:33:01 +08:00
2021-10-09 03:07:12 +08:00
// InitServerNetclient - intializes the server netclient
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) {
2021-12-07 04:31:08 +08:00
os.MkdirAll(netclientDir+"/config", 0744)
} else if err != nil {
2021-12-07 04:31:08 +08:00
logger.Log(1, "could not find or create", netclientDir)
return err
}
return nil
}
2021-10-09 03:07:12 +08:00
// HandleContainedClient - function for checkins on server
func HandleContainedClient() error {
2021-10-27 00:27:29 +08:00
servernets, err := logic.GetNetworks()
if err != nil && !database.IsEmptyRecord(err) {
return err
}
if len(servernets) > 0 {
if err != nil {
return err
}
2021-10-14 03:15:20 +08:00
for _, serverNet := range servernets {
err = logic.ServerCheckin(servercfg.GetNodeID(), serverNet.NetID)
2021-10-14 23:58:09 +08:00
if err != nil {
2021-12-07 04:31:08 +08:00
logger.Log(1, "error occurred during server checkin:", err.Error())
2021-10-27 04:12:13 +08:00
} else {
2021-12-07 04:31:08 +08:00
logger.Log(3, "completed peers check of network", serverNet.NetID)
2021-10-14 23:58:09 +08:00
}
}
2021-12-11 04:01:10 +08:00
syncErr := SyncNetworks(servernets[:])
if syncErr != nil {
logger.Log(1, "error syncing networks:", syncErr.Error())
syncErr = nil
2021-10-29 05:29:44 +08:00
}
2021-12-07 04:31:08 +08:00
// logger.Log("completed a checkin call", 3)
}
2021-09-29 02:08:16 +08:00
return nil
}
2021-10-09 03:07:12 +08:00
// SyncNetworks - syncs the networks for servers
func SyncNetworks(servernets []models.Network) error {
2021-10-29 04:59:16 +08:00
localnets, err := net.Interfaces()
if err != nil {
return err
}
// check networks to join
for _, servernet := range servernets {
exists := false
for _, localnet := range localnets {
2021-10-29 04:59:16 +08:00
if servernet.DefaultInterface == localnet.Name {
exists = true
}
}
if !exists {
success, err := AddNetwork(servernet.NetID)
if err != nil || !success {
if err == nil {
err = errors.New("network add failed for " + servernet.NetID)
}
2021-12-07 04:31:08 +08:00
if !strings.Contains(err.Error(), "macaddress_unique") { // ignore macaddress unique error throws
logger.Log(1, "error adding network", servernet.NetID, "during sync:", err.Error())
2021-10-07 22:57:06 +08:00
}
}
}
}
// check networks to leave
for _, localnet := range localnets {
2021-10-29 05:29:44 +08:00
if strings.Contains(localnet.Name, "nm-") {
var exists = ""
for _, servernet := range servernets {
if servernet.DefaultInterface == localnet.Name {
exists = servernet.NetID
}
2021-10-29 05:29:44 +08:00
}
if exists == "" {
success, err := RemoveNetwork(exists)
if err != nil || !success {
if err == nil {
err = errors.New("network delete failed for " + exists)
}
2021-12-07 04:31:08 +08:00
logger.Log(1, "error removing network", exists, "during sync", err.Error())
2021-10-14 03:15:20 +08:00
}
}
}
}
2021-10-29 05:29:44 +08:00
return nil
}
2021-10-09 03:07:12 +08:00
// AddNetwork - add a network to server in client mode
func AddNetwork(network string) (bool, error) {
2021-10-27 22:09:15 +08:00
var err = logic.ServerJoin(network, servercfg.GetNodeID(), "")
return true, err
2021-04-07 00:33:01 +08:00
}