Refactor subprocess to cmd.py part 2

This commit is contained in:
Qubasa
2024-01-03 14:25:34 +01:00
parent 98fff1f390
commit 7049573d28
7 changed files with 18 additions and 35 deletions

View File

@@ -1,9 +1,9 @@
import argparse
import subprocess
from dataclasses import dataclass
from pathlib import Path
from tempfile import TemporaryDirectory
from ..cmd import Log, run
from ..machines.machines import Machine
from ..nix import nix_shell
from ..secrets.generate import generate_secrets
@@ -40,12 +40,12 @@ def install_nixos(machine: Machine, kexec: str | None = None) -> None:
cmd += ["--kexec", kexec]
cmd.append(target_host)
subprocess.run(
run(
nix_shell(
["nixpkgs#nixos-anywhere"],
cmd,
),
check=True,
log=Log.BOTH,
)

View File

@@ -4,7 +4,7 @@ import subprocess
import sys
from pathlib import Path
from ..errors import ClanError
from ..cmd import run
from ..nix import nix_build, nix_config, nix_eval
from ..ssh import Host, parse_deployment_address
@@ -13,21 +13,14 @@ def build_machine_data(machine_name: str, clan_dir: Path) -> dict:
config = nix_config()
system = config["system"]
proc = subprocess.run(
proc = run(
nix_build(
[
f'{clan_dir}#clanInternals.machines."{system}"."{machine_name}".config.system.clan.deployment.file'
]
),
stdout=subprocess.PIPE,
check=True,
text=True,
)
if proc.returncode != 0:
ClanError("failed to build machine data")
exit(1)
return json.loads(Path(proc.stdout.strip()).read_text())
@@ -99,11 +92,8 @@ class Machine:
if attr in self.eval_cache and not refresh:
return self.eval_cache[attr]
output = subprocess.run(
output = run(
nix_eval([f"path:{self.flake_dir}#{attr}"]),
stdout=subprocess.PIPE,
check=True,
text=True,
).stdout.strip()
self.eval_cache[attr] = output
return output
@@ -115,11 +105,8 @@ class Machine:
"""
if attr in self.build_cache and not refresh:
return self.build_cache[attr]
outpath = subprocess.run(
outpath = run(
nix_build([f"path:{self.flake_dir}#{attr}"]),
stdout=subprocess.PIPE,
check=True,
text=True,
).stdout.strip()
self.build_cache[attr] = Path(outpath)
return Path(outpath)

View File

@@ -4,6 +4,7 @@ import os
import subprocess
from pathlib import Path
from ..cmd import run
from ..errors import ClanError
from ..machines.machines import Machine
from ..nix import nix_build, nix_command, nix_config
@@ -79,11 +80,8 @@ def deploy_nixos(hosts: HostGroup, clan_dir: Path) -> None:
def get_all_machines(clan_dir: Path) -> HostGroup:
config = nix_config()
system = config["system"]
machines_json = subprocess.run(
nix_build([f'{clan_dir}#clanInternals.all-machines-json."{system}"']),
stdout=subprocess.PIPE,
check=True,
text=True,
machines_json = run(
nix_build([f'{clan_dir}#clanInternals.all-machines-json."{system}"'])
).stdout
machines = json.loads(Path(machines_json.rstrip()).read_text())