builder: fetch and attach build logs

Use the new log API introduced in osbuild-composer >= 24, to fetch
the logs for the compose and attach them to the task. It is a non-
fatal error if fetching the logs fails; in that case a warning is
emitted. NB: logs are attached no matter the compose result.
Logs are per image-request, the content is JSON, but otherwise not
further specified.
This commit is contained in:
Christian Kellner 2020-11-12 18:48:40 +01:00
parent 9e10eb58eb
commit e81ac2d946

View file

@ -158,6 +158,16 @@ class ComposeStatus:
return self.status in [self.SUCCESS]
class ComposeLogs:
def __init__(self, image_logs: List):
self.image_logs = image_logs
@classmethod
def from_dict(cls, data: Dict):
image_logs = data["image_logs"]
return cls(image_logs)
class Client:
def __init__(self, url):
self.server = url
@ -203,6 +213,18 @@ class Client:
return ComposeStatus.from_dict(res.json())
def compose_logs(self, compose_id: str):
url = urllib.parse.urljoin(self.url, f"compose/{compose_id}/logs")
res = self.http.get(url)
if res.status_code != 200:
body = res.content.decode("utf-8").strip()
msg = f"Failed to get the compose logs: {body}"
raise koji.GenericError(msg) from None
return ComposeLogs.from_dict(res.json())
def wait_for_compose(self, compose_id: str, *, sleep_time=2):
while True:
status = self.compose_status(compose_id)
@ -263,6 +285,30 @@ class OSBuildImage(BaseTaskHandler):
3, # retries
self.logger)
def attach_logs(self, compose_id: str, ireqs: ImageRequest):
self.logger.debug("Fetching logs")
try:
logs = self.client.compose_logs(compose_id)
except koji.GenericError as e:
self.logger.warning("Failed to fetch logs: %s", str(e))
return
ilogs = zip(logs.image_logs, ireqs)
for log, ireq in ilogs:
name = "%s-%s" % (ireq.architecture, ireq.image_type)
self.logger.debug("Uploading logs for %s", name)
fd = io.StringIO()
json.dump(log, fd, indent=4, sort_keys=True)
fd.seek(0)
path = koji.pathinfo.taskrelpath(self.id)
fast_incremental_upload(self.session,
name + ".log.json",
fd,
path,
3, # retries
self.logger)
@staticmethod
def arches_for_config(buildconfig: Dict):
archstr = buildconfig["arches"]
@ -357,6 +403,8 @@ class OSBuildImage(BaseTaskHandler):
self.logger.debug("Compose finished: %s", str(status))
self.logger.info("Compose result: %s", status.status)
self.attach_logs(cid, ireqs)
if not status.is_success:
raise koji.BuildError(f"Compose failed (id: {cid})")