2021-04-07 00:33:01 +08:00
|
|
|
package serverctl
|
|
|
|
|
|
|
|
import (
|
2021-07-21 05:18:45 +08:00
|
|
|
"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-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-05-06 06:03:37 +08:00
|
|
|
"github.com/gravitl/netmaker/models"
|
2021-09-20 02:03:47 +08:00
|
|
|
"github.com/gravitl/netmaker/netclient/ncutils"
|
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 {
|
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) {
|
2021-12-07 04:31:08 +08:00
|
|
|
os.MkdirAll(netclientDir+"/config", 0744)
|
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
|
|
|
|
}
|
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
|
|
|
|
func SyncServerNetwork(serverNode *models.Node) error {
|
|
|
|
serverNetworkSettings, err := logic.GetNetwork(serverNode.Network)
|
|
|
|
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-01-18 06:44:12 +08:00
|
|
|
exists := false
|
|
|
|
for _, localnet := range localnets {
|
|
|
|
if serverNetworkSettings.DefaultInterface == localnet.Name {
|
|
|
|
exists = true
|
2021-10-06 03:02:09 +08:00
|
|
|
}
|
2022-01-18 06:44:12 +08:00
|
|
|
}
|
|
|
|
if !exists {
|
|
|
|
err := logic.ServerJoin(&serverNetworkSettings)
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
2021-10-06 03:02:09 +08:00
|
|
|
for _, localnet := range localnets {
|
2021-10-29 05:29:44 +08:00
|
|
|
if strings.Contains(localnet.Name, "nm-") {
|
|
|
|
var exists = ""
|
2022-01-18 06:44:12 +08:00
|
|
|
if serverNetworkSettings.DefaultInterface == localnet.Name {
|
|
|
|
exists = serverNetworkSettings.NetID
|
2021-10-29 05:29:44 +08:00
|
|
|
}
|
|
|
|
if exists == "" {
|
2022-01-18 06:44:12 +08:00
|
|
|
err := logic.DeleteNodeByID(serverNode, true)
|
|
|
|
if err != nil {
|
2021-10-29 05:29:44 +08:00
|
|
|
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-06 03:02:09 +08:00
|
|
|
}
|
|
|
|
}
|
2021-09-28 05:51:20 +08:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|