Merge lp:~oreilldf/wicd/resolvconf2 into lp:wicd/1.6

Proposed by Dan O'Reilly
Status: Superseded
Proposed branch: lp:~oreilldf/wicd/resolvconf2
Merge into: lp:wicd/1.6
Diff against target: None lines
To merge this branch: bzr merge lp:~oreilldf/wicd/resolvconf2
Reviewer Review Type Date Requested Status
David Paleino Needs Fixing
Wicd-devel Pending
Review via email: mp+4474@code.launchpad.net

This proposal has been superseded by a proposal from 2009-03-15.

To post a comment you must log in.
Revision history for this message
Dan O'Reilly (oreilldf) wrote :

Adds support for using resolvconf to set DNS settings instead of directly editing resolv.conf.

Revision history for this message
David Paleino (dpaleino) wrote :
Download full text (3.3 KiB)

I suggest changing the last chunk of the patch as follows:

---8<---
@@ -518,7 +496,51 @@
             return
         if self.verbose: print cmd
         misc.Run(cmd)
-
+
+ def SetDNS(self, dns1=None, dns2=None, dns3=None,
+ dns_dom=None, search_dom=None):
+ """ Set the DNS of the system to the specified DNS servers.
+
+ Opens up resolv.conf and writes in the nameservers.
+
+ Keyword arguments:
+ dns1 -- IP address of DNS server 1
+ dns2 -- IP address of DNS server 2
+ dns3 -- IP address of DNS server 3
+ dns_dom -- DNS domain
+ search_dom -- DNS search domain
+
+ """
+ resolv_params = ""
+ if dns_dom:
+ resolv_params = ''.join([resolv_params, 'domain ', dns_dom, '\n'])
+ if search_dom:
+ resolv_params = ''.join([resolv_params, 'search ', search_dom,
+ '\n'])
+ valid_dns_list = []
+ for dns in [dns1, dns2, dns3]:
+ if dns:
+ if misc.IsValidIP(dns):
+ print 'Setting DNS : ' + dns
+ valid_dns_list.append(dns)
+ else:
+ print 'DNS IP %s is not a valid IP address, skipping' % dns
+
+ if self.resolvconf_cmd:
+ # Make sure we have at least one DNS in the list.
+ if valid_dns_list:
+ resolv_params += 'nameserver ' + ' '.join(valid_dns_list) + '\n'
+
+ print "running resolvconf"
+ p = misc.Run(' '.join([self.resolvconf_cmd, '-a', self.iface]),
+ include_stderr=True, return_obj=True)
+ p.communicate(input=resolv_params)[0]
+ else:
+ resolv = open("/etc/resolv.conf", "w")
+ resolv.write(resolv_params + "\n")
+ for dns in valid_dns_list:
+ resolv.write('nameserver %s\n' % dns)
+ resolv.close()

     def FlushRoutes(self):
         """ Flush network routes for this device. """
--->8---

Let's go step-by-step.
Here:

+ else:
+ print 'DNS IP is not a valid IP address, skipping'

It would be rather useful printing *which* DNS is not a valid IP address.

Also, later we have:

In the same file:

+ # Make sure we have more than just 'nameserver' in the list.
+ if len(valid_dns_list) > 1:
+ resolv_params += ' '.join(valid_dns_list) + '\n'
+
+ if self.resolvconf_cmd:
+ print "running resolvconf"
+ p = misc.Run(' '.join([self.resolvconf_cmd, '-a', self.iface]),
+ include_stderr=True, return_obj=True)
+ p.communicate(input=resolv_params)[0]
+ else:
+ resolv = open("/etc/resolv.conf", "w")
+ resolv.write(resolv_params + "\n")
+ resolv.close()

This is wrong. /etc/resolv.conf accepts maximum MAXNS (currently 3, see <resolv.h>) IPs listed after "nameserver", while resolvconf gracefully handles that by putting each IP on a separate line (i.e. "nameserver A B" used with resolvconf would actually give "nameserver A\nnameserver B" in /etc/resolv.conf).

O...

Read more...

review: Needs Fixing
lp:~oreilldf/wicd/resolvconf2 updated
326. By Dan O'Reilly

Fix how we set nameserver entries after code review.

327. By Dan O'Reilly

Only print "Setting DNS" message if debug mode is on.

