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, running_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 |
running_process
|
RunningSSHProcess
|
Runtime model for the process being monitored |
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[ExitStatus], 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
Methods:
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
SSHClientWatcherThread(uuid, running_process, manager, hostname, user, metadata_file, logger)
Bases: Thread
Thread that monitors the local SSH client and classifies its 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[ExitStatus], Optional[Exception]], None]]
|
Optional callback function(uuid, exit_status, exception) invoked when process exits |
None
|
Source code in drunc/processes/ssh_process_lifetime_manager_shell.py
Methods:
crash_process(uuid, signal='KILL')
Simulate an unexpected process crash by sending by ending the remote process without cleanup. 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
get_runtime_pids(uuid)
Return best-effort runtime PID snapshot for a managed process.
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[ExitStatus]]
|
Dictionary mapping all process UUIDs to their exit statuses |
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 |
|---|---|
ExitStatus | None
|
ExitStatus of the terminated process, or None if not found or still running |
Source code in drunc/processes/ssh_process_lifetime_manager_shell.py
1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 | |
kill_process_without_metadata(uuid, signal_name='KILL', as_manual_pm_kill=True, timeout=ProcessLifetimeManager.DEFAULT_TIMEOUT_FOR_KILLING_PROCESS)
Terminate process by signalling the local SSH client without using remote metadata. Prefer kill_process(..) to this method, this is mainly intended to help with testing
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
uuid
|
str
|
Process UUID to terminate |
required |
signal_name
|
str
|
Signal to send to SSH client process group (QUIT/KILL) |
'KILL'
|
as_manual_pm_kill
|
bool
|
If True, classify as process-manager initiated kill. If False, classify as external kill i.e. outside of process manager control |
True
|
timeout
|
float
|
Maximum time to wait for process termination in seconds |
DEFAULT_TIMEOUT_FOR_KILLING_PROCESS
|
Returns:
| Type | Description |
|---|---|
Optional[ExitStatus]
|
ExitStatus if termination state can be determined, None otherwise |
Source code in drunc/processes/ssh_process_lifetime_manager_shell.py
1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 | |
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[ExitStatus]]
|
Dictionary mapping process UUIDs to their exit statuses |
Source code in drunc/processes/ssh_process_lifetime_manager_shell.py
732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 | |
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[ExitStatus]]
|
Dictionary mapping terminated process UUIDs to their exit statuses |
Source code in drunc/processes/ssh_process_lifetime_manager_shell.py
pop_early_exit_status(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[ExitStatus]
|
ExitStatus 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
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 | |
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.
Source code in drunc/processes/ssh_process_lifetime_manager_shell.py
wait_for_process_exit_code(uuid, timeout)
Wait for specified timeout to see if a process exit code is available.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
uuid
|
str
|
Process UUID to wait for |
required |
timeout
|
float
|
Maximum time to wait in seconds |
required |
Returns:
| Type | Description |
|---|---|
Optional[int]
|
Exit code if process has terminated, None if still running or not found |