Add buildroot_ready, disttag related function

Signed-off-by: Lubos Kocman <lkocman@redhat.com>
This commit is contained in:
Lubos Kocman
2016-07-20 10:25:54 +02:00
parent ff84228502
commit 05980dacff

View File

@@ -35,6 +35,12 @@ import os
from optparse import OptionParser
from kobo.shortcuts import run
import koji
import tempfile
import glob
import datetime
import time
import random
import string
log = logging.getLogger(__name__)
@@ -97,6 +103,14 @@ class GenericBuilder:
"""
raise NotImplementedError()
@abstractmethod
def buildroot_ready(self, artifacts=None):
"""
:param artifacts=None : a list of artifacts supposed to be in buildroot
return when buildroot is ready (or contain specified artifact)
"""
raise NotImplementedError()
@abstractmethod
def buildroot_add_dependency(self, dependencies):
"""
@@ -173,6 +187,91 @@ class KojiModuleBuilder(GenericBuilder):
return "<KojiModuleBuilder module: %s, tag: %s>" % (
self.module_str, self.module_tag)
def buildroot_ready(self, artifacts=None):
target_info = self.koji_session.getBuildTarget(self.module_target)
assert target_info, "Invalid build target"
timeout = 120 # minutes see * 60
tag_id = target_info['build_tag']
start = time.time()
last_repo = None
repo = self.koji_session.getRepo(tag_id)
while True:
if artifacts and repo and repo != last_repo:
if koji.util.checkForBuilds(self.koji_session, tag_id, artifacts, repo['create_event'], latest=True):
return
if (time.time() - start) >= (timeout * 60.0):
return 1
time.sleep(60)
last_repo = repo
repo = self.koji_session.getRepo(tag_id)
if not artifacts:
if repo != last_repo:
return
@staticmethod
def get_disttag_srpm(disttag):
#Taken from Karsten's create-distmacro-pkg.sh
# - however removed any provides to system-release/redhat-release
name = 'module-build-macros'
version = "0.1"
release = "1"
today = datetime.date.today().strftime('%a %b %d %Y')
spec_content = """%global dist {disttag}
Name: {name}
Version: {version}
Release: {release}%dist
Summary: Package containing macros required to build generic module
BuildArch: noarch
Group: System Environment/Base
License: MIT
URL: http://fedoraproject.org
%description
This package is used for building modules with a different dist tag.
It provides a file /usr/lib/rpm/macros.d/macro.modules and gets read
after macro.dist, thus overwriting macros of macro.dist like %%dist
It should NEVER be installed on any system as it will really mess up
updates, builds, ....
%build
%install
mkdir -p %buildroot/%_rpmconfigdir/macro.d 2>/dev/null |:
echo %%dist %dist > %buildroot/%_rpmconfigdir/macro.modules
chmod 644 %buildroot/%_rpmconfigdir/macro.modules
%files
%_rpmconfigdir/macro.modules
%changelog
* {today} Fedora-Modularity - {version}-{release}{disttag}
- autogenerated macro by Rida "The Orchestrator"
""".format(disttag=disttag, today=today, name=name, version=version, release=release)
td = tempfile.mkdtemp(prefix="rida-build-macros")
fd = open(os.path.join(td, "%s.spec" % name), "w")
fd.write(spec_content)
fd.close()
log.debug("Building %s.spec" % name)
ret, out = run('rpmbuild -bs %s.spec --define "_topdir %s"' % (name, td), workdir=td)
sdir = os.path.join(td, "SRPMS")
srpm_paths = glob.glob("%s/*.src.rpm" % sdir)
assert len(srpm_paths) == 1, "Expected exactly 1 srpm in %s. Got %s" % (sdir, srpm_paths)
log.debug("Wrote srpm into %s" % srpm_paths[0])
return srpm_paths[0]
@staticmethod
def get_session_from_config(config):
@@ -261,12 +360,29 @@ class KojiModuleBuilder(GenericBuilder):
:param source : scmurl to spec repository
:return koji build task id
"""
# Taken from /usr/bin/koji
def _unique_path(prefix):
"""Create a unique path fragment by appending a path component
to prefix. The path component will consist of a string of letter and numbers
that is unlikely to be a duplicate, but is not guaranteed to be unique."""
# Use time() in the dirname to provide a little more information when
# browsing the filesystem.
# For some reason repr(time.time()) includes 4 or 5
# more digits of precision than str(time.time())
return '%s/%r.%s' % (prefix, time.time(),
''.join([random.choice(string.ascii_letters) for i in range(8)]))
if not self.__prep:
raise RuntimeError("Buildroot is not prep-ed")
if '://' not in source:
raise NotImplementedError("Only scm url is currently supported, got source='%s'" % source)
self._koji_whitelist_packages([artifact_name,])
if '://' not in source:
#treat source as an srpm and upload it
serverdir = _unique_path('cli-build')
callback =None
self.koji_session.uploadWrapper(source, serverdir, callback=callback)
source = "%s/%s" % (serverdir, os.path.basename(source))
task_id = self.koji_session.build(source, self.module_target['name'])
log.info("%r submitted build of %s (task_id=%s)" % (
self, source, task_id))