From e776a77048f9c36946beadcc7a7210803f9958f3 Mon Sep 17 00:00:00 2001 From: "Owen W. Taylor" Date: Mon, 9 Nov 2020 12:09:57 -0500 Subject: [PATCH] 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. --- module_build_service/builder/MockModuleBuilder.py | 8 +++++--- module_build_service/builder/utils.py | 5 +++-- module_build_service/common/logger.py | 15 +++++++++++++++ tests/test_common/test_logger.py | 8 +++++++- 4 files changed, 30 insertions(+), 6 deletions(-) diff --git a/module_build_service/builder/MockModuleBuilder.py b/module_build_service/builder/MockModuleBuilder.py index 5ca4eb5e..5d7d8879 100644 --- a/module_build_service/builder/MockModuleBuilder.py +++ b/module_build_service/builder/MockModuleBuilder.py @@ -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=""): """ diff --git a/module_build_service/builder/utils.py b/module_build_service/builder/utils.py index 35afac14..bb78b87b 100644 --- a/module_build_service/builder/utils.py +++ b/module_build_service/builder/utils.py @@ -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') diff --git a/module_build_service/common/logger.py b/module_build_service/common/logger.py index a29fc3b5..78d300b9 100644 --- a/module_build_service/common/logger.py +++ b/module_build_service/common/logger.py @@ -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): diff --git a/tests/test_common/test_logger.py b/tests/test_common/test_logger.py index 0ce0b2ab..0a049aad 100644 --- a/tests/test_common/test_logger.py +++ b/tests/test_common/test_logger.py @@ -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()