[elbe-devel] [PATCH] elbepack: implement "elbe initvm destroy"

Thomas Weißschuh thomas.weissschuh at linutronix.de
Thu Jun 20 15:48:08 CEST 2024


Provide a command to delete the initvm and all its data.
This is encapsulates the specific commands to use.

It is useful to recreate the initvm after elbe updates.

Signed-off-by: Thomas Weißschuh <thomas.weissschuh at linutronix.de>
---
 docs/elbe-initvm.rst                      |  4 ++++
 elbepack/initvmaction.py                  | 17 +++++++++++++----
 elbepack/tests/test_xml.py                |  7 +------
 newsfragments/+initvm_destroy.feature.rst |  1 +
 4 files changed, 19 insertions(+), 10 deletions(-)

diff --git a/docs/elbe-initvm.rst b/docs/elbe-initvm.rst
index f0d8209a5c30..af219d2de035 100644
--- a/docs/elbe-initvm.rst
+++ b/docs/elbe-initvm.rst
@@ -19,6 +19,7 @@ SYNOPSIS
       elbe initvm [options] start
       elbe initvm [options] stop
       elbe initvm [options] ensure
+      elbe initvm [options] destroy
 
 DESCRIPTION
 ===========
@@ -126,6 +127,9 @@ COMMANDS
 *ensure*
    Make sure an initvm is running in the Background.
 
+*destroy*
+   Clean up resources used by the initvm. initvm should be stopped before.
+
 Examples
 ========
 
diff --git a/elbepack/initvmaction.py b/elbepack/initvmaction.py
index d15f47f02c18..a3eed12b550c 100644
--- a/elbepack/initvmaction.py
+++ b/elbepack/initvmaction.py
@@ -379,6 +379,17 @@ class StopAction(InitVMAction):
         print('\nInitvm shutoff')
 
 
+ at InitVMAction.register('destroy')
+class DestroyAction(InitVMAction):
+
+    def execute(self, initvmdir, opt, _args):
+        if not opt.qemu_mode:
+            self.initvm.destroy()
+            self.initvm.undefine()
+
+        shutil.rmtree(initvmdir)
+
+
 @InitVMAction.register('attach')
 class AttachAction(InitVMAction):
 
@@ -675,8 +686,7 @@ class CreateAction(InitVMAction):
         if self.initvm is not None and not opt.qemu_mode:
             print(f"Initvm is already defined for the libvirt domain '{cfg['initvm_domain']}'.\n")
             print('If you want to build in your old initvm, use `elbe initvm submit <xml>`.')
-            print('If you want to remove your old initvm from libvirt '
-                  f"run `virsh --connect qemu:///system undefine {cfg['initvm_domain']}`.\n")
+            print('If you want to remove your old initvm from libvirt run `elbe initvm destroy`.\n')
             print('You can specify another libvirt domain by setting the '
                   'ELBE_INITVM_DOMAIN environment variable to an unused domain name.\n')
             print('Note:')
@@ -767,8 +777,7 @@ class CreateAction(InitVMAction):
                 self.conn.defineXML(xml)
             except subprocess.CalledProcessError:
                 print('Registering initvm in libvirt failed', file=sys.stderr)
-                print(f"Try `virsh --connect qemu:///system undefine {cfg['initvm_domain']}`"
-                      'to delete existing initvm',
+                print('Try `elbe initvm destroy` to delete existing initvm',
                       file=sys.stderr)
                 sys.exit(146)
 
diff --git a/elbepack/tests/test_xml.py b/elbepack/tests/test_xml.py
index 413c71735ea9..a305234930b2 100644
--- a/elbepack/tests/test_xml.py
+++ b/elbepack/tests/test_xml.py
@@ -22,12 +22,7 @@ def initvm(tmp_path_factory, request):
 
     yield
 
-    subprocess.run([
-        'virsh', '--connect', 'qemu:///system', 'undefine', 'initvm',
-    ])
-    subprocess.run([
-        'virsh', '--connect', 'qemu:///system', 'destroy', 'initvm',
-    ])
+    run_elbe(['initvm', 'destroy', '--directory', initvm_dir])
 
 
 def _delete_project(uuid):
diff --git a/newsfragments/+initvm_destroy.feature.rst b/newsfragments/+initvm_destroy.feature.rst
new file mode 100644
index 000000000000..ed0790572c90
--- /dev/null
+++ b/newsfragments/+initvm_destroy.feature.rst
@@ -0,0 +1 @@
+Implement `elbe initvm destroy`.

---
base-commit: 6706783bfad7009801bfd404e7884651d00caab2
change-id: 20240620-initvm-destroy-148e86b83aea

Best regards,
-- 
Thomas Weißschuh <thomas.weissschuh at linutronix.de>



More information about the elbe-devel mailing list