Skip to content

Implement dynamic IP address fetching #628

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
15 changes: 13 additions & 2 deletions vrtManager/instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
import os.path
try:
from libvirt import libvirtError, VIR_DOMAIN_XML_SECURE, VIR_MIGRATE_LIVE, \
VIR_MIGRATE_UNSAFE, VIR_DOMAIN_UNDEFINE_SNAPSHOTS_METADATA
VIR_DOMAIN_INTERFACE_ADDRESSES_SRC_LEASE, VIR_MIGRATE_UNSAFE, \
VIR_DOMAIN_UNDEFINE_SNAPSHOTS_METADATA
except:
from libvirt import libvirtError, VIR_DOMAIN_XML_SECURE, VIR_MIGRATE_LIVE
from libvirt import libvirtError, VIR_DOMAIN_XML_SECURE, VIR_MIGRATE_LIVE, \
VIR_DOMAIN_INTERFACE_ADDRESSES_SRC_LEASE
from vrtManager import util
from xml.etree import ElementTree
from datetime import datetime
Expand Down Expand Up @@ -182,6 +184,7 @@ def fixed(ctx):

def networks(ctx):
result = []
ifaddrs = self.instance.interfaceAddresses(VIR_DOMAIN_INTERFACE_ADDRESSES_SRC_LEASE)
for net in ctx.xpathEval('/domain/devices/interface'):
mac_host = net.xpathEval('mac/@address')[0].content
nic_host = net.xpathEval('source/@network|source/@bridge|source/@dev')[0].content
Expand All @@ -190,6 +193,14 @@ def networks(ctx):
ip = get_mac_ipaddr(net, mac_host)
except:
ip = None

if not ip:
# Try to get the IP address from domifaddrs
for iface in ifaddrs.values():
if iface.get('hwaddr') == mac_host:
ip = ', '.join(['%s/%d' % (a['addr'], a['prefix']) for a in iface.get('addrs', [])])
break

result.append({'mac': mac_host, 'nic': nic_host, 'ip': ip})
return result

Expand Down