[virt-tools-list] [virt-manager] [PATCH 4/9] create: Call virt-bootstrap asynchronously

Radostin Stoyanov rstoyanov1 at gmail.com
Thu Jun 22 14:54:01 UTC 2017


---
 virtManager/create.py | 74 +++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 74 insertions(+)

diff --git a/virtManager/create.py b/virtManager/create.py
index 658135c..4bedcfe 100644
--- a/virtManager/create.py
+++ b/virtManager/create.py
@@ -22,6 +22,7 @@ import logging
 import threading
 import time
 import subprocess
+from os.path import exists, isdir
 try:
     import commands  # Python2 only
 except Exception:
@@ -1963,6 +1964,26 @@ class vmmCreate(vmmGObjectUI):
             if not fs:
                 return self.err.val_err(_("An OS directory path is required."))
 
+            if self.widget("install-oscontainer-bootstrap").get_active():
+
+                src_url = self.widget("install-oscontainer-source-url-entry"
+                ).get_text().strip()
+                user = self.widget("install-oscontainer-source-user"
+                ).get_text().strip()
+                passwd = self.widget("install-oscontainer-source-passwd"
+                ).get_text()
+
+                # Check if the source path was provided
+                if not src_url:
+                    return self.err.val_err(_("Source URL is required"))
+
+                # Show error if destination path exist and is not folder
+                if exists(fs) and not isdir(fs):
+                    return self.err.val_err(_("Path already exist and"
+                                              " it is not directory: " + fs))
+                # Start container bootstrap
+                self._container_image_bootstrap(src_url, fs, user, passwd)
+
         elif instmethod == INSTALL_PAGE_VZ_TEMPLATE:
             instclass = virtinst.ContainerInstaller
             template = self.widget("install-container-template").get_text()
@@ -2517,3 +2538,56 @@ class vmmCreate(vmmGObjectUI):
             self.err.show_err(_("Error continue install: %s") % str(e))
 
         return True
+
+    def _create_directory_tree(self, asyncjob, src, dest, user, passwd):
+        """
+        Call virt-bootstrap and wait until exitCode is returned
+        """
+
+        cmd = ["virt-bootstrap", src, dest]
+        if user:
+            cmd += ["--username", user]
+        if passwd:
+            cmd += ["--password", passwd]
+
+        logging.debug("Start asyncjob job: %s", " ".join(cmd))
+
+        bootstrap_proc = subprocess.Popen(cmd,
+                                          stdout=subprocess.PIPE,
+                                          stderr=subprocess.PIPE)
+
+        stdout, stderr = bootstrap_proc.communicate()
+
+        if bootstrap_proc.returncode != 0:
+            asyncjob.set_error("virt-bootstrap did not complete successfully",
+                               "{}\n{}".format(stdout, stderr))
+
+    def _container_image_bootstrap(self, src, dest,
+                                   src_auth_user=None,
+                                   src_auth_passwd=None):
+        """
+        Launch virt-bootstrap as async job
+        """
+
+        params = [src, dest, src_auth_user, src_auth_passwd]
+
+        logging.debug("Start asynchronous job to bootstrap container.")
+
+        parentobj = self._customize_window or self
+        progWin = vmmAsyncJob(self._create_directory_tree, params,
+                              self._container_bootstrap_finished_cb,
+                              [parentobj],
+                              _("Bootstrap Container"),
+                              _("The process of download and untar the "
+                                "container image may take a few minutes "
+                                "to complete."),
+                              parentobj.topwin)
+        progWin.run()
+
+    def _container_bootstrap_finished_cb(self, error, details, parentobj):
+        """
+        Thread callback - See self._container_image_bootstrap()
+        """
+        if error:
+            parentobj.err.show_err(_("The container bootstrap did not finish "
+                                     "successfully."), details=error + details)
-- 
2.9.4




More information about the virt-tools-list mailing list