ssh_process_lifetime_manager_shell
drunc.processes.ssh_process_lifetime_manager_shell
Provides SSH connection and lifetime management using sh library to invoke shell commands over SSH.
Classes
ProcessWatcherThread(uuid, process, manager, hostname, user, metadata_file, on_exit, logger)
Bases: Thread
Thread that monitors a background SSH process and invokes callback on exit.
Initialise process watcher thread.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
uuid
|
str
|
Process UUID to monitor |
required |
process
|
RunningCommand
|
sh.RunningCommand instance to monitor |
required |
manager
|
SSHProcessLifetimeManagerShell
|
Parent manager instance for metadata updates |
required |
hostname
|
str
|
Remote hostname for metadata retrieval |
required |
user
|
str
|
Remote user for metadata retrieval |
required |
metadata_file
|
str
|
Path to metadata file on remote host |
required |
on_exit
|
Optional[Callable[[str, Optional[int], Optional[Exception]], None]]
|
Callback function invoked on process exit |
required |
logger
|
Logger
|
Logger instance for output |
required |
Source code in drunc/processes/ssh_process_lifetime_manager_shell.py
Functions
is_monitoring_remotely()
Check if the watcher is monitoring the remote process directly.
Returns:
| Type | Description |
|---|---|
bool
|
True if monitoring remote process, False if monitoring SSH client |
Source code in drunc/processes/ssh_process_lifetime_manager_shell.py
run()
Monitor process, read metadata asynchronously, and invoke callback on exit.
Source code in drunc/processes/ssh_process_lifetime_manager_shell.py
SSHProcessLifetimeManagerShell(disable_host_key_check=False, disable_localhost_host_key_check=False, logger=None, on_process_exit=None)
Bases: ProcessLifetimeManager
Manages process lifecycle using sh library for SSH connections. Uses the sh library's SSH command wrapper to start and manage remote processes.
Initialise SSH process lifetime manager using sh library.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
disable_host_key_check
|
bool
|
Disable SSH host key verification for all hosts |
False
|
disable_localhost_host_key_check
|
bool
|
Disable SSH host key verification for localhost |
False
|
logger
|
Optional[Logger]
|
Logger instance for real-time output logging |
None
|
on_process_exit
|
Optional[Callable[[str, Optional[int], Optional[Exception]], None]]
|
Optional callback function(uuid, exit_code, exception) invoked when process exits |
None
|
Source code in drunc/processes/ssh_process_lifetime_manager_shell.py
Functions
crash_process(uuid)
Simulate a process crash by sending SIGKILL without performing any cleanup.
Sends SIGKILL to the remote process identified by uuid but deliberately skips all cleanup steps (metadata file removal, internal tracking cleanup, SSH client termination). This leaves the process manager in the same state as if the process had crashed unexpectedly, allowing crash-recovery logic to be exercised in tests.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
uuid
|
str
|
Process UUID to crash |
required |
Source code in drunc/processes/ssh_process_lifetime_manager_shell.py
get_active_process_keys()
Get list of active process UUIDs.
Returns:
| Type | Description |
|---|---|
List[str]
|
List of active process UUID strings |
get_metadata_file_path(uuid)
staticmethod
Generate metadata file path for a given process UUID.
Uses XDG_RUNTIME_DIR if available, otherwise falls back to /tmp. The path will be expanded on the remote host when the command executes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
uuid
|
str
|
Process UUID to generate metadata file path for |
required |
Returns:
| Type | Description |
|---|---|
str
|
Shell-expandable path string containing environment variable reference |
Source code in drunc/processes/ssh_process_lifetime_manager_shell.py
get_process_stderr(uuid)
Get stderr from process.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
uuid
|
str
|
Process UUID |
required |
Returns:
| Type | Description |
|---|---|
Optional[str]
|
Accumulated stderr content as string, None if not found |
Source code in drunc/processes/ssh_process_lifetime_manager_shell.py
get_process_stdout(uuid)
Get stdout from process.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
uuid
|
str
|
Process UUID |
required |
Returns:
| Type | Description |
|---|---|
Optional[str]
|
Accumulated stdout content as string, None if not found |
Source code in drunc/processes/ssh_process_lifetime_manager_shell.py
get_remote_pid(uuid)
Return the remote PID for the process identified by uuid.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
uuid
|
str
|
Process UUID to query. |
required |
Returns:
| Type | Description |
|---|---|
RemotePidResult
|
RemotePidResult with |
RemotePidResult
|
set to |
RemotePidResult
|
been written or could not be read. |
Source code in drunc/processes/ssh_process_lifetime_manager_shell.py
is_process_alive(uuid)
Check if process is alive.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
uuid
|
str
|
Process UUID to check |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if process is alive, False otherwise |
Source code in drunc/processes/ssh_process_lifetime_manager_shell.py
kill_all_processes(process_timeouts=None)
Kill all active processes in role-based shutdown order.
Retrieves all active process UUIDs and delegates to kill_processes() for role-based termination. Waits for all monitoring threads to complete after termination.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
process_timeouts
|
Optional[Dict[str, float]]
|
Dictionary mapping process UUIDs to timeout values in seconds. Uses default timeout for unmapped UUIDs. |
None
|
Returns:
| Type | Description |
|---|---|
Dict[str, Optional[int]]
|
Dictionary mapping all process UUIDs to their exit codes |
Source code in drunc/processes/ssh_process_lifetime_manager_shell.py
kill_process(uuid, timeout=ProcessLifetimeManager.DEFAULT_TIMEOUT_FOR_KILLING_PROCESS)
Kill a remote process and clean up all associated resources.
Sends termination signals to the remote process, waits for it to die, cleans up remote metadata files, terminates the SSH client, and removes the process from internal tracking. Safe to call multiple times.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
uuid
|
str
|
Process UUID to terminate |
required |
timeout
|
float
|
Timeout for graceful termination in seconds |
DEFAULT_TIMEOUT_FOR_KILLING_PROCESS
|
Returns:
| Type | Description |
|---|---|
int | None
|
Exit code of the terminated process, or None if not found or still running |
Source code in drunc/processes/ssh_process_lifetime_manager_shell.py
995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 | |
kill_processes(uuids, process_timeouts=None)
Kill multiple processes by their UUIDs in role-based shutdown order.
Executes a staged shutdown by role. Processes within each role are terminated asynchronously. After all roles complete, any remaining processes are killed asynchronously as a fallback.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
uuids
|
List[str]
|
List of process UUIDs to terminate |
required |
process_timeouts
|
Optional[Dict[str, float]]
|
Dictionary mapping process UUIDs to timeout values in seconds. Uses default timeout for unmapped UUIDs. |
None
|
Returns:
| Type | Description |
|---|---|
Dict[str, Optional[int]]
|
Dictionary mapping process UUIDs to their exit codes |
Source code in drunc/processes/ssh_process_lifetime_manager_shell.py
568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 | |
kill_processes_by_role(role, candidate_uuids, process_timeouts=None)
Kill all processes with the specified role from candidate UUID list.
Filters candidate UUIDs by matching metadata roles, then terminates matching processes asynchronously using a thread pool.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
role
|
str
|
Process role to match |
required |
candidate_uuids
|
List[str]
|
List of process UUIDs to filter by role |
required |
process_timeouts
|
Optional[Dict[str, float]]
|
Dictionary mapping process UUIDs to timeout values in seconds. Uses default timeout for unmapped UUIDs. |
None
|
Returns:
| Type | Description |
|---|---|
Dict[str, Optional[int]]
|
Dictionary mapping terminated process UUIDs to their exit codes |
Source code in drunc/processes/ssh_process_lifetime_manager_shell.py
pop_early_exit_code(uuid)
Get process exit code if process exited early without being killed.
This method checks if a process has terminated unexpectedly (without kill_process being called). If an exit code is found, the process resources are cleaned up automatically.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
uuid
|
str
|
Process UUID |
required |
Returns:
| Type | Description |
|---|---|
Optional[int]
|
Exit code if process has terminated early, None if still running or not found |
Source code in drunc/processes/ssh_process_lifetime_manager_shell.py
read_log_file(hostname, user, log_file, num_lines=100)
Read remote log file via SSH.
Source code in drunc/processes/ssh_process_lifetime_manager_shell.py
read_process_metadata(uuid, metadata_file, hostname, user, timeout=ProcessLifetimeManager.DEFAULT_TIMEOUT_FOR_READING_METADATA)
Read process metadata from remote JSON file with a single SSH call.
Uses a remote-side wait loop to avoid multiple SSH round-trips. The remote command polls for file existence and reads it once available, all within a single SSH session.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
uuid
|
str
|
Process UUID for identification in logs |
required |
metadata_file
|
str
|
Absolute path to metadata file on remote host |
required |
hostname
|
str
|
Target hostname for SSH connection |
required |
user
|
str
|
SSH username for authentication |
required |
timeout
|
float
|
Maximum time in seconds to wait for metadata file availability |
DEFAULT_TIMEOUT_FOR_READING_METADATA
|
Returns:
| Type | Description |
|---|---|
Optional[ProcessMetadata]
|
ProcessMetadata instance if file exists and is valid, None otherwise |
Source code in drunc/processes/ssh_process_lifetime_manager_shell.py
start_process(uuid, boot_request)
Start a remote process via SSH using the boot request configuration.
Extracts all necessary parameters from the boot request and executes the process on the remote host using sh library's SSH wrapper.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
uuid
|
str
|
Unique identifier for this process |
required |
boot_request
|
BootRequest
|
BootRequest containing process configuration, metadata, environment variables, and execution parameters |
required |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If SSH connection or process execution fails |
Source code in drunc/processes/ssh_process_lifetime_manager_shell.py
validate_host_connection(host, auth_method, user=getpass.getuser())
Validate SSH connection to the specified host.