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
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
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 |
|---|---|
Optional[int]
|
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
961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 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 | |
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
534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 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 | |
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.