diff --git a/cloudbaseinit/osutils/windows.py b/cloudbaseinit/osutils/windows.py index 5630d212..c83cb1ae 100644 --- a/cloudbaseinit/osutils/windows.py +++ b/cloudbaseinit/osutils/windows.py @@ -933,28 +933,7 @@ def _fix_network_adapter_dhcp(interface_name, @staticmethod def _set_interface_dns(interface_name, dnsnameservers): - # Import here to avoid loading errors on Windows versions where MI is - # not available - import mi - - conn = wmi.WMI(moniker='//./root/standardcimv2') - # Requires Windows >= 6.2 - dns_client = conn.MSFT_DnsClientServerAddress( - InterfaceAlias=interface_name) - if not len(dns_client): - raise exception.ItemNotFoundException( - 'Network interface with name "%s" not found' % - interface_name) - dns_client = dns_client[0] - - custom_options = [{ - u'name': u'ServerAddresses', - u'value_type': mi.MI_ARRAY | mi.MI_STRING, - u'value': dnsnameservers - }] - - operation_options = {u'custom_options': custom_options} - dns_client.put(operation_options=operation_options) + os.system(f'netsh interface ip set dns {interface_name} static {dnsnameservers}') def enable_network_adapter(self, name, enabled): adapter = self._get_network_adapter(name) diff --git a/cloudbaseinit/utils/debiface.py b/cloudbaseinit/utils/debiface.py index 4c4c26a1..8cb762b3 100644 --- a/cloudbaseinit/utils/debiface.py +++ b/cloudbaseinit/utils/debiface.py @@ -53,7 +53,7 @@ r"(?P<{}>\S+)".format(GATEWAY)), GATEWAY6: re.compile(r"post-up ip -6 route add default via " r"(?P<{}>.+) dev".format(GATEWAY6)), - DNSNS: re.compile(r"dns-nameservers\s+(?P<{}>.+)".format(DNSNS)) + DNSNS: re.compile(r"(dns-nameservers|dns_nameservers|dns-nameserver|dns_nameserver)\s+(?P.+)".format(DNSNS)) } IFACE_TEMPLATE = dict.fromkeys(FIELDS.keys()) # Map IPv6 availability by value index under `NetworkDetails`. @@ -98,10 +98,16 @@ def _get_field(line): yield field, match.group(field) -def _add_nic(iface, nics): +def _add_nic(iface, nics, dns): if not iface or iface == IFACE_TEMPLATE: return # no information gathered + + # setting dns globally for all interfaces if provided + if dns: + iface[DNSNS] = dns + LOG.debug("Found new interface: %s", iface) + # Each missing detail is marked as None. nic = network_model.NetworkDetails(**iface) nics.append(nic) @@ -115,6 +121,7 @@ def parse(data): LOG.info("Parsing Debian config...\n%s", data) nics = [] # list of NetworkDetails objects + global_dns = None for lines_pair in _get_iface_blocks(data): iface = IFACE_TEMPLATE.copy() for lines, use_proxy in zip(lines_pair, (False, True)): @@ -126,6 +133,10 @@ def parse(data): continue func = DETAIL_PREPROCESS.get(field, lambda value: value) iface[field] = func(value) if value != "None" else None - _add_nic(iface, nics) + + # obtain dns global settings if provided + if field == DNSNS and value != None: + global_dns = value + _add_nic(iface, nics, global_dns) return nics