Unmerged revisions

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'wicd/misc.py'
--- wicd/misc.py 2009-03-09 03:39:41 +0000
+++ wicd/misc.py 2009-03-09 04:07:39 +0000
@@ -95,6 +95,10 @@
95 else:95 else:
96 err = None96 err = None
97 fds = False97 fds = False
98 if return_obj:
99 std_in = PIPE
100 else:
101 std_in = None
98 102
99 # We need to make sure that the results of the command we run103 # We need to make sure that the results of the command we run
100 # are in English, so we set up a temporary environment.104 # are in English, so we set up a temporary environment.
@@ -105,8 +109,8 @@
105 tmpenv["LANG"] = __LANG109 tmpenv["LANG"] = __LANG
106 110
107 try:111 try:
108 f = Popen(cmd, shell=False, stdout=PIPE, stderr=err, close_fds=fds,112 f = Popen(cmd, shell=False, stdout=PIPE, stdin=std_in, stderr=err,
109 cwd='/', env=tmpenv)113 close_fds=fds, cwd='/', env=tmpenv)
110 except OSError, e:114 except OSError, e:
111 print "Running command %s failed: %s" % (str(cmd), str(e))115 print "Running command %s failed: %s" % (str(cmd), str(e))
112 return ""116 return ""
113117
=== modified file 'wicd/networking.py'
--- wicd/networking.py 2009-03-09 03:39:41 +0000
+++ wicd/networking.py 2009-03-09 04:07:39 +0000
@@ -427,7 +427,7 @@
427 return427 return
428 428
429 @abortable429 @abortable
430 def set_dns_addresses(self):430 def set_dns_addresses(self, iface):
431 """ Set the DNS address(es).431 """ Set the DNS address(es).
432432
433 If static DNS servers or global DNS servers are specified, set them.433 If static DNS servers or global DNS servers are specified, set them.
@@ -435,19 +435,19 @@
435 435
436 """436 """
437 if self.network.get('use_global_dns'):437 if self.network.get('use_global_dns'):
438 BACKEND.SetDNS(misc.Noneify(self.global_dns_1),438 iface.SetDNS(misc.Noneify(self.global_dns_1),
439 misc.Noneify(self.global_dns_2), 439 misc.Noneify(self.global_dns_2),
440 misc.Noneify(self.global_dns_3),440 misc.Noneify(self.global_dns_3),
441 misc.Noneify(self.global_dns_dom),441 misc.Noneify(self.global_dns_dom),
442 misc.Noneify(self.global_search_dom))442 misc.Noneify(self.global_search_dom))
443 elif self.network.get('use_static_dns') and (self.network.get('dns1') or443 elif self.network.get('use_static_dns') and (self.network.get('dns1') or
444 self.network.get('dns2') or self.network.get('dns3')):444 self.network.get('dns2') or self.network.get('dns3')):
445 self.SetStatus('setting_static_dns')445 self.SetStatus('setting_static_dns')
446 BACKEND.SetDNS(self.network.get('dns1'),446 iface.SetDNS(self.network.get('dns1'),
447 self.network.get('dns2'),447 self.network.get('dns2'),
448 self.network.get('dns3'),448 self.network.get('dns3'),
449 self.network.get('dns_domain'),449 self.network.get('dns_domain'),
450 self.network.get('search_domain'))450 self.network.get('search_domain'))
451451
452 @abortable452 @abortable
453 def release_dhcp_clients(self, iface):453 def release_dhcp_clients(self, iface):
@@ -837,7 +837,7 @@
837 # Set up gateway, IP address, and DNS servers.837 # Set up gateway, IP address, and DNS servers.
838 self.set_broadcast_address(wiface)838 self.set_broadcast_address(wiface)
839 self.set_ip_address(wiface)839 self.set_ip_address(wiface)
840 self.set_dns_addresses()840 self.set_dns_addresses(wiface)
841 841
842 # Run post-connection script.842 # Run post-connection script.
843 self.run_global_scripts_if_needed(wpath.postconnectscripts)843 self.run_global_scripts_if_needed(wpath.postconnectscripts)
@@ -1025,7 +1025,7 @@
1025 # Set gateway, IP adresses, and DNS servers.1025 # Set gateway, IP adresses, and DNS servers.
1026 self.set_broadcast_address(liface)1026 self.set_broadcast_address(liface)
1027 self.set_ip_address(liface)1027 self.set_ip_address(liface)
1028 self.set_dns_addresses()1028 self.set_dns_addresses(liface)
1029 1029
1030 # Run post-connection script.1030 # Run post-connection script.
1031 self.run_global_scripts_if_needed(wpath.postconnectscripts)1031 self.run_global_scripts_if_needed(wpath.postconnectscripts)
10321032
=== modified file 'wicd/wnettools.py'
--- wicd/wnettools.py 2009-03-09 03:09:12 +0000
+++ wicd/wnettools.py 2009-03-09 04:07:39 +0000
@@ -74,7 +74,7 @@
74blacklist_norm = ";`$!*|><&\\"74blacklist_norm = ";`$!*|><&\\"
75blank_trans = maketrans("", "")75blank_trans = maketrans("", "")
7676
77__all__ = ["SetDNS", "GetDefaultGateway", "GetWiredInterfaces",77__all__ = ["GetDefaultGateway", "GetWiredInterfaces",
78 "GetWirelessInterfaces", "IsValidWpaSuppDriver"]78 "GetWirelessInterfaces", "IsValidWpaSuppDriver"]
7979
80def _sanitize_string(string):80def _sanitize_string(string):
@@ -89,33 +89,6 @@
89 else:89 else:
90 return string90 return string
9191
92def SetDNS(dns1=None, dns2=None, dns3=None, dns_dom=None, search_dom=None):
93 """ Set the DNS of the system to the specified DNS servers.
94
95 Opens up resolv.conf and writes in the nameservers.
96
97 Keyword arguments:
98 dns1 -- IP address of DNS server 1
99 dns2 -- IP address of DNS server 2
100 dns3 -- IP address of DNS server 3
101 dns_dom -- DNS domain
102 search_dom -- DNS search domain
103
104 """
105 resolv = open("/etc/resolv.conf", "w")
106 if dns_dom:
107 resolv.write("domain %s\n" % dns_dom)
108 if search_dom:
109 resolv.write('search %s\n' % search_dom)
110 for dns in [dns1, dns2, dns3]:
111 if dns:
112 if misc.IsValidIP(dns):
113 print 'Setting DNS : ' + dns
114 resolv.write('nameserver ' + dns + '\n')
115 else:
116 print 'DNS IP is not a valid IP address, not writing to resolv.conf'
117 resolv.close()
118
119def GetDefaultGateway():92def GetDefaultGateway():
120 """ Attempts to determine the default gateway by parsing route -n. """93 """ Attempts to determine the default gateway by parsing route -n. """
121 route_info = misc.Run("route -n")94 route_info = misc.Run("route -n")
@@ -291,6 +264,11 @@
291 self.CheckWirelessTools()264 self.CheckWirelessTools()
292 self.CheckSudoApplications()265 self.CheckSudoApplications()
293 self.CheckRouteFlushTool()266 self.CheckRouteFlushTool()
267 self.CheckResolvConf()
268
269 def CheckResolvConf(self):
270 """ Checks for the existence of resolvconf."""
271 self.resolvconf_cmd = self._find_program_path("resolvconf")
294 272
295 def CheckDHCP(self):273 def CheckDHCP(self):
296 """ Check for the existence of valid DHCP clients. 274 """ Check for the existence of valid DHCP clients.
@@ -518,7 +496,48 @@
518 return 496 return
519 if self.verbose: print cmd497 if self.verbose: print cmd
520 misc.Run(cmd)498 misc.Run(cmd)
521 499
500 def SetDNS(self, dns1=None, dns2=None, dns3=None,
501 dns_dom=None, search_dom=None):
502 """ Set the DNS of the system to the specified DNS servers.
503
504 Opens up resolv.conf and writes in the nameservers.
505
506 Keyword arguments:
507 dns1 -- IP address of DNS server 1
508 dns2 -- IP address of DNS server 2
509 dns3 -- IP address of DNS server 3
510 dns_dom -- DNS domain
511 search_dom -- DNS search domain
512
513 """
514 resolv_params = ""
515 if dns_dom:
516 resolv_params = ''.join([resolv_params, 'domain ', dns_dom, '\n'])
517 if search_dom:
518 resolv_params = ''.join([resolv_params, 'search ', search_dom,
519 '\n'])
520 valid_dns_list = ['nameserver']
521 for dns in [dns1, dns2, dns3]:
522 if dns:
523 if misc.IsValidIP(dns):
524 print 'Setting DNS : ' + dns
525 valid_dns_list.append(dns)
526 else:
527 print 'DNS IP is not a valid IP address, skipping'
528 # Make sure we have more than just 'nameserver' in the list.
529 if len(valid_dns_list) > 1:
530 resolv_params += ' '.join(valid_dns_list) + '\n'
531
532 if self.resolvconf_cmd:
533 print "running resolvconf"
534 p = misc.Run(' '.join([self.resolvconf_cmd, '-a', self.iface]),
535 include_stderr=True, return_obj=True)
536 p.communicate(input=resolv_params)[0]
537 else:
538 resolv = open("/etc/resolv.conf", "w")
539 resolv.write(resolv_params + "\n")
540 resolv.close()
522 541
523 def FlushRoutes(self):542 def FlushRoutes(self):
524 """ Flush network routes for this device. """543 """ Flush network routes for this device. """

Subscribers

People subscribed via source and target branches

to status/vote changes: