Source code for graflag.ssh

"""SSH operations for GraFlag."""

import shlex
import shutil
import subprocess
from pathlib import Path, PurePosixPath
from typing import List
import logging

from .errors import ManagerUnreachable

logger = logging.getLogger(__name__)


[docs] def remote_path(*parts: str) -> str: """Join path components and quote the result for the remote shell. Every remote command is a string interpreted by the manager's shell, so any component that comes from user input (experiment name, method name, dataset) must be quoted before interpolation. Use this for paths and :func:`shlex.quote` for bare values. Separators are collapsed only at the join boundaries -- the content of a component is never altered, so a name that happens to contain ``/`` stays intact (and quoted) rather than being silently rewritten. """ cleaned = [] last = len(parts) - 1 for i, part in enumerate(parts): p = str(part) if i > 0: p = p.lstrip("/") if i < last: p = p.rstrip("/") if p: cleaned.append(p) return shlex.quote("/".join(cleaned))
#: Options that make ssh fail instead of asking. A client with nobody at the #: keyboard -- the MCP server, whose stdin and stdout are the protocol -- must #: not wait on a password or passphrase prompt: the tool call would hang until #: the client gave up, with nothing saying why. #: The exit status ssh uses for its own errors, as opposed to the remote command's. SSH_FAILED = 255 NON_INTERACTIVE_OPTS = ["-o", "BatchMode=yes", "-o", "ConnectTimeout=15"]
[docs] def require_rsync() -> None: """Raise a usable error when there is no local rsync. Every copy and sync goes through rsync. Without it -- native Windows ships OpenSSH but not rsync -- the first copy died in subprocess with a bare FileNotFoundError that named neither the program nor a way out. """ if shutil.which("rsync") is None: raise RuntimeError( "rsync was not found on this machine; graflag copy and graflag " "sync need it. Install rsync, or on Windows run graflag inside " "WSL, where it is one package away (apt install rsync).")
[docs] class SSHManager: """Handle SSH operations to remote manager.""" def __init__(self, manager_ip: str, ssh_port: str = "22", ssh_key: str = None, batch_mode: bool = False, user: str = "root", proxy_jump: str = None): """Initialize SSH manager. Args: batch_mode: Never prompt (see :data:`NON_INTERACTIVE_OPTS`). Off by default, so a CLI user whose key needs a passphrase still gets asked for it. user: The account on the manager (``SSH_USER``). ``root`` unless configured; another account needs to be in the ``docker`` group and able to write ``SHARED_DIR``. proxy_jump: A jump host for every connection (``SSH_PROXY_JUMP``, ssh's ``-o ProxyJump=``), for a manager that is only reachable through a bastion. """ self.manager_ip = manager_ip self.ssh_port = ssh_port self.ssh_key = ssh_key self.batch_mode = batch_mode self.user = user or "root" self.proxy_jump = proxy_jump or None @property def destination(self) -> str: """``user@manager`` -- the one place every ssh and rsync target is built. It was ``root@{manager_ip}`` spelled out at five call sites, so a cluster whose manager takes another account, or sits behind a jump host, needed a hand-written ~/.ssh/config block to be usable at all. """ return f"{self.user}@{self.manager_ip}" def _option_args(self) -> List[str]: """The -o options every ssh this manager starts carries.""" opts = ["-o", "StrictHostKeyChecking=no"] if self.proxy_jump: opts.extend(["-o", f"ProxyJump={self.proxy_jump}"]) if self.batch_mode: opts.extend(NON_INTERACTIVE_OPTS) return opts def _ssh_args(self) -> List[str]: """Build the ssh argv prefix shared by execute() and log following.""" args = ["ssh"] if self.ssh_key: args.extend(["-i", str(Path(self.ssh_key).expanduser())]) args.extend(["-p", str(self.ssh_port)]) args.extend(self._option_args()) args.append(self.destination) return args
[docs] def execute(self, command: str, capture_output: bool = True) -> subprocess.CompletedProcess: """Execute command on manager via SSH. The command is passed to ssh as a single argv element, so no local shell ever parses it. This keeps quoting, heredocs and newlines in ``command`` intact -- the manager's shell sees exactly what was built here. Callers are still responsible for quoting values they interpolate (see :func:`remote_path`), because the *remote* shell does parse the string. """ ssh_args = self._ssh_args() + [command] logger.debug(f"Executing SSH command: {command}") # stdin is /dev/null because ssh forwards whatever it can read from its # own stdin to the remote command, and reads eagerly. Nothing here # sends data that way (heredocs carry it inside `command`), so all an # inherited stdin could do is be consumed: the rest of a shell loop's # input (`while read m; do graflag run -m "$m" ...; done < list`), or, # under the MCP server, the client's next requests. result = subprocess.run( ssh_args, capture_output=capture_output, text=True, stdin=subprocess.DEVNULL, ) # 255 is ssh's own failure -- unresolvable name, refused key, jump # host down, timeout -- and no remote command here exits with it. # Returned as an ordinary failure, it read as an answer: `graflag list # methods` printed "No methods found" for a manager it never reached, # and path_exists() turned an outage into "Method not found". if result.returncode == SSH_FAILED: detail = (result.stderr or "").strip() if capture_output else "" raise ManagerUnreachable( f"Cannot reach the manager as {self.destination}" + (f" via {self.proxy_jump}" if self.proxy_jump else "") + (f": {detail}" if detail else "") + ". Check MANAGER_IP, SSH_USER, SSH_KEY and SSH_PROXY_JUMP " "in the GraFlag config.") return result
[docs] def path_exists(self, remote_shared_dir: str, path: str) -> bool: """Check if path exists on remote manager.""" result = self.execute(f"test -e {remote_path(remote_shared_dir, path)}") return result.returncode == 0
[docs] def read_file(self, remote_shared_dir: str, path: str) -> str: """Read file content from remote manager.""" result = self.execute(f"cat {remote_path(remote_shared_dir, path)}") if result.returncode == 0: return result.stdout return ""
[docs] def mkdir(self, remote_shared_dir: str, path: str) -> bool: """Create directory on remote manager.""" result = self.execute(f"mkdir -p {remote_path(remote_shared_dir, path)}") return result.returncode == 0
[docs] def claim_dir(self, remote_shared_dir: str, path: str) -> bool: """Create a directory that must not exist yet, atomically. Returns True if this call created it and False if it was already there; raises on any other failure. ``mkdir`` without ``-p`` is the atomic step: of two clients racing for the same name exactly one succeeds, which a ``test -e`` followed by ``mkdir -p`` cannot promise. """ full = remote_path(remote_shared_dir, path) parent = remote_path(remote_shared_dir, str(PurePosixPath(path).parent)) result = self.execute( f"mkdir -p {parent} && " f"if mkdir {full} 2>/dev/null; then echo created; " f"elif [ -d {full} ]; then echo exists; " f"else mkdir {full}; fi" ) reply = (result.stdout or "").strip().splitlines() if result.returncode == 0 and reply and reply[-1] == "created": return True if result.returncode == 0 and reply and reply[-1] == "exists": return False raise RuntimeError( f"could not create {remote_shared_dir}/{path} on the manager: " f"{(result.stderr or '').strip() or f'exit {result.returncode}'}")
[docs] def list_dir(self, remote_shared_dir: str, path: str) -> List[str]: """List directory contents on remote manager.""" result = self.execute( f"ls -1 {remote_path(remote_shared_dir, path)} 2>/dev/null || true" ) if result.returncode == 0 and result.stdout.strip(): return [ item.strip() for item in result.stdout.strip().split("\n") if item.strip() ] return []
[docs] def copy_files(self, source_paths, dest_path: str, recursive: bool = False, from_remote: bool = False) -> str: """ Copy files/directories bidirectionally via rsync. Args: source_paths: Source path(s) - can be single string or list dest_path: Destination path recursive: Accepted for compatibility; rsync -a always recurses from_remote: If True, copy from remote to local; if False (default), copy from local to remote Returns: Destination path """ # Handle single string or list of paths if isinstance(source_paths, str): source_paths = [source_paths] require_rsync() if from_remote: # Copy from remote to local return self._copy_from_remote(source_paths, dest_path, recursive) else: # Copy from local to remote return self._copy_to_remote(source_paths, dest_path, recursive)
def _copy_to_remote(self, local_paths, remote_dest: str, recursive: bool = False) -> str: """Copy files/directories from local to remote via rsync.""" # Validate all local paths exist local_path_objs = [] for local_path in local_paths: local_path_obj = Path(local_path).expanduser() if not local_path_obj.exists(): raise FileNotFoundError(f"Local path does not exist: {local_path}") local_path_objs.append(local_path_obj) # Ensure remote destination directory exists parent_dir = str(Path(remote_dest).parent) self.execute(f"mkdir -p {shlex.quote(parent_dir)}") logger.info(f"[INFO] Copying {len(local_paths)} item(s) to {self.manager_ip}:{remote_dest}") # Build rsync command - more robust than scp. # -a implies -r, so recursion is always on and the `recursive` # argument has no effect. Left that way deliberately: rsync without # -r skips directories *silently* and still exits 0, so honouring the # flag would turn a harmless no-op into a copy that quietly does # nothing. The CLI help documents recursion as automatic instead. rsync_parts = ["rsync", "-avz", "--progress", "--force"] # SSH options ssh_opts = self._option_args() if self.ssh_key: key_path = Path(self.ssh_key).expanduser() if str(key_path).endswith('.pub'): key_path = key_path.with_suffix('') ssh_opts.extend(["-i", str(key_path)]) ssh_opts.extend(["-p", self.ssh_port]) rsync_parts.extend(["-e", "ssh " + shlex.join(ssh_opts)]) # Add all source paths. rsync's trailing slash is load-bearing: # "src/" copies the *contents* of src into dest, "src" copies src # itself as a child of dest. Path() normalises that slash away, so # sync()'s f"{local_dir}/" silently became a copy into # methods/<name>/<name>/ -- a directory nothing reads -- and still # reported [OK]. Validate through Path, transmit the caller's spelling. for original, local_path_obj in zip(local_paths, local_path_objs): trailing = "/" if str(original).endswith(("/", "/.")) else "" rsync_parts.append(str(local_path_obj) + trailing) # Add destination rsync_parts.append(f"{self.destination}:{remote_dest}") logger.debug(f"Executing rsync command: {' '.join(rsync_parts)}") result = subprocess.run(rsync_parts, capture_output=True, text=True, stdin=subprocess.DEVNULL) # see execute() if result.returncode == 0: logger.info(f"[OK] Successfully copied {len(local_paths)} item(s) to {remote_dest}") else: raise RuntimeError(f"Failed to copy files with rsync: {result.stderr}") return remote_dest def _copy_from_remote(self, remote_paths, local_dest: str, recursive: bool = False) -> str: """Copy files/directories from remote to local via rsync.""" # Ensure local destination directory exists local_dest_obj = Path(local_dest).expanduser() local_dest_obj.parent.mkdir(parents=True, exist_ok=True) logger.info(f"[INFO] Copying {len(remote_paths)} item(s) from {self.manager_ip} to {local_dest}") # Build rsync command rsync_parts = ["rsync", "-avz", "--progress", "--force"] # SSH options ssh_opts = self._option_args() if self.ssh_key: key_path = Path(self.ssh_key).expanduser() if str(key_path).endswith('.pub'): key_path = key_path.with_suffix('') ssh_opts.extend(["-i", str(key_path)]) ssh_opts.extend(["-p", self.ssh_port]) rsync_parts.extend(["-e", "ssh " + shlex.join(ssh_opts)]) # Add all source paths (remote) for remote_path in remote_paths: rsync_parts.append(f"{self.destination}:{remote_path}") # Add destination (local) rsync_parts.append(str(local_dest_obj)) logger.debug(f"Executing rsync command: {' '.join(rsync_parts)}") result = subprocess.run(rsync_parts, capture_output=True, text=True, stdin=subprocess.DEVNULL) # see execute() if result.returncode == 0: logger.info(f"[OK] Successfully copied {len(remote_paths)} item(s) to {local_dest}") else: raise RuntimeError(f"Failed to copy files with rsync: {result.stderr}") return str(local_dest_obj)