srunx.ssh package#

Subpackages#

Submodules#

srunx.ssh.example module#

srunx.ssh.example.main()[source]#

Module contents#

class srunx.ssh.SSHSlurmClient(hostname, username, password=None, key_filename=None, port=22, proxy_jump=None, ssh_config_path=None, env_vars=None, verbose=False)[source]#

Bases: object

__init__(hostname, username, password=None, key_filename=None, port=22, proxy_jump=None, ssh_config_path=None, env_vars=None, verbose=False)[source]#
cleanup_file(remote_path)[source]#

Remove a file from the server

Return type:

None

cleanup_job_files(job)[source]#

Cleanup temporary files for a job if it was a local script

Return type:

None

connect()[source]#
Return type:

bool

disconnect()[source]#
execute_command(command)[source]#
Return type:

tuple[str, str, int]

file_exists(remote_path)[source]#

Check if a file exists on the server

Return type:

bool

get_job_output(job_id, job_name=None, stdout_offset=0, stderr_offset=0)[source]#

Get job output from SLURM log files.

First tries scontrol show job to discover the actual StdOut/StdErr paths configured for the job. Falls back to pattern-based search if scontrol doesn’t return usable paths.

When stdout_offset / stderr_offset are non-zero, only the bytes after that position are returned (tail-like incremental reads).

Return type:

tuple[str, str, int, int]

Returns:

(stdout, stderr, new_stdout_offset, new_stderr_offset)

get_job_output_detailed(job_id, job_name=None)[source]#

Get detailed job output information including found log files

Return type:

dict[str, str | list[str] | None]

get_job_status(job_id)[source]#

Get job status using SLURM commands

Return type:

str

monitor_job(job, poll_interval=10, timeout=None)[source]#

Monitor a job until completion

Return type:

SlurmJob

submit_sbatch_file(script_path, job_name=None, cleanup=True)[source]#

Submit an sbatch job from a local or remote file

Return type:

SlurmJob | None

submit_sbatch_job(script_content, job_name=None, dependency=None)[source]#

Submit an sbatch job with script content

Return type:

SlurmJob | None

sync_project(local_path=None, remote_path=None, *, delete=True, dry_run=False, exclude_patterns=None)[source]#

Sync the local project directory to the remote workspace via rsync.

Parameters:
  • local_path (str | None) – Local project root to sync. If None, uses git toplevel or cwd.

  • remote_path (str | None) – Remote destination. If None, uses the default ~/.config/srunx/workspace/{repo_name}/.

  • delete (bool) – Remove remote files not present locally (default True).

  • dry_run (bool) – Preview what would be transferred without syncing.

  • exclude_patterns (list[str] | None) – Additional exclude patterns for this sync.

Return type:

str

Returns:

The remote project path (for use with sbatch --chdir).

Raises:

RuntimeError – If rsync is not available or key-based auth is not configured.

tail_log(job_id, job_name=None, follow=False, last_n=None, poll_interval=1.0)[source]#

Display job logs with optional real-time streaming via SSH.

Parameters:
  • job_id (str) – SLURM job ID

  • job_name (str | None) – Job name for better log file detection

  • follow (bool) – If True, continuously stream new log lines (like tail -f)

  • last_n (int | None) – Show only the last N lines

  • poll_interval (float) – Polling interval in seconds for follow mode

Returns:

  • success: Whether log retrieval was successful

  • log_content: Log content (empty in follow mode)

  • tail_command: Command to execute for follow mode (None in static mode)

  • status_message: Status or error message

  • log_file: Path to the primary log file

Return type:

Dictionary with log information

test_connection()[source]#

Test SSH connection and SLURM availability.

Returns:

  • ssh_connected: Whether SSH connection succeeded

  • slurm_available: Whether SLURM commands are available

  • hostname: Remote hostname

  • user: Remote username

  • slurm_version: SLURM version if available

  • error: Error message if connection failed

Return type:

Dictionary with test results including

upload_file(local_path, remote_path=None)[source]#

Upload a local file to the server and return the remote path

Return type:

str

validate_remote_script(remote_path)[source]#

Validate a remote script file and return (is_valid, error_message)

Return type:

tuple[bool, str]

class srunx.ssh.SlurmJob(job_id, name, status='UNKNOWN', output_file=None, error_file=None, script_path=None, is_local_script=False, _cleanup=False)[source]#

Bases: object

__init__(job_id, name, status='UNKNOWN', output_file=None, error_file=None, script_path=None, is_local_script=False, _cleanup=False)#
error_file: str | None = None#
is_local_script: bool = False#
output_file: str | None = None#
script_path: str | None = None#
status: str = 'UNKNOWN'#
job_id: str#
name: str#