SPB Git
3.1 KB · 76 lines python
1"""SSH / rsync vers les nœuds via le LAN 192.168.2.x (le SSH inter-nœuds Tailscale est bloqué par ACL)."""2import os3import shlex4import subprocess5from . import config67SSH_OPTS = [8    "-o", "BatchMode=yes", "-o", "ConnectTimeout=8", "-o", "StrictHostKeyChecking=no",9    "-o", "UserKnownHostsFile=/dev/null", "-o", "LogLevel=ERROR", "-o", "ServerAliveInterval=15",10    "-i", config.SSH_KEY,11]12# PATH complet sur le nœud (les shells non interactifs n'ont pas Homebrew)13REMOTE_PATH = "export PATH=/opt/homebrew/bin:/opt/homebrew/sbin:$HOME/bin:$HOME/.local/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin; "141516def target(ip):17    return "%s@%s" % (config.USER, ip)181920def run(ip, cmd, timeout=120, stdin=None, path=True):21    """Exécute `cmd` (bash) sur le nœud. Retourne (rc, stdout, stderr)."""22    full = (REMOTE_PATH if path else "") + cmd23    try:24        p = subprocess.run(["ssh"] + SSH_OPTS + [target(ip), "bash -s"], input=full if stdin is None else stdin,25                           capture_output=True, text=True, timeout=timeout)26        return p.returncode, p.stdout, p.stderr27    except subprocess.TimeoutExpired:28        return 124, "", "timeout after %ss" % timeout293031def run_script(ip, script, timeout=300):32    """Envoie un script complet sur stdin de `bash -s`."""33    return run(ip, "", timeout=timeout, stdin=REMOTE_PATH + "\n" + script)343536def ok(ip, cmd, timeout=60):37    return run(ip, cmd, timeout)[0] == 0383940def rsync_push(local_dir, ip, remote_dir, excludes=(), delete=True, timeout=7200, extra=()):41    """Passerelle -> nœud. remote_dir peut contenir ~ (résolu côté nœud)."""42    args = ["rsync", "-a", "--stats", "-e", "ssh " + " ".join(shlex.quote(o) for o in SSH_OPTS)]43    if delete:44        args.append("--delete")45    for e in excludes:46        args += ["--exclude", e]47    args += list(extra)48    args += ["--rsync-path", "mkdir -p %s && rsync" % shlex.quote(remote_dir), local_dir.rstrip("/") + "/", "%s:%s/" % (target(ip), remote_dir)]49    p = subprocess.run(args, capture_output=True, text=True, timeout=timeout)50    return p.returncode, p.stdout, p.stderr515253def rsync_pull(ip, remote_dir, local_dir, excludes=(), delete=True, timeout=7200):54    """Nœud -> passerelle (staging)."""55    os.makedirs(local_dir, exist_ok=True)56    args = ["rsync", "-a", "--stats", "-e", "ssh " + " ".join(shlex.quote(o) for o in SSH_OPTS)]57    if delete:58        args.append("--delete")59    for e in excludes:60        args += ["--exclude", e]61    args += ["%s:%s/" % (target(ip), remote_dir), local_dir.rstrip("/") + "/"]62    p = subprocess.run(args, capture_output=True, text=True, timeout=timeout)63    return p.returncode, p.stdout, p.stderr646566def scp_to(ip, local_file, remote_path):67    p = subprocess.run(["scp"] + SSH_OPTS + [local_file, "%s:%s" % (target(ip), remote_path)], capture_output=True, text=True)68    return p.returncode == 0697071def write_remote_file(ip, remote_path, content, mode="644"):72    """Écrit un fichier sur le nœud via heredoc (contenu texte)."""73    q = shlex.quote(remote_path)74    script = "mkdir -p $(dirname %s) && cat > %s <<'__MLD_EOF__'\n%s\n__MLD_EOF__\nchmod %s %s" % (q, q, content, mode, q)75    return run(ip, script, timeout=60)76
Files · 29a50362078452d2dd01b6b79868199cb58a3d0f
Menu

Theme