Skip to content

Commit f8db943

Browse files
omertuceranco74
authored andcommitted
_prepare_build_container runner calls should not output to stdout
All output generated by the container runtime during the `_prepare_build_container` stage should not be included in stdout - we should redirect it to stderr, as the user is just using skipper as a wrapper to run commands like make, run etc - they don't care that as a side-effect of running their commands a container is being built, and they don't want the output from that build process to be included in the stdout for the commands they wrap with skipper. This allows users to do things like VERSION=$(skipper make get_version) without having the build process output be included in their VERSION env var. Surprisingly (to me), docker/podman build output such as: ``` Step 13/18 : ENV GOPATH=/go ---> Using cache ---> 152ad1ea9005 Step 14/18 : ENV GOCACHE=/go/.cache ---> Using cache ---> 4b6a4256c84f Step 15/18 : ENV PATH=$PATH:/usr/local/go/bin:/go/bin ---> Using cache ---> 56bee68a9cda Step 16/18 : COPY . . ``` Goes to stdout and not stderr, and this causes this kind of pollution.
1 parent 0f23176 commit f8db943

File tree

4 files changed

+33
-13
lines changed

4 files changed

+33
-13
lines changed

.pylintrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ confidence=
6565
# --enable=similarities". If you want to run only the classes checker, but have
6666
# no Warning level messages displayed, use"--disable=all --enable=classes
6767
# --disable=W"
68-
disable=import-star-module-level,old-octal-literal,oct-method,print-statement,unpacking-in-except,parameter-unpacking,backtick,old-raise-syntax,old-ne-operator,long-suffix,dict-view-method,dict-iter-method,metaclass-assignment,next-method-called,raising-string,indexing-exception,raw_input-builtin,long-builtin,file-builtin,execfile-builtin,coerce-builtin,cmp-builtin,buffer-builtin,basestring-builtin,apply-builtin,filter-builtin-not-iterating,using-cmp-argument,useless-suppression,range-builtin-not-iterating,suppressed-message,no-absolute-import,old-division,cmp-method,reload-builtin,zip-builtin-not-iterating,intern-builtin,unichr-builtin,reduce-builtin,standarderror-builtin,unicode-builtin,xrange-builtin,coerce-method,delslice-method,getslice-method,setslice-method,input-builtin,round-builtin,hex-method,nonzero-method,map-builtin-not-iterating,missing-docstring,fixme,superfluous-parens
68+
disable=import-star-module-level,old-octal-literal,oct-method,print-statement,unpacking-in-except,parameter-unpacking,backtick,old-raise-syntax,old-ne-operator,long-suffix,dict-view-method,dict-iter-method,metaclass-assignment,next-method-called,raising-string,indexing-exception,raw_input-builtin,long-builtin,file-builtin,execfile-builtin,coerce-builtin,cmp-builtin,buffer-builtin,basestring-builtin,apply-builtin,filter-builtin-not-iterating,using-cmp-argument,useless-suppression,range-builtin-not-iterating,suppressed-message,no-absolute-import,old-division,cmp-method,reload-builtin,zip-builtin-not-iterating,intern-builtin,unichr-builtin,reduce-builtin,standarderror-builtin,unicode-builtin,xrange-builtin,coerce-method,delslice-method,getslice-method,setslice-method,input-builtin,round-builtin,hex-method,nonzero-method,map-builtin-not-iterating,missing-docstring,fixme,superfluous-parens,too-many-locals
6969

7070

7171
[REPORTS]

skipper/cli.py

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,21 @@ def _push_to_registry(registry, fqdn_image):
378378

379379

380380
def _prepare_build_container(registry, image, tag, git_revision, container_context, username, password, use_cache):
381+
def runner_run(command):
382+
"""
383+
All output generated by the container runtime during this stage should
384+
not be included in stdout - we should redirect it to stderr, as the
385+
user is just using skipper as a wrapper to run commands like make, run
386+
etc - they don't care that as a side-effect of running their commands a
387+
container is being built, and they don't want the output from that
388+
build process to be included in the stdout for the commands they wrap
389+
with skipper.
390+
391+
This allows users to do things like VERSION=$(skipper make get_version)
392+
without having the build process output be included in their VERSION
393+
env var.
394+
"""
395+
return runner.run(command, stdout_to_stderr=True)
381396

382397
if tag is not None:
383398

