raise exception if vm doesnt have any networks
parent
d73dc5f692
commit
2878ede2d6
|
@ -1,3 +1,4 @@
|
||||||
|
from onedns import exception
|
||||||
from onedns.clients import one
|
from onedns.clients import one
|
||||||
from onedns.clients import skydns
|
from onedns.clients import skydns
|
||||||
from onedns.logger import log
|
from onedns.logger import log
|
||||||
|
@ -13,10 +14,12 @@ class OneDNS(object):
|
||||||
self._one = one.OneClient(**one_kwargs)
|
self._one = one.OneClient(**one_kwargs)
|
||||||
self._skydns = skydns.SkyDNSClient(domain, etcd_kwargs=etcd_kwargs)
|
self._skydns = skydns.SkyDNSClient(domain, etcd_kwargs=etcd_kwargs)
|
||||||
|
|
||||||
|
def _check_for_networks(self, vm):
|
||||||
|
if not hasattr(vm.template, 'nics'):
|
||||||
|
raise exception.NoNetworksError(vm)
|
||||||
|
|
||||||
def _get_vm_dns_entries(self, vm):
|
def _get_vm_dns_entries(self, vm):
|
||||||
entries = {}
|
entries = {}
|
||||||
if not hasattr(vm.template, 'nics'):
|
|
||||||
return entries
|
|
||||||
hostname = vm.name
|
hostname = vm.name
|
||||||
primary_ip = vm.template.nics[0].ip
|
primary_ip = vm.template.nics[0].ip
|
||||||
entries[hostname] = primary_ip
|
entries[hostname] = primary_ip
|
||||||
|
@ -27,21 +30,15 @@ class OneDNS(object):
|
||||||
return entries
|
return entries
|
||||||
|
|
||||||
def add_vm(self, vm):
|
def add_vm(self, vm):
|
||||||
|
self._check_for_networks(vm)
|
||||||
dns_entries = self._get_vm_dns_entries(vm)
|
dns_entries = self._get_vm_dns_entries(vm)
|
||||||
if not dns_entries:
|
|
||||||
log.warn("No networks found for VM {id}: {vm} - skipping".format(
|
|
||||||
vm=vm.name, id=vm.id))
|
|
||||||
return
|
|
||||||
log.info("Adding VM {id}: {vm}".format(id=vm.id, vm=vm.name))
|
log.info("Adding VM {id}: {vm}".format(id=vm.id, vm=vm.name))
|
||||||
for name, ip in dns_entries.items():
|
for name, ip in dns_entries.items():
|
||||||
self._skydns.add_host(name, ip)
|
self._skydns.add_host(name, ip)
|
||||||
|
|
||||||
def remove_vm(self, vm):
|
def remove_vm(self, vm):
|
||||||
|
self._check_for_networks(vm)
|
||||||
dns_entries = self._get_vm_dns_entries(vm)
|
dns_entries = self._get_vm_dns_entries(vm)
|
||||||
if not dns_entries:
|
|
||||||
log.warn("No networks found for VM {id}: {vm} - skipping".format(
|
|
||||||
vm=vm.name, id=vm.id))
|
|
||||||
return
|
|
||||||
log.info("Removing VM {id}: {vm}".format(id=vm.id, vm=vm.name))
|
log.info("Removing VM {id}: {vm}".format(id=vm.id, vm=vm.name))
|
||||||
for name, ip in dns_entries.items():
|
for name, ip in dns_entries.items():
|
||||||
self._skydns.remove_host(name, ip)
|
self._skydns.remove_host(name, ip)
|
||||||
|
@ -56,4 +53,7 @@ class OneDNS(object):
|
||||||
|
|
||||||
def sync(self):
|
def sync(self):
|
||||||
for vm in self._one.vms():
|
for vm in self._one.vms():
|
||||||
self.add_vm(vm)
|
try:
|
||||||
|
self.add_vm(vm)
|
||||||
|
except exception.NoNetworksError as e:
|
||||||
|
e.log(warn=True)
|
||||||
|
|
|
@ -0,0 +1,30 @@
|
||||||
|
from onedns.logger import log
|
||||||
|
|
||||||
|
|
||||||
|
class OneDnsException(Exception):
|
||||||
|
def __init__(self, *args):
|
||||||
|
self.args = args
|
||||||
|
self.msg = args[0]
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
return self.msg
|
||||||
|
|
||||||
|
def explain(self):
|
||||||
|
return '%s: %s' % (self.__class__.__name__, self.msg)
|
||||||
|
|
||||||
|
def log(self, warn=False, show_tb=False):
|
||||||
|
if show_tb:
|
||||||
|
log.exception(self.explain())
|
||||||
|
elif warn:
|
||||||
|
log.warn(self.explain())
|
||||||
|
else:
|
||||||
|
log.error(self.explain())
|
||||||
|
|
||||||
|
|
||||||
|
class NoNetworksError(OneDnsException):
|
||||||
|
"""
|
||||||
|
Raised when a VM doesn't have any NICs
|
||||||
|
"""
|
||||||
|
def __init__(self, vm):
|
||||||
|
self.msg = "No networks found for VM {id}: {vm}".format(vm=vm.name,
|
||||||
|
id=vm.id)
|
Loading…
Reference in New Issue