Skip to content

_utils

FUNCTION DESCRIPTION
check_ssh_or_fail_and_cleanup

Check SSH connection.

edit_pyproject_toml_in_place_ssh

Wrapper of simplify_pyproject_toml, with I/O.

Classes

Functions:

_copy_wheel_file_ssh(*, task_group, fractal_ssh, logger_name)

Handle the situation where task_group.archive_path is not part of task_group.path, by copying archive_path into path.

RETURNS DESCRIPTION
str

The new archive_path.

Source code in fractal_server/tasks/v2/ssh/_utils.py
def _copy_wheel_file_ssh(
    *,
    task_group: TaskGroupV2,
    fractal_ssh: FractalSSH,
    logger_name: str,
) -> str:
    """
    Handle the situation where `task_group.archive_path` is not part of
    `task_group.path`, by copying `archive_path` into `path`.

    Returns:
        The new `archive_path`.
    """
    logger = get_logger(logger_name=logger_name)
    source = task_group.archive_path
    dest = (
        Path(task_group.path) / Path(task_group.archive_path).name
    ).as_posix()
    cmd = f"cp {source} {dest}"
    logger.debug(f"[_copy_wheel_file_ssh] START {source=} {dest=}")
    fractal_ssh.run_command(cmd=cmd)
    logger.debug(f"[_copy_wheel_file_ssh] END {source=} {dest=}")
    return dest

_customize_and_run_template(*, template_filename, replacements, script_dir_local, script_dir_remote, prefix, fractal_ssh, logger_name)

Customize one of the template bash scripts, transfer it to the remote host via SFTP and then run it via SSH.

PARAMETER DESCRIPTION

template_filename

Filename of the template file (ends with ".sh").

TYPE: str

replacements

Dictionary of replacements.

TYPE: set[tuple[str, str]]

script_dir_remote

Remote scripts directory

TYPE: str

script_dir_local

Local folder where the script will be placed.

TYPE: str

prefix

Prefix for the script filename.

TYPE: str

fractal_ssh

FractalSSH object

TYPE: FractalSSH

Source code in fractal_server/tasks/v2/ssh/_utils.py
def _customize_and_run_template(
    *,
    template_filename: str,
    replacements: set[tuple[str, str]],
    script_dir_local: str,
    script_dir_remote: str,
    prefix: str,
    fractal_ssh: FractalSSH,
    logger_name: str,
) -> str:
    """
    Customize one of the template bash scripts, transfer it to the remote host
    via SFTP and then run it via SSH.

    Args:
        template_filename: Filename of the template file (ends with ".sh").
        replacements: Dictionary of replacements.
        script_dir_remote: Remote scripts directory
        script_dir_local: Local folder where the script will be placed.
        prefix: Prefix for the script filename.
        fractal_ssh: FractalSSH object
    """
    logger = get_logger(logger_name=logger_name)
    logger.debug(f"_customize_and_run_template {template_filename} - START")

    script_path_remote = _customize_and_send_template(
        template_filename=template_filename,
        replacements=replacements,
        script_dir_local=script_dir_local,
        script_dir_remote=script_dir_remote,
        prefix=prefix,
        fractal_ssh=fractal_ssh,
        logger_name=logger_name,
    )

    # Execute script remotely
    cmd = f"bash {script_path_remote}"
    logger.debug(f"Now run '{cmd}' over SSH.")
    stdout = fractal_ssh.run_command(cmd=cmd)

    logger.debug(f"_customize_and_run_template {template_filename} - END")
    return stdout

_customize_and_send_template(*, template_filename, replacements, script_dir_local, script_dir_remote, prefix, fractal_ssh, logger_name)

Customize a template bash scripts and transfer it to the remote host.

PARAMETER DESCRIPTION

template_filename

Filename of the template file (ends with ".sh").

TYPE: str

replacements

Dictionary of replacements.

TYPE: set[tuple[str, str]]

script_dir_local

Local folder where the script will be placed.

TYPE: str

script_dir_remote

Remote scripts directory

TYPE: str

prefix