@@ -414,20 +429,20 @@ def _prepare_build_container(registry, image, tag, git_revision, container_conte
414429

415430
if use_cache:
416431
cache_image = utils.generate_fqdn_image(registry, namespace=None, image=image, tag=DOCKER_TAG_FOR_CACHE)
417-
runner.run(['pull', cache_image])
432+
runner_run(['pull', cache_image])
418433
command.extend(['--cache-from', cache_image])
419434

420-
if runner.run(command) != 0:
435+
if runner_run(command) != 0:
421436
exit('Failed to build image: %(image)s' % dict(image=image))
422437

423438
if git_revision and not git.uncommitted_changes():
424439
utils.logger.info("Tagging image with git revision: %(tag)s", dict(tag=tag))
425-
runner.run(['tag', image, tagged_image_name])
440+
runner_run(['tag', image, tagged_image_name])
426441

427442
if use_cache:
428443
cache_image = utils.generate_fqdn_image(registry, namespace=None, image=image, tag=DOCKER_TAG_FOR_CACHE)
429-
runner.run(['tag', image, cache_image])
430-
runner.run(['push', cache_image])
444+
runner_run(['tag', image, cache_image])
445+
runner_run(['push', cache_image])
431446

432447
return image
433448

skipper/runner.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def get_default_net():
1717

1818
# pylint: disable=too-many-arguments
1919
def run(command, fqdn_image=None, environment=None, interactive=False, name=None, net=None, publish=(), volumes=None,
20-
workdir=None, use_cache=False, workspace=None, env_file=()):
20+
workdir=None, use_cache=False, workspace=None, env_file=(), stdout_to_stderr=False):
2121

2222
if not net:
2323
net = get_default_net()
@@ -26,15 +26,17 @@ def run(command, fqdn_image=None, environment=None, interactive=False, name=None
2626
return _run_nested(fqdn_image, environment, command, interactive, name, net, publish, volumes,
2727
workdir, use_cache, workspace, env_file)
2828

29-
return _run(command)
29+
return _run(command, stdout_to_stderr=stdout_to_stderr)
3030

3131

32-
def _run(cmd_args):
32+
def _run(cmd_args, stdout_to_stderr=False):
3333
logger = logging.getLogger('skipper')
3434
cmd = [utils.get_runtime_command()]
3535
cmd.extend(cmd_args)
3636
logger.debug(' '.join(cmd))
37-
proc = subprocess.Popen(cmd)
37+
proc = (subprocess.Popen(cmd)
38+
if not stdout_to_stderr else
39+
subprocess.Popen(cmd, stdout=sys.stderr))
3840
proc.wait()
3941
return proc.returncode
4042

tests/test_cli.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,8 @@ def test_make_without_build_container_tag_with_context(self, skipper_runner_run_
338338
mock.call(['build', '--network=host',
339339
'-t', 'build-container-image', '-f',
340340
'Dockerfile.build-container-image',
341-
SKIPPER_CONF_CONTAINER_CONTEXT, '--ulimit', 'nofile=65536:65536']),
341+
SKIPPER_CONF_CONTAINER_CONTEXT, '--ulimit', 'nofile=65536:65536'],
342+
stdout_to_stderr=True),
342343
mock.call(['make'] + make_params, fqdn_image='build-container-image', environment=[],
343344
interactive=False, name=None, net=None, publish=(), volumes=None, workdir=None,
344345
use_cache=False, workspace=None, env_file=()),
@@ -1401,7 +1402,8 @@ def test_run_without_build_container_tag(self, skipper_runner_run_mock):
14011402
)
14021403
expected_commands = [
14031404
mock.call(['build', '--network=host', '-t', 'build-container-image', '-f',
1404-
'Dockerfile.build-container-image', '.', '--ulimit', 'nofile=65536:65536']),
1405+
'Dockerfile.build-container-image', '.', '--ulimit', 'nofile=65536:65536'],
1406+
stdout_to_stderr=True),
14051407
mock.call(command, fqdn_image='build-container-image', environment=[],
14061408
interactive=False, name=None, net=None, publish=(), volumes=None, workdir=None, workspace=None,
14071409
use_cache=False, env_file=()),
@@ -1804,7 +1806,8 @@ def test_make_without_build_container_tag(self, skipper_runner_run_mock):
18041806
)
18051807
expected_commands = [
18061808
mock.call(['build', '--network=host', '-t', 'build-container-image', '-f',
1807-
'Dockerfile.build-container-image', '.', '--ulimit', 'nofile=65536:65536']),
1809+
'Dockerfile.build-container-image', '.', '--ulimit', 'nofile=65536:65536'],
1810+
stdout_to_stderr=True),
18081811
mock.call(['make'] + make_params, fqdn_image='build-container-image', environment=[],
18091812
interactive=False, name=None, net=None, publish=(), volumes=None, workdir=None, workspace=None,
18101813
use_cache=False, env_file=()),

0 commit comments

Comments
 (0)