def __init__(self, chroot_path, environ={}): env = { 'DEBIAN_FRONTEND': 'noninteractive', 'DEBIAN_PRIORITY': 'critical' } env.update(environ) self.chroot = Chroot(chroot_path, environ=env)
def run_command(self, platform, *argv): cmd = self._wrap_cmd(self._get_command(platform, *argv)) with Chroot(self.env_config.get_chroot(platform)): self._rename_log() try: print("[%s] " % platform + " ".join(cmd)) with open(os.devnull, 'wb') as null: check_call(" ".join(cmd), stdout=null, shell=True, executable='/bin/bash') except CalledProcessError: failed_projs = self.__get_failed_projects() if not failed_projs: raise self.__failed_exception__( "%s failed. \n Error log: %s" % (" ".join(cmd), self.get_platform_log(platform))) return failed_projs
def _code_sign(self, platform): spks = self.package.spk_config.chroot_spks( self.env_config.get_chroot(platform)) if not spks: raise SignPackageError('[%s] No spk found' % platform) for spk in spks: cmd = ' php ' + PkgScripts + '/CodeSign.php --sign=/image/packages/' + os.path.basename( spk) with Chroot(self.env_config.get_chroot(platform)): if not self.check_gpg_key_exist(): raise SignPackageError( "[%s] Gpg key not exist. You can add `-S' to skip package code sign or import gpg key first." % platform) print("[%s] Sign package: " % platform + cmd) try: check_call(cmd, shell=True, executable="/bin/bash") except CalledProcessError: raise SignPackageError('Failed to create signature: ' + spk)
class Installer(object): def __init__(self, chroot_path, environ={}): env = { 'DEBIAN_FRONTEND': 'noninteractive', 'DEBIAN_PRIORITY': 'critical' } env.update(environ) self.chroot = Chroot(chroot_path, environ=env) @staticmethod def _get_packages_priority(packages): """high priority packages must be installed before regular packages APT should handle this, but in some circumstances it chokes... """ HIGH_PRIORITY = ('linux-image') high = [] regular = [] for package in packages: if package.startswith(HIGH_PRIORITY): high.append(package) else: regular.append(package) return high, regular def _install(self, packages, ignore_errors=[], extra_apt_args=[]): high, regular = self._get_packages_priority(packages) lines = [ "#!/bin/sh", "echo", "echo \"Warning: Fake invoke-rc.d called\"" ] fake_invoke_rcd = RevertibleScript( join(self.chroot.path, "usr/sbin/invoke-rc.d"), lines) lines = [ "#!/bin/sh", "echo", "echo \"Warning: Fake start-stop-daemon called\"" ] fake_start_stop = RevertibleScript( join(self.chroot.path, "sbin/start-stop-daemon"), lines) defer_log = "var/lib/update-initramfs.deferred" lines = [ "#!/bin/sh", "echo", "echo \"Warning: Deferring update-initramfs $@\"", "echo \"update-initramfs $@\" >> /%s" % defer_log ] fake_update_initramfs = RevertibleScript( join(self.chroot.path, "usr/sbin/update-initramfs"), lines) fake_initctl = RevertibleInitctl(self.chroot) for packages in (high, regular): if packages: try: args = ['install', '--assume-yes'] args.extend(extra_apt_args) self.chroot.system("apt-get", *(args + packages)) except executil.ExecError, e: def get_last_log(path): log = [] for line in reversed(file(path).readlines()): if line.startswith("Log ended: "): continue if line.startswith("Log started: "): break log.append(line.strip()) log.reverse() return log def get_errors(log, error_str): errors = [] for line in reversed(log): if line == error_str: break errors.append(basename(line).split("_")[0]) return errors log = get_last_log( join(self.chroot.path, "var/log/apt/term.log")) error_str = "Errors were encountered while processing:" if error_str not in log: raise errors = get_errors(log, error_str) ignored_errors = set(errors) & set(ignore_errors) errors = set(errors) - set(ignore_errors) if ignored_errors: print "Warning: ignoring package installation errors (%s)" % " ".join( ignored_errors) if errors: raise fake_update_initramfs.revert() defer_log = join(self.chroot.path, defer_log) if exists(defer_log): kversion = "all" boot_path = join(self.chroot.path, "boot") for f in os.listdir(boot_path): if f.startswith("vmlinuz-"): kversion = f.replace("vmlinuz-", "") break if exists(join(boot_path, "initrd.img-%s" % kversion)): self.chroot.system("update-initramfs -u") else: self.chroot.system("update-initramfs -c -k %s" % kversion) os.remove(defer_log)
def system(self, *command): try: _Chroot.system(self, *command) except ExecError, e: return e.exitcode
def __init__(self, chroot_path, environ={}): env = {'DEBIAN_FRONTEND': 'noninteractive', 'DEBIAN_PRIORITY': 'critical'} env.update(environ) self.chroot = Chroot(chroot_path, environ=env)
class Installer(object): def __init__(self, chroot_path, environ={}): env = {'DEBIAN_FRONTEND': 'noninteractive', 'DEBIAN_PRIORITY': 'critical'} env.update(environ) self.chroot = Chroot(chroot_path, environ=env) @staticmethod def _get_packages_priority(packages): """high priority packages must be installed before regular packages APT should handle this, but in some circumstances it chokes... """ HIGH_PRIORITY = ('linux-image') high = [] regular = [] for package in packages: if package.startswith(HIGH_PRIORITY): high.append(package) else: regular.append(package) return high, regular def _install(self, packages, ignore_errors=[], extra_apt_args=[]): high, regular = self._get_packages_priority(packages) lines = [ "#!/bin/sh", "echo", "echo \"Warning: Fake invoke-rc.d called\"" ] fake_invoke_rcd = RevertibleScript(join(self.chroot.path, "usr/sbin/invoke-rc.d"), lines) lines = [ "#!/bin/sh", "echo", "echo \"Warning: Fake start-stop-daemon called\"" ] fake_start_stop = RevertibleScript(join(self.chroot.path, "sbin/start-stop-daemon"), lines) defer_log = "var/lib/update-initramfs.deferred" lines = [ "#!/bin/sh", "echo", "echo \"Warning: Deferring update-initramfs $@\"", "echo \"update-initramfs $@\" >> /%s" % defer_log ] fake_update_initramfs = RevertibleScript(join(self.chroot.path, "usr/sbin/update-initramfs"), lines) fake_initctl = RevertibleInitctl(self.chroot) for packages in (high, regular): if packages: try: args = ['install', '--force-yes', '--assume-yes'] args.extend(extra_apt_args) self.chroot.system("apt-get", *(args + packages)) except executil.ExecError, e: def get_last_log(path): log = [] for line in reversed(file(path).readlines()): if line.startswith("Log ended: "): continue if line.startswith("Log started: "): break log.append(line.strip()) log.reverse() return log def get_errors(log, error_str): errors = [] for line in reversed(log): if line == error_str: break errors.append(basename(line).split("_")[0]) return errors log = get_last_log(join(self.chroot.path, "var/log/apt/term.log")) error_str = "Errors were encountered while processing:" if error_str not in log: raise errors = get_errors(log, error_str) ignored_errors = set(errors) & set(ignore_errors) errors = set(errors) - set(ignore_errors) if ignored_errors: print "Warning: ignoring package installation errors (%s)" % " ".join(ignored_errors) if errors: raise fake_update_initramfs.revert() defer_log = join(self.chroot.path, defer_log) if exists(defer_log): kversion = "all" boot_path = join(self.chroot.path, "boot") for f in os.listdir(boot_path): if f.startswith("vmlinuz-"): kversion = f.replace("vmlinuz-", "") break if exists(join(boot_path, "initrd.img-%s" % kversion)): self.chroot.system("update-initramfs -u") else: self.chroot.system("update-initramfs -c -k %s" % kversion) os.remove(defer_log)
def test_Chroot(): if sys.hexversion >= 0x03030000: patcher = patch('chroot.sethostname') sethostname = patcher.start() # testing Chroot.mount() with patch('chroot.os.geteuid', return_value=0), \ patch('chroot.bind') as bind, \ patch('chroot.exists') as exists, \ patch('chroot.dictbool') as dictbool, \ patch('chroot.unshare') as unshare: c = Chroot('/') with raises(ChrootMountError): c.mount() bind.side_effect = MountError('fake exception') c.unshare() with raises(ChrootMountError): c.mount() bind.reset_mock() bind.side_effect = None exists.return_value = False dictbool.return_value = True c.mount() assert not bind.called with patch('chroot.os.geteuid') as geteuid, \ patch('chroot.os.fork') as fork, \ patch('chroot.os.chroot') as chroot, \ patch('chroot.os.chdir') as chdir, \ patch('chroot.os.remove') as remove, \ patch('chroot.os._exit') as exit, \ patch('chroot.exists') as exists, \ patch('chroot.Chroot.mount') as mount, \ patch('chroot.os.waitpid') as waitpid, \ patch('chroot.unshare') as unshare: geteuid.return_value = 1 # not running as root with raises(ChrootError): chroot = Chroot('/') geteuid.return_value = 0 # invalid hostname with raises(ChrootError): Chroot('/', hostname=True) # bad path exists.return_value = False with raises(ChrootError): Chroot('/nonexistent/path') exists.return_value = True # $FAKEPATH not defined in environment with raises(ChrootMountError): Chroot('/', mountpoints={'$FAKEVAR': {}}) with patch('chroot.os.getenv', return_value='/fake/src/path'): Chroot('/', mountpoints={'$FAKEVAR': {}}) # test parent process fork.return_value = 10 c = Chroot('/', hostname='test') with c: pass # test child process fork.return_value = 0 c = Chroot('/') with c: pass remove.side_effect = Exception('fake exception') exists.side_effect = chain([True], cycle([False])) c = Chroot('/', mountpoints={'/root': {'dest': '/blah'}}) with raises(ChrootMountError): with c: pass remove.side_effect = None exists.side_effect = None chdir.side_effect = Exception('fake exception') with c: pass chdir.side_effect = None patch.stopall()