Skip to content
Snippets Groups Projects
Commit fbb6b864 authored by Michael Hanselmann's avatar Michael Hanselmann
Browse files

import/export daemon: Simplify command building


Instead of appending strings, stage parts in a list. Building the "dd"
command is moved to a separate function.

Signed-off-by: default avatarMichael Hanselmann <hansmi@google.com>
Reviewed-by: default avatarGuido Trotter <ultrotter@google.com>
parent 0559f745
No related merge requests found
...@@ -197,14 +197,10 @@ class CommandBuilder(object): ...@@ -197,14 +197,10 @@ class CommandBuilder(object):
",".join(addr1), ",".join(addr2) ",".join(addr1), ",".join(addr2)
] ]
def _GetTransportCommand(self): def _GetDdCommand(self):
"""Returns the command for the transport part of the daemon. """Returns the command for measuring throughput.
""" """
socat_cmd = ("%s 2>&%d" %
(utils.ShellQuoteArgs(self._GetSocatCommand()),
self._socat_stderr_fd))
dd_cmd = StringIO() dd_cmd = StringIO()
# Setting LC_ALL since we want to parse the output and explicitely # Setting LC_ALL since we want to parse the output and explicitely
# redirecting stdin, as the background process (dd) would have /dev/null as # redirecting stdin, as the background process (dd) would have /dev/null as
...@@ -216,32 +212,46 @@ class CommandBuilder(object): ...@@ -216,32 +212,46 @@ class CommandBuilder(object):
# And wait for dd # And wait for dd
dd_cmd.write(" wait $pid;") dd_cmd.write(" wait $pid;")
dd_cmd.write(" }") dd_cmd.write(" }")
return dd_cmd.getvalue()
def _GetTransportCommand(self):
"""Returns the command for the transport part of the daemon.
"""
socat_cmd = ("%s 2>&%d" %
(utils.ShellQuoteArgs(self._GetSocatCommand()),
self._socat_stderr_fd))
dd_cmd = self._GetDdCommand()
compr = self._opts.compress compr = self._opts.compress
assert compr in constants.IEC_ALL assert compr in constants.IEC_ALL
parts = []
if self._mode == constants.IEM_IMPORT: if self._mode == constants.IEM_IMPORT:
parts.append(socat_cmd)
if compr == constants.IEC_GZIP: if compr == constants.IEC_GZIP:
transport_cmd = "%s | gunzip -c" % socat_cmd parts.append("gunzip -c")
else:
transport_cmd = socat_cmd parts.append(dd_cmd)
transport_cmd += " | %s" % dd_cmd.getvalue()
elif self._mode == constants.IEM_EXPORT: elif self._mode == constants.IEM_EXPORT:
parts.append(dd_cmd)
if compr == constants.IEC_GZIP: if compr == constants.IEC_GZIP:
transport_cmd = "gzip -c | %s" % socat_cmd parts.append("gzip -c")
else:
transport_cmd = socat_cmd parts.append(socat_cmd)
transport_cmd = "%s | %s" % (dd_cmd.getvalue(), transport_cmd)
else: else:
raise errors.GenericError("Invalid mode '%s'" % self._mode) raise errors.GenericError("Invalid mode '%s'" % self._mode)
# TODO: Run transport as separate user # TODO: Run transport as separate user
# The transport uses its own shell to simplify running it as a separate user # The transport uses its own shell to simplify running it as a separate user
# in the future. # in the future.
return self.GetBashCommand(transport_cmd) return self.GetBashCommand(" | ".join(parts))
def GetCommand(self): def GetCommand(self):
"""Returns the complete child process command. """Returns the complete child process command.
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment