#!/usr/bin/env python

# Copyright 2016 Mirantis
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from __future__ import print_function
import sys

import libvirt


def main():
    conn = libvirt.open("qemu:///system")
    domains = conn.listAllDomains()

    print("Cleaning up VMs")

    for domain in domains:
        domain_name = domain.name()

        print("Destroying VM", domain_name)
        if domain.destroy() < 0:
            sys.stderr.write("Failed to destroy VM %s\n" % domain_name)
            sys.exit(1)

        print("Undefining VM", domain_name)
        if domain.undefine() < 0:
            sys.stderr.write("Failed to undefine VM %s\n" % domain_name)
            sys.exit(1)

    print("All VMs cleaned")

    pool = conn.storagePoolLookupByName("default")
    volumes = pool.listAllVolumes()

    print("Cleaning up volumes")

    for volume in volumes:
        volume_name = volume.name()

        print("Deleting volume", volume_name)
        if volume.delete() < 0:
            sys.stderr.write("Failed to remove volume %s\n" % volume_name)
            sys.exit(1)

    print("All volumes cleaned")


if __name__ == "__main__":
    main()

@@ -637,16 +637,33 @@
 port sys
+%0Aimport traceback
 %0A%0Aimport
@@ -681,251 +681,358 @@
 def 
-main():%0A    conn = libvirt.open(%22qemu:///system%22)%0A
+destroy_domain(domain):%0A    if domain.state() != libvirt.VIR_DOMAIN_RUNNING:%0A        return%0A%0A    try:%0A   
     
+ 
 domain
-s = conn.listAllDomains()%0A%0A    print(%22Cleaning up VMs%22)%0A%0A    for domain in domains:%0A        domain_name = domain.name()%0A%0A        print(%22Destroying VM%22, domain_name)%0A    
+.destroy()%0A    except libvirt.libvirtError:%0A        sys.stderr.write(%22Failed to destroy VM %25s%5Cn%22 %25 domain.name())%0A        traceback.print_exc(file=sys.stderr)%0A        sys.exit(1)%0A%0A%0Adef undefine_domain(domain):%0A    try:%0A
     
-if
+   
  domain.
 dest
@@ -1031,27 +1031,52 @@
 ain.
-destroy() %3C 0:%0A    
+undefine()%0A    except libvirt.libvirtError:%0A
     
@@ -1099,39 +1099,40 @@
 rite(%22Failed to 
-destroy
+undefine
  VM %25s%5Cn%22 %25 doma
@@ -1125,37 +1125,39 @@
 M %25s%5Cn%22 %25 domain
-_
+.
 name
+()
 )%0A            sy
@@ -1154,174 +1154,337 @@
     
-    sys.exit(1)%0A%0A        print(%22Undefining VM%22, domain_name)%0A        if domain.undefine() %3C 0:%0A            sys.stderr.write(%22Failed to undefine VM %25s%5Cn%22 %25
+traceback.print_exc(file=sys.stderr)%0A        sys.exit(1)%0A%0A%0Adef main():%0A    conn = libvirt.open(%22qemu:///system%22)%0A    domains = conn.listAllDomains()%0A%0A    print(%22Cleaning up VMs%22)%0A%0A    for domain in domains:%0A        print(%22Destroying VM%22, domain.name())%0A        destroy_domain(domain)%0A%0A        print(%22Undefining VM%22,
  domain
-_
+.
 name
+()
 )%0A  
@@ -1485,38 +1485,46 @@
 ())%0A        
-    sys.exit(1
+undefine_domain(domain
 )%0A%0A    print

Check for domain status before destroying it