Skip to content

Commit

Permalink
transport.clustershell: handle str when reporting commands
Browse files Browse the repository at this point in the history
When the callback methods (ev_*) are called by clustershell, the
worker passed has a property 'command' that is a string, all the other
methods use 'self.commands[some_index]' and that's a Command object.
We were only taking into account the Command objects but forgot to
handle the 'str' passed by clustershell.

Bug: T275210
Change-Id: I0cf430ec301f0151712beeec7f7bd15dc85fc7d7
Signed-off-by: David Caro <david@dcaro.es>
  • Loading branch information
david-caro committed Feb 22, 2021
1 parent 75cb44d commit 09d8616
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions cumin/transports/clustershell.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,17 +210,17 @@ def _get_log_message(self, num: int, num_hosts: int, message: str, # pylint: di

return log_message, nodes_string

def _get_short_command(self, command: Command) -> str:
def _get_short_command(self, command: Union[str, Command]) -> str:
"""Return a shortened representation of a command omitting the central part, if it's too long.
Arguments:
command (Command): the command to be shortened.
command (str or Command optional): the command to be shortened.
Returns:
str: the short command.
"""
cmd = command.command
cmd = str(command)
sublen = (self.short_command_length - 3) // 2 # The -3 is for the ellipsis
return (cmd[:sublen] + '...' + cmd[-sublen:]) if len(cmd) > self.short_command_length else cmd

Expand Down Expand Up @@ -258,7 +258,7 @@ def commands_output_report(self, buffer_iterator: Any, command: Optional[Command

tqdm.write(Colored.blue(message), file=sys.stdout)

def report_single_command_output(self, command: Command) -> None:
def report_single_command_output(self, command: str) -> None:
"""Reports a single command execution."""
output_message = "----- OUTPUT of '{command}' -----".format(
command=self._get_short_command(command))
Expand Down Expand Up @@ -512,7 +512,7 @@ def _success_nodes_report(self, command: Optional[Command] = None) -> None:
"""Print how many nodes successfully executed all commands in a colored and tqdm-friendly way.
Arguments:
command (str, optional): the command the report is referring to.
command (Command, optional): the command the report is referring to.
"""
if self.global_timedout and command is None:
Expand Down Expand Up @@ -700,9 +700,9 @@ def ev_timer(self, timer): # noqa, mccabe: MC0001 too complex (15) FIXME
# No more nodes were left for the execution of the current command
with self.lock: # Avoid modifications of the same data from other callbacks triggered by ClusterShell
try:
command = self.commands[self.current_command_index].command
command: Optional[str] = self.commands[self.current_command_index].command
except IndexError:
command = None # Last command reached
command: Optional[str] = None # Last command reached

# Get a list of the nodes still in pending state
pending = [pending_node.name for pending_node in self.nodes.values() if pending_node.state.is_pending]
Expand Down

0 comments on commit 09d8616

Please sign in to comment.