1
0
forked from clan/clan-core

Merge pull request 'move FlakeId to flake id' (#1701) from refactor-machine into main

This commit is contained in:
clan-bot 2024-07-03 16:37:44 +00:00
commit 3eb445cd0e
31 changed files with 99 additions and 95 deletions

View File

@ -25,6 +25,7 @@ from . import (
state,
vms,
)
from .clan_uri import FlakeId
from .custom_logger import setup_logging
from .dirs import get_clan_flake_toplevel_or_env
from .errors import ClanCmdError, ClanError
@ -41,11 +42,18 @@ except ImportError:
pass
def flake_path(arg: str) -> str | Path:
def flake_path(arg: str) -> FlakeId:
flake_dir = Path(arg).resolve()
if flake_dir.exists() and flake_dir.is_dir():
return flake_dir
return arg
return FlakeId(flake_dir)
return FlakeId(arg)
def default_flake() -> FlakeId | None:
val = get_clan_flake_toplevel_or_env()
if val:
return FlakeId(val)
return None
def add_common_flags(parser: argparse.ArgumentParser) -> None:
@ -68,7 +76,7 @@ def add_common_flags(parser: argparse.ArgumentParser) -> None:
parser.add_argument(
"--flake",
help="path to the flake where the clan resides in, can be a remote flake or local, can be set through the [CLAN_DIR] environment variable",
default=get_clan_flake_toplevel_or_env(),
default=default_flake(),
metavar="PATH",
type=flake_path,
)

View File

@ -2,7 +2,6 @@ import argparse
import json
import logging
from ..clan_uri import FlakeId
from ..completions import (
add_dynamic_completer,
complete_backup_providers_for_machine,
@ -41,7 +40,7 @@ def create_backup(machine: Machine, provider: str | None = None) -> None:
def create_command(args: argparse.Namespace) -> None:
if args.flake is None:
raise ClanError("Could not find clan flake toplevel directory")
machine = Machine(name=args.machine, flake=FlakeId(args.flake))
machine = Machine(name=args.machine, flake=args.flake)
create_backup(machine=machine, provider=args.provider)

View File

@ -3,7 +3,6 @@ import json
import subprocess
from dataclasses import dataclass
from ..clan_uri import FlakeId
from ..completions import (
add_dynamic_completer,
complete_backup_providers_for_machine,
@ -56,7 +55,7 @@ def list_backups(machine: Machine, provider: str | None = None) -> list[Backup]:
def list_command(args: argparse.Namespace) -> None:
if args.flake is None:
raise ClanError("Could not find clan flake toplevel directory")
machine = Machine(name=args.machine, flake=FlakeId(args.flake))
machine = Machine(name=args.machine, flake=args.flake)
backups = list_backups(machine=machine, provider=args.provider)
for backup in backups:
print(backup.name)

View File

@ -2,7 +2,6 @@ import argparse
import json
import subprocess
from ..clan_uri import FlakeId
from ..completions import (
add_dynamic_completer,
complete_backup_providers_for_machine,
@ -87,7 +86,7 @@ def restore_backup(
def restore_command(args: argparse.Namespace) -> None:
if args.flake is None:
raise ClanError("Could not find clan flake toplevel directory")
machine = Machine(name=args.machine, flake=FlakeId(args.flake))
machine = Machine(name=args.machine, flake=args.flake)
restore_backup(
machine=machine,
provider=args.provider,

View File

@ -109,7 +109,7 @@ class InspectOptions:
def inspect_command(args: argparse.Namespace) -> None:
inspect_options = InspectOptions(
machine=args.machine,
flake=FlakeId(args.flake or Path.cwd()),
flake=args.flake or FlakeId(Path.cwd()),
)
res = inspect_flake(
flake_url=str(inspect_options.flake), machine_name=inspect_options.machine

View File

@ -69,7 +69,7 @@ def show_clan_meta(uri: str | Path) -> ClanMetaInfo:
def show_command(args: argparse.Namespace) -> None:
flake_path = Path(args.flake).resolve()
flake_path = args.flake.path
meta = show_clan_meta(flake_path)
print(f"Name: {meta.name}")

View File

@ -12,6 +12,11 @@ class FlakeId:
# FIXME: this is such a footgun if you accidnetally pass a string
_value: str | Path
def __post_init__(self) -> None:
assert isinstance(
self._value, str | Path
), f"Flake {self._value} has an invalid type: {type(self._value)}"
def __str__(self) -> str:
return str(
self._value
@ -19,12 +24,12 @@ class FlakeId:
@property
def path(self) -> Path:
assert isinstance(self._value, Path)
assert isinstance(self._value, Path), f"Flake {self._value} is not a local path"
return self._value
@property
def url(self) -> str:
assert isinstance(self._value, str)
assert isinstance(self._value, str), f"Flake {self._value} is not a remote url"
return self._value
def is_local(self) -> bool:

View File

@ -178,12 +178,12 @@ def get_or_set_option(args: argparse.Namespace) -> None:
options = json.load(f)
# compute settings json file location
if args.settings_file is None:
settings_file = machine_settings_file(Path(args.flake), args.machine)
settings_file = machine_settings_file(args.flake.path, args.machine)
else:
settings_file = args.settings_file
# set the option with the given value
set_option(
flake_dir=Path(args.flake),
flake_dir=args.flake.path,
option=args.option,
value=args.value,
options=options,

View File

@ -2,7 +2,6 @@ import argparse
import importlib
import logging
from ..clan_uri import FlakeId
from ..completions import add_dynamic_completer, complete_machines
from ..machines.machines import Machine
@ -50,7 +49,7 @@ def check_secrets(machine: Machine, service: None | str = None) -> bool:
def check_command(args: argparse.Namespace) -> None:
machine = Machine(
name=args.machine,
flake=FlakeId(args.flake),
flake=args.flake,
)
check_secrets(machine, service=args.service)

View File

@ -208,13 +208,14 @@ def generate_facts(
was_regenerated |= _generate_facts_for_machine(
machine, service, regenerate, tmpdir, prompt
)
except Exception as exc:
except (OSError, ClanError) as exc:
log.error(f"Failed to generate facts for {machine.name}: {exc}")
errors += 1
if errors > 0:
raise ClanError(
msg = (
f"Failed to generate facts for {errors} hosts. Check the logs above"
)
raise ClanError(msg)
if not was_regenerated:
print("All secrets and facts are already up to date")

View File

@ -3,7 +3,6 @@ import importlib
import json
import logging
from ..clan_uri import FlakeId
from ..completions import add_dynamic_completer, complete_machines
from ..machines.machines import Machine
@ -27,7 +26,7 @@ def get_all_facts(machine: Machine) -> dict:
def get_command(args: argparse.Namespace) -> None:
machine = Machine(name=args.machine, flake=FlakeId(args.flake))
machine = Machine(name=args.machine, flake=args.flake)
# the raw_facts are bytestrings making them not json serializable
raw_facts = get_all_facts(machine)

View File

@ -4,7 +4,6 @@ import logging
from pathlib import Path
from tempfile import TemporaryDirectory
from ..clan_uri import FlakeId
from ..cmd import Log, run
from ..completions import add_dynamic_completer, complete_machines
from ..machines.machines import Machine
@ -45,7 +44,7 @@ def upload_secrets(machine: Machine) -> None:
def upload_command(args: argparse.Namespace) -> None:
machine = Machine(name=args.machine, flake=FlakeId(args.flake))
machine = Machine(name=args.machine, flake=args.flake)
upload_secrets(machine)

View File

@ -120,7 +120,7 @@ class AppendDiskAction(argparse.Action):
def flash_command(args: argparse.Namespace) -> None:
opts = FlashOptions(
flake=FlakeId(args.flake),
flake=args.flake,
machine=args.machine,
disks=args.disk,
ssh_keys_path=args.ssh_pubkey,

View File

@ -3,27 +3,28 @@ import logging
import re
from pathlib import Path
from clan_cli.api import API
from clan_cli.errors import ClanError
from clan_cli.git import commit_file
from clan_cli.inventory import Inventory, Machine
from ..api import API
from ..clan_uri import FlakeId
from ..errors import ClanError
from ..git import commit_file
from ..inventory import Inventory, Machine
log = logging.getLogger(__name__)
@API.register
def create_machine(flake_dir: str | Path, machine: Machine) -> None:
def create_machine(flake: FlakeId, machine: Machine) -> None:
hostname_regex = r"^(?!-)[A-Za-z0-9-]{1,63}(?<!-)$"
if not re.match(hostname_regex, machine.name):
raise ClanError(
"Machine name must be a valid hostname", location="Create Machine"
)
inventory = Inventory.load_file(flake_dir)
inventory = Inventory.load_file(flake.path)
inventory.machines.update({machine.name: machine})
inventory.persist(flake_dir, f"Create machine {machine.name}")
inventory.persist(flake.path, f"Create machine {machine.name}")
commit_file(Inventory.get_path(flake_dir), Path(flake_dir))
commit_file(Inventory.get_path(flake.path), Path(flake.path))
def create_command(args: argparse.Namespace) -> None:

View File

@ -1,26 +1,25 @@
import argparse
import shutil
from pathlib import Path
from clan_cli.api import API
from clan_cli.inventory import Inventory
from ..api import API
from ..clan_uri import FlakeId
from ..completions import add_dynamic_completer, complete_machines
from ..dirs import specific_machine_dir
from ..errors import ClanError
from ..inventory import Inventory
@API.register
def delete_machine(base_dir: str | Path, name: str) -> None:
inventory = Inventory.load_file(base_dir)
def delete_machine(flake: FlakeId, name: str) -> None:
inventory = Inventory.load_file(flake.path)
machine = inventory.machines.pop(name, None)
if machine is None:
raise ClanError(f"Machine {name} does not exist")
inventory.persist(base_dir, f"Delete machine {name}")
inventory.persist(flake.path, f"Delete machine {name}")
folder = specific_machine_dir(Path(base_dir), name)
folder = specific_machine_dir(flake.path, name)
if folder.exists():
shutil.rmtree(folder)

View File

@ -147,7 +147,7 @@ def generate_machine_hardware_info(
def hw_generate_command(args: argparse.Namespace) -> None:
flake_path = Path(args.flake).resolve()
flake_path = args.flake.path
hw_info = generate_machine_hardware_info(
flake_path, args.machine, args.hostname, args.password, args.force
)

View File

@ -124,7 +124,7 @@ def install_command(args: argparse.Namespace) -> None:
password = None
opts = InstallOptions(
flake=FlakeId(args.flake),
flake=args.flake,
machine=args.machine,
target_host=target_host,
kexec=args.kexec,

View File

@ -8,11 +8,11 @@ from .machines import Machine
# function to speedup eval if we want to evaluate all machines
def get_all_machines(flake_dir: Path, nix_options: list[str]) -> list[Machine]:
def get_all_machines(flake: FlakeId, nix_options: list[str]) -> list[Machine]:
config = nix_config()
system = config["system"]
json_path = run(
nix_build([f'{flake_dir}#clanInternals.all-machines-json."{system}"'])
nix_build([f'{flake}#clanInternals.all-machines-json."{system}"'])
).stdout
machines_json = json.loads(Path(json_path.rstrip()).read_text())
@ -22,7 +22,7 @@ def get_all_machines(flake_dir: Path, nix_options: list[str]) -> list[Machine]:
machines.append(
Machine(
name=name,
flake=FlakeId(flake_dir),
flake=flake,
cached_deployment=machine_data,
nix_options=nix_options,
)
@ -31,11 +31,9 @@ def get_all_machines(flake_dir: Path, nix_options: list[str]) -> list[Machine]:
def get_selected_machines(
flake_dir: Path, nix_options: list[str], machine_names: list[str]
flake: FlakeId, nix_options: list[str], machine_names: list[str]
) -> list[Machine]:
machines = []
for name in machine_names:
machines.append(
Machine(name=name, flake=FlakeId(flake_dir), nix_options=nix_options)
)
machines.append(Machine(name=name, flake=flake, nix_options=nix_options))
return machines

View File

@ -29,7 +29,7 @@ def list_machines(flake_url: str | Path, debug: bool = False) -> dict[str, Machi
def list_command(args: argparse.Namespace) -> None:
flake_path = Path(args.flake).resolve()
flake_path = args.flake.path
for name in list_machines(flake_path, args.debug).keys():
print(name)

View File

@ -45,8 +45,7 @@ def show_machine(flake_url: str | Path, machine_name: str) -> MachineInfo:
def show_command(args: argparse.Namespace) -> None:
flake_path = Path(args.flake).resolve()
machine = show_machine(flake_path, args.machine)
machine = show_machine(args.flake.path, args.machine)
print(f"Name: {machine.machine_name}")
print(f"Description: {machine.machine_description or ''}")
print(f"Icon: {machine.machine_icon or ''}")

View File

@ -5,7 +5,6 @@ import os
import shlex
import sys
from ..clan_uri import FlakeId
from ..cmd import run
from ..completions import add_dynamic_completer, complete_machines
from ..errors import ClanError
@ -138,7 +137,7 @@ def update(args: argparse.Namespace) -> None:
machines = []
if len(args.machines) == 1 and args.target_host is not None:
machine = Machine(
name=args.machines[0], flake=FlakeId(args.flake), nix_options=args.option
name=args.machines[0], flake=args.flake, nix_options=args.option
)
machine.target_host_address = args.target_host
machines.append(machine)

View File

@ -74,7 +74,7 @@ def list_groups(flake_dir: Path) -> list[Group]:
def list_command(args: argparse.Namespace) -> None:
for group in list_groups(Path(args.flake)):
for group in list_groups(args.flake.path):
print(group.name)
if group.machines:
print("machines:")
@ -158,7 +158,7 @@ def add_user(flake_dir: Path, group: str, name: str) -> None:
def add_user_command(args: argparse.Namespace) -> None:
add_user(Path(args.flake), args.group, args.user)
add_user(args.flake.path, args.group, args.user)
def remove_user(flake_dir: Path, group: str, name: str) -> None:
@ -166,7 +166,7 @@ def remove_user(flake_dir: Path, group: str, name: str) -> None:
def remove_user_command(args: argparse.Namespace) -> None:
remove_user(Path(args.flake), args.group, args.user)
remove_user(args.flake.path, args.group, args.user)
def add_machine(flake_dir: Path, group: str, name: str) -> None:
@ -184,7 +184,7 @@ def add_machine(flake_dir: Path, group: str, name: str) -> None:
def add_machine_command(args: argparse.Namespace) -> None:
add_machine(Path(args.flake), args.group, args.machine)
add_machine(args.flake.path, args.group, args.machine)
def remove_machine(flake_dir: Path, group: str, name: str) -> None:
@ -192,7 +192,7 @@ def remove_machine(flake_dir: Path, group: str, name: str) -> None:
def remove_machine_command(args: argparse.Namespace) -> None:
remove_machine(Path(args.flake), args.group, args.machine)
remove_machine(args.flake.path, args.group, args.machine)
def add_group_argument(parser: argparse.ArgumentParser) -> None:
@ -209,7 +209,7 @@ def add_secret(flake_dir: Path, group: str, name: str) -> None:
def add_secret_command(args: argparse.Namespace) -> None:
add_secret(Path(args.flake), args.group, args.secret)
add_secret(args.flake.path, args.group, args.secret)
def remove_secret(flake_dir: Path, group: str, name: str) -> None:
@ -224,7 +224,7 @@ def remove_secret(flake_dir: Path, group: str, name: str) -> None:
def remove_secret_command(args: argparse.Namespace) -> None:
remove_secret(Path(args.flake), args.group, args.secret)
remove_secret(args.flake.path, args.group, args.secret)
def register_groups_parser(parser: argparse.ArgumentParser) -> None:

View File

@ -40,15 +40,15 @@ def import_sops(args: argparse.Namespace) -> None:
file=sys.stderr,
)
continue
if (sops_secrets_folder(Path(args.flake)) / k / "secret").exists():
if (sops_secrets_folder(args.flake.path) / k / "secret").exists():
print(
f"WARNING: {k} already exists, skipping",
file=sys.stderr,
)
continue
encrypt_secret(
Path(args.flake),
sops_secrets_folder(Path(args.flake)) / k,
args.flake.path,
sops_secrets_folder(args.flake.path) / k,
v,
add_groups=args.group,
add_machines=args.machine,

View File

@ -58,7 +58,7 @@ def show_command(args: argparse.Namespace) -> None:
def update_command(args: argparse.Namespace) -> None:
flake_dir = Path(args.flake)
flake_dir = args.flake.path
commit_files(update_secrets(flake_dir), flake_dir, "Updated secrets with new keys.")

View File

@ -81,7 +81,7 @@ def remove_secret(flake_dir: Path, machine: str, secret: str) -> None:
def list_command(args: argparse.Namespace) -> None:
if args.flake is None:
raise ClanError("Could not find clan flake toplevel directory")
lst = list_machines(Path(args.flake))
lst = list_machines(args.flake.path)
if len(lst) > 0:
print("\n".join(lst))
@ -89,31 +89,31 @@ def list_command(args: argparse.Namespace) -> None:
def add_command(args: argparse.Namespace) -> None:
if args.flake is None:
raise ClanError("Could not find clan flake toplevel directory")
add_machine(Path(args.flake), args.machine, args.key, args.force)
add_machine(args.flake.path, args.machine, args.key, args.force)
def get_command(args: argparse.Namespace) -> None:
if args.flake is None:
raise ClanError("Could not find clan flake toplevel directory")
print(get_machine(Path(args.flake), args.machine))
print(get_machine(args.flake.path, args.machine))
def remove_command(args: argparse.Namespace) -> None:
if args.flake is None:
raise ClanError("Could not find clan flake toplevel directory")
remove_machine(Path(args.flake), args.machine)
remove_machine(args.flake.path, args.machine)
def add_secret_command(args: argparse.Namespace) -> None:
if args.flake is None:
raise ClanError("Could not find clan flake toplevel directory")
add_secret(Path(args.flake), args.machine, args.secret)
add_secret(args.flake.path, args.machine, args.secret)
def remove_secret_command(args: argparse.Namespace) -> None:
if args.flake is None:
raise ClanError("Could not find clan flake toplevel directory")
remove_secret(Path(args.flake), args.machine, args.secret)
remove_secret(args.flake.path, args.machine, args.secret)
def register_machines_parser(parser: argparse.ArgumentParser) -> None:

View File

@ -158,7 +158,7 @@ def remove_secret(flake_dir: Path, secret: str) -> None:
def remove_command(args: argparse.Namespace) -> None:
remove_secret(Path(args.flake), args.secret)
remove_secret(args.flake.path, args.secret)
def add_secret_argument(parser: argparse.ArgumentParser, autocomplete: bool) -> None:
@ -270,7 +270,7 @@ class ListSecretsOptions:
def list_command(args: argparse.Namespace) -> None:
options = ListSecretsOptions(
flake=FlakeId(args.flake),
flake=args.flake,
pattern=args.pattern,
)
lst = list_secrets(options.flake.path, options.pattern)
@ -287,7 +287,7 @@ def decrypt_secret(flake_dir: Path, secret: str) -> str:
def get_command(args: argparse.Namespace) -> None:
print(decrypt_secret(Path(args.flake), args.secret), end="")
print(decrypt_secret(args.flake.path, args.secret), end="")
def set_command(args: argparse.Namespace) -> None:
@ -300,8 +300,8 @@ def set_command(args: argparse.Namespace) -> None:
elif tty.is_interactive():
secret_value = getpass.getpass(prompt="Paste your secret: ")
encrypt_secret(
Path(args.flake),
sops_secrets_folder(Path(args.flake)) / args.secret,
args.flake.path,
sops_secrets_folder(args.flake.path) / args.secret,
secret_value,
args.user,
args.machine,
@ -310,7 +310,7 @@ def set_command(args: argparse.Namespace) -> None:
def rename_command(args: argparse.Namespace) -> None:
flake_dir = Path(args.flake)
flake_dir = args.flake.path
old_path = sops_secrets_folder(flake_dir) / args.secret
new_path = sops_secrets_folder(flake_dir) / args.new_name
if not old_path.exists():

View File

@ -86,7 +86,7 @@ def remove_secret(flake_dir: Path, user: str, secret: str) -> None:
def list_command(args: argparse.Namespace) -> None:
if args.flake is None:
raise ClanError("Could not find clan flake toplevel directory")
lst = list_users(Path(args.flake))
lst = list_users(args.flake.path)
if len(lst) > 0:
print("\n".join(lst))
@ -94,31 +94,31 @@ def list_command(args: argparse.Namespace) -> None:
def add_command(args: argparse.Namespace) -> None:
if args.flake is None:
raise ClanError("Could not find clan flake toplevel directory")
add_user(Path(args.flake), args.user, args.key, args.force)
add_user(args.flake.path, args.user, args.key, args.force)
def get_command(args: argparse.Namespace) -> None:
if args.flake is None:
raise ClanError("Could not find clan flake toplevel directory")
print(get_user(Path(args.flake), args.user))
print(get_user(args.flake.path, args.user))
def remove_command(args: argparse.Namespace) -> None:
if args.flake is None:
raise ClanError("Could not find clan flake toplevel directory")
remove_user(Path(args.flake), args.user)
remove_user(args.flake.path, args.user)
def add_secret_command(args: argparse.Namespace) -> None:
if args.flake is None:
raise ClanError("Could not find clan flake toplevel directory")
add_secret(Path(args.flake), args.user, args.secret)
add_secret(args.flake.path, args.user, args.secret)
def remove_secret_command(args: argparse.Namespace) -> None:
if args.flake is None:
raise ClanError("Could not find clan flake toplevel directory")
remove_secret(Path(args.flake), args.user, args.secret)
remove_secret(args.flake.path, args.user, args.secret)
def register_users_parser(parser: argparse.ArgumentParser) -> None:

View File

@ -36,7 +36,7 @@ class InspectOptions:
def inspect_command(args: argparse.Namespace) -> None:
inspect_options = InspectOptions(
machine=args.machine,
flake=FlakeId(args.flake or Path.cwd()),
flake=args.flake or FlakeId(Path.cwd()),
)
machine = Machine(inspect_options.machine, inspect_options.flake)

View File

@ -7,7 +7,6 @@ from contextlib import ExitStack
from pathlib import Path
from tempfile import TemporaryDirectory
from ..clan_uri import FlakeId
from ..cmd import Log, run
from ..completions import add_dynamic_completer, complete_machines
from ..dirs import module_root, user_cache_dir, vm_state_dir
@ -194,16 +193,13 @@ def run_vm(
def run_command(
machine: str,
flake: Path,
option: list[str] = [],
**kwargs: dict[str, str],
args: argparse.Namespace,
) -> None:
machine_obj: Machine = Machine(machine, FlakeId(flake))
machine_obj: Machine = Machine(args.machine, args.flake)
vm: VmConfig = inspect_vm(machine=machine_obj)
run_vm(vm, nix_options=option)
run_vm(vm, nix_options=args.option)
def register_run_parser(parser: argparse.ArgumentParser) -> None:
@ -211,4 +207,4 @@ def register_run_parser(parser: argparse.ArgumentParser) -> None:
"machine", type=str, help="machine in the flake to run"
)
add_dynamic_completer(machine_action, complete_machines)
parser.set_defaults(func=lambda args: run_command(**args.__dict__))
parser.set_defaults(func=lambda args: run_command(args))

View File

@ -1,6 +1,7 @@
import pytest
from fixtures_flakes import FlakeForTest
from clan_cli.clan_uri import FlakeId
from clan_cli.config.machine import (
config_for_machine,
set_config_for_machine,
@ -23,7 +24,7 @@ def test_create_machine_on_minimal_clan(test_flake_minimal: FlakeForTest) -> Non
assert list_machines(test_flake_minimal.path) == {}
create_machine(
test_flake_minimal.path,
FlakeId(test_flake_minimal.path),
Machine(
name="foo",
system="x86_64-linux",

View File

@ -5,6 +5,7 @@ import pytest
from fixtures_flakes import FlakeForTest
from clan_cli.api.modules import list_modules, update_module_instance
from clan_cli.clan_uri import FlakeId
from clan_cli.inventory import Machine, Role, Service, ServiceMeta
from clan_cli.machines.create import create_machine
from clan_cli.nix import nix_eval, run_no_stdout
@ -50,7 +51,9 @@ def test_add_module_to_inventory(
age_keys[0].pubkey,
]
)
create_machine(base_path, Machine(name="machine1", tags=[], system="x86_64-linux"))
create_machine(
FlakeId(base_path), Machine(name="machine1", tags=[], system="x86_64-linux")
)
update_module_instance(
base_path,
"borgbackup",