Skip to content

_utils

_copy_wheel_file_ssh(*, task_group, fractal_ssh, logger_name)

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

Returns:

Type Description
str

The new wheel_path.

Source code in fractal_server/tasks/v2/ssh/_utils.py
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
def _copy_wheel_file_ssh(
    *, task_group: TaskGroupV2, fractal_ssh: FractalSSH, logger_name: str
) -> str:
    """
    Handle the situation where `task_group.wheel_path` is not part of
    `task_group.path`, by copying `wheel_path` into `path`.

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

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

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

Parameters:

Name Type Description Default
template_filename str

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

required
replacements list[tuple[str, str]]

Dictionary of replacements.

required
script_dir

Local folder where the script will be placed.

required
prefix str

Prefix for the script filename.

required
fractal_ssh FractalSSH

FractalSSH object

required
script_dir_remote str

Remote scripts directory

required
Source code in fractal_server/tasks/v2/ssh/_utils.py
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
def _customize_and_run_template(
    *,
    template_filename: str,
    replacements: list[tuple[str, str]],
    script_dir_local: str,
    prefix: str,
    fractal_ssh: FractalSSH,
    script_dir_remote: str,
    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: Local folder where the script will be placed.
        prefix: Prefix for the script filename.
        fractal_ssh: FractalSSH object
        script_dir_remote: Remote scripts directory
    """
    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,
    )

    # 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