Skip to content

Commit 7695024

Browse files
author
saville
committed
Use debug log level for artifact listing
1 parent 3a7ce7c commit 7695024

File tree

4 files changed

+45
-23
lines changed

4 files changed

+45
-23
lines changed

buildrunner/docker/runner.py

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
from pathlib import Path
1919
import tarfile
2020
from types import GeneratorType
21-
from typing import Optional
21+
from typing import Optional, Union
2222

2323
from docker.utils import compare_version
2424
from retry import retry
@@ -31,7 +31,7 @@
3131
force_remove_container,
3232
BuildRunnerContainerError,
3333
)
34-
from buildrunner.loggers import ContainerLogger, DockerPullProgress
34+
from buildrunner.loggers import ConsoleLogger, ContainerLogger, DockerPullProgress
3535
from buildrunner.utils import (
3636
acquire_flock_open_read_binary,
3737
acquire_flock_open_write_binary,
@@ -75,7 +75,13 @@ def __init__(self, image_name, pull_image=True, platform=None):
7575
self.pull_image = pull_image
7676
self.platform = platform
7777

78-
def __init__(self, image_config, dockerd_url=None, log=None):
78+
def __init__(
79+
self,
80+
image_config,
81+
dockerd_url=None,
82+
log: Union[ContainerLogger, None] = None,
83+
run_log_debug: bool = False,
84+
):
7985
image_name = image_config.image_name
8086
pull_image = image_config.pull_image
8187
image_platform = image_config.platform
@@ -91,6 +97,7 @@ def __init__(self, image_config, dockerd_url=None, log=None):
9197
# Disable timeouts for running commands
9298
timeout=0,
9399
)
100+
self.run_log_debug = run_log_debug
94101
self.container = None
95102
self.shell = None
96103
self.committed_image = None
@@ -131,6 +138,21 @@ def __init__(self, image_config, dockerd_url=None, log=None):
131138
if log:
132139
log.write("\nImage pulled successfully\n")
133140

141+
def _run_log(
142+
self,
143+
log: Union[ConsoleLogger, ContainerLogger, None],
144+
output: Union[str, bytes],
145+
):
146+
"""
147+
Log a message to the given log stream at a debug or info level depending on configuration.
148+
"""
149+
if log:
150+
for line in log.clean_output(output).split("\n"):
151+
if self.run_log_debug:
152+
log.debug(line)
153+
else:
154+
log.info(line)
155+
134156
def start(
135157
self,
136158
shell="/bin/sh",
@@ -154,7 +176,7 @@ def start(
154176
cap_add=None,
155177
privileged=False,
156178
network=None,
157-
): # pylint: disable=too-many-arguments,too-many-locals
179+
):
158180
"""
159181
Kwargs:
160182
volumes (dict): mount the local dir (key) to the given container
@@ -549,7 +571,6 @@ def save_caches(
549571
f"It will not be saved again to `{local_cache_archive_file}`\n"
550572
)
551573

552-
# pylint: disable=too-many-branches,too-many-arguments
553574
def run(self, cmd, console=None, stream=True, log=None, workdir=None):
554575
"""
555576
Run the given command in the container.
@@ -577,8 +598,7 @@ def run(self, cmd, console=None, stream=True, log=None, workdir=None):
577598
"Cannot call run if container cmd not shell"
578599
)
579600

580-
if log:
581-
log.write(f"Executing: {cmdv}\n")
601+
self._run_log(log, f"Executing: {cmdv}")
582602

583603
create_res = self.docker_client.exec_create(
584604
self.container["Id"],
@@ -591,17 +611,13 @@ def run(self, cmd, console=None, stream=True, log=None, workdir=None):
591611
stream=stream,
592612
)
593613
if isinstance(output_buffer, (bytes, str)):
594-
if console:
595-
console.write(output_buffer)
596-
if log:
597-
log.write(output_buffer)
614+
self._run_log(console, output_buffer)
615+
self._run_log(log, output_buffer)
598616
elif hasattr(output_buffer, "next") or isinstance(output_buffer, GeneratorType):
599617
try:
600618
for line in output_buffer:
601-
if console:
602-
console.write(line)
603-
if log:
604-
log.write(line)
619+
self._run_log(console, line)
620+
self._run_log(log, line)
605621
except socket.timeout:
606622
# Ignore timeouts since we check for the exit code anyways at the end
607623
pass

buildrunner/loggers.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -92,16 +92,20 @@ class ConsoleLogger:
9292
def __init__(self, name: str):
9393
self.logger = logging.getLogger(name)
9494

95+
@staticmethod
96+
def clean_output(output: Union[bytes, str]) -> str:
97+
if not isinstance(output, str):
98+
output = str(output, encoding=ENCODING, errors="replace")
99+
if output and output[-1] == "\n":
100+
output = output[:-1]
101+
return output
102+
95103
def write(self, output: Union[bytes, str]):
96104
"""
97105
Write the given text to stdout and streams decorating output to stdout
98106
with color.
99107
"""
100-
if not isinstance(output, str):
101-
output = str(output, encoding=ENCODING, errors="replace")
102-
if output and output[-1] == "\n":
103-
output = output[:-1]
104-
for line in output.split("\n"):
108+
for line in self.clean_output(output).split("\n"):
105109
self.logger.info(line)
106110

107111
# Delegates all methods to the logger if they don't exist (allowing the logger methods to be used directly)

buildrunner/steprunner/tasks/remote.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,8 @@ def run(self, context): # pylint: disable=unused-argument,too-many-branches,too
117117
)
118118
self.step_runner.log.write("\nGathered artifacts:\n")
119119
for _art in _arts:
120-
self.step_runner.log.write(
121-
f"- found {os.path.basename(_art)}\n",
120+
self.step_runner.log.debug(
121+
f"- found {os.path.basename(_art)}",
122122
)
123123
self.step_runner.log.write("\n")
124124

buildrunner/steprunner/tasks/run.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,8 @@ def _retrieve_artifacts(self, console=None): # pylint: disable=too-many-locals
172172
artifact_lister = DockerRunner(
173173
image_config,
174174
log=self.step_runner.log,
175+
# Log run messages at a debug level
176+
run_log_debug=True,
175177
)
176178
# NOTE: see if we can use archive commands to eliminate the need for
177179
# the /stepresults volume when we can move to api v1.20
@@ -440,7 +442,7 @@ def _archive_file(
440442
# Unused arg
441443
_ = new_artifact_file
442444

443-
self.step_runner.log.info(f"- found {file_type} {filename}")
445+
self.step_runner.log.debug(f"- found {file_type} {filename}")
444446

445447
exit_code = artifact_lister.run(
446448
archive_command,

0 commit comments

Comments
 (0)