From: Loic Dachary Date: Thu, 8 Jun 2017 17:00:05 +0000 (+0200) Subject: ceph-disk: implement command_with_stdin X-Git-Tag: ses5-milestone9~1^2~6^2~13 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=597393d5d09c52170b7db6f609ce142edfd4d8fe;p=ceph.git ceph-disk: implement command_with_stdin Signed-off-by: Loic Dachary --- diff --git a/src/ceph-disk/ceph_disk/main.py b/src/ceph-disk/ceph_disk/main.py index c5d06e079fdd..e7d179948618 100755 --- a/src/ceph-disk/ceph_disk/main.py +++ b/src/ceph-disk/ceph_disk/main.py @@ -478,6 +478,26 @@ def command(arguments, **kwargs): return _bytes2str(out), _bytes2str(err), process.returncode +def command_with_stdin(arguments, stdin): + LOG.info("Running command with stdin: " + " ".join(arguments)) + process = subprocess.Popen( + arguments, + stdin=subprocess.PIPE, + stdout=subprocess.PIPE, + stderr=subprocess.PIPE) + out, err = process.communicate(stdin) + LOG.debug(out) + if process.returncode != 0: + LOG.error(err) + raise SystemExit( + "'{cmd}' failed with status code {returncode}".format( + cmd=arguments, + returncode=process.returncode, + ) + ) + return out + + def _bytes2str(string): return string.decode('utf-8') if isinstance(string, bytes) else string