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:
host: str
opts: list[str]
tty: bool
@classmethod
def from_arg(cls, host: str | None) -> Self | None:
def from_arg(cls, host: str | None, tty: bool | None) -> Self | None:
if host:
opts = os.getenv("SSH_OPTS", "").split()
return cls(host, opts)
return cls(host, opts, bool(tty))
else:
return None

View File

@ -10,6 +10,7 @@ from .models import SSH
# Not exhaustive, but we can always extend it later.
class RunKwargs(TypedDict, total=False):
capture_output: bool
input: str | None
stderr: int | None
stdin: int | None
stdout: int | None
@ -33,7 +34,10 @@ def run_wrapper(
args = ["env", *extra_env_args, *args]
if sudo:
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:
if 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:
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")
assert m.SSH.from_arg("user@localhost") == m.SSH(
"user@localhost", ["-f", "foo", "-b", "bar"]
assert m.SSH.from_arg("user@localhost", True) == m.SSH(
"user@localhost",
["-f", "foo", "-b", "bar"],
True,
)

View File

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