Skip to content

Commit

Permalink
add update capability to virt-hosts
Browse files Browse the repository at this point in the history
  • Loading branch information
larsks committed Oct 4, 2013
1 parent d4b0078 commit 9be255a
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 31 deletions.
8 changes: 0 additions & 8 deletions update-virt-hosts

This file was deleted.

57 changes: 34 additions & 23 deletions virt-hosts
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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()
Expand Down

0 comments on commit 9be255a

Please sign in to comment.