fixing windows remove conf

This commit is contained in:
afeiszli
2022-03-14 21:03:49 -04:00
parent dcb048d948
commit 3b37532eaa
3 changed files with 33 additions and 21 deletions

View File

@@ -166,21 +166,7 @@ func InitWireguard(node *models.Node, privkey string, peers []wgtypes.PeerConfig
} }
} }
// ensure you clear any existing interface first // ensure you clear any existing interface first
d, _ := wgclient.Device(deviceiface) RemoveConfGraceful(deviceiface)
startTime := time.Now()
for d != nil && d.Name == deviceiface {
if err = RemoveConf(deviceiface, false); err != nil { // remove interface first
if strings.Contains(err.Error(), "does not exist") {
err = nil
break
}
}
time.Sleep(time.Second >> 2)
d, _ = wgclient.Device(deviceiface)
if time.Now().After(startTime.Add(time.Second << 4)) {
break
}
}
ApplyConf(node, ifacename, confPath) // Apply initially ApplyConf(node, ifacename, confPath) // Apply initially
ncutils.PrintLog("waiting for interface...", 1) // ensure interface is created ncutils.PrintLog("waiting for interface...", 1) // ensure interface is created
output, _ := ncutils.RunCmd("wg", false) output, _ := ncutils.RunCmd("wg", false)
@@ -301,6 +287,7 @@ func ApplyConf(node *models.Node, ifacename string, confPath string) error {
var err error var err error
switch os { switch os {
case "windows": case "windows":
RemoveConfGraceful(ifacename)
ApplyWindowsConf(confPath) ApplyWindowsConf(confPath)
case "darwin": case "darwin":
ApplyMacOSConf(node, ifacename, confPath) ApplyMacOSConf(node, ifacename, confPath)
@@ -478,3 +465,29 @@ func UpdatePrivateKey(file, privateKey string) error {
} }
return nil return nil
} }
// RemoveConfGraceful - Run remove conf and wait for it to actually be gone before proceeding
func RemoveConfGraceful(ifacename string) {
// ensure you clear any existing interface first
wgclient, err := wgctrl.New()
if err != nil {
ncutils.PrintLog("could not create wgclient", 0)
return
}
defer wgclient.Close()
d, _ := wgclient.Device(ifacename)
startTime := time.Now()
for d != nil && d.Name == ifacename {
if err = RemoveConf(ifacename, false); err != nil { // remove interface first
if strings.Contains(err.Error(), "does not exist") {
err = nil
break
}
}
time.Sleep(time.Second >> 2)
d, _ = wgclient.Device(ifacename)
if time.Now().After(startTime.Add(time.Second << 4)) {
break
}
}
}

View File

@@ -7,7 +7,6 @@ import (
"strconv" "strconv"
"strings" "strings"
"github.com/gravitl/netmaker/logger"
"github.com/gravitl/netmaker/models" "github.com/gravitl/netmaker/models"
"github.com/gravitl/netmaker/netclient/config" "github.com/gravitl/netmaker/netclient/config"
"github.com/gravitl/netmaker/netclient/ncutils" "github.com/gravitl/netmaker/netclient/ncutils"
@@ -72,7 +71,7 @@ func ApplyWithoutWGQuick(node *models.Node, ifacename string, confPath string) e
} }
} }
if _, err := ncutils.RunCmd(ipExec+" link set down dev "+ifacename, false); err != nil { if _, err := ncutils.RunCmd(ipExec+" link set down dev "+ifacename, false); err != nil {
logger.Log(2, "attempted to remove interface before editing") ncutils.PrintLog("attempted to remove interface before editing", 1)
return err return err
} }
if node.PostDown != "" { if node.PostDown != "" {
@@ -81,7 +80,7 @@ func ApplyWithoutWGQuick(node *models.Node, ifacename string, confPath string) e
} }
// set MTU of node interface // set MTU of node interface
if _, err := ncutils.RunCmd(ipExec+" link set mtu "+strconv.Itoa(int(node.MTU))+" up dev "+ifacename, true); err != nil { if _, err := ncutils.RunCmd(ipExec+" link set mtu "+strconv.Itoa(int(node.MTU))+" up dev "+ifacename, true); err != nil {
logger.Log(2, "failed to create interface with mtu", strconv.Itoa(int(node.MTU)), "-", ifacename) ncutils.PrintLog("failed to create interface with mtu "+strconv.Itoa(int(node.MTU))+"-"+ifacename, 1)
return err return err
} }
if node.PostUp != "" { if node.PostUp != "" {
@@ -89,7 +88,7 @@ func ApplyWithoutWGQuick(node *models.Node, ifacename string, confPath string) e
_ = ncutils.RunCmds(runcmds, true) _ = ncutils.RunCmds(runcmds, true)
} }
if node.Address6 != "" && node.IsDualStack == "yes" { if node.Address6 != "" && node.IsDualStack == "yes" {
logger.Log(1, "adding address:", node.Address6) ncutils.PrintLog("adding address: "+node.Address6, 1)
_, _ = ncutils.RunCmd(ipExec+" address add dev "+ifacename+" "+node.Address6+"/64", true) _, _ = ncutils.RunCmd(ipExec+" address add dev "+ifacename+" "+node.Address6+"/64", true)
} }
return nil return nil
@@ -104,8 +103,8 @@ func RemoveWithoutWGQuick(ifacename string) error {
out, err := ncutils.RunCmd(ipExec+" link del "+ifacename, false) out, err := ncutils.RunCmd(ipExec+" link del "+ifacename, false)
dontprint := strings.Contains(out, "does not exist") || strings.Contains(out, "Cannot find device") dontprint := strings.Contains(out, "does not exist") || strings.Contains(out, "Cannot find device")
if err != nil && !dontprint { if err != nil && !dontprint {
logger.Log(1, "error running command:", ipExec, "link del", ifacename) ncutils.PrintLog("error running command: "+ipExec+" link del "+ifacename, 1)
logger.Log(1, out) ncutils.PrintLog(out, 1)
} }
network := strings.ReplaceAll(ifacename, "nm-", "") network := strings.ReplaceAll(ifacename, "nm-", "")
nodeconf, err := config.ReadConfig(network) nodeconf, err := config.ReadConfig(network)