Skip to content

Commit eaa7d87

Browse files
authored
Adding option to pipe stdout and err on run command (#14)
1 parent 1c0fdbe commit eaa7d87

File tree

3 files changed

+41
-5
lines changed

3 files changed

+41
-5
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
*~
22
*.pyc
33
bin/
4+
venv/
45
include/
56
lib/
67
local/

manof/image.py

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import os
2+
import sys
23
import pipes
34
import inspect
45
import re
56

67
from twisted.internet import defer
78

89
import manof
10+
import utils
911

1012

1113
class Image(manof.Target):
@@ -218,10 +220,12 @@ def run(self):
218220
print command
219221

220222
try:
221-
yield self._run_command(command)
222-
except Exception as exc:
223-
self._logger.warn('Failed running container', err=str(exc))
223+
out, _, _ = yield self._run_command(command)
224+
225+
if self.pipe_stdout:
226+
print >> sys.stdout, out
224227

228+
except Exception as exc:
225229
dangling_container_error = re.search(
226230
'endpoint with name (?P<container_name>.*) already exists in network (?P<network>.*).',
227231
exc.message)
@@ -231,9 +235,17 @@ def run(self):
231235
network = dangling_container_error.group('network')
232236
yield self._disconnect_container_from_network(container_name, network)
233237

234-
self._logger.debug('Reruning container', command=command)
238+
self._logger.debug('Re-running container', command=command)
235239
yield self._run_command(command)
240+
236241
else:
242+
243+
if self.pipe_stderr:
244+
if isinstance(exc, utils.CommandFailedError):
245+
print >> sys.stderr, exc.err
246+
else:
247+
print >> sys.stderr, str(exc)
248+
237249
raise exc
238250

239251
@defer.inlineCallbacks
@@ -350,6 +362,14 @@ def container_name(self):
350362
def command(self):
351363
return None
352364

365+
@property
366+
def pipe_stdout(self):
367+
return False
368+
369+
@property
370+
def pipe_stderr(self):
371+
return False
372+
353373
@property
354374
def hostname(self):
355375
return None

manof/utils/__init__.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ def __init__(self, command=None, code=None, cwd=None, out=None, err=None, signal
2222
:param signal: the signal which killed the process
2323
:type signal: int
2424
"""
25+
self._code = code
26+
self._out = out
27+
self._err = err
2528

2629
if code is not None:
2730
message = '\'{0}\' exited with code {1}'.format(command, code)
@@ -39,6 +42,18 @@ def __init__(self, command=None, code=None, cwd=None, out=None, err=None, signal
3942

4043
super(CommandFailedError, self).__init__(message)
4144

45+
@property
46+
def code(self):
47+
return self._code
48+
49+
@property
50+
def out(self):
51+
return self._out
52+
53+
@property
54+
def err(self):
55+
return self._err
56+
4257

4358
def git_pull(logger, path):
4459
logger.debug('Pulling', **locals())
@@ -201,7 +216,7 @@ def retry_until_successful(num_of_tries, logger, function, *args, **kwargs):
201216
"""
202217

203218
def _on_operation_callback_error(failure):
204-
logger.warn('Exception during operation execution', function=function.__name__, tb=failure.getBriefTraceback())
219+
logger.debug('Exception during operation execution', function=function.__name__, tb=failure.getBriefTraceback())
205220
raise failure
206221

207222
tries = 1

0 commit comments

Comments
 (0)