Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/fix dns setup for Proxmox #116

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 1 addition & 22 deletions cloudbaseinit/osutils/windows.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
17 changes: 14 additions & 3 deletions cloudbaseinit/utils/debiface.py
Original file line number Diff line number Diff line change
Expand Up @@ -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<dnsnameservers>.+)".format(DNSNS))
}
IFACE_TEMPLATE = dict.fromkeys(FIELDS.keys())
# Map IPv6 availability by value index under `NetworkDetails`.
Expand Down Expand Up @@ -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)
Expand All @@ -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)):
Expand All @@ -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