only apply ssh control master to local ssh connection

This commit is contained in:
Jörg Thalheim
2025-05-06 11:08:40 +02:00
parent 5846f5d63a
commit f3c673d165
2 changed files with 19 additions and 13 deletions

View File

@@ -179,11 +179,11 @@ def deploy_machine(machine: Machine) -> None:
switch_cmd = [f"{machine._class_}-rebuild", "switch", *nix_options] switch_cmd = [f"{machine._class_}-rebuild", "switch", *nix_options]
test_cmd = [f"{machine._class_}-rebuild", "test", *nix_options] test_cmd = [f"{machine._class_}-rebuild", "test", *nix_options]
env = host.nix_ssh_env(None) remote_env = host.nix_ssh_env(None, local_ssh=False)
ret = host.run( ret = host.run(
switch_cmd, switch_cmd,
RunOpts(check=False, msg_color=MsgColor(stderr=AnsiColor.DEFAULT)), RunOpts(check=False, msg_color=MsgColor(stderr=AnsiColor.DEFAULT)),
extra_env=env, extra_env=remote_env,
become_root=become_root, become_root=become_root,
) )
@@ -209,7 +209,7 @@ def deploy_machine(machine: Machine) -> None:
msg_color=MsgColor(stderr=AnsiColor.DEFAULT), msg_color=MsgColor(stderr=AnsiColor.DEFAULT),
needs_user_terminal=True, needs_user_terminal=True,
), ),
extra_env=env, extra_env=remote_env,
become_root=become_root, become_root=become_root,
) )

View File

@@ -42,14 +42,8 @@ class Host:
_temp_dir: TemporaryDirectory | None = None _temp_dir: TemporaryDirectory | None = None
def setup_control_master(self, control_path: Path) -> None:
self.ssh_options["ControlMaster"] = "auto"
self.ssh_options["ControlPath"] = str(control_path / "clan-%h-%p-%r")
self.ssh_options["ControlPersist"] = "30m"
def __enter__(self) -> "Host": def __enter__(self) -> "Host":
self._temp_dir = TemporaryDirectory(prefix="clan-ssh-") self._temp_dir = TemporaryDirectory(prefix="clan-ssh-")
self.setup_control_master(Path(self._temp_dir.name))
return self return self
def __exit__( def __exit__(
@@ -188,15 +182,17 @@ class Host:
# Run the ssh command # Run the ssh command
return run(ssh_cmd, opts) return run(ssh_cmd, opts)
def nix_ssh_env(self, env: dict[str, str] | None) -> dict[str, str]: def nix_ssh_env(
self, env: dict[str, str] | None, local_ssh: bool = True
) -> dict[str, str]:
if env is None: if env is None:
env = {} env = {}
env["NIX_SSHOPTS"] = " ".join(self.ssh_cmd_opts) env["NIX_SSHOPTS"] = " ".join(self.ssh_cmd_opts(local_ssh=local_ssh))
return env return env
@property
def ssh_cmd_opts( def ssh_cmd_opts(
self, self,
local_ssh: bool = True,
) -> list[str]: ) -> list[str]:
ssh_opts = ["-A"] if self.forward_agent else [] ssh_opts = ["-A"] if self.forward_agent else []
if self.port: if self.port:
@@ -210,6 +206,16 @@ class Host:
if self.private_key: if self.private_key:
ssh_opts.extend(["-i", str(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 return ssh_opts
def ssh_cmd( def ssh_cmd(
@@ -227,7 +233,7 @@ class Host:
self.password, self.password,
] ]
ssh_opts = self.ssh_cmd_opts ssh_opts = self.ssh_cmd_opts()
if verbose_ssh or self.verbose_ssh: if verbose_ssh or self.verbose_ssh:
ssh_opts.extend(["-v"]) ssh_opts.extend(["-v"])
if tty: if tty: