def _write_network(self, settings): entries = net_util.translate_network(settings) nameservers = [] searchdomains = [] dev_names = entries.keys() for (device, info) in entries.items(): # Skip the loopback interface. if device.startswith('lo'): continue dev = self.getnetifname(device) LOG.info('Configuring interface %s', dev) if info.get('bootproto') == 'static': LOG.debug('Configuring dev %s with %s / %s', dev, info.get('address'), info.get('netmask')) # Configure an ipv4 address. ifconfig = (info.get('address') + ' netmask ' + info.get('netmask')) # Configure the gateway. self.updatercconf('defaultrouter', info.get('gateway')) if 'dns-nameservers' in info: nameservers.extend(info['dns-nameservers']) if 'dns-search' in info: searchdomains.extend(info['dns-search']) else: ifconfig = 'DHCP' self.updatercconf('ifconfig_' + dev, ifconfig) # Try to read the /etc/resolv.conf or just start from scratch if that # fails. try: resolvconf = ResolvConf(util.load_file(self.resolv_conf_fn)) resolvconf.parse() except IOError: util.logexc(LOG, "Failed to parse %s, use new empty file", self.resolv_conf_fn) resolvconf = ResolvConf('') resolvconf.parse() # Add some nameservers for server in nameservers: try: resolvconf.add_nameserver(server) except ValueError: util.logexc(LOG, "Failed to add nameserver %s", server) # And add any searchdomains. for domain in searchdomains: try: resolvconf.add_search_domain(domain) except ValueError: util.logexc(LOG, "Failed to add search domain %s", domain) util.write_file(self.resolv_conf_fn, str(resolvconf), 0o644) return dev_names
def update_resolve_conf_file(fn, dns_servers, search_servers): try: r_conf = ResolvConf(util.load_file(fn)) r_conf.parse() empty = False except IOError: LOG.info("Failed at parsing %s creating an empty instance", fn) r_conf = ResolvConf('') r_conf.parse() empty = True if dns_servers: for s in dns_servers: try: r_conf.add_nameserver(s) empty = False except ValueError: util.logexc(LOG, "Failed at adding nameserver %s", s) if search_servers: for s in search_servers: try: r_conf.add_search_domain(s) except ValueError: util.logexc(LOG, "Failed at adding search domain %s", s) if empty is False: write_resolv_conf_file(fn, r_conf)
def _resolve_conf(self, settings): nameservers = settings.dns_nameservers searchdomains = settings.dns_searchdomains for interface in settings.iter_interfaces(): for subnet in interface.get("subnets", []): if "dns_nameservers" in subnet: nameservers.extend(subnet["dns_nameservers"]) if "dns_search" in subnet: searchdomains.extend(subnet["dns_search"]) # Try to read the /etc/resolv.conf or just start from scratch if that # fails. try: resolvconf = ResolvConf( util.load_file( subp.target_path(self.target, self.resolv_conf_fn) ) ) resolvconf.parse() except IOError: util.logexc( LOG, "Failed to parse %s, use new empty file", subp.target_path(self.target, self.resolv_conf_fn), ) resolvconf = ResolvConf("") resolvconf.parse() # Add some nameservers for server in set(nameservers): try: resolvconf.add_nameserver(server) except ValueError: util.logexc(LOG, "Failed to add nameserver %s", server) # And add any searchdomains. for domain in set(searchdomains): try: resolvconf.add_search_domain(domain) except ValueError: util.logexc(LOG, "Failed to add search domain %s", domain) util.write_file( subp.target_path(self.target, self.resolv_conf_fn), str(resolvconf), 0o644, )
def update_resolve_conf_file(fn, dns_servers, search_servers): try: r_conf = ResolvConf(util.load_file(fn)) r_conf.parse() except IOError: util.logexc(LOG, "Failed at parsing %s reverting to an empty " "instance", fn) r_conf = ResolvConf('') r_conf.parse() if dns_servers: for s in dns_servers: try: r_conf.add_nameserver(s) except ValueError: util.logexc(LOG, "Failed at adding nameserver %s", s) if search_servers: for s in search_servers: try: r_conf.add_search_domain(s) except ValueError: util.logexc(LOG, "Failed at adding search domain %s", s) util.write_file(fn, str(r_conf), 0o644)
def remove_resolve_conf_file(fn): r_conf = ResolvConf('') r_conf.parse() write_resolv_conf_file(fn, r_conf)