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
1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 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 | |
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
1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 | |
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
729 730 731 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 | |
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
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 |