clan-cli: Rename Host -> Remote move to clan_lib and mark as frozen
This commit is contained in:
@@ -10,15 +10,15 @@ from clan_lib.async_run import AsyncRuntime
|
||||
from clan_lib.cmd import run
|
||||
from clan_lib.errors import ClanError
|
||||
from clan_lib.nix import nix_shell
|
||||
from clan_lib.ssh.parse import parse_deployment_address
|
||||
from clan_lib.ssh.remote import Remote, is_ssh_reachable
|
||||
|
||||
from clan_cli.completions import (
|
||||
add_dynamic_completer,
|
||||
complete_machines,
|
||||
)
|
||||
from clan_cli.machines.machines import Machine
|
||||
from clan_cli.ssh.host import Host, is_ssh_reachable
|
||||
from clan_cli.ssh.host_key import HostKeyCheck
|
||||
from clan_cli.ssh.parse import parse_deployment_address
|
||||
from clan_cli.ssh.tor import TorTarget, spawn_tor, ssh_tor_reachable
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
@@ -51,12 +51,12 @@ def is_ipv6(ip: str) -> bool:
|
||||
|
||||
def find_reachable_host(
|
||||
deploy_info: DeployInfo, host_key_check: HostKeyCheck
|
||||
) -> Host | None:
|
||||
) -> Remote | None:
|
||||
host = None
|
||||
for addr in deploy_info.addrs:
|
||||
host_addr = f"[{addr}]" if is_ipv6(addr) else addr
|
||||
host_ = parse_deployment_address(
|
||||
machine_name="uknown", host=host_addr, host_key_check=host_key_check
|
||||
machine_name="uknown", address=host_addr, host_key_check=host_key_check
|
||||
)
|
||||
if is_ssh_reachable(host_):
|
||||
host = host_
|
||||
@@ -88,7 +88,8 @@ def ssh_shell_from_deploy(
|
||||
deploy_info: DeployInfo, runtime: AsyncRuntime, host_key_check: HostKeyCheck
|
||||
) -> None:
|
||||
if host := find_reachable_host(deploy_info, host_key_check):
|
||||
host.interactive_ssh()
|
||||
with host.ssh_control_master() as ssh:
|
||||
ssh.interactive_ssh()
|
||||
else:
|
||||
log.info("Could not reach host via clearnet 'addrs'")
|
||||
log.info(f"Trying to reach host via tor '{deploy_info.tor}'")
|
||||
@@ -97,7 +98,13 @@ def ssh_shell_from_deploy(
|
||||
msg = "No tor address provided, please provide a tor address."
|
||||
raise ClanError(msg)
|
||||
if ssh_tor_reachable(TorTarget(onion=deploy_info.tor, port=22)):
|
||||
host = Host(host=deploy_info.tor, password=deploy_info.pwd, tor_socks=True)
|
||||
host = Remote(
|
||||
address=deploy_info.tor,
|
||||
user="root",
|
||||
password=deploy_info.pwd,
|
||||
tor_socks=True,
|
||||
command_prefix="tor",
|
||||
)
|
||||
else:
|
||||
msg = "Could not reach host via tor either."
|
||||
raise ClanError(msg)
|
||||
|
||||
@@ -1,288 +0,0 @@
|
||||
# Adapted from https://github.com/numtide/deploykit
|
||||
|
||||
import logging
|
||||
import os
|
||||
import shlex
|
||||
import socket
|
||||
import subprocess
|
||||
import sys
|
||||
import types
|
||||
from dataclasses import dataclass, field
|
||||
from pathlib import Path
|
||||
from shlex import quote
|
||||
from tempfile import TemporaryDirectory
|
||||
from typing import Any
|
||||
|
||||
from clan_lib.cmd import CmdOut, RunOpts, run
|
||||
from clan_lib.colors import AnsiColor
|
||||
from clan_lib.errors import ClanError
|
||||
from clan_lib.nix import nix_shell
|
||||
|
||||
from clan_cli.ssh.host_key import HostKeyCheck
|
||||
|
||||
cmdlog = logging.getLogger(__name__)
|
||||
|
||||
|
||||
# Seconds until a message is printed when _run produces no output.
|
||||
NO_OUTPUT_TIMEOUT = 20
|
||||
|
||||
|
||||
@dataclass
|
||||
class Host:
|
||||
host: str
|
||||
user: str | None = None
|
||||
port: int | None = None
|
||||
private_key: Path | None = None
|
||||
password: str | None = None
|
||||
forward_agent: bool = False
|
||||
command_prefix: str | None = None
|
||||
host_key_check: HostKeyCheck = HostKeyCheck.ASK
|
||||
meta: dict[str, Any] = field(default_factory=dict)
|
||||
verbose_ssh: bool = False
|
||||
ssh_options: dict[str, str] = field(default_factory=dict)
|
||||
tor_socks: bool = False
|
||||
|
||||
_temp_dir: TemporaryDirectory | None = None
|
||||
|
||||
def __enter__(self) -> "Host":
|
||||
directory = None
|
||||
if sys.platform == "darwin" and os.environ.get("TMPDIR", "").startswith(
|
||||
"/var/folders/"
|
||||
):
|
||||
# macOS's tmpdir is too long for unix domain sockets
|
||||
directory = "/tmp/"
|
||||
|
||||
self._temp_dir = TemporaryDirectory(prefix="clan-ssh-", dir=directory)
|
||||
return self
|
||||
|
||||
def __exit__(
|
||||
self,
|
||||
exc_type: type[BaseException] | None,
|
||||
exc_value: BaseException | None,
|
||||
traceback: types.TracebackType | None,
|
||||
) -> None:
|
||||
try:
|
||||
if self._temp_dir:
|
||||
self._temp_dir.cleanup()
|
||||
except OSError:
|
||||
pass
|
||||
|
||||
def __post_init__(self) -> None:
|
||||
if not self.command_prefix:
|
||||
self.command_prefix = self.host
|
||||
if not self.user:
|
||||
self.user = "root"
|
||||
|
||||
def __str__(self) -> str:
|
||||
return self.target
|
||||
|
||||
@property
|
||||
def target(self) -> str:
|
||||
return f"{self.user}@{self.host}"
|
||||
|
||||
@classmethod
|
||||
def from_host(cls, host: "Host") -> "Host":
|
||||
return cls(
|
||||
host=host.host,
|
||||
user=host.user,
|
||||
port=host.port,
|
||||
private_key=host.private_key,
|
||||
forward_agent=host.forward_agent,
|
||||
command_prefix=host.command_prefix,
|
||||
host_key_check=host.host_key_check,
|
||||
meta=host.meta.copy(),
|
||||
verbose_ssh=host.verbose_ssh,
|
||||
ssh_options=host.ssh_options.copy(),
|
||||
)
|
||||
|
||||
def run_local(
|
||||
self,
|
||||
cmd: list[str],
|
||||
opts: RunOpts | None = None,
|
||||
extra_env: dict[str, str] | None = None,
|
||||
) -> CmdOut:
|
||||
"""
|
||||
Command to run locally for the host
|
||||
"""
|
||||
if opts is None:
|
||||
opts = RunOpts()
|
||||
env = opts.env or os.environ.copy()
|
||||
if extra_env:
|
||||
env.update(extra_env)
|
||||
|
||||
displayed_cmd = " ".join(cmd)
|
||||
cmdlog.info(
|
||||
f"$ {displayed_cmd}",
|
||||
extra={
|
||||
"command_prefix": self.command_prefix,
|
||||
"color": AnsiColor.GREEN.value,
|
||||
},
|
||||
)
|
||||
opts.env = env
|
||||
opts.prefix = self.command_prefix
|
||||
return run(cmd, opts)
|
||||
|
||||
def run(
|
||||
self,
|
||||
cmd: list[str],
|
||||
opts: RunOpts | None = None,
|
||||
become_root: bool = False,
|
||||
extra_env: dict[str, str] | None = None,
|
||||
tty: bool = False,
|
||||
verbose_ssh: bool = False,
|
||||
quiet: bool = False,
|
||||
) -> CmdOut:
|
||||
"""
|
||||
Command to run on the host via ssh
|
||||
"""
|
||||
if extra_env is None:
|
||||
extra_env = {}
|
||||
|
||||
if opts is None:
|
||||
opts = RunOpts()
|
||||
|
||||
# Quote all added environment variables
|
||||
env_vars = []
|
||||
for k, v in extra_env.items():
|
||||
env_vars.append(f"{shlex.quote(k)}={shlex.quote(v)}")
|
||||
|
||||
sudo = []
|
||||
if become_root and self.user != "root":
|
||||
# If we are not root and we need to become root, prepend sudo
|
||||
sudo = ["sudo", "--"]
|
||||
|
||||
if opts.prefix is None:
|
||||
opts.prefix = self.command_prefix
|
||||
# always set needs_user_terminal to True because ssh asks for passwords
|
||||
opts.needs_user_terminal = True
|
||||
|
||||
if opts.cwd is not None:
|
||||
msg = "cwd is not supported for remote commands"
|
||||
raise ClanError(msg)
|
||||
|
||||
# Build a pretty command for logging
|
||||
displayed_cmd = ""
|
||||
export_cmd = ""
|
||||
if env_vars:
|
||||
export_cmd = f"export {' '.join(env_vars)}; "
|
||||
displayed_cmd += export_cmd
|
||||
displayed_cmd += " ".join(cmd)
|
||||
|
||||
if not quiet:
|
||||
cmdlog.info(
|
||||
f"$ {displayed_cmd}",
|
||||
extra={
|
||||
"command_prefix": self.command_prefix,
|
||||
"color": AnsiColor.GREEN.value,
|
||||
},
|
||||
)
|
||||
|
||||
# Build the ssh command
|
||||
bash_cmd = export_cmd
|
||||
if opts.shell:
|
||||
bash_cmd += " ".join(cmd)
|
||||
opts.shell = False
|
||||
else:
|
||||
bash_cmd += 'exec "$@"'
|
||||
# FIXME we assume bash to be present here? Should be documented...
|
||||
ssh_cmd = [
|
||||
*self.ssh_cmd(verbose_ssh=verbose_ssh, tty=tty),
|
||||
"--",
|
||||
*sudo,
|
||||
"bash",
|
||||
"-c",
|
||||
quote(bash_cmd),
|
||||
"--",
|
||||
" ".join(map(quote, cmd)),
|
||||
]
|
||||
|
||||
# Run the ssh command
|
||||
return run(ssh_cmd, opts)
|
||||
|
||||
def nix_ssh_env(
|
||||
self, env: dict[str, str] | None, local_ssh: bool = True
|
||||
) -> dict[str, str]:
|
||||
if env is None:
|
||||
env = {}
|
||||
env["NIX_SSHOPTS"] = " ".join(self.ssh_cmd_opts(local_ssh=local_ssh))
|
||||
return env
|
||||
|
||||
def ssh_cmd_opts(
|
||||
self,
|
||||
local_ssh: bool = True,
|
||||
) -> list[str]:
|
||||
ssh_opts = ["-A"] if self.forward_agent else []
|
||||
if self.port:
|
||||
ssh_opts.extend(["-p", str(self.port)])
|
||||
|
||||
for k, v in self.ssh_options.items():
|
||||
ssh_opts.extend(["-o", f"{k}={shlex.quote(v)}"])
|
||||
|
||||
ssh_opts.extend(self.host_key_check.to_ssh_opt())
|
||||
|
||||
if self.private_key:
|
||||
ssh_opts.extend(["-i", str(self.private_key)])
|
||||
|
||||
if local_ssh and self._temp_dir:
|
||||
ssh_opts.extend(["-o", "ControlPersist=30m"])
|
||||
ssh_opts.extend(
|
||||
[
|
||||
"-o",
|
||||
f"ControlPath={Path(self._temp_dir.name) / 'clan-%h-%p-%r'}",
|
||||
]
|
||||
)
|
||||
ssh_opts.extend(["-o", "ControlMaster=auto"])
|
||||
|
||||
return ssh_opts
|
||||
|
||||
def ssh_cmd(
|
||||
self,
|
||||
verbose_ssh: bool = False,
|
||||
tty: bool = False,
|
||||
) -> list[str]:
|
||||
packages = []
|
||||
password_args = []
|
||||
if self.password:
|
||||
packages.append("sshpass")
|
||||
password_args = [
|
||||
"sshpass",
|
||||
"-p",
|
||||
self.password,
|
||||
]
|
||||
|
||||
ssh_opts = self.ssh_cmd_opts()
|
||||
if verbose_ssh or self.verbose_ssh:
|
||||
ssh_opts.extend(["-v"])
|
||||
if tty:
|
||||
ssh_opts.extend(["-t"])
|
||||
|
||||
if self.tor_socks:
|
||||
packages.append("netcat")
|
||||
ssh_opts.append("-o")
|
||||
ssh_opts.append("ProxyCommand=nc -x 127.0.0.1:9050 -X 5 %h %p")
|
||||
|
||||
cmd = [
|
||||
*password_args,
|
||||
"ssh",
|
||||
self.target,
|
||||
*ssh_opts,
|
||||
]
|
||||
|
||||
return nix_shell(packages, cmd)
|
||||
|
||||
def interactive_ssh(self) -> None:
|
||||
subprocess.run(self.ssh_cmd())
|
||||
|
||||
|
||||
def is_ssh_reachable(host: Host) -> bool:
|
||||
with socket.socket(
|
||||
socket.AF_INET6 if ":" in host.host else socket.AF_INET, socket.SOCK_STREAM
|
||||
) as sock:
|
||||
sock.settimeout(2)
|
||||
try:
|
||||
sock.connect((host.host, host.port or 22))
|
||||
sock.close()
|
||||
except OSError:
|
||||
return False
|
||||
else:
|
||||
return True
|
||||
@@ -1,70 +0,0 @@
|
||||
import re
|
||||
import urllib.parse
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
from clan_lib.errors import ClanError
|
||||
|
||||
from clan_cli.ssh.host import Host
|
||||
from clan_cli.ssh.host_key import HostKeyCheck
|
||||
|
||||
|
||||
def parse_deployment_address(
|
||||
machine_name: str,
|
||||
host: str,
|
||||
host_key_check: HostKeyCheck,
|
||||
forward_agent: bool = True,
|
||||
meta: dict[str, Any] | None = None,
|
||||
private_key: Path | None = None,
|
||||
) -> Host:
|
||||
parts = host.split("?", maxsplit=1)
|
||||
endpoint, maybe_options = parts if len(parts) == 2 else (parts[0], "")
|
||||
|
||||
parts = endpoint.split("@")
|
||||
match len(parts):
|
||||
case 2:
|
||||
user, host_port = parts
|
||||
case 1:
|
||||
user, host_port = "", parts[0]
|
||||
case _:
|
||||
msg = f"Invalid host, got `{host}` but expected something like `[user@]hostname[:port]`"
|
||||
raise ClanError(msg)
|
||||
|
||||
# Make this check now rather than failing with a `ValueError`
|
||||
# when looking up the port from the `urlsplit` result below:
|
||||
if host_port.count(":") > 1 and not re.match(r".*\[.*]", host_port):
|
||||
msg = f"Invalid hostname: {host}. IPv6 addresses must be enclosed in brackets , e.g. [::1]"
|
||||
raise ClanError(msg)
|
||||
|
||||
options: dict[str, str] = {}
|
||||
for o in maybe_options.split("&"):
|
||||
if len(o) == 0:
|
||||
continue
|
||||
parts = o.split("=", maxsplit=1)
|
||||
if len(parts) != 2:
|
||||
msg = (
|
||||
f"Invalid option in host `{host}`: option `{o}` does not have "
|
||||
f"a value (i.e. expected something like `name=value`)"
|
||||
)
|
||||
raise ClanError(msg)
|
||||
name, value = parts
|
||||
options[name] = value
|
||||
|
||||
result = urllib.parse.urlsplit(f"//{host_port}")
|
||||
if not result.hostname:
|
||||
msg = f"Invalid host, got `{host}` but expected something like `[user@]hostname[:port]`"
|
||||
raise ClanError(msg)
|
||||
hostname = result.hostname
|
||||
port = result.port
|
||||
|
||||
return Host(
|
||||
hostname,
|
||||
user=user,
|
||||
port=port,
|
||||
private_key=private_key,
|
||||
host_key_check=host_key_check,
|
||||
command_prefix=machine_name,
|
||||
forward_agent=forward_agent,
|
||||
meta={} if meta is None else meta.copy(),
|
||||
ssh_options=options,
|
||||
)
|
||||
@@ -2,14 +2,14 @@ from dataclasses import dataclass
|
||||
from typing import Generic
|
||||
|
||||
from clan_lib.errors import CmdOut
|
||||
from clan_lib.ssh.remote import Remote
|
||||
|
||||
from clan_cli.ssh import T
|
||||
from clan_cli.ssh.host import Host
|
||||
|
||||
|
||||
@dataclass
|
||||
class HostResult(Generic[T]):
|
||||
host: Host
|
||||
host: Remote
|
||||
_result: T | Exception
|
||||
|
||||
@property
|
||||
|
||||
@@ -4,12 +4,11 @@ from tempfile import TemporaryDirectory
|
||||
|
||||
from clan_lib.cmd import Log, RunOpts
|
||||
from clan_lib.errors import ClanError
|
||||
|
||||
from clan_cli.ssh.host import Host
|
||||
from clan_lib.ssh.remote import Remote
|
||||
|
||||
|
||||
def upload(
|
||||
host: Host,
|
||||
host: Remote,
|
||||
local_src: Path,
|
||||
remote_dest: Path, # must be a directory
|
||||
file_user: str = "root",
|
||||
@@ -99,8 +98,8 @@ def upload(
|
||||
raise ClanError(msg)
|
||||
|
||||
# TODO accept `input` to be an IO object instead of bytes so that we don't have to read the tarfile into memory.
|
||||
with tar_path.open("rb") as f:
|
||||
host.run(
|
||||
with tar_path.open("rb") as f, host.ssh_control_master() as ssh:
|
||||
ssh.run(
|
||||
[
|
||||
"bash",
|
||||
"-c",
|
||||
|
||||
Reference in New Issue
Block a user