#!/usr/bin/python3
# Restart all running libvirt domains

import libvirt
import sys
import time

conn = libvirt.open('qemu:///system')
if not conn:
    print('Failed to open connection to the hypervisor!')
    sys.exit(1)

for domain in conn.listAllDomains():
    name = domain.name()
    if domain.isActive():
        print('Restarting {}'.format(name), end='', flush=True)

        domain.shutdown()
        while domain.isActive():
            print('.', end='', flush=True)
            time.sleep(1)
        print()

        domain.create()