"vscode:/vscode.git/clone" did not exist on "27a6ad456097c5ef43f3fcb9223416e3762c6f73"
utils.py 2.11 KB
Newer Older
1
import multiprocessing as mp
2
import os
3
4
5
import subprocess
from typing import Optional

6
7
8
9
10
11

def run(ssh_cmd):
    subprocess.check_call(ssh_cmd, shell=True)


def execute_remote(
12
    cmd: str, ip: str, port: Optional[int] = 22, username: Optional[str] = ""
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
) -> mp.Process:
    """Execute command line on remote machine via ssh.

    Args:
        cmd: User-defined command (udf) to execute on the remote host.
        ip: The ip-address of the host to run the command on.
        port: Port number that the host is listening on.
        username: Optional. If given, this will specify a username to use when issuing commands over SSH.
            Useful when your infra requires you to explicitly specify a username to avoid permission issues.

    Returns:
        Process: The Process whose run() is to run the `cmd` on the remote host. Returns when the cmd completes
            on the remote host.
    """
    ip_prefix = ""
    if username:
        ip_prefix += "{username}@".format(username=username)
30

31
    custom_port = os.getenv("DIST_DGL_TEST_SSH_PORT", "")
32
33
34
    if custom_port:
        port = custom_port

35
    custom_ssh_key = os.getenv("DIST_DGL_TEST_SSH_KEY", "")
36
37
38
39
    if custom_ssh_key:
        custom_ssh_key = os.path.expanduser(custom_ssh_key)
        custom_ssh_key = "-i " + custom_ssh_key

40
    ssh_setup = os.getenv("DIST_DGL_TEST_SSH_SETUP", "")
41
    if ssh_setup:
42
        cmd = ssh_setup + ";" + cmd
43
    # Construct ssh command that executes `cmd` on the remote host
44
45
    ssh_cmd = "ssh -o StrictHostKeyChecking=no {ssh_key} -p {port} {ip_prefix}{ip} '{cmd}'".format(
        ssh_key=custom_ssh_key,
46
47
48
49
50
        port=str(port),
        ip_prefix=ip_prefix,
        ip=ip,
        cmd=cmd,
    )
51
    ctx = mp.get_context("spawn")
52
53
54
55
    proc = ctx.Process(target=run, args=(ssh_cmd,))
    proc.start()
    return proc

56

57
58
59
60
61
62
63
def get_ips(ip_config):
    ips = []
    with open(ip_config) as f:
        for line in f:
            result = line.strip().split()
            if len(result) != 1:
                raise RuntimeError(
64
65
                    "Invalid format of ip_config:{}".format(ip_config)
                )
66
67
            ips.append(result[0])
    return ips