Redirect createrepo_c output to the build logs

It's more useful to have the createrepo_c output in the build logs
than cluttering the build stdout.
This commit is contained in:
Owen W. Taylor
2020-11-09 12:09:57 -05:00
parent cff90e587a
commit e776a77048
4 changed files with 30 additions and 6 deletions

View File

@@ -23,7 +23,7 @@ from module_build_service.builder.utils import (
get_koji_config,
validate_koji_tag,
)
from module_build_service.common import conf, log, models
from module_build_service.common import build_logs, conf, log, models
from module_build_service.common.koji import get_session
from module_build_service.common.modulemd import Modulemd
from module_build_service.common.utils import import_mmd, load_mmd_file, mmd_to_str
@@ -409,14 +409,16 @@ class MockModuleBuilder(GenericBuilder):
m1_mmd.add_rpm_artifact(artifact_to_add)
# Generate repo.
execute_cmd(["/usr/bin/createrepo_c", "--pkglist", pkglist, path])
execute_cmd(["/usr/bin/createrepo_c", "--pkglist", pkglist, path],
output=build_logs.current_log_stream)
# ...and inject modules.yaml there if asked.
if include_module_yaml:
mmd_path = os.path.join(path, "modules.yaml")
with open(mmd_path, "wb") as f:
f.write(mmd_to_str(m1_mmd).encode("utf-8"))
execute_cmd(["/usr/bin/modifyrepo_c", "--mdtype=modules", mmd_path, repodata_path])
execute_cmd(["/usr/bin/modifyrepo_c", "--mdtype=modules", mmd_path, repodata_path],
output=build_logs.current_log_stream)
def _add_repo(self, name, baseurl, extra=""):
"""

View File

@@ -13,7 +13,7 @@ import subprocess
import munch
import requests
from module_build_service.common import conf, log, models
from module_build_service.common import build_logs, conf, log, models
from module_build_service.common.errors import ValidationError, ProgrammingError
@@ -177,7 +177,8 @@ def create_local_repo_from_koji_tag(config, tag, repo_dir, archs=None):
shutil.rmtree(repodata_path)
log.info("Creating local repository in %s" % repo_dir)
execute_cmd(["/usr/bin/createrepo_c", repo_dir])
execute_cmd(["/usr/bin/createrepo_c", repo_dir],
output=build_logs.current_log_stream)
log.local_repo_done(tag, 'Finished downloading packages')

View File

@@ -205,6 +205,21 @@ class ModuleBuildLogs(object):
log.removeHandler(handler)
del self.handlers[build.id]
@property
def current_log_stream(self):
# Imported here because of importing cycle between __init__.py,
# scheduler and models.
from module_build_service.scheduler.consumer import MBSConsumer
if MBSConsumer.current_module_build_id:
handler = self.handlers.get(MBSConsumer.current_module_build_id)
if handler:
return handler.stream
else:
return sys.stderr
else:
return sys.stderr
class LocalRepo(object):
def __init__(self, koji_tag):

View File

@@ -8,6 +8,7 @@ from os import path
import re
import pytest
import shutil
import sys
import tempfile
import textwrap
from unittest.mock import patch
@@ -69,10 +70,11 @@ class TestLogger:
log.info("ignore this test msg")
log.warning("ignore this test msg")
log.error("ignore this test msg")
assert self.build_log.current_log_stream is sys.stderr
self.build_log.stop(build)
assert not os.path.exists(path)
# Try logging with current_module_build_id set to 2 and then to 2.
# Try logging with current_module_build_id set to 1 and then to 2.
# Only messages with current_module_build_id set to 2 should appear in
# the log.
self.build_log.start(db_session, build)
@@ -81,12 +83,14 @@ class TestLogger:
log.info("ignore this test msg1")
log.warning("ignore this test msg1")
log.error("ignore this test msg1")
assert self.build_log.current_log_stream is sys.stderr
MBSConsumer.current_module_build_id = 2
log.debug("ignore this test msg2")
log.info("ignore this test msg2")
log.warning("ignore this test msg2")
log.error("ignore this test msg2")
self.build_log.current_log_stream.write("ignore this test output2")
self.build_log.stop(build)
assert os.path.exists(path)
@@ -95,6 +99,7 @@ class TestLogger:
# Note that DEBUG is not present unless configured server-wide.
for level in ["INFO", "WARNING", "ERROR"]:
assert data.find("MBS - {0} - ignore this test msg2".format(level)) != -1
assert data.find("ignore this test output2")
# Try to log more messages when build_log for module 1 is stopped.
# New messages should not appear in a log.
@@ -103,6 +108,7 @@ class TestLogger:
log.info("ignore this test msg3")
log.warning("ignore this test msg3")
log.error("ignore this test msg3")
assert self.build_log.current_log_stream is sys.stderr
self.build_log.stop(build)
with open(path, "r") as f:
data = f.read()