def vmip(name): """Prints IP address of libvirt VM. If name of VM is not given as argument prints information of all running VMs """ rc, out, err = utils.execute( 'virsh list --state-running --name', can_fail=False ) if rc: click.echo(err) exit(1) for vm in out.split('\n'): vm = vm.strip() if not vm: continue if name and name != vm: continue # get VM MAC address rc, out, err = utils.execute( 'virsh dumpxml {} | grep "mac address="'.format(vm), can_fail=False ) if rc: click.echo(err) exit(1) mac = re.search( '<mac address=\'(?P<mac>[\w:]+)\'\/>', out ).group('mac') # find IP address for MAC address rc, out, err = utils.execute('arp -n', can_fail=False) if rc: click.echo(err) exit(1) for record in out.split('\n'): record = record.strip() if not record: continue record = record.split() if mac == record[2]: if not name: click.echo('{} '.format(vm), nl=False) click.echo(record[0]) break
def vmprep(regex, images, domain, steps): """Prrepares libvirt VMs for usage.=""" for img in os.listdir(os.path.abspath(images)): if not re.search(regex, img): continue click.echo('Preparing {0}'.format(img)) hostname = img.split('.', 1)[0] rc, out, err = utils.execute( 'virt-sysprep ' '--hostname "{hostname}.{domain}" ' '--enable "{steps}" ' '-a {images}/{img}', can_fail=False ) if rc: click.echo(err) exit(1)