From 9be255aee50575f8867599111a2cf714ca62c932 Mon Sep 17 00:00:00 2001 From: Lars Kellogg-Stedman Date: Fri, 4 Oct 2013 12:03:52 -0400 Subject: [PATCH] add update capability to virt-hosts --- update-virt-hosts | 8 ------- virt-hosts | 57 ++++++++++++++++++++++++++++------------------- 2 files changed, 34 insertions(+), 31 deletions(-) delete mode 100755 update-virt-hosts diff --git a/update-virt-hosts b/update-virt-hosts deleted file mode 100755 index de608ba..0000000 --- a/update-virt-hosts +++ /dev/null @@ -1,8 +0,0 @@ -#!/bin/sh - -: ${LIBVIRT_NETWORK:=${1:-default}} -: ${DNSMASQ_DOMAIN:=${LIBVIRT_NETWORK}.virt} - -virt-hosts -D $DNSMASQ_DOMAIN -N $LIBVIRT_NETWORK > /var/lib/libvirt/dnsmasq/${LIBVIRT_NETWORK}.addnhosts -killall -HUP dnsmasq - diff --git a/virt-hosts b/virt-hosts index 8ee5afd..31da096 100755 --- a/virt-hosts +++ b/virt-hosts @@ -12,8 +12,10 @@ def parse_args(): p = argparse.ArgumentParser() p.add_argument('--connect', '-c') p.add_argument('--network', '-N', default='default') - p.add_argument('--domain', '-D', default='localdomain') + p.add_argument('--domain', '-D') p.add_argument('--leases', '-L') + p.add_argument('--hosts', '-H') + p.add_argument('--update', '-u', action='store_true') return p.parse_args() def read_leases_file(path): @@ -28,38 +30,47 @@ def read_leases_file(path): def main(): args = parse_args() + if args.domain is None: + args.domain = '%s.virt' % args.network + if args.leases is None: args.leases = '/var/lib/libvirt/dnsmasq/%s.leases' % args.network + if args.hosts is None: + args.hosts = '/var/lib/libvirt/dnsmasq/%s.addnhosts' % args.network + assert os.path.exists(args.leases) - leases = read_leases_file(args.leases) + with open(args.hosts, 'w') if args.update else sys.stdout as fd: + + leases = read_leases_file(args.leases) + + con = libvirt.openReadOnly(args.connect) + assert con is not None - con = libvirt.openReadOnly(args.connect) - assert con is not None + for domId in con.listDomainsID(): + dom = con.lookupByID(domId) + desc = etree.fromstring(dom.XMLDesc()) - for domId in con.listDomainsID(): - dom = con.lookupByID(domId) - desc = etree.fromstring(dom.XMLDesc()) + primary=True + for iface in desc.xpath('/domain/devices/interface[@type="network"]'): + source = iface.find('source') - primary=True - for iface in desc.xpath('/domain/devices/interface[@type="network"]'): - source = iface.find('source') + if source.get('network') != args.network: + continue - if source.get('network') != args.network: - continue + macaddr = iface.find('mac').get('address') + alias = iface.find('alias').get('name') - macaddr = iface.find('mac').get('address') - alias = iface.find('alias').get('name') + if macaddr in leases: + ipaddr = leases[macaddr]['ipaddr'] + fd.write('%s\t%s' % ( + ipaddr, + '%s-%s.%s' % (dom.name(), alias, args.domain))) - if macaddr in leases: - ipaddr = leases[macaddr]['ipaddr'] - print '%s\t%s' % ( - ipaddr, - '%s-%s.%s' % (dom.name(), alias, args.domain)), - if primary: - print '%s.%s' % (dom.name(), args.domain), - print - primary=False + if primary: + fd.write(' %s.%s' % (dom.name(), args.domain)) + fd.write('\n') + primary=False if __name__ == '__main__': main()