def __init__(self, opts): self.no_wait = opts['no_wait'] self.regen_repos = opts['regen_repos'] self.scratch = opts['scratch'] self.cn = opts['kojilogin'] or get_cn() if not self.cn: raise KojiError("""\ Unable to determine your Koji login. Either pass --kojilogin or verify that 'openssl x509 -in %s -noout -subject' gives you a subject with a CN""" % KOJI_CLIENT_CERT) if KojiInter.backend is None: if HAVE_KOJILIB and opts.get('koji_backend') != 'shell': log.debug("KojiInter Using KojiLib backend") KojiInter.backend = KojiLibInter(self.cn, opts['dry_run']) KojiInter.backend.read_config_file() KojiInter.backend.init_koji_session() elif not HAVE_KOJILIB and opts['koji_backend'] == 'kojilib': raise KojiError( "KojiLib backend requested, but can't import it!") else: log.debug("KojiInter Using shell backend") KojiInter.backend = KojiShellInter(self.cn, opts['dry_run'], opts['koji_wrapper']) self.target = opts['koji_target'] self.build_tag, self.dest_tag = KojiInter.backend.get_build_and_dest_tags( self.target) if opts['koji_tag'] == 'TARGET': self.tag = self.dest_tag else: self.tag = opts['koji_tag']
def init_koji_session(self, login=True): print "Initializing koji session to", self.server self.kojisession = kojilib.ClientSession(self.server, {'user': self.user}) if login and not self.dry_run: print "Logging in to koji as", self.user try: self.kojisession.ssl_login(self.cert, self.ca, self.serverca) except Exception, err: raise KojiError("Couldn't do ssl_login: "******"Couldn't log in to koji for unknown reason")
def get_build_and_dest_tags(self, target): """Return the build and destination tags for the current target.""" line = utils.checked_backtick(self.koji_cmd + ["-q", "list-targets", "--name", target]) if not line: raise KojiError("Unable to find koji target with the name " + target) else: try: fields = re.split(r"\s*", line, 2) target_build_tag = fields[1] target_dest_tag = fields[2] except IndexError: raise KojiError("Unable to determine koji tags from target") return (target_build_tag, target_dest_tag)
def tag_build(self, tag, build, force=False): tag_pkg_subcmd = ["tag-pkg", tag, build] if force: tag_pkg_subcmd.append("--force") err = utils.unchecked_call(self.koji_cmd + tag_pkg_subcmd) if err: raise KojiError("koji tag-pkg failed with exit code " + str(err))
def __init__(self, opts): self.no_wait = opts['no_wait'] self.regen_repos = opts['regen_repos'] self.scratch = opts['scratch'] self.arch_override = opts.get('target_arch', None) if self.arch_override and not self.scratch: log.warning("target-arch ignored on non-scratch builds") self.arch_override = None self.cn = opts['kojilogin'] or None if KojiInter.backend is None: if HAVE_KOJILIB and opts.get('koji_backend') != 'shell': log.debug("KojiInter Using KojiLib backend") KojiInter.backend = KojiLibInter(self.cn, opts['dry_run']) KojiInter.backend.read_config_file() KojiInter.backend.init_koji_session() elif not HAVE_KOJILIB and opts['koji_backend'] == 'kojilib': raise KojiError( "KojiLib backend requested, but can't import it!") else: log.debug("KojiInter Using shell backend") KojiInter.backend = KojiShellInter(self.cn, opts['dry_run'], opts['koji_wrapper']) self.target = opts['koji_target'] self.build_tag, self.dest_tag = KojiInter.backend.get_build_and_dest_tags( self.target) if opts['koji_tag'] == 'TARGET': self.tag = self.dest_tag else: self.tag = opts['koji_tag'] self.background = opts['background']
def read_config_file(self, config_file=None): if not config_file: config_file = (utils.find_file( 'config', [OSG_KOJI_USER_CONFIG_DIR, KOJI_USER_CONFIG_DIR]) or utils.find_file(KOJI_CONF, DATA_FILE_SEARCH_PATH) or utils.find_file(OLD_KOJI_CONF, DATA_FILE_SEARCH_PATH)) if not config_file or not os.path.isfile(config_file): raise KojiError("Can't find koji config file.") try: cfg = ConfigParser.ConfigParser() cfg.read(config_file) items = dict(cfg.items('koji')) except ConfigParser.Error, err: raise KojiError("Can't read config file from %s: %s" % (config_file, str(err)))
def build(self, url, target, scratch=False, **kwargs): """build package at url for target. Using **kwargs so signature is same as KojiLibInter.build. kwargs recognized: no_wait, regen_repos, background, arch_override """ log.debug("building " + url) no_wait = kwargs.get('no_wait', False) regen_repos = kwargs.get('regen_repos', False) background = kwargs.get('background', False) arch_override = kwargs.get('arch_override', None) build_subcmd = ["build", target, url] if scratch: build_subcmd += ["--scratch"] if no_wait: build_subcmd += ["--nowait"] if background: build_subcmd += ["--background"] if arch_override: build_subcmd += ["--arch-override=" + arch_override] log.info("Calling koji to build the package for target %s", target) if not self.dry_run: err = utils.unchecked_call(self.koji_cmd + build_subcmd) else: print " ".join(self.koji_cmd + build_subcmd) err = 0 if err: raise KojiError("koji build failed with exit code " + str(err)) if regen_repos and not scratch: build_tag = self.get_build_and_dest_tags(target)[0] regen_repo_subcmd = ["regen-repo", build_tag] if no_wait: regen_repo_subcmd += ["--nowait"] log.info("Calling koji to regen " + build_tag) if not self.dry_run: err2 = utils.unchecked_call(self.koji_cmd + regen_repo_subcmd) else: print " ".join(self.koji_cmd + regen_repo_subcmd) err2 = 0 if err2: raise KojiError("koji regen-repo failed with exit code " + str(err2))
def mock_config(self, arch, tag, dist, outpath, name): tag_obj = self.kojisession.getTag(tag) if not tag_obj: raise KojiError("Invalid tag %s" % tag) config = self.kojisession.getBuildConfig(tag_obj['id']) if not config: raise KojiError("Couldn't get config for tag %s" % tag) repo = self.kojisession.getRepo(config['id']) if not repo: raise KojiError("Couldn't get repo for tag %s" % tag) opts = { 'tag_name': tag_obj['name'], 'repoid': repo['id'], 'distribution': dist, 'topurl': KOJI_HUB + "/mnt/koji" } output = kojilib.genMockConfig(name, arch, **opts) utils.unslurp(outpath, output)
def get_targets(self): """Get a list of the names of targets (as strings) from koji""" out, err = utils.sbacktick(self.koji_cmd + ["list-targets", "--quiet"]) if err: raise KojiError("koji list-targets failed with exit code " + str(err)) lines = out.split("\n") target_names = [re.split(r"\s+", x)[0] for x in lines] return target_names
def get_koji_cmd(use_osg_koji): """Get the command used to call koji.""" # Use osg-koji wrapper if available and configured. if utils.which("osg-koji") and use_osg_koji: return ["osg-koji"] elif utils.which("koji"): # Not using osg-koji, so we need to find the conf file and do some # checks ourselves. conf_file = (utils.find_file(KOJI_CONF, DATA_FILE_SEARCH_PATH) or utils.find_file(OLD_KOJI_CONF, DATA_FILE_SEARCH_PATH)) if not conf_file: raise KojiError("Can't find %s or %s; search path was: %s" % (KOJI_CONF, OLD_KOJI_CONF, os.pathsep.join(DATA_FILE_SEARCH_PATH))) return ["koji", "--config", conf_file, "--authtype", "ssl"] else: raise KojiError("Can't find koji or osg-koji!")
def koji_error_wrap_helper(function_to_wrap): def wrapped_function(*args, **kwargs): try: return function_to_wrap(*args, **kwargs) except kojilib.ServerOffline, err: raise KojiError("Server outage detected while %s: %s" % (description, str(err))) except kojilib.GenericError, err: raise KojiError("Error of type %s while %s: %s" % (type_of_error(err), description, str(err)))
def __init__(self, user=None, dry_run=False): if not HAVE_KOJILIB: raise KojiError("Cannot use KojiLibInter without kojilib!") self.ca = None self.cert = KOJI_CLIENT_CERT self.kojisession = None self.server = os.path.join(KOJI_HUB, "kojihub") self.serverca = None self.user = user or get_cn() self.weburl = os.path.join(HTTPS_KOJI_HUB, "koji") self.dry_run = dry_run
def mock_config(self, arch, tag, dist, outpath, name): """Request a mock config from koji-hub""" mock_config_subcmd = [ "mock-config", "--arch=" + arch, "--tag=" + tag, "--distribution=" + dist, "--topurl=" + KOJI_HUB + "/mnt/koji", "-o", outpath, name ] err = utils.unchecked_call(self.koji_cmd + mock_config_subcmd) if err: raise KojiError("koji mock-config failed with exit code " + str(err))
def search_names(self, terms, stype, match): search_subcmd = ["search", stype] if match == 'regex': search_subcmd.append("--regex") elif match == 'exact': search_subcmd.append("--exact") search_subcmd.append(terms) out, err = utils.sbacktick(self.koji_cmd + search_subcmd) if err: raise KojiError("koji search failed with exit code " + str(err)) return out.split("\n")
def add_pkg(self, tag, package, owner=None): if owner is None: owner = self.user tag_obj = self.kojisession.getTag(tag) if not tag_obj: raise KojiError("Invalid tag %s" % tag) try: package_list = self.kojisession.listPackages(tagID=tag_obj['id'], pkgID=package) except kojilib.GenericError: # koji raises this if the package doesn't exist package_list = None if not package_list: if not self.dry_run: return self.kojisession.packageListAdd(tag, package, owner) else: log.info("kojisession.packageListAdd(%r, %r, %r)", tag, package, owner)
def get_build_and_dest_tags(self, target): """Return the build and destination tags for target.""" info = self.kojisession.getBuildTargets(target) # TESTME if not info: raise KojiError("Couldn't get info for target %s" % target) return (info[0]['build_tag_name'], info[0]['dest_tag_name'])
def init_koji_session(self, login=True): log.info("Initializing koji session to %s", self.server) self.kojisession = kojilib.ClientSession(self.server, {'user': self.user}) if login and not self.dry_run: self.login_to_koji() def login_to_koji(self): log.info("Logging in to koji as %s", self.user) try: self.kojisession.ssl_login(self.cert, self.ca, self.serverca) except Exception, err: raise KojiError("Couldn't do ssl_login: "******"Couldn't log in to koji for unknown reason") @koji_error_wrap('adding package') def add_pkg(self, tag, package, owner=None): if owner is None: owner = self.user tag_obj = self.kojisession.getTag(tag) if not tag_obj: raise KojiError("Invalid tag %s" % tag) try: package_list = self.kojisession.listPackages(tagID=tag_obj['id'], pkgID=package) except kojilib.GenericError: # koji raises this if the package doesn't exist package_list = None if not package_list: if not self.dry_run:
def wrapped_function(*args, **kwargs): try: return function_to_wrap(*args, **kwargs) except kojilib.ServerOffline, err: raise KojiError("Server outage detected while %s: %s" % (description, str(err)))
def login_to_koji(self): log.info("Logging in to koji as %s", self.user) try: self.kojisession.ssl_login(self.cert, self.ca, self.serverca) except Exception, err: raise KojiError("Couldn't do ssl_login: " + str(err))