Browse Source

Merge pull request #918 from gravitl/bugfix_v0.12.1_hostctl_profile_names

use interface as hostctl profile
dcarns 3 years ago
parent
commit
b397f5014f
2 changed files with 13 additions and 7 deletions
  1. 3 0
      netclient/functions/daemon.go
  2. 10 7
      netclient/functions/mqhandlers.go

+ 3 - 0
netclient/functions/daemon.go

@@ -38,6 +38,9 @@ func Daemon() error {
 	// == initial pull of all networks ==
 	networks, _ := ncutils.GetSystemNetworks()
 	for _, network := range networks {
+		//temporary code --- remove in version v0.13.0
+		removeHostDNS(network, ncutils.IsWindows())
+		// end of code to be removed in version v0.13.0
 		var cfg config.ClientConfig
 		cfg.Network = network
 		cfg.ReadConfig()

+ 10 - 7
netclient/functions/mqhandlers.go

@@ -144,7 +144,7 @@ func NodeUpdate(client mqtt.Client, msg mqtt.Message) {
 	//deal with DNS
 	if newNode.DNSOn != "yes" && shouldDNSChange && nodeCfg.Node.Interface != "" {
 		ncutils.Log("settng DNS off")
-		if err := removeHostDNS(nodeCfg.Network, ncutils.IsWindows()); err != nil {
+		if err := removeHostDNS(nodeCfg.Node.Interface, ncutils.IsWindows()); err != nil {
 			ncutils.Log("error removing netmaker profile from /etc/hosts " + err.Error())
 		}
 		//		_, err := ncutils.RunCmd("/usr/bin/resolvectl revert "+nodeCfg.Node.Interface, true)
@@ -204,19 +204,19 @@ func UpdatePeers(client mqtt.Client, msg mqtt.Message) {
 		return
 	}
 	if cfg.Node.DNSOn == "yes" {
-		if err := setHostDNS(peerUpdate.DNS, cfg.Node.Network, ncutils.IsWindows()); err != nil {
+		if err := setHostDNS(peerUpdate.DNS, cfg.Node.Interface, ncutils.IsWindows()); err != nil {
 			ncutils.Log("error updating /etc/hosts " + err.Error())
 			return
 		}
 	} else {
-		if err := removeHostDNS(cfg.Node.Network, ncutils.IsWindows()); err != nil {
+		if err := removeHostDNS(cfg.Node.Interface, ncutils.IsWindows()); err != nil {
 			ncutils.Log("error removing profile from /etc/hosts " + err.Error())
 			return
 		}
 	}
 }
 
-func setHostDNS(dns, network string, windows bool) error {
+func setHostDNS(dns, iface string, windows bool) error {
 	etchosts := "/etc/hosts"
 	if windows {
 		etchosts = "c:\\windows\\system32\\drivers\\etc\\hosts"
@@ -230,7 +230,7 @@ func setHostDNS(dns, network string, windows bool) error {
 	if err != nil {
 		return err
 	}
-	profile.Name = network
+	profile.Name = iface
 	profile.Status = types.Enabled
 	if err := hosts.ReplaceProfile(profile); err != nil {
 		return err
@@ -241,7 +241,7 @@ func setHostDNS(dns, network string, windows bool) error {
 	return nil
 }
 
-func removeHostDNS(network string, windows bool) error {
+func removeHostDNS(iface string, windows bool) error {
 	etchosts := "/etc/hosts"
 	if windows {
 		etchosts = "c:\\windows\\system32\\drivers\\etc\\hosts"
@@ -250,7 +250,10 @@ func removeHostDNS(network string, windows bool) error {
 	if err != nil {
 		return err
 	}
-	if err := hosts.RemoveProfile(network); err != nil {
+	if err := hosts.RemoveProfile(iface); err != nil {
+		if err == types.ErrUnknownProfile {
+			return nil
+		}
 		return err
 	}
 	if err := hosts.Flush(); err != nil {