Skip to content

Commit

Permalink
Enable redirecting non-interactive run console output to file
Browse files Browse the repository at this point in the history
  • Loading branch information
tfoote committed Jan 30, 2025
1 parent 9cb9d13 commit 8c58828
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions src/rocker/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# limitations under the License.

from collections import OrderedDict
from contextlib import nullcontext
import io
import os
import pwd
Expand Down Expand Up @@ -391,6 +392,7 @@ def run(self, command='', **kwargs):

cmd = self.generate_docker_cmd(command, **kwargs)
operating_mode = self.get_operating_mode(kwargs)
console_output_file = kwargs.get('console_output_file')

# $DOCKER_OPTS \
if operating_mode == OPERATIONS_DRY_RUN:
Expand All @@ -399,10 +401,13 @@ def run(self, command='', **kwargs):
return 0
elif operating_mode == OPERATIONS_NON_INTERACTIVE:
try:
print("Executing command: ")
print(cmd)
p = subprocess.run(shlex.split(cmd), check=True, stderr=subprocess.STDOUT)
return p.returncode
with open(console_output_file, 'a') if console_output_file else nullcontext() as consoleout_fh:
if console_output_file:
print(f"Logging output to file {console_output_file}")
print("Executing command: ")
print(cmd)
p = subprocess.run(shlex.split(cmd), check=True, stdout=consoleout_fh if console_output_file else None, stderr=subprocess.STDOUT)
return p.returncode
except subprocess.CalledProcessError as ex:
print("Non-interactive Docker run failed\n", ex)
return ex.returncode
Expand Down

0 comments on commit 8c58828

Please sign in to comment.