diff options
| author | Michael Still <mikal@stillhq.com> | 2013-05-07 13:48:18 +1000 |
|---|---|---|
| committer | Michael Still <mikal@stillhq.com> | 2013-05-08 05:55:26 +1000 |
| commit | 19718560a7c3f6b1335dde0b62b544ee4bc83f58 (patch) | |
| tree | 8b1a4034606e90dbb8668ed0b5a5db9d92f9fc08 /nova/openstack | |
| parent | f917d24bd333e7068ae17cc1027dd8e80896e5a5 (diff) | |
| download | nova-19718560a7c3f6b1335dde0b62b544ee4bc83f58.tar.gz nova-19718560a7c3f6b1335dde0b62b544ee4bc83f58.tar.xz nova-19718560a7c3f6b1335dde0b62b544ee4bc83f58.zip | |
Import new additions to oslo's processutils.
trycmd() and ssh_execute() have been moved into oslo. Copy the
oslo version here. Conversion will occur in a later review.
Change-Id: I69926af33ff824f712ae2b8e4023233afe1dff2f
Diffstat (limited to 'nova/openstack')
| -rw-r--r-- | nova/openstack/common/processutils.py | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/nova/openstack/common/processutils.py b/nova/openstack/common/processutils.py index 45a5bf963..6474db4ad 100644 --- a/nova/openstack/common/processutils.py +++ b/nova/openstack/common/processutils.py @@ -34,6 +34,11 @@ from nova.openstack.common import log as logging LOG = logging.getLogger(__name__) +class InvalidArgumentError(Exception): + def __init__(self, message=None): + super(InvalidArgumentError, self).__init__(message) + + class UnknownArgumentError(Exception): def __init__(self, message=None): super(UnknownArgumentError, self).__init__(message) @@ -179,3 +184,64 @@ def execute(*cmd, **kwargs): # call clean something up in between calls, without # it two execute calls in a row hangs the second one greenthread.sleep(0) + + +def trycmd(*args, **kwargs): + """ + A wrapper around execute() to more easily handle warnings and errors. + + Returns an (out, err) tuple of strings containing the output of + the command's stdout and stderr. If 'err' is not empty then the + command can be considered to have failed. + + :discard_warnings True | False. Defaults to False. If set to True, + then for succeeding commands, stderr is cleared + + """ + discard_warnings = kwargs.pop('discard_warnings', False) + + try: + out, err = execute(*args, **kwargs) + failed = False + except ProcessExecutionError, exn: + out, err = '', str(exn) + failed = True + + if not failed and discard_warnings and err: + # Handle commands that output to stderr but otherwise succeed + err = '' + + return out, err + + +def ssh_execute(ssh, cmd, process_input=None, + addl_env=None, check_exit_code=True): + LOG.debug(_('Running cmd (SSH): %s'), cmd) + if addl_env: + raise InvalidArgumentError(_('Environment not supported over SSH')) + + if process_input: + # This is (probably) fixable if we need it... + raise InvalidArgumentError(_('process_input not supported over SSH')) + + stdin_stream, stdout_stream, stderr_stream = ssh.exec_command(cmd) + channel = stdout_stream.channel + + # NOTE(justinsb): This seems suspicious... + # ...other SSH clients have buffering issues with this approach + stdout = stdout_stream.read() + stderr = stderr_stream.read() + stdin_stream.close() + + exit_status = channel.recv_exit_status() + + # exit_status == -1 if no exit code was returned + if exit_status != -1: + LOG.debug(_('Result was %s') % exit_status) + if check_exit_code and exit_status != 0: + raise ProcessExecutionError(exit_code=exit_status, + stdout=stdout, + stderr=stderr, + cmd=cmd) + + return (stdout, stderr) |
