implement Bootstrap.run to do bootstrap stuff
authorGui Chen <gui.chen@intel.com>
Thu, 16 Aug 2012 06:29:28 +0000 (14:29 +0800)
committerGui Chen <gui.chen@intel.com>
Thu, 16 Aug 2012 13:32:55 +0000 (21:32 +0800)
Signed-off-by: Gui Chen <gui.chen@intel.com>
mic/bootstrap.py
mic/rt_util.py

index e4b7950..ab98ebf 100644 (file)
@@ -19,11 +19,12 @@ from __future__ import with_statement
 import os
 import sys
 import shutil
+import subprocess
 import rpm
 from mic import msger
-from mic.utils import errors, runner
-from mic.utils.misc import get_package
+from mic.utils import errors, proxy, misc
 from mic.utils.rpmmisc import readRpmHeader, RPMInstallCallback
+from mic.chroot import setup_chrootenv, cleanup_chrootenv
 
 RPMTRANS_FLAGS = [rpm.RPMTRANS_FLAG_ALLFILES,
                   rpm.RPMTRANS_FLAG_NOSCRIPTS,
@@ -80,7 +81,7 @@ class MiniBackend(object):
         nonexist = []
         for pkg in self.dlpkgs:
             try:
-                localpth = get_package(pkg, self.repomd, None)
+                localpth = misc.get_package(pkg, self.repomd, None)
                 if not localpth:
                     # skip non-existent rpm
                     nonexist.append(pkg)
@@ -143,9 +144,31 @@ class Bootstrap(object):
             tzdist = self._path('/etc/%s-release' % self.distro)
             if not os.path.exists(tzdist):
                 with open(tzdist, 'w') as wf:
-                    wf.wrtie("bootstrap")
-        except:
-            raise errors.BootstrapError("Failed to create bootstrap")
+                    wf.write("bootstrap")
+
+        except (OSError, IOError, errors.CreatorError), err:
+            raise errors.BootstrapError("%s" % err)
+
+    def run(self, cmd, chdir, bindmounts=None):
+        def mychroot():
+            os.chroot(self.rootdir)
+            os.chdir(chdir)
+
+        if isinstance(cmd, list):
+            shell = False
+        else:
+            shell = True
+
+        gloablmounts = None
+        try:
+            proxy.set_proxy_environ()
+            gloablmounts = setup_chrootenv(self.rootdir, bindmounts)
+            subprocess.call(cmd, preexec_fn = mychroot, shell=shell)
+        except (OSError, IOError), err:
+            raise RuntimeError(err)
+        finally:
+            cleanup_chrootenv(self.rootdir, bindmounts, gloablmounts)
+            proxy.unset_proxy_environ()
 
     def cleanup(self):
         try:
index bbfb526..80b3f43 100644 (file)
@@ -54,30 +54,27 @@ def bootstrap_mic(argv=None):
     pkglist = bsopts[distro]
     cwd = os.getcwd()
 
-    # create bootstrap
-    bsenv = bootstrap.Bootstrap(rootdir)
+    # create bootstrap and run mic in bootstrap
+    bsenv = bootstrap.Bootstrap(rootdir, distro)
     try:
+        msger.info("Creating %s bootstrap ..." % distro)
         bsenv.create(cropts['repomd'], pkglist)
         sync_mic(rootdir)
+
+        msger.info("Start mic in bootstrap: %s\n" % rootdir)
+        bindmounts = get_bindmounts(cropts)
+        bsenv.run(argv, cwd, bindmounts)
+
     except errors.BootstrapError, err:
-        bsenv.cleanup()
         msger.warning('\n%s' % err)
-        msger.info("Use native running for failure to create bootstrap")
-        return
-
-    # run mic in bootstrap
-    globalmounts = None
-    bindmounts = get_bindmounts(cropts)
-    msger.info("Start mic in bootstrap: %s\n" % rootdir)
-    try:
-        proxy.set_proxy_environ()
-        globalmounts = setup_chrootenv(rootdir, bindmounts)
-        subprocess.call(argv, preexec_fn=mychroot)
-    except (OSError, IOError), err:
-        raise errors.BootstrapError("Failed to run mic in bootstrap: %s" % err)
+        if msger.ask("Switch to native mode and continue?"):
+            return
+        else:
+            raise errors.BootstrapError("Failed to create bootstrap: %s" % err)
+    except RuntimeError, err:
+        raise errors.BootstrapError("Failed to create bootstrap: %s" % err)
     finally:
-        cleanup_chrootenv(rootdir, bindmounts, globalmounts)
-        proxy.unset_proxy_environ()
+        bsenv.cleanup()
 
     sys.exit(0)