Skip to content
Snippets Groups Projects
Commit 5a78eded authored by Anael Beutot's avatar Anael Beutot
Browse files

Added nic tags for VMs.

parent 9b68a4e7
No related branches found
No related tags found
No related merge requests found
...@@ -2,6 +2,7 @@ import logging ...@@ -2,6 +2,7 @@ import logging
import weakref import weakref
from StringIO import StringIO from StringIO import StringIO
from xml.etree import cElementTree as et from xml.etree import cElementTree as et
from collections import namedtuple
from ccnode.tags import Tag, tag_inspector from ccnode.tags import Tag, tag_inspector
from ccnode.hypervisor import lib as _libvirt from ccnode.hypervisor import lib as _libvirt
...@@ -12,6 +13,9 @@ from ccnode.hypervisor.domains import vm_tags ...@@ -12,6 +13,9 @@ from ccnode.hypervisor.domains import vm_tags
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
NetworkInterface = namedtuple('NetworkInterface', ('source', 'mac', 'model'))
class VirtualMachine(object): class VirtualMachine(object):
"""Represent a VM instance.""" """Represent a VM instance."""
def __init__(self, dom, hypervisor): def __init__(self, dom, hypervisor):
...@@ -39,6 +43,14 @@ class VirtualMachine(object): ...@@ -39,6 +43,14 @@ class VirtualMachine(object):
self.tags[t.name] = t self.tags[t.name] = t
i += 1 i += 1
i = 0
for nic in self.iter_nics():
for t in (
Tag('nic%s_mac' % i, nic.mac),
Tag('nic%s_source' % i, nic.source),
Tag('nic%s_model' %i, nic.model),
):
self.tags[t.name] = t
logger.debug(self.tags) logger.debug(self.tags)
...@@ -88,3 +100,30 @@ class VirtualMachine(object): ...@@ -88,3 +100,30 @@ class VirtualMachine(object):
continue continue
yield volume yield volume
@property
def nics(self):
return list(self.iter_nics())
def iter_nics(self):
for nic in et.ElementTree().parse(
StringIO(self.lv_dom.XMLDesc(0))
).findall('devices/interface'):
if nic.get('type') == 'bridge':
try:
mac = nic.find('mac').get('address')
except AttributeError:
mac = None
try:
model = nic.find('model').get('type')
except AttributeError:
model = None
try:
source = nic.find('source').get('bridge')
except AttributeError:
source = None
yield NetworkInterface(
mac=mac,
source=source,
model=model,
)
...@@ -69,8 +69,9 @@ status.ttl = 1 ...@@ -69,8 +69,9 @@ status.ttl = 1
def disk(dom): def disk(dom):
"""Get backend disks.""" """Get backend disks."""
return u' '.join(map(str, xrange(len(dom.disks)))) return u' '.join(map(str, xrange(len(dom.disks)))) or None
def nic_(): def nic(dom):
pass """VM network interfaces."""
return u' '.join(map(str, xrange(len(dom.nics)))) or None
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment