clan_cli: refactor secrets code into Machine class
This commit is contained in:
@@ -1,30 +1,24 @@
|
||||
import argparse
|
||||
import json
|
||||
import subprocess
|
||||
from pathlib import Path
|
||||
from tempfile import TemporaryDirectory
|
||||
|
||||
from ..dirs import get_clan_flake_toplevel
|
||||
from ..nix import nix_build, nix_config, nix_shell
|
||||
from ..secrets.generate import run_generate_secrets
|
||||
from ..secrets.upload import get_decrypted_secrets
|
||||
from ..ssh import Host, parse_deployment_address
|
||||
from ..machines.machines import Machine
|
||||
from ..nix import nix_shell
|
||||
from ..secrets.generate import generate_secrets
|
||||
|
||||
|
||||
def install_nixos(h: Host, clan_dir: Path) -> None:
|
||||
def install_nixos(machine: Machine) -> None:
|
||||
h = machine.host
|
||||
target_host = f"{h.user or 'root'}@{h.host}"
|
||||
|
||||
flake_attr = h.meta.get("flake_attr", "")
|
||||
|
||||
run_generate_secrets(h.meta["generateSecrets"], clan_dir)
|
||||
generate_secrets(machine)
|
||||
|
||||
with TemporaryDirectory() as tmpdir_:
|
||||
tmpdir = Path(tmpdir_)
|
||||
get_decrypted_secrets(
|
||||
h.meta["uploadSecrets"],
|
||||
clan_dir,
|
||||
target_directory=tmpdir / h.meta["secretsUploadDirectory"].lstrip("/"),
|
||||
)
|
||||
machine.upload_secrets(tmpdir / machine.secrets_upload_directory)
|
||||
|
||||
subprocess.run(
|
||||
nix_shell(
|
||||
@@ -32,7 +26,7 @@ def install_nixos(h: Host, clan_dir: Path) -> None:
|
||||
[
|
||||
"nixos-anywhere",
|
||||
"-f",
|
||||
f"{clan_dir}#{flake_attr}",
|
||||
f"{machine.clan_dir}#{flake_attr}",
|
||||
"-t",
|
||||
"--no-reboot",
|
||||
"--extra-files",
|
||||
@@ -44,24 +38,11 @@ def install_nixos(h: Host, clan_dir: Path) -> None:
|
||||
)
|
||||
|
||||
|
||||
def install(args: argparse.Namespace) -> None:
|
||||
clan_dir = get_clan_flake_toplevel()
|
||||
config = nix_config()
|
||||
system = config["system"]
|
||||
json_file = subprocess.run(
|
||||
nix_build(
|
||||
[
|
||||
f'{clan_dir}#clanInternals.machines."{system}"."{args.machine}".config.system.clan.deployment.file'
|
||||
]
|
||||
),
|
||||
stdout=subprocess.PIPE,
|
||||
check=True,
|
||||
text=True,
|
||||
).stdout.strip()
|
||||
machine_json = json.loads(Path(json_file).read_text())
|
||||
host = parse_deployment_address(args.machine, args.target_host, machine_json)
|
||||
def install_command(args: argparse.Namespace) -> None:
|
||||
machine = Machine(args.machine)
|
||||
machine.deployment_address = args.target_host
|
||||
|
||||
install_nixos(host, clan_dir)
|
||||
install_nixos(machine)
|
||||
|
||||
|
||||
def register_install_parser(parser: argparse.ArgumentParser) -> None:
|
||||
@@ -76,4 +57,4 @@ def register_install_parser(parser: argparse.ArgumentParser) -> None:
|
||||
help="ssh address to install to in the form of user@host:2222",
|
||||
)
|
||||
|
||||
parser.set_defaults(func=install)
|
||||
parser.set_defaults(func=install_command)
|
||||
|
||||
108
pkgs/clan-cli/clan_cli/machines/machines.py
Normal file
108
pkgs/clan-cli/clan_cli/machines/machines.py
Normal file
@@ -0,0 +1,108 @@
|
||||
import json
|
||||
import os
|
||||
import subprocess
|
||||
import sys
|
||||
from pathlib import Path
|
||||
from typing import Optional
|
||||
|
||||
from ..dirs import get_clan_flake_toplevel
|
||||
from ..nix import nix_build, nix_config, nix_eval
|
||||
from ..ssh import Host, parse_deployment_address
|
||||
|
||||
|
||||
def build_machine_data(machine_name: str, clan_dir: Path) -> dict:
|
||||
config = nix_config()
|
||||
system = config["system"]
|
||||
|
||||
outpath = subprocess.run(
|
||||
nix_build(
|
||||
[
|
||||
f'path:{clan_dir}#clanInternals.machines."{system}"."{machine_name}".config.system.clan.deployment.file'
|
||||
]
|
||||
),
|
||||
stdout=subprocess.PIPE,
|
||||
check=True,
|
||||
text=True,
|
||||
).stdout.strip()
|
||||
return json.loads(Path(outpath).read_text())
|
||||
|
||||
|
||||
class Machine:
|
||||
def __init__(
|
||||
self,
|
||||
name: str,
|
||||
clan_dir: Optional[Path] = None,
|
||||
machine_data: Optional[dict] = None,
|
||||
) -> None:
|
||||
"""
|
||||
Creates a Machine
|
||||
@name: the name of the machine
|
||||
@clan_dir: the directory of the clan, optional, if not set it will be determined from the current working directory
|
||||
@machine_json: can be optionally used to skip evaluation of the machine, location of the json file with machine data
|
||||
"""
|
||||
self.name = name
|
||||
if clan_dir is None:
|
||||
self.clan_dir = get_clan_flake_toplevel()
|
||||
else:
|
||||
self.clan_dir = clan_dir
|
||||
|
||||
if machine_data is None:
|
||||
self.machine_data = build_machine_data(name, self.clan_dir)
|
||||
else:
|
||||
self.machine_data = machine_data
|
||||
|
||||
self.deployment_address = self.machine_data["deploymentAddress"]
|
||||
self.upload_secrets = self.machine_data["uploadSecrets"]
|
||||
self.generate_secrets = self.machine_data["generateSecrets"]
|
||||
self.secrets_upload_directory = self.machine_data["secretsUploadDirectory"]
|
||||
|
||||
@property
|
||||
def host(self) -> Host:
|
||||
return parse_deployment_address(
|
||||
self.name, self.deployment_address, meta={"machine": self}
|
||||
)
|
||||
|
||||
def run_upload_secrets(self, secrets_dir: Path) -> None:
|
||||
"""
|
||||
Upload the secrets to the provided directory
|
||||
@secrets_dir: the directory to store the secrets in
|
||||
"""
|
||||
env = os.environ.copy()
|
||||
env["CLAN_DIR"] = str(self.clan_dir)
|
||||
env["PYTHONPATH"] = str(
|
||||
":".join(sys.path)
|
||||
) # TODO do this in the clanCore module
|
||||
env["SECRETS_DIR"] = str(secrets_dir)
|
||||
subprocess.run(
|
||||
[self.upload_secrets],
|
||||
env=env,
|
||||
check=True,
|
||||
stdout=subprocess.PIPE,
|
||||
text=True,
|
||||
)
|
||||
|
||||
def eval_nix(self, attr: str) -> str:
|
||||
"""
|
||||
eval a nix attribute of the machine
|
||||
@attr: the attribute to get
|
||||
"""
|
||||
output = subprocess.run(
|
||||
nix_eval([f"path:{self.clan_dir}#{attr}"]),
|
||||
stdout=subprocess.PIPE,
|
||||
check=True,
|
||||
text=True,
|
||||
).stdout.strip()
|
||||
return output
|
||||
|
||||
def build_nix(self, attr: str) -> Path:
|
||||
"""
|
||||
build a nix attribute of the machine
|
||||
@attr: the attribute to get
|
||||
"""
|
||||
outpath = subprocess.run(
|
||||
nix_build([f"path:{self.clan_dir}#{attr}"]),
|
||||
stdout=subprocess.PIPE,
|
||||
check=True,
|
||||
text=True,
|
||||
).stdout.strip()
|
||||
return Path(outpath)
|
||||
@@ -3,12 +3,12 @@ import json
|
||||
import os
|
||||
import subprocess
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
from ..dirs import get_clan_flake_toplevel
|
||||
from ..machines.machines import Machine
|
||||
from ..nix import nix_build, nix_command, nix_config
|
||||
from ..secrets.generate import run_generate_secrets
|
||||
from ..secrets.upload import run_upload_secrets
|
||||
from ..secrets.generate import generate_secrets
|
||||
from ..secrets.upload import upload_secrets
|
||||
from ..ssh import Host, HostGroup, HostKeyCheck, parse_deployment_address
|
||||
|
||||
|
||||
@@ -40,13 +40,8 @@ def deploy_nixos(hosts: HostGroup, clan_dir: Path) -> None:
|
||||
|
||||
flake_attr = h.meta.get("flake_attr", "")
|
||||
|
||||
run_generate_secrets(h.meta["generateSecrets"], clan_dir)
|
||||
run_upload_secrets(
|
||||
h.meta["uploadSecrets"],
|
||||
clan_dir,
|
||||
target=target,
|
||||
target_directory=h.meta["secretsUploadDirectory"],
|
||||
)
|
||||
generate_secrets(h.meta["machine"])
|
||||
upload_secrets(h.meta["machine"])
|
||||
|
||||
target_host = h.meta.get("target_host")
|
||||
if target_host:
|
||||
@@ -81,49 +76,36 @@ def deploy_nixos(hosts: HostGroup, clan_dir: Path) -> None:
|
||||
hosts.run_function(deploy)
|
||||
|
||||
|
||||
def build_json(targets: list[str]) -> list[dict[str, Any]]:
|
||||
outpaths = subprocess.run(
|
||||
nix_build(targets),
|
||||
# function to speedup eval if we want to evauluate all machines
|
||||
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,
|
||||
).stdout
|
||||
parsed = []
|
||||
for outpath in outpaths.splitlines():
|
||||
parsed.append(json.loads(Path(outpath).read_text()))
|
||||
return parsed
|
||||
|
||||
|
||||
def get_all_machines(clan_dir: Path) -> HostGroup:
|
||||
config = nix_config()
|
||||
system = config["system"]
|
||||
what = f'{clan_dir}#clanInternals.all-machines-json."{system}"'
|
||||
machines = build_json([what])[0]
|
||||
machines = json.loads(Path(machines_json).read_text())
|
||||
|
||||
hosts = []
|
||||
for name, machine in machines.items():
|
||||
for name, machine_data in machines.items():
|
||||
# very hacky. would be better to do a MachinesGroup instead
|
||||
host = parse_deployment_address(
|
||||
name, machine["deploymentAddress"], meta=machine
|
||||
name,
|
||||
machine_data["deploymentAddress"],
|
||||
meta={"machine": Machine(name=name, machine_data=machine_data)},
|
||||
)
|
||||
hosts.append(host)
|
||||
return HostGroup(hosts)
|
||||
|
||||
|
||||
def get_selected_machines(machine_names: list[str], clan_dir: Path) -> HostGroup:
|
||||
config = nix_config()
|
||||
system = config["system"]
|
||||
what = []
|
||||
for name in machine_names:
|
||||
what.append(
|
||||
f'{clan_dir}#clanInternals.machines."{system}"."{name}".config.system.clan.deployment.file'
|
||||
)
|
||||
machines = build_json(what)
|
||||
hosts = []
|
||||
for i, machine in enumerate(machines):
|
||||
host = parse_deployment_address(
|
||||
machine_names[i], machine["deploymentAddress"], machine
|
||||
)
|
||||
hosts.append(host)
|
||||
for name in machine_names:
|
||||
machine = Machine(name=name, clan_dir=clan_dir)
|
||||
hosts.append(machine.host)
|
||||
return HostGroup(hosts)
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user