ClusterShell¶
Transport ClusterShell: worker and event handlers.
- class cumin.transports.clustershell.ClusterShellWorker(config: dict, target: Target)[source]¶
Bases:
BaseWorker
It provides a Cumin worker for SSH using the ClusterShell library.
This transport uses the
ClusterShell
Python library to connect to the selected hosts and execute a list of commands. This transport accept the following customizations:sync
execution mode: given a list of commands, the first one will be executed on all the hosts, then, if the success ratio is reached, the second one will be executed on all hosts where the first one was successful, and so on.async
execution mode: given a list of commands, on each hosts the commands will be executed sequentially, interrupting the execution on any single host at the first command that fails. The execution on the hosts is independent between each other.custom execution mode: can be achieved creating a custom event handler class that extends the
BaseEventHandler
class defined incumin/transports/clustershell.py
, implementing its abstract methods and setting to this class object the handler to the transport.
Worker ClusterShell constructor.
- Parameters:
according to parent
cumin.transports.BaseWorker.__init__()
.
- execute() int [source]¶
Execute the commands on all the targets using the handler.
Concrete implementation of parent abstract method.
- Parameters:
according to parent
cumin.transports.BaseWorker.execute()
.
- get_results()[source]¶
Get the results of the last task execution.
Concrete implementation of parent abstract method.
- Parameters:
according to parent
cumin.transports.BaseWorker.get_results()
.
- property handler: Type[BaseEventHandler] | None¶
Concrete implementation of parent abstract getter and setter.
Accepted values for the setter: * an instance of a custom handler class derived from
BaseEventHandler
. * astr
with one of the available default handler listed inDEFAULT_HANDLERS
.The event handler is mandatory for this transport.
- Parameters:
according to parent
cumin.transports.BaseWorker.handler
.
- property reporter: Type[BaseReporter]¶
Getter for the reporter property.
It must be a subclass of
cumin.transports.clustershell.BaseReporter
.
- class cumin.transports.clustershell.Node(name: str, commands: List[Command])[source]¶
Bases:
object
Node class to represent each target node.
Additional attributes available are:
- state¶
cumin.transports.State
: the state of the node.
Node class constructor with default values.
- Parameters:
name (str) -- the hostname of the node.
commands (list) -- a list of
cumin.transports.Command
objects to be executed on the node.
- class cumin.transports.clustershell.BaseReporter[source]¶
Bases:
object
abstract
Reporter base class that does not report anything.Initializes a Reporter.
- abstract global_timeout_nodes(nodes: Dict[str, Node], num_hosts: int) None [source]¶
Print the nodes that were caught by the global timeout in a colored and tqdm-friendly way.
- abstract failed_nodes(nodes: Dict[str, Node], num_hosts: int, commands: List[Command], filter_command_index: int = -1) None [source]¶
Print the nodes that failed to execute commands in a colored and tqdm-friendly way.
- Parameters:
- abstract success_nodes(command: Command | None, num_successfull_nodes: int, success_ratio: float, tot: int, num_hosts: int, success_threshold: float, nodes: Dict[str, Node]) None [source]¶
Print how many nodes successfully executed all commands in a colored and tqdm-friendly way.
- Parameters:
command (Command) -- the command that was executed
num_successfull_nodes (int) -- the number of nodes on which the execution was successful
success_ratio (float) -- the ratio of successful nodes
tot (int) -- total number of successful executions
num_hosts (int) -- the total number of nodes.
success_threshold (float) -- the threshold of successful nodes above which the command execution is deemed successful
nodes (list) -- the nodes on which the command was executed
- abstract command_completed() None [source]¶
To be called on completion of processing, when no command specific output is required.
- abstract command_output(buffer_iterator: Any, command: Command | None = None) None [source]¶
Print the command output in a colored and tqdm-friendly way.
- Parameters:
buffer_iterator (mixed) -- any ClusterShell object that implements
iter_buffers()
likeClusterShell.Task.Task
and all the Worker objects.command (Command, optional) -- the command the output is referring to.
- class cumin.transports.clustershell.NullReporter(*args, **kwargs)[source]¶
Bases:
BaseReporter
abstract
Reporter class that does not report anything.Initializes a Reporter.
- class cumin.transports.clustershell.TqdmQuietReporter(*args, **kwargs)[source]¶
Bases:
NullReporter
abstract
Reports the progress of command execution without the command output.Initializes a Reporter.
- short_command_length = 35¶
the length to which a command should be shortened in various outputs.
- Type:
- _report_line(message: str, color_func: ~typing.Callable[[str], str] = <function ColoredType.__getattr__.<locals>.<lambda>>, nodes_string: str = '') None [source]¶
Print a tqdm-friendly colored status line with success/failure ratio and optional list of nodes.
- _get_log_message(num: int, num_hosts: int, message: str, nodes: List[str] | None = None) Tuple[str, str] [source]¶
Get a pre-formatted message suitable for logging or printing.
- _get_short_command(command: str | Command) str [source]¶
Return a shortened representation of a command omitting the central part, if it's too long.
- global_timeout_nodes(nodes: Dict[str, Node], num_hosts: int) None [source]¶
Print the nodes that were caught by the global timeout in a colored and tqdm-friendly way.
- Parameters:
according to parent
BaseReporter.global_timeout_nodes()
.
- failed_nodes(nodes: Dict[str, Node], num_hosts: int, commands: List[Command], filter_command_index: int = -1) None [source]¶
Print the nodes that failed to execute commands in a colored and tqdm-friendly way.
- Parameters:
according to parent
BaseReporter.failed_nodes()
.
- success_nodes(command: Command | None, num_successfull_nodes: int, success_ratio: float, tot: int, num_hosts: int, success_threshold: float, nodes: Dict[str, Node]) None [source]¶
Print how many nodes successfully executed all commands in a colored and tqdm-friendly way.
- Parameters:
according to parent
BaseReporter.success_nodes()
.
- class cumin.transports.clustershell.TqdmReporter(*args, **kwargs)[source]¶
Bases:
TqdmQuietReporter
Reports the progress of command execution with full command output.
Initializes a Reporter.
- command_completed() None [source]¶
To be called on completion of processing, when no command specific output is required.
- Parameters:
according to parent
BaseReporter.command_completed()
.
- command_output(buffer_iterator: Any, command: Command | None = None) None [source]¶
Print the command output in a colored and tqdm-friendly way.
- Parameters:
according to parent
BaseReporter.command_output()
.
- command_header(command: str) None [source]¶
Reports a single command execution.
- Parameters:
according to parent
BaseReporter.command_header()
.
- message_element(message: MsgTreeElem) None [source]¶
Report a single message as received from the execution of a command on a node.
- Parameters:
according to parent
BaseReporter.message_element()
.
- class cumin.transports.clustershell.BaseEventHandler(target: Target, commands: List[Command], reporter: BaseReporter, progress_bars: BaseExecutionProgress, success_threshold: float = 1.0, **kwargs: Any)[source]¶
Bases:
EventHandler
ClusterShell event handler base class.
Inherit from
ClusterShell.Event.EventHandler
class and define a base EventHandler class to be used in Cumin. It can be subclassed to generate custom EventHandler classes while taking advantage of some common functionalities.Event handler ClusterShell extension constructor.
- Parameters:
target (cumin.transports.Target) -- a Target instance.
commands (list) -- the list of Command objects that has to be executed on the nodes.
reporter (cumin.transports.clustershell.BaseReporter) -- reporter used to output progress.
progress_bars (BaseExecutionProgress) -- the progress bars instance.
success_threshold (float, optional) -- the success threshold, a
float
between0
and1
, to consider the execution successful.**kwargs (optional) -- additional keyword arguments that might be used by derived classes.
- close(task)[source]¶
Additional method called at the end of the whole execution, useful for reporting and final actions.
- Parameters:
task (ClusterShell.Task.Task) -- a ClusterShell Task instance.
- on_timeout(task)[source]¶
Update the state of the nodes and the timeout counter.
Callback called by the
ClusterShellWorker
when aClusterShell.Task.TimeoutError
is raised. It means that the whole execution timed out.- Parameters:
task (ClusterShell.Task.Task) -- a ClusterShell Task instance.
- ev_pickup(worker, node)[source]¶
Command execution started on a node, remove the command from the node's queue.
This callback is triggered by the ClusterShell library for each node when it starts executing a command.
- Parameters:
according to parent
ClusterShell.Event.EventHandler.ev_pickup()
.
- ev_read(worker, node, _, msg)[source]¶
Worker has data to read from a specific node. Print it if running on a single host.
This callback is triggered by ClusterShell for each node when output is available.
- Parameters:
according to parent
ClusterShell.Event.EventHandler.ev_read()
.
- ev_close(worker, timedout)[source]¶
Worker has finished or timed out.
This callback is triggered by ClusterShell when the execution has completed or timed out.
- Parameters:
according to parent
ClusterShell.Event.EventHandler.ev_close()
.
- class cumin.transports.clustershell.SyncEventHandler(target: Target, commands: List[Command], reporter: BaseReporter, progress_bars: BaseExecutionProgress, success_threshold: float = 1.0, **kwargs: Any)[source]¶
Bases:
BaseEventHandler
Custom ClusterShell event handler class that execute commands synchronously.
The implemented logic is:
execute command #N on all nodes where command #`N-1` was successful according to batch_size.
the success ratio is checked at each command completion on every node, and will abort if not met, however nodes already scheduled for execution with ClusterShell will execute the command anyway. The use of the batch_size allow to control this aspect.
if the execution of command #N is completed and the success ratio is greater than the success threshold, re-start from the top with N=N+1.
The typical use case is to orchestrate some operation across a fleet, ensuring that each command is completed by enough nodes before proceeding with the next one.
Define a custom ClusterShell event handler to execute commands synchronously.
- Parameters:
according to parent
BaseEventHandler.__init__()
.
- start_command(schedule: bool = False) None [source]¶
Initialize progress bars and variables for this command execution.
Executed at the start of each command.
- Parameters:
schedule (bool, optional) -- whether the next command should be sent to ClusterShell for execution or not.
- end_command() bool [source]¶
Command terminated, print the result and schedule the next command if criteria are met.
Executed at the end of each command inside a lock.
- on_timeout(task: <module 'ClusterShell.Task' from '/src/.tox/py39-sphinx/lib/python3.9/site-packages/ClusterShell/Task.py'>) None [source]¶
Override parent class on_timeout method to run end_command.
- Parameters:
according to parent
BaseEventHandler.on_timeout()
.
- ev_hup(worker, node, rc)[source]¶
Command execution completed on a node.
This callback is triggered by ClusterShell for each node when it completes the execution of a command. Update the progress bars and keep track of nodes based on the success/failure of the command's execution. Schedule a timer for further decisions.
- Parameters:
according to parent
ClusterShell.Event.EventHandler.ev_hup()
.
- ev_timer(timer)[source]¶
Schedule the current command on the next node or the next command on the first batch of nodes.
This callback is triggered by ClusterShell when a scheduled Task.timer() goes off.
- Parameters:
according to parent
ClusterShell.Event.EventHandler.ev_timer()
.
- class cumin.transports.clustershell.AsyncEventHandler(target: Target, commands: List[Command], reporter: BaseReporter, progress_bars: BaseExecutionProgress, success_threshold: float = 1.0, **kwargs: Any)[source]¶
Bases:
BaseEventHandler
Custom ClusterShell event handler class that execute commands asynchronously.
The implemented logic is:
execute on all nodes independently every command in a sequence, aborting the execution on that node if any command fails.
The success ratio is checked at each node completion (either because it completed all commands or aborted earlier), however nodes already scheduled for execution with ClusterShell will execute the commands anyway. The use of the batch_size allows to control this aspect.
if the success ratio is met, schedule the execution of all commands to the next node.
The typical use case is to execute read-only commands to gather the status of a fleet without any special need of orchestration between the nodes.
Define a custom ClusterShell event handler to execute commands asynchronously between nodes.
- Parameters:
according to parent
BaseEventHandler.__init__()
.
- ev_hup(worker, node, rc)[source]¶
Command execution completed on a node.
This callback is triggered by ClusterShell for each node when it completes the execution of a command. Enqueue the next command if the success criteria are met, track the failure otherwise. Update the progress bars accordingly.
- Parameters:
according to parent
ClusterShell.Event.EventHandler.ev_hup()
.
- ev_timer(timer)[source]¶
Schedule the current command on the next node or the next command on the first batch of nodes.
This callback is triggered by ClusterShell when a scheduled Task.timer() goes off.
- Parameters:
according to parent
ClusterShell.Event.EventHandler.ev_timer()
.
- close(task: <module 'ClusterShell.Task' from '/src/.tox/py39-sphinx/lib/python3.9/site-packages/ClusterShell/Task.py'>) None [source]¶
Concrete implementation of parent abstract method to print the nodes reports and close progress bars.
- Parameters:
according to parent
cumin.transports.BaseEventHandler.close()
.
- cumin.transports.clustershell.worker_class¶
Required by the transport auto-loader in
cumin.transport.Transport.new()
.
- cumin.transports.clustershell.DEFAULT_HANDLERS: Dict[str, Type[EventHandler]] = {'async': <class 'cumin.transports.clustershell.AsyncEventHandler'>, 'sync': <class 'cumin.transports.clustershell.SyncEventHandler'>}¶
mapping of available default event handlers for
ClusterShellWorker
.- Type: