def run(self, cmd, chdir, rootdir=None, bindmounts=None):
        def mychroot():
            os.chroot(rootdir)
            os.chdir(chdir)

        def sync_timesetting(rootdir):
            try:
                # sync time and zone info to bootstrap
                if os.path.exists(rootdir + "/etc/localtime"):
                    os.unlink(rootdir + "/etc/localtime")
                shutil.copyfile("/etc/localtime", rootdir + "/etc/localtime")
            except:
                pass

        def sync_passwdfile(rootdir):
            try:
                # sync passwd file to bootstrap, saving the user info
                if os.path.exists(rootdir + "/etc/passwd"):
                    os.unlink(rootdir + "/etc/passwd")
                shutil.copyfile("/etc/passwd", rootdir + "/etc/passwd")
            except:
                pass

        if not rootdir:
            rootdir = self.rootdir

        if isinstance(cmd, list):
            shell = False
        else:
            shell = True

        env = os.environ
        env['PATH'] = "%s:%s" % (PATH_BOOTSTRAP, env['PATH'])

        retcode = 0
        gloablmounts = None
        try:
            proxy.set_proxy_environ()
            gloablmounts = setup_chrootenv(rootdir, bindmounts, False)
            sync_timesetting(rootdir)
            sync_passwdfile(rootdir)
            retcode = subprocess.call(cmd,
                                      preexec_fn=mychroot,
                                      env=env,
                                      shell=shell)
        except (OSError, IOError):
            # add additional information to original exception
            value, tb = sys.exc_info()[1:]
            value = '%s: %s' % (value, ' '.join(cmd))
            raise RuntimeError, value, tb
        finally:
            if self.logfile and os.path.isfile(self.logfile):
                msger.log(file(self.logfile).read())
            cleanup_chrootenv(rootdir, bindmounts, gloablmounts)
            proxy.unset_proxy_environ()
        return retcode
Ejemplo n.º 2
0
    def run(self, cmd, chdir, rootdir=None, bindmounts=None):
        def mychroot():
            os.chroot(rootdir)
            os.chdir(chdir)

        def sync_timesetting(rootdir):
            try:
                # sync time and zone info to bootstrap
                if os.path.exists(rootdir + "/etc/localtime"):
                    os.unlink(rootdir + "/etc/localtime")
                shutil.copyfile("/etc/localtime", rootdir + "/etc/localtime")
            except:
                pass

        def sync_passwdfile(rootdir):
            try:
                # sync passwd file to bootstrap, saving the user info
                if os.path.exists(rootdir + "/etc/passwd"):
                    os.unlink(rootdir + "/etc/passwd")
                shutil.copyfile("/etc/passwd", rootdir + "/etc/passwd")
            except:
                pass

        if not rootdir:
            rootdir = self.rootdir

        if isinstance(cmd, list):
            shell = False
        else:
            shell = True

        env = os.environ
        env['PATH'] = "%s:%s" % (PATH_BOOTSTRAP, env['PATH'])

        retcode = 0
        gloablmounts = None
        try:
            proxy.set_proxy_environ()
            gloablmounts = setup_chrootenv(rootdir, bindmounts)
            sync_timesetting(rootdir)
            sync_passwdfile(rootdir)
            retcode = subprocess.call(cmd, preexec_fn=mychroot, env=env, shell=shell)
        except (OSError, IOError):
            # add additional information to original exception
            value, tb = sys.exc_info()[1:]
            value = '%s: %s' % (value, ' '.join(cmd))
            raise RuntimeError, value, tb
        finally:
            #if self.logfile and os.path.isfile(self.logfile):
            #    msger.log(file(self.logfile).read())
            cleanup_chrootenv(rootdir, bindmounts, gloablmounts)
            proxy.unset_proxy_environ()
        return retcode
Ejemplo n.º 3
0
    def run(self, name, cmd, chdir='/', bindmounts=None):
        self.rootdir = name
        def mychroot():
            os.chroot(self.rootdir)
            os.chdir(chdir)

        if isinstance(cmd, list):
            cmd = ' '.join(cmd)

        lvl = msger.get_loglevel()
        msger.set_loglevel('quiet')
        globalmounts = chroot.setup_chrootenv(self.rootdir, bindmounts)
        try:
            proxy.set_proxy_environ()
            subprocess.call(cmd, preexec_fn=mychroot, shell=True)
            proxy.unset_proxy_environ()
        except:
            raise errors.BootstrapError("Run in bootstrap fail")
        finally:
            chroot.cleanup_chrootenv(self.rootdir, bindmounts, globalmounts)

        msger.set_loglevel(lvl)
Ejemplo n.º 4
0
            shell = True

        env = os.environ
        env['PATH'] = "%s:%s" % (PATH_BOOTSTRAP, env['PATH'])

        retcode = 0
        gloablmounts = None
        try:
            proxy.set_proxy_environ()
            gloablmounts = setup_chrootenv(rootdir, bindmounts, False)
            sync_timesetting(rootdir)
            sync_passwdfile(rootdir)
            retcode = subprocess.call(cmd, preexec_fn=mychroot, env=env, shell=shell)
        except (OSError, IOError), err:
            raise RuntimeError(err)
        finally:
            if self.logfile and os.path.isfile(self.logfile):
                msger.log(file(self.logfile).read())
            cleanup_chrootenv(rootdir, bindmounts, gloablmounts)
            proxy.unset_proxy_environ()
        return retcode

    def cleanup(self):
        try:
            # clean mounts
            cleanup_mounts(self.rootdir)
            # remove rootdir
            shutil.rmtree(self.rootdir, ignore_errors=True)
        except:
            pass
Ejemplo n.º 5
0
    def run(self, cmd, chdir, rootdir=None, bindmounts=None):
        def mychroot():
            os.chroot(rootdir)
            os.chdir(chdir)

        def sync_timesetting(rootdir):
            try:
                # sync time and zone info to bootstrap
                if os.path.exists(rootdir + "/etc/localtime"):
                    os.unlink(rootdir + "/etc/localtime")
                shutil.copyfile("/etc/localtime", rootdir + "/etc/localtime")
            except:
                pass

        def sync_passwdfile(rootdir):
            try:
                # sync passwd file to bootstrap, saving the user info
                if os.path.exists(rootdir + "/etc/passwd"):
                    os.unlink(rootdir + "/etc/passwd")
                shutil.copyfile("/etc/passwd", rootdir + "/etc/passwd")
            except:
                pass

        def sync_hostfile(rootdir):
            try:
                # sync host info to bootstrap
                if os.path.exists(rootdir + "/etc/hosts"):
                    os.unlink(rootdir + "/etc/hosts")
                shutil.copyfile("/etc/hosts", rootdir + "/etc/hosts")
            except:
                pass

        def sync_signfile(rootdir,homedir):
            if os.path.exists(homedir + "/.sign"):
                signfile = rootdir + homedir + "/.sign"
                try:
                    # sync sign info to bootstrap
                    if os.path.exists(signfile):
                        if os.path.isdir(signfile):
                            shutil.rmtree(signfile)
                        else:
                            os.unlink(signfile)
                    try:
                        shutil.copytree(homedir + "/.sign", signfile)
                    except OSError as exc:
                        if exc.errno == errno.ENOTDIR:
                            shutil.copy(homedir + "/.sign", signfile)
                        else: raise
                except:
                    pass

        if not rootdir:
            rootdir = self.rootdir

        if isinstance(cmd, list):
            shell = False
        else:
            shell = True

        env = os.environ
        env['PATH'] = "%s:%s" % (PATH_BOOTSTRAP, env['PATH'])
        cropts = configmgr.create
        if cropts["ks"]:
            lang = cropts["ks"].handler.lang.lang
            env['LANG'] = lang
            env['LC_ALL'] = lang

        retcode = 0
        gloablmounts = None
        try:
            proxy.set_proxy_environ()
            gloablmounts = setup_chrootenv(rootdir, bindmounts)
            sync_timesetting(rootdir)
            sync_passwdfile(rootdir)
            sync_hostfile(rootdir)
            env['SUDO_USER'] = '******'
            if 'SUDO_USER' in env:
                sync_signfile(rootdir, os.path.expanduser('~' + env['SUDO_USER']))
            else:
                sync_signfile(rootdir, os.path.expanduser('~'))
            retcode = subprocess.call(cmd, preexec_fn=mychroot, env=env, shell=shell)
        except (OSError, IOError):
            # add additional information to original exception
            value, tb = sys.exc_info()[1:]
            value = '%s: %s' % (value, ' '.join(cmd))
            raise RuntimeError, value, tb
        finally:
            #if self.logfile and os.path.isfile(self.logfile):
            #    msger.log(file(self.logfile).read())
            cleanup_chrootenv(rootdir, bindmounts, gloablmounts)
            proxy.unset_proxy_environ()
        return retcode