nixos-rebuild-ng: move TMPDIR to its own file

This commit is contained in:
Thiago Kenji Okada
2024-12-16 13:01:04 +00:00
parent 3cf4242c05
commit 927d182d90
3 changed files with 13 additions and 8 deletions
@@ -8,7 +8,7 @@ from pathlib import Path
from subprocess import CalledProcessError, run
from typing import assert_never
from . import nix
from . import nix, tmpdir
from .constants import EXECUTABLE, WITH_NIX_2_18, WITH_REEXEC, WITH_SHELL_FILES
from .models import Action, BuildAttr, Flake, NRError, Profile
from .process import Remote, cleanup_ssh
@@ -280,7 +280,10 @@ def reexec(
argv[0],
new,
)
# Manually call clean-up functions since os.execve() will replace
# the process immediately
cleanup_ssh()
tmpdir.TMPDIR.cleanup()
os.execve(new, argv, os.environ | {"_NIXOS_REBUILD_REEXEC": "1"})
@@ -4,19 +4,17 @@ import shlex
import subprocess
from dataclasses import dataclass
from getpass import getpass
from pathlib import Path
from tempfile import TemporaryDirectory
from typing import Self, Sequence, TypedDict, Unpack
from . import tmpdir
logger = logging.getLogger(__name__)
TMPDIR = TemporaryDirectory(prefix="nixos-rebuild.")
TMPDIR_PATH = Path(TMPDIR.name)
SSH_DEFAULT_OPTS = [
"-o",
"ControlMaster=auto",
"-o",
f"ControlPath={TMPDIR_PATH / "ssh-%n"}",
f"ControlPath={tmpdir.TMPDIR_PATH / "ssh-%n"}",
"-o",
"ControlPersist=60",
]
@@ -70,13 +68,12 @@ class RunKwargs(TypedDict, total=False):
def cleanup_ssh() -> None:
"Close SSH ControlMaster connection."
for ctrl in TMPDIR_PATH.glob("ssh-*"):
for ctrl in tmpdir.TMPDIR_PATH.glob("ssh-*"):
run_wrapper(
["ssh", "-o", f"ControlPath={ctrl}", "-O", "exit", "dummyhost"],
check=False,
capture_output=True,
)
TMPDIR.cleanup()
def run_wrapper(
@@ -0,0 +1,5 @@
from pathlib import Path
from tempfile import TemporaryDirectory
TMPDIR = TemporaryDirectory(prefix="nixos-rebuild.")
TMPDIR_PATH = Path(TMPDIR.name)