chiark / gitweb /
buildserver: make --clean destroy reliably
authorHans-Christoph Steiner <hans@eds.org>
Tue, 27 Sep 2016 06:26:33 +0000 (02:26 -0400)
committerHans-Christoph Steiner <hans@eds.org>
Tue, 23 May 2017 18:04:08 +0000 (20:04 +0200)
This prevents v.destroy() from running if Vagrantfile.yaml does not exist,
since that is required for vagrant to run: is the core config including the
name of the box, etc.  Otherwise, it would exit with an error.

This also does complete cleanup when using libvirt.

makebuildserver

index 48828925f04b509daa6b9a3e8d3d987e3b44050e..158d718c102110a853adc0a3ab9e05286d8e6862 100755 (executable)
@@ -291,6 +291,32 @@ def sha256_for_file(path):
         return s.hexdigest()
 
 
+def destroy_current_image(v, serverdir):
+    global config
+
+    # cannot run vagrant without the config in the YAML file
+    if os.path.exists(os.path.join(serverdir, 'Vagrantfile.yaml')):
+        v.destroy()
+    elif options.verbose:
+        print('Cannot run destroy vagrant setup since Vagrantfile.yaml is not setup!')
+    if config['vm_provider'] == 'libvirt':
+        import libvirt
+        try:
+            domain = 'buildserver_default'
+            virConnect = libvirt.open('qemu:///system')
+            virDomain = virConnect.lookupByName(domain)
+            if virDomain:
+                virDomain.undefineFlags(libvirt.VIR_DOMAIN_UNDEFINE_MANAGED_SAVE
+                                        | libvirt.VIR_DOMAIN_UNDEFINE_SNAPSHOTS_METADATA
+                                        | libvirt.VIR_DOMAIN_UNDEFINE_NVRAM)
+            storagePool = virConnect.storagePoolLookupByName('default')
+            if storagePool:
+                for vol in storagePool.listAllVolumes():
+                    vol.delete()
+        except libvirt.libvirtError as e:
+            print(e)
+
+
 def run_via_vagrant_ssh(v, cmdlist):
     if (isinstance(cmdlist, str) or isinstance(cmdlist, bytes)):
         cmd = cmdlist
@@ -383,10 +409,7 @@ def main():
         tail.start()
 
     if options.clean:
-        v.destroy()
-        if config['vm_provider'] == 'libvirt':
-            subprocess.call(['virsh', 'undefine', 'buildserver_default'])
-            subprocess.call(['virsh', 'vol-delete', '/var/lib/libvirt/images/buildserver_default.img'])
+        destroy_current_image(v, serverdir)
 
     # Check against the existing Vagrantfile.yaml, and if they differ, we
     # need to create a new box:
@@ -399,7 +422,7 @@ def main():
             oldconfig = yaml.load(f)
         if config != oldconfig:
             print("Server configuration has changed, rebuild from scratch is required")
-            v.destroy()
+            destroy_current_image(v, serverdir)
         else:
             print("Re-provisioning existing server")
             writevf = False