nixos-rebuild-ng: add TTY allocation in SSH

This commit is contained in:
Thiago Kenji Okada 2024-11-20 10:41:09 +00:00
parent 31e9e8c0aa
commit a6b9aaba1b
4 changed files with 17 additions and 9 deletions

View File

@ -119,11 +119,12 @@ class Profile:
class SSH: class SSH:
host: str host: str
opts: list[str] opts: list[str]
tty: bool
@classmethod @classmethod
def from_arg(cls, host: str | None) -> Self | None: def from_arg(cls, host: str | None, tty: bool | None) -> Self | None:
if host: if host:
opts = os.getenv("SSH_OPTS", "").split() opts = os.getenv("SSH_OPTS", "").split()
return cls(host, opts) return cls(host, opts, bool(tty))
else: else:
return None return None

View File

@ -10,6 +10,7 @@ from .models import SSH
# Not exhaustive, but we can always extend it later. # Not exhaustive, but we can always extend it later.
class RunKwargs(TypedDict, total=False): class RunKwargs(TypedDict, total=False):
capture_output: bool capture_output: bool
input: str | None
stderr: int | None stderr: int | None
stdin: int | None stdin: int | None
stdout: int | None stdout: int | None
@ -33,7 +34,10 @@ def run_wrapper(
args = ["env", *extra_env_args, *args] args = ["env", *extra_env_args, *args]
if sudo: if sudo:
args = ["sudo", *args] args = ["sudo", *args]
args = ["ssh", *remote.opts, remote.host, "--", *args] if remote.tty:
args = ["ssh", "-t", *remote.opts, remote.host, "--", *args]
else:
args = ["ssh", *remote.opts, remote.host, "--", *args]
else: else:
if extra_env: if extra_env:
env = (env or os.environ) | extra_env env = (env or os.environ) | extra_env

View File

@ -101,9 +101,11 @@ def test_profile_from_name(mock_mkdir: Any) -> None:
def test_ssh_from_name(monkeypatch: Any) -> None: def test_ssh_from_name(monkeypatch: Any) -> None:
assert m.SSH.from_arg("user@localhost") == m.SSH("user@localhost", []) assert m.SSH.from_arg("user@localhost", None) == m.SSH("user@localhost", [], False)
monkeypatch.setenv("SSH_OPTS", "-f foo -b bar") monkeypatch.setenv("SSH_OPTS", "-f foo -b bar")
assert m.SSH.from_arg("user@localhost") == m.SSH( assert m.SSH.from_arg("user@localhost", True) == m.SSH(
"user@localhost", ["-f", "foo", "-b", "bar"] "user@localhost",
["-f", "foo", "-b", "bar"],
True,
) )

View File

@ -41,7 +41,7 @@ def test_run(mock_run: Any) -> None:
p.run_wrapper( p.run_wrapper(
["test", "--with", "flags"], ["test", "--with", "flags"],
check=True, check=True,
remote=m.SSH("user@localhost", ["--ssh", "opt"]), remote=m.SSH("user@localhost", ["--ssh", "opt"], False),
) )
mock_run.assert_called_with( mock_run.assert_called_with(
["ssh", "--ssh", "opt", "user@localhost", "--", "test", "--with", "flags"], ["ssh", "--ssh", "opt", "user@localhost", "--", "test", "--with", "flags"],
@ -69,11 +69,12 @@ def test_run(mock_run: Any) -> None:
check=True, check=True,
sudo=True, sudo=True,
extra_env={"FOO": "bar"}, extra_env={"FOO": "bar"},
remote=m.SSH("user@localhost", ["--ssh", "opt"]), remote=m.SSH("user@localhost", ["--ssh", "opt"], True),
) )
mock_run.assert_called_with( mock_run.assert_called_with(
[ [
"ssh", "ssh",
"-t",
"--ssh", "--ssh",
"opt", "opt",
"user@localhost", "user@localhost",
@ -96,5 +97,5 @@ def test_run(mock_run: Any) -> None:
["test", "--with", "flags"], ["test", "--with", "flags"],
check=False, check=False,
env={"foo": "bar"}, env={"foo": "bar"},
remote=m.SSH("user@localhost", []), remote=m.SSH("user@localhost", [], False),
) )