Prefix for the script filename.

TYPE: str

fractal_ssh

FractalSSH object

TYPE: FractalSSH

Source code in fractal_server/tasks/v2/ssh/_utils.py
def _customize_and_send_template(
    *,
    template_filename: str,
    replacements: set[tuple[str, str]],
    script_dir_local: str,
    script_dir_remote: str,
    prefix: str,
    fractal_ssh: FractalSSH,
    logger_name: str,
) -> str:
    """
    Customize a template bash scripts and transfer it to the remote host.

    Args:
        template_filename: Filename of the template file (ends with ".sh").
        replacements: Dictionary of replacements.
        script_dir_local: Local folder where the script will be placed.
        script_dir_remote: Remote scripts directory
        prefix: Prefix for the script filename.
        fractal_ssh: FractalSSH object
    """
    logger = get_logger(logger_name=logger_name)
    logger.debug(f"_customize_and_run_template {template_filename} - START")
    # Prepare name and path of script
    if not template_filename.endswith(".sh"):
        raise ValueError(
            f"Invalid {template_filename=} (it must end with '.sh')."
        )
    script_filename = f"{prefix}_{template_filename}"
    script_path_local = (Path(script_dir_local) / script_filename).as_posix()

    customize_template(
        template_name=template_filename,
        replacements=replacements,
        script_path=script_path_local,
    )

    # Transfer script to remote host
    script_path_remote = os.path.join(
        script_dir_remote,
        script_filename,
    )
    logger.debug(f"Now transfer {script_path_local=} over SSH.")
    fractal_ssh.send_file(
        local=script_path_local,
        remote=script_path_remote,
    )
    return script_path_remote

check_ssh_or_fail_and_cleanup(*, fractal_ssh, task_group, task_group_activity, logger_name, log_file_path, db)

Check SSH connection.

RETURNS DESCRIPTION
bool

Whether SSH connection is OK.

Source code in fractal_server/tasks/v2/ssh/_utils.py
def check_ssh_or_fail_and_cleanup(
    *,
    fractal_ssh: FractalSSH,
    task_group: TaskGroupV2,
    task_group_activity: TaskGroupActivityV2,
    logger_name: str,
    log_file_path: Path,
    db: Session,
) -> bool:
    """
    Check SSH connection.

    Returns:
        Whether SSH connection is OK.
    """
    try:
        fractal_ssh.check_connection()
        return True
    except Exception as e:
        logger = get_logger(logger_name=logger_name)
        logger.error(
            f"Cannot establish SSH connection. Original error: {str(e)}"
        )
        fail_and_cleanup(
            task_group=task_group,
            task_group_activity=task_group_activity,
            logger_name=logger_name,
            log_file_path=log_file_path,
            exception=e,
            db=db,
        )
        return False

edit_pyproject_toml_in_place_ssh(*, fractal_ssh, pyproject_toml_path, resource)

Wrapper of simplify_pyproject_toml, with I/O.

Source code in fractal_server/tasks/v2/ssh/_utils.py
def edit_pyproject_toml_in_place_ssh(
    *,
    fractal_ssh: FractalSSH,
    pyproject_toml_path: Path,
    resource: Resource,
) -> None:
    """
    Wrapper of `simplify_pyproject_toml`, with I/O.
    """

    # Read `pyproject.toml`
    pyproject_contents = fractal_ssh.read_remote_text_file(
        pyproject_toml_path.as_posix()
    )

    # Simplify contents
    new_pyproject_contents = simplify_pyproject_toml(
        original_toml_string=pyproject_contents,
        pixi_environment=resource.tasks_pixi_config["DEFAULT_ENVIRONMENT"],
        pixi_platform=resource.tasks_pixi_config["DEFAULT_PLATFORM"],
    )
    # Write new `pyproject.toml`
    fractal_ssh.write_remote_file(
        path=pyproject_toml_path.as_posix(),
        content=new_pyproject_contents,
    )
    logger.debug(
        f"Replaced remote {pyproject_toml_path.as_posix()} "
        "with simplified version."
    )