def copy_mic(bootstrap_pth, bin_pth = '/usr/bin', lib_pth='/usr/lib', \ pylib_pth = '/usr/lib/python2.6/site-packages'): # copy python lib files mic_pylib = get_mic_modpath() bs_mic_pylib = bootstrap_pth + os.path.join(pylib_pth, 'mic') if os.path.commonprefix([mic_pylib, bs_mic_pylib]) == mic_pylib: raise errors.BootstrapError('Invalid Bootstrap: %s' % bootstrap_pth) shutil.rmtree(bs_mic_pylib, ignore_errors=True) shutil.copytree(mic_pylib, bs_mic_pylib) clean_files(".*\.py[co]$", bs_mic_pylib) # copy lib files mic_libpth = get_mic_libpath() bs_mic_libpth = bootstrap_pth + os.path.join(lib_pth, 'mic') if os.path.commonprefix([mic_libpth, bs_mic_libpth]) == mic_libpth: raise errors.BootstrapError('Invalid Bootstrap: %s' % bootstrap_pth) shutil.rmtree(bs_mic_libpth, ignore_errors=True) shutil.copytree(mic_libpth, bs_mic_libpth) os.system('cp -af %s %s' % (mic_libpth, os.path.dirname(bs_mic_libpth))) # copy bin files mic_binpth = get_mic_binpath() bs_mic_binpth = bootstrap_pth + os.path.join(bin_pth, 'mic') shutil.rmtree(bs_mic_binpth, ignore_errors=True) shutil.copy2(mic_binpth, bs_mic_binpth)
def sync_mic(bootstrap, binpth='/usr/bin/mic', libpth='/usr/lib', plugin='/usr/lib/mic/plugins', pylib='/usr/lib/python2.7/site-packages', conf='/etc/mic/mic.conf'): _path = lambda p: os.path.join(bootstrap, p.lstrip('/')) micpaths = { 'binpth': get_mic_binpath(), 'libpth': get_mic_libpath(), 'plugin': plugin, 'pylib': get_mic_modpath(), 'conf': '/etc/mic/mic.conf', } if not os.path.exists(_path(pylib)): pyptn = '/usr/lib/python?.?/site-packages' pylibs = glob.glob(_path(pyptn)) if pylibs: pylib = pylibs[0].replace(bootstrap, '') else: raise errors.BootstrapError("Can't find python site dir in: %s" % bootstrap) for key, value in micpaths.items(): try: safecopy(value, _path(eval(key)), False, ["*.pyc", "*.pyo"]) except (OSError, IOError), err: raise errors.BootstrapError(err)
def get_mic_modpath(): try: import mic except ImportError: raise errors.BootstrapError("Can't find mic module in host OS") path = os.path.abspath(mic.__file__) return os.path.dirname(path)
def _setPkgmgr(self, name): backend_plugins = pluginmgr.get_plugins('backend') for (key, cls) in backend_plugins.iteritems(): if key == name: self._pkgmgr = cls if not self._pkgmgr: raise errors.BootstrapError( "Backend: %s can't be loaded correctly" % name)
def cleanup(self, name): self.rootdir = name try: chroot.cleanup_mounts(self.rootdir) shutil.rmtree(self.rootdir, ignore_errors=True) except: raise errors.BootstrapError("Bootstrap: %s clean up fail " % self.rootdir)
def create(self, repomd, pkglist, optlist=()): try: pkgmgr = MiniBackend(self.get_rootdir()) pkgmgr.arch = self.arch pkgmgr.repomd = repomd pkgmgr.optionals = list(optlist) map(pkgmgr.selectPackage, pkglist + list(optlist)) pkgmgr.runInstall() except (OSError, IOError, errors.CreatorError), err: raise errors.BootstrapError("%s" % err)
def query_package_metadat(root='/', tag='name', pattern=None): name = pattern version = None try: with open(root + '/.metadata', 'r') as f: metadata = pickle.load(f) f.close() except: raise errors.BootstrapError("Load %s/.metadata error" % root) else: for pkg in metadata.keys(): m = misc.RPM_RE.match(pkg) if m: (n, a, v, r) = m.groups() else: raise errors.BootstrapError("Wrong Format .metadata in %s" % root) if n == pattern: version = v return (name, version)
def copy_mic(bootstrap_pth, bin_pth = '/usr/bin', lib_pth='/usr/lib', \ pylib_pth = '/usr/lib/python2.7/site-packages'): # copy python lib files mic_pylib = get_mic_modpath() bs_mic_pylib = bootstrap_pth + os.path.join(pylib_pth, 'mic') if os.path.commonprefix([mic_pylib, bs_mic_pylib]) == mic_pylib: raise errors.BootstrapError('Invalid Bootstrap: %s' % bootstrap_pth) shutil.rmtree(bs_mic_pylib, ignore_errors=True) shutil.copytree(mic_pylib, bs_mic_pylib) clean_files(".*\.py[co]$", bs_mic_pylib) # copy lib files mic_libpth = get_mic_libpath() bs_mic_libpth = bootstrap_pth + os.path.join(lib_pth, 'mic') if os.path.commonprefix([mic_libpth, bs_mic_libpth]) == mic_libpth: raise errors.BootstrapError('Invalid Bootstrap: %s' % bootstrap_pth) shutil.rmtree(bs_mic_libpth, ignore_errors=True) shutil.copytree(mic_libpth, bs_mic_libpth) os.system('cp -af %s %s' % (mic_libpth, os.path.dirname(bs_mic_libpth))) # copy bin files mic_binpth = get_mic_binpath() bs_mic_binpth = bootstrap_pth + os.path.join(bin_pth, 'mic') shutil.rmtree(bs_mic_binpth, ignore_errors=True) shutil.copy2(mic_binpth, bs_mic_binpth) # copy mic.conf mic_cfgpth = '/etc/mic/mic.conf' bs_mic_cfgpth = bootstrap_pth + mic_cfgpth if not os.path.exists(os.path.dirname(bs_mic_cfgpth)): os.makedirs(os.path.dirname(bs_mic_cfgpth)) shutil.copy2(mic_cfgpth, bs_mic_cfgpth) # remove yum backend try: yumpth = "/usr/lib/mic/plugins/backend/yumpkgmgr.py" os.unlink(bootstrap_pth + yumpth) except: pass
def update(self, name): self.rootdir = name chrootdir = self.rootdir def mychroot(): os.chroot(chrootdir) shutil.copyfile("/etc/resolv.conf", chrootdir + "/etc/resolv.conf") try: subprocess.call("zypper -n --no-gpg-checks update", preexec_fn=mychroot, shell=True) except OSError, err: raise errors.BootstrapError("Bootstrap: %s update failed" %\ chrootdir)
def _get_local_packages(self, pkg): """Return local mic-bootstrap rpm path.""" cropts = configmgr.create if cropts['local_pkgs_path']: if os.path.isdir(cropts['local_pkgs_path']): pkglist = glob.glob( os.path.join(cropts['local_pkgs_path'], pkg + '*.rpm')) if len(pkglist) > 1: raise errors.BootstrapError("Many %s packages in folder, put only one %s package in it." % (pkg, pkg)) elif len(pkglist) == 1: return ''.join(pkglist) elif os.path.splitext(cropts['local_pkgs_path'])[-1] == '.rpm': if cropts['local_pkgs_path'].find(pkg) >= 0: return cropts['local_pkgs_path'] return None
def query_package_metadat(root='/', tag='name', pattern=None): name = pattern version = None try: with open(root + '/.metadata', 'r') as f: metadata = pickle.load(f) f.close() except: raise errors.BootstrapError("Load %s/.metadata error" % root) else: for pkg in metadata.keys(): (n, v, r, e, a) = rpmmisc.splitFilename(pkg) if n == pattern: version = v return (name, version)
def downloadPkgs(self): nonexist = [] for pkg in self.dlpkgs: localpth = misc.get_package(pkg, self.repomd, self.arch) if localpth: self.localpkgs[pkg] = localpth elif pkg in self.optionals: # skip optional rpm continue else: # mark nonexist rpm nonexist.append(pkg) if nonexist: raise errors.BootstrapError("Can't get rpm binary: %s" % ','.join(nonexist))
def get_mic_binpath(): fp = None try: import pkg_resources # depends on 'setuptools' except ImportError: pass else: dist = pkg_resources.get_distribution('mic') # the real script is under EGG_INFO/scripts if dist.has_metadata('scripts/mic'): fp = os.path.join(dist.egg_info, "scripts/mic") if fp: return fp # not found script if 'flat' egg installed try: return find_binary_path('mic') except errors.CreatorError: raise errors.BootstrapError("Can't find mic binary in host OS")
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)
def installPkgs(self): for pkg in self.localpkgs.keys(): rpmpath = self.localpkgs[pkg] hdr = readRpmHeader(self.ts, rpmpath) # save prein and postin scripts self.preins[pkg] = (hdr['PREINPROG'], hdr['PREIN']) self.postins[pkg] = (hdr['POSTINPROG'], hdr['POSTIN']) # mark pkg as install self.ts.addInstall(hdr, rpmpath, 'u') # run transaction self.ts.order() cb = RPMInstallCallback(self.ts) errs = self.ts.run(cb.callback, '') # ts.run() exit codes are, hmm, "creative": None means all ok, empty # list means some errors happened in the transaction and non-empty # list that there were errors preventing the ts from starting... if errs is not None: raise errors.BootstrapError("Transaction couldn't start: %s" % '\n'.join(errs))
def _getRootdir(self): if not os.path.exists(self._rootdir): raise errors.BootstrapError("Root dir: %s not exist" % self._rootdir) return self._rootdir
def create(self, name, repolist, **kwargs): self.name = name self.pkgmgr = 'zypp' self.arch = 'i686' self.rootdir = name self.cachedir = '/var/tmp/mic/cache' # TBD from conf, do NOT hardcode if 'arch' in kwargs: self.arch = kwargs['arch'] if 'cachedir' in kwargs: self.cachedir = kwargs['cachedir'] if os.path.exists(self._rootdir): metadata_fp = os.path.join(self._rootdir, '.metadata') if os.path.exists(metadata_fp) and \ 0 != os.path.getsize(metadata_fp): msger.warning("bootstrap already exists") # TBD more details return else: shutil.rmtree(self._rootdir) if not os.path.exists(self._rootdir): os.makedirs(self._rootdir) pkg_manager = self.pkgmgr(self.arch, self.rootdir, self.cachedir) pkg_manager.setup() for repo in repolist: if 'proxy' in repo.keys(): pkg_manager.addRepository(repo['name'], repo['baseurl'], proxy=repo['proxy']) else: pkg_manager.addRepository(repo['name'], repo['baseurl']) rpm.addMacro("_dbpath", "/var/lib/rpm") rpm.addMacro("__file_context_path", "%{nil}") for pkg in minibase_pkgs: pkg_manager.selectPackage(pkg) for pkg in required_pkgs: pkg_manager.selectPackage(pkg) try: pkg_manager.runInstall(512 * 1024L * 1024L) except: raise errors.BootstrapError("Create bootstrap fail") else: metadata = pkg_manager.getAllContent() metadata_fp = os.path.join(self.rootdir, '.metadata') with open(metadata_fp, 'w') as f: pickle.dump(metadata, f) f.close() finally: pkg_manager.closeRpmDB() pkg_manager.close() # Copy bootstrap repo files srcdir = "%s/etc/zypp/repos.d/" % self.cachedir destdir = "%s/etc/zypp/repos.d/" % os.path.abspath(self.rootdir) shutil.rmtree(destdir, ignore_errors=True) shutil.copytree(srcdir, destdir) msger.info("Bootstrap created.")