Skip to content

Commit

Permalink
Merge pull request #513 from nvgoldin/dns_backward
Browse files Browse the repository at this point in the history
 Make DNS changes introduced in 6386bc8 backward compatible
  • Loading branch information
ovirt-infra authored Apr 23, 2017
2 parents b6cce03 + c913a86 commit 2e33c9d
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 24 deletions.
16 changes: 9 additions & 7 deletions lago/prefix.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import urllib
import uuid
import warnings
import pkg_resources
from os.path import join

import xmltodict
Expand Down Expand Up @@ -107,7 +108,8 @@ def __init__(self, prefix):
self._virt_env = None
self._metadata = None

def _get_metadata(self):
@property
def metadata(self):
"""
Retrieve the metadata info for this prefix
Expand All @@ -117,11 +119,7 @@ def _get_metadata(self):
if self._metadata is None:
try:
with open(self.paths.metadata()) as metadata_fd:
json_data = metadata_fd.read()
if json_data:
self._metadata = json.load(json_data)
else:
raise IOError()
self._metadata = json.load(metadata_fd)
except IOError:
self._metadata = {}
return self._metadata
Expand All @@ -134,7 +132,7 @@ def _save_metadata(self):
None
"""
with open(self.paths.metadata(), 'w') as metadata_fd:
utils.json_dump(self._get_metadata(), metadata_fd)
utils.json_dump(self.metadata, metadata_fd)

def save(self):
"""
Expand Down Expand Up @@ -1203,6 +1201,10 @@ def virt_conf(
net_specs=conf['nets'],
)

self._metadata = {
'lago_version': pkg_resources.get_distribution("lago").version,
}

if do_bootstrap:
self.virt_env.bootstrap()

Expand Down
57 changes: 41 additions & 16 deletions lago/providers/libvirt/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,15 @@


class Network(object):
def __init__(self, env, spec):
def __init__(self, env, spec, compat):
self._env = env
libvirt_url = config.get('libvirt_url')
self.libvirt_con = libvirt_utils.get_libvirt_connection(
name=env.uuid + libvirt_url,
libvirt_url=libvirt_url,
)
self._spec = spec
self.compat = compat

def name(self):
return self._spec['name']
Expand Down Expand Up @@ -142,8 +143,8 @@ def _generate_dns_disable(self):
dns = ET.Element('dns', enable='no')
return dns

def _generate_main_dns(self, records, domain, subnet):
dns = ET.Element('dns', forwardPlainNames='no')
def _generate_main_dns(self, records, subnet, forward_plain='no'):
dns = ET.Element('dns', forwardPlainNames=forward_plain)
reverse_records = defaultdict(list)
ipv6_prefix = self._ipv6_prefix(subnet=subnet)
for hostname, ip in records.iteritems():
Expand Down Expand Up @@ -227,24 +228,48 @@ def make_ipv4(last):
)
)

if self.is_management():
domain_xml = ET.Element(
'domain', name=self._spec['dns_domain_name'], localOnly='yes'
)
net_xml.append(domain_xml)
net_xml.append(
self._generate_main_dns(
self._spec['dns_records'], self._spec['dns_domain_name'],
subnet
if utils.ver_cmp(self.compat, '0.36.11') >= 0:
if self.is_management():
domain_xml = ET.Element(
'domain',
name=self._spec['dns_domain_name'],
localOnly='yes'
)
)
net_xml.append(domain_xml)
net_xml.append(
self._generate_main_dns(self._spec['dns_records'], subnet)
)
else:
if self.libvirt_con.getLibVersion() < 2002000:
net_xml.append(
self._generate_dns_forward(self._spec['dns_forward'])
)
else:
net_xml.append(self._generate_dns_disable())
else:
if self.libvirt_con.getLibVersion() < 2002000:
LOGGER.debug(
'Generating network XML with compatibility prior to %s',
self.compat
)
# Prior to v0.37, DNS records were only the mappings of the
# management network.
if self.is_management():
if 'dns_domain_name' in self._spec:
domain_xml = ET.Element(
'domain',
name=self._spec['dns_domain_name'],
localOnly='yes'
)
net_xml.append(domain_xml)

net_xml.append(
self._generate_dns_forward(self._spec['dns_forward'])
self._generate_main_dns(
self._spec['mapping'], subnet, forward_plain='yes'
)
)
else:
net_xml.append(self._generate_dns_disable())
dns = ET.Element('dns', forwardPlainNames='yes', enable='yes')
net_xml.append(dns)

LOGGER.debug(
'Generated Network XML\n {0}'.
Expand Down
19 changes: 19 additions & 0 deletions lago/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import textwrap
import time
import yaml
import pkg_resources
from io import StringIO
import lockfile
import argparse
Expand Down Expand Up @@ -773,6 +774,24 @@ def remove_key(path, spec):
raise


def ver_cmp(ver1, ver2):
"""
Compare lago versions
Args:
ver1(str): version string
ver2(str): version string
Returns:
Return negative if ver1<ver2, zero if ver1==ver2, positive if
ver1>ver2.
"""

return cmp(
pkg_resources.parse_version(ver1), pkg_resources.parse_version(ver2)
)


class LagoException(Exception):
pass

Expand Down
9 changes: 8 additions & 1 deletion lago/virt.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ def _create_net(self, net_spec):
cls = NATNetwork
elif net_spec['type'] == 'bridge':
cls = BridgeNetwork
return cls(self, net_spec)
return cls(self, net_spec, compat=self.get_compat())

def _create_vm(self, vm_spec):
default_vm_type = config.get('default_vm_type')
Expand Down Expand Up @@ -511,3 +511,10 @@ def get_snapshots(self, domains=None):
snapshots[vm_name] = vm._spec['snapshots']

return snapshots

def get_compat(self):
"""Get compatibility level for this environment - which is the Lago
version used to create this environment """
# Prior to version 0.37.0, the version which the environment was
# initialized in was not saved, so we default to 0.36.
return self.prefix.metadata.get('lago_version', '0.36.0')

0 comments on commit 2e33c9d

Please sign in to comment.