move FlakeId to flake id #1701
@ -25,6 +25,7 @@ from . import (
|
|||||||
state,
|
state,
|
||||||
vms,
|
vms,
|
||||||
)
|
)
|
||||||
|
from .clan_uri import FlakeId
|
||||||
from .custom_logger import setup_logging
|
from .custom_logger import setup_logging
|
||||||
from .dirs import get_clan_flake_toplevel_or_env
|
from .dirs import get_clan_flake_toplevel_or_env
|
||||||
from .errors import ClanCmdError, ClanError
|
from .errors import ClanCmdError, ClanError
|
||||||
@ -41,11 +42,18 @@ except ImportError:
|
|||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
def flake_path(arg: str) -> str | Path:
|
def flake_path(arg: str) -> FlakeId:
|
||||||
flake_dir = Path(arg).resolve()
|
flake_dir = Path(arg).resolve()
|
||||||
if flake_dir.exists() and flake_dir.is_dir():
|
if flake_dir.exists() and flake_dir.is_dir():
|
||||||
return flake_dir
|
return FlakeId(flake_dir)
|
||||||
return arg
|
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:
|
def add_common_flags(parser: argparse.ArgumentParser) -> None:
|
||||||
@ -68,7 +76,7 @@ def add_common_flags(parser: argparse.ArgumentParser) -> None:
|
|||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
"--flake",
|
"--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",
|
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",
|
metavar="PATH",
|
||||||
type=flake_path,
|
type=flake_path,
|
||||||
)
|
)
|
||||||
|
@ -2,7 +2,6 @@ import argparse
|
|||||||
import json
|
import json
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
from ..clan_uri import FlakeId
|
|
||||||
from ..completions import (
|
from ..completions import (
|
||||||
add_dynamic_completer,
|
add_dynamic_completer,
|
||||||
complete_backup_providers_for_machine,
|
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:
|
def create_command(args: argparse.Namespace) -> None:
|
||||||
if args.flake is None:
|
if args.flake is None:
|
||||||
raise ClanError("Could not find clan flake toplevel directory")
|
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)
|
create_backup(machine=machine, provider=args.provider)
|
||||||
|
|
||||||
|
|
||||||
|
@ -3,7 +3,6 @@ import json
|
|||||||
import subprocess
|
import subprocess
|
||||||
from dataclasses import dataclass
|
from dataclasses import dataclass
|
||||||
|
|
||||||
from ..clan_uri import FlakeId
|
|
||||||
from ..completions import (
|
from ..completions import (
|
||||||
add_dynamic_completer,
|
add_dynamic_completer,
|
||||||
complete_backup_providers_for_machine,
|
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:
|
def list_command(args: argparse.Namespace) -> None:
|
||||||
if args.flake is None:
|
if args.flake is None:
|
||||||
raise ClanError("Could not find clan flake toplevel directory")
|
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)
|
backups = list_backups(machine=machine, provider=args.provider)
|
||||||
for backup in backups:
|
for backup in backups:
|
||||||
print(backup.name)
|
print(backup.name)
|
||||||
|
@ -2,7 +2,6 @@ import argparse
|
|||||||
import json
|
import json
|
||||||
import subprocess
|
import subprocess
|
||||||
|
|
||||||
from ..clan_uri import FlakeId
|
|
||||||
from ..completions import (
|
from ..completions import (
|
||||||
add_dynamic_completer,
|
add_dynamic_completer,
|
||||||
complete_backup_providers_for_machine,
|
complete_backup_providers_for_machine,
|
||||||
@ -87,7 +86,7 @@ def restore_backup(
|
|||||||
def restore_command(args: argparse.Namespace) -> None:
|
def restore_command(args: argparse.Namespace) -> None:
|
||||||
if args.flake is None:
|
if args.flake is None:
|
||||||
raise ClanError("Could not find clan flake toplevel directory")
|
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(
|
restore_backup(
|
||||||
machine=machine,
|
machine=machine,
|
||||||
provider=args.provider,
|
provider=args.provider,
|
||||||
|
@ -109,7 +109,7 @@ class InspectOptions:
|
|||||||
def inspect_command(args: argparse.Namespace) -> None:
|
def inspect_command(args: argparse.Namespace) -> None:
|
||||||
inspect_options = InspectOptions(
|
inspect_options = InspectOptions(
|
||||||
machine=args.machine,
|
machine=args.machine,
|
||||||
flake=FlakeId(args.flake or Path.cwd()),
|
flake=args.flake or FlakeId(Path.cwd()),
|
||||||
)
|
)
|
||||||
res = inspect_flake(
|
res = inspect_flake(
|
||||||
flake_url=str(inspect_options.flake), machine_name=inspect_options.machine
|
flake_url=str(inspect_options.flake), machine_name=inspect_options.machine
|
||||||
|
@ -69,7 +69,7 @@ def show_clan_meta(uri: str | Path) -> ClanMetaInfo:
|
|||||||
|
|
||||||
|
|
||||||
def show_command(args: argparse.Namespace) -> None:
|
def show_command(args: argparse.Namespace) -> None:
|
||||||
flake_path = Path(args.flake).resolve()
|
flake_path = args.flake.path
|
||||||
meta = show_clan_meta(flake_path)
|
meta = show_clan_meta(flake_path)
|
||||||
|
|
||||||
print(f"Name: {meta.name}")
|
print(f"Name: {meta.name}")
|
||||||
|
@ -12,6 +12,11 @@ class FlakeId:
|
|||||||
# FIXME: this is such a footgun if you accidnetally pass a string
|
# FIXME: this is such a footgun if you accidnetally pass a string
|
||||||
_value: str | Path
|
_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:
|
def __str__(self) -> str:
|
||||||
return str(
|
return str(
|
||||||
self._value
|
self._value
|
||||||
@ -19,12 +24,12 @@ class FlakeId:
|
|||||||
|
|
||||||
@property
|
@property
|
||||||
def path(self) -> Path:
|
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
|
return self._value
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def url(self) -> str:
|
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
|
return self._value
|
||||||
|
|
||||||
def is_local(self) -> bool:
|
def is_local(self) -> bool:
|
||||||
|
@ -178,12 +178,12 @@ def get_or_set_option(args: argparse.Namespace) -> None:
|
|||||||
options = json.load(f)
|
options = json.load(f)
|
||||||
# compute settings json file location
|
# compute settings json file location
|
||||||
if args.settings_file is None:
|
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:
|
else:
|
||||||
settings_file = args.settings_file
|
settings_file = args.settings_file
|
||||||
# set the option with the given value
|
# set the option with the given value
|
||||||
set_option(
|
set_option(
|
||||||
flake_dir=Path(args.flake),
|
flake_dir=args.flake.path,
|
||||||
option=args.option,
|
option=args.option,
|
||||||
value=args.value,
|
value=args.value,
|
||||||
options=options,
|
options=options,
|
||||||
|
@ -2,7 +2,6 @@ import argparse
|
|||||||
import importlib
|
import importlib
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
from ..clan_uri import FlakeId
|
|
||||||
from ..completions import add_dynamic_completer, complete_machines
|
from ..completions import add_dynamic_completer, complete_machines
|
||||||
from ..machines.machines import Machine
|
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:
|
def check_command(args: argparse.Namespace) -> None:
|
||||||
machine = Machine(
|
machine = Machine(
|
||||||
name=args.machine,
|
name=args.machine,
|
||||||
flake=FlakeId(args.flake),
|
flake=args.flake,
|
||||||
)
|
)
|
||||||
check_secrets(machine, service=args.service)
|
check_secrets(machine, service=args.service)
|
||||||
|
|
||||||
|
@ -208,13 +208,14 @@ def generate_facts(
|
|||||||
was_regenerated |= _generate_facts_for_machine(
|
was_regenerated |= _generate_facts_for_machine(
|
||||||
machine, service, regenerate, tmpdir, prompt
|
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}")
|
log.error(f"Failed to generate facts for {machine.name}: {exc}")
|
||||||
errors += 1
|
errors += 1
|
||||||
if errors > 0:
|
if errors > 0:
|
||||||
raise ClanError(
|
msg = (
|
||||||
f"Failed to generate facts for {errors} hosts. Check the logs above"
|
f"Failed to generate facts for {errors} hosts. Check the logs above"
|
||||||
)
|
)
|
||||||
|
raise ClanError(msg)
|
||||||
|
|
||||||
if not was_regenerated:
|
if not was_regenerated:
|
||||||
print("All secrets and facts are already up to date")
|
print("All secrets and facts are already up to date")
|
||||||
|
@ -3,7 +3,6 @@ import importlib
|
|||||||
import json
|
import json
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
from ..clan_uri import FlakeId
|
|
||||||
from ..completions import add_dynamic_completer, complete_machines
|
from ..completions import add_dynamic_completer, complete_machines
|
||||||
from ..machines.machines import Machine
|
from ..machines.machines import Machine
|
||||||
|
|
||||||
@ -27,7 +26,7 @@ def get_all_facts(machine: Machine) -> dict:
|
|||||||
|
|
||||||
|
|
||||||
def get_command(args: argparse.Namespace) -> None:
|
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
|
# the raw_facts are bytestrings making them not json serializable
|
||||||
raw_facts = get_all_facts(machine)
|
raw_facts = get_all_facts(machine)
|
||||||
|
@ -4,7 +4,6 @@ import logging
|
|||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from tempfile import TemporaryDirectory
|
from tempfile import TemporaryDirectory
|
||||||
|
|
||||||
from ..clan_uri import FlakeId
|
|
||||||
from ..cmd import Log, run
|
from ..cmd import Log, run
|
||||||
from ..completions import add_dynamic_completer, complete_machines
|
from ..completions import add_dynamic_completer, complete_machines
|
||||||
from ..machines.machines import Machine
|
from ..machines.machines import Machine
|
||||||
@ -45,7 +44,7 @@ def upload_secrets(machine: Machine) -> None:
|
|||||||
|
|
||||||
|
|
||||||
def upload_command(args: argparse.Namespace) -> 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)
|
upload_secrets(machine)
|
||||||
|
|
||||||
|
|
||||||
|
@ -120,7 +120,7 @@ class AppendDiskAction(argparse.Action):
|
|||||||
|
|
||||||
def flash_command(args: argparse.Namespace) -> None:
|
def flash_command(args: argparse.Namespace) -> None:
|
||||||
opts = FlashOptions(
|
opts = FlashOptions(
|
||||||
flake=FlakeId(args.flake),
|
flake=args.flake,
|
||||||
machine=args.machine,
|
machine=args.machine,
|
||||||
disks=args.disk,
|
disks=args.disk,
|
||||||
ssh_keys_path=args.ssh_pubkey,
|
ssh_keys_path=args.ssh_pubkey,
|
||||||
|
@ -3,27 +3,28 @@ import logging
|
|||||||
import re
|
import re
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
from clan_cli.api import API
|
from ..api import API
|
||||||
from clan_cli.errors import ClanError
|
from ..clan_uri import FlakeId
|
||||||
from clan_cli.git import commit_file
|
from ..errors import ClanError
|
||||||
from clan_cli.inventory import Inventory, Machine
|
from ..git import commit_file
|
||||||
|
from ..inventory import Inventory, Machine
|
||||||
|
|
||||||
log = logging.getLogger(__name__)
|
log = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
@API.register
|
@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}(?<!-)$"
|
hostname_regex = r"^(?!-)[A-Za-z0-9-]{1,63}(?<!-)$"
|
||||||
if not re.match(hostname_regex, machine.name):
|
if not re.match(hostname_regex, machine.name):
|
||||||
raise ClanError(
|
raise ClanError(
|
||||||
"Machine name must be a valid hostname", location="Create Machine"
|
"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.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:
|
def create_command(args: argparse.Namespace) -> None:
|
||||||
|
@ -1,26 +1,25 @@
|
|||||||
import argparse
|
import argparse
|
||||||
import shutil
|
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 ..completions import add_dynamic_completer, complete_machines
|
||||||
from ..dirs import specific_machine_dir
|
from ..dirs import specific_machine_dir
|
||||||
from ..errors import ClanError
|
from ..errors import ClanError
|
||||||
|
from ..inventory import Inventory
|
||||||
|
|
||||||
|
|
||||||
@API.register
|
@API.register
|
||||||
def delete_machine(base_dir: str | Path, name: str) -> None:
|
def delete_machine(flake: FlakeId, name: str) -> None:
|
||||||
inventory = Inventory.load_file(base_dir)
|
inventory = Inventory.load_file(flake.path)
|
||||||
|
|
||||||
machine = inventory.machines.pop(name, None)
|
machine = inventory.machines.pop(name, None)
|
||||||
if machine is None:
|
if machine is None:
|
||||||
raise ClanError(f"Machine {name} does not exist")
|
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():
|
if folder.exists():
|
||||||
shutil.rmtree(folder)
|
shutil.rmtree(folder)
|
||||||
|
|
||||||
|
@ -147,7 +147,7 @@ def generate_machine_hardware_info(
|
|||||||
|
|
||||||
|
|
||||||
def hw_generate_command(args: argparse.Namespace) -> None:
|
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(
|
hw_info = generate_machine_hardware_info(
|
||||||
flake_path, args.machine, args.hostname, args.password, args.force
|
flake_path, args.machine, args.hostname, args.password, args.force
|
||||||
)
|
)
|
||||||
|
@ -124,7 +124,7 @@ def install_command(args: argparse.Namespace) -> None:
|
|||||||
password = None
|
password = None
|
||||||
|
|
||||||
opts = InstallOptions(
|
opts = InstallOptions(
|
||||||
flake=FlakeId(args.flake),
|
flake=args.flake,
|
||||||
machine=args.machine,
|
machine=args.machine,
|
||||||
target_host=target_host,
|
target_host=target_host,
|
||||||
kexec=args.kexec,
|
kexec=args.kexec,
|
||||||
|
@ -8,11 +8,11 @@ from .machines import Machine
|
|||||||
|
|
||||||
|
|
||||||
# function to speedup eval if we want to evaluate all machines
|
# 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()
|
config = nix_config()
|
||||||
system = config["system"]
|
system = config["system"]
|
||||||
json_path = run(
|
json_path = run(
|
||||||
nix_build([f'{flake_dir}#clanInternals.all-machines-json."{system}"'])
|
nix_build([f'{flake}#clanInternals.all-machines-json."{system}"'])
|
||||||
).stdout
|
).stdout
|
||||||
|
|
||||||
machines_json = json.loads(Path(json_path.rstrip()).read_text())
|
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(
|
machines.append(
|
||||||
Machine(
|
Machine(
|
||||||
name=name,
|
name=name,
|
||||||
flake=FlakeId(flake_dir),
|
flake=flake,
|
||||||
cached_deployment=machine_data,
|
cached_deployment=machine_data,
|
||||||
nix_options=nix_options,
|
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(
|
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]:
|
) -> list[Machine]:
|
||||||
machines = []
|
machines = []
|
||||||
for name in machine_names:
|
for name in machine_names:
|
||||||
machines.append(
|
machines.append(Machine(name=name, flake=flake, nix_options=nix_options))
|
||||||
Machine(name=name, flake=FlakeId(flake_dir), nix_options=nix_options)
|
|
||||||
)
|
|
||||||
return machines
|
return machines
|
||||||
|
@ -29,7 +29,7 @@ def list_machines(flake_url: str | Path, debug: bool = False) -> dict[str, Machi
|
|||||||
|
|
||||||
|
|
||||||
def list_command(args: argparse.Namespace) -> None:
|
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():
|
for name in list_machines(flake_path, args.debug).keys():
|
||||||
print(name)
|
print(name)
|
||||||
|
|
||||||
|
@ -45,8 +45,7 @@ def show_machine(flake_url: str | Path, machine_name: str) -> MachineInfo:
|
|||||||
|
|
||||||
|
|
||||||
def show_command(args: argparse.Namespace) -> None:
|
def show_command(args: argparse.Namespace) -> None:
|
||||||
flake_path = Path(args.flake).resolve()
|
machine = show_machine(args.flake.path, args.machine)
|
||||||
machine = show_machine(flake_path, args.machine)
|
|
||||||
print(f"Name: {machine.machine_name}")
|
print(f"Name: {machine.machine_name}")
|
||||||
print(f"Description: {machine.machine_description or ''}")
|
print(f"Description: {machine.machine_description or ''}")
|
||||||
print(f"Icon: {machine.machine_icon or ''}")
|
print(f"Icon: {machine.machine_icon or ''}")
|
||||||
|
@ -5,7 +5,6 @@ import os
|
|||||||
import shlex
|
import shlex
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
from ..clan_uri import FlakeId
|
|
||||||
from ..cmd import run
|
from ..cmd import run
|
||||||
from ..completions import add_dynamic_completer, complete_machines
|
from ..completions import add_dynamic_completer, complete_machines
|
||||||
from ..errors import ClanError
|
from ..errors import ClanError
|
||||||
@ -138,7 +137,7 @@ def update(args: argparse.Namespace) -> None:
|
|||||||
machines = []
|
machines = []
|
||||||
if len(args.machines) == 1 and args.target_host is not None:
|
if len(args.machines) == 1 and args.target_host is not None:
|
||||||
machine = Machine(
|
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
|
machine.target_host_address = args.target_host
|
||||||
machines.append(machine)
|
machines.append(machine)
|
||||||
|
@ -74,7 +74,7 @@ def list_groups(flake_dir: Path) -> list[Group]:
|
|||||||
|
|
||||||
|
|
||||||
def list_command(args: argparse.Namespace) -> None:
|
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)
|
print(group.name)
|
||||||
if group.machines:
|
if group.machines:
|
||||||
print("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:
|
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:
|
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:
|
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:
|
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:
|
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:
|
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:
|
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:
|
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:
|
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:
|
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:
|
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:
|
def register_groups_parser(parser: argparse.ArgumentParser) -> None:
|
||||||
|
@ -40,15 +40,15 @@ def import_sops(args: argparse.Namespace) -> None:
|
|||||||
file=sys.stderr,
|
file=sys.stderr,
|
||||||
)
|
)
|
||||||
continue
|
continue
|
||||||
if (sops_secrets_folder(Path(args.flake)) / k / "secret").exists():
|
if (sops_secrets_folder(args.flake.path) / k / "secret").exists():
|
||||||
print(
|
print(
|
||||||
f"WARNING: {k} already exists, skipping",
|
f"WARNING: {k} already exists, skipping",
|
||||||
file=sys.stderr,
|
file=sys.stderr,
|
||||||
)
|
)
|
||||||
continue
|
continue
|
||||||
encrypt_secret(
|
encrypt_secret(
|
||||||
Path(args.flake),
|
args.flake.path,
|
||||||
sops_secrets_folder(Path(args.flake)) / k,
|
sops_secrets_folder(args.flake.path) / k,
|
||||||
v,
|
v,
|
||||||
add_groups=args.group,
|
add_groups=args.group,
|
||||||
add_machines=args.machine,
|
add_machines=args.machine,
|
||||||
|
@ -58,7 +58,7 @@ def show_command(args: argparse.Namespace) -> None:
|
|||||||
|
|
||||||
|
|
||||||
def update_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.")
|
commit_files(update_secrets(flake_dir), flake_dir, "Updated secrets with new keys.")
|
||||||
|
|
||||||
|
|
||||||
|
@ -81,7 +81,7 @@ def remove_secret(flake_dir: Path, machine: str, secret: str) -> None:
|
|||||||
def list_command(args: argparse.Namespace) -> None:
|
def list_command(args: argparse.Namespace) -> None:
|
||||||
if args.flake is None:
|
if args.flake is None:
|
||||||
raise ClanError("Could not find clan flake toplevel directory")
|
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:
|
if len(lst) > 0:
|
||||||
print("\n".join(lst))
|
print("\n".join(lst))
|
||||||
|
|
||||||
@ -89,31 +89,31 @@ def list_command(args: argparse.Namespace) -> None:
|
|||||||
def add_command(args: argparse.Namespace) -> None:
|
def add_command(args: argparse.Namespace) -> None:
|
||||||
if args.flake is None:
|
if args.flake is None:
|
||||||
raise ClanError("Could not find clan flake toplevel directory")
|
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:
|
def get_command(args: argparse.Namespace) -> None:
|
||||||
if args.flake is None:
|
if args.flake is None:
|
||||||
raise ClanError("Could not find clan flake toplevel directory")
|
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:
|
def remove_command(args: argparse.Namespace) -> None:
|
||||||
if args.flake is None:
|
if args.flake is None:
|
||||||
raise ClanError("Could not find clan flake toplevel directory")
|
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:
|
def add_secret_command(args: argparse.Namespace) -> None:
|
||||||
if args.flake is None:
|
if args.flake is None:
|
||||||
raise ClanError("Could not find clan flake toplevel directory")
|
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:
|
def remove_secret_command(args: argparse.Namespace) -> None:
|
||||||
if args.flake is None:
|
if args.flake is None:
|
||||||
raise ClanError("Could not find clan flake toplevel directory")
|
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:
|
def register_machines_parser(parser: argparse.ArgumentParser) -> None:
|
||||||
|
@ -158,7 +158,7 @@ def remove_secret(flake_dir: Path, secret: str) -> None:
|
|||||||
|
|
||||||
|
|
||||||
def remove_command(args: argparse.Namespace) -> 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:
|
def add_secret_argument(parser: argparse.ArgumentParser, autocomplete: bool) -> None:
|
||||||
@ -270,7 +270,7 @@ class ListSecretsOptions:
|
|||||||
|
|
||||||
def list_command(args: argparse.Namespace) -> None:
|
def list_command(args: argparse.Namespace) -> None:
|
||||||
options = ListSecretsOptions(
|
options = ListSecretsOptions(
|
||||||
flake=FlakeId(args.flake),
|
flake=args.flake,
|
||||||
pattern=args.pattern,
|
pattern=args.pattern,
|
||||||
)
|
)
|
||||||
lst = list_secrets(options.flake.path, options.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:
|
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:
|
def set_command(args: argparse.Namespace) -> None:
|
||||||
@ -300,8 +300,8 @@ def set_command(args: argparse.Namespace) -> None:
|
|||||||
elif tty.is_interactive():
|
elif tty.is_interactive():
|
||||||
secret_value = getpass.getpass(prompt="Paste your secret: ")
|
secret_value = getpass.getpass(prompt="Paste your secret: ")
|
||||||
encrypt_secret(
|
encrypt_secret(
|
||||||
Path(args.flake),
|
args.flake.path,
|
||||||
sops_secrets_folder(Path(args.flake)) / args.secret,
|
sops_secrets_folder(args.flake.path) / args.secret,
|
||||||
secret_value,
|
secret_value,
|
||||||
args.user,
|
args.user,
|
||||||
args.machine,
|
args.machine,
|
||||||
@ -310,7 +310,7 @@ def set_command(args: argparse.Namespace) -> None:
|
|||||||
|
|
||||||
|
|
||||||
def rename_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
|
old_path = sops_secrets_folder(flake_dir) / args.secret
|
||||||
new_path = sops_secrets_folder(flake_dir) / args.new_name
|
new_path = sops_secrets_folder(flake_dir) / args.new_name
|
||||||
if not old_path.exists():
|
if not old_path.exists():
|
||||||
|
@ -86,7 +86,7 @@ def remove_secret(flake_dir: Path, user: str, secret: str) -> None:
|
|||||||
def list_command(args: argparse.Namespace) -> None:
|
def list_command(args: argparse.Namespace) -> None:
|
||||||
if args.flake is None:
|
if args.flake is None:
|
||||||
raise ClanError("Could not find clan flake toplevel directory")
|
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:
|
if len(lst) > 0:
|
||||||
print("\n".join(lst))
|
print("\n".join(lst))
|
||||||
|
|
||||||
@ -94,31 +94,31 @@ def list_command(args: argparse.Namespace) -> None:
|
|||||||
def add_command(args: argparse.Namespace) -> None:
|
def add_command(args: argparse.Namespace) -> None:
|
||||||
if args.flake is None:
|
if args.flake is None:
|
||||||
raise ClanError("Could not find clan flake toplevel directory")
|
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:
|
def get_command(args: argparse.Namespace) -> None:
|
||||||
if args.flake is None:
|
if args.flake is None:
|
||||||
raise ClanError("Could not find clan flake toplevel directory")
|
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:
|
def remove_command(args: argparse.Namespace) -> None:
|
||||||
if args.flake is None:
|
if args.flake is None:
|
||||||
raise ClanError("Could not find clan flake toplevel directory")
|
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:
|
def add_secret_command(args: argparse.Namespace) -> None:
|
||||||
if args.flake is None:
|
if args.flake is None:
|
||||||
raise ClanError("Could not find clan flake toplevel directory")
|
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:
|
def remove_secret_command(args: argparse.Namespace) -> None:
|
||||||
if args.flake is None:
|
if args.flake is None:
|
||||||
raise ClanError("Could not find clan flake toplevel directory")
|
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:
|
def register_users_parser(parser: argparse.ArgumentParser) -> None:
|
||||||
|
@ -36,7 +36,7 @@ class InspectOptions:
|
|||||||
def inspect_command(args: argparse.Namespace) -> None:
|
def inspect_command(args: argparse.Namespace) -> None:
|
||||||
inspect_options = InspectOptions(
|
inspect_options = InspectOptions(
|
||||||
machine=args.machine,
|
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)
|
machine = Machine(inspect_options.machine, inspect_options.flake)
|
||||||
|
@ -7,7 +7,6 @@ from contextlib import ExitStack
|
|||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from tempfile import TemporaryDirectory
|
from tempfile import TemporaryDirectory
|
||||||
|
|
||||||
from ..clan_uri import FlakeId
|
|
||||||
from ..cmd import Log, run
|
from ..cmd import Log, run
|
||||||
from ..completions import add_dynamic_completer, complete_machines
|
from ..completions import add_dynamic_completer, complete_machines
|
||||||
from ..dirs import module_root, user_cache_dir, vm_state_dir
|
from ..dirs import module_root, user_cache_dir, vm_state_dir
|
||||||
@ -194,16 +193,13 @@ def run_vm(
|
|||||||
|
|
||||||
|
|
||||||
def run_command(
|
def run_command(
|
||||||
machine: str,
|
args: argparse.Namespace,
|
||||||
flake: Path,
|
|
||||||
option: list[str] = [],
|
|
||||||
**kwargs: dict[str, str],
|
|
||||||
) -> None:
|
) -> None:
|
||||||
machine_obj: Machine = Machine(machine, FlakeId(flake))
|
machine_obj: Machine = Machine(args.machine, args.flake)
|
||||||
|
|
||||||
vm: VmConfig = inspect_vm(machine=machine_obj)
|
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:
|
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"
|
"machine", type=str, help="machine in the flake to run"
|
||||||
)
|
)
|
||||||
add_dynamic_completer(machine_action, complete_machines)
|
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))
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
import pytest
|
import pytest
|
||||||
from fixtures_flakes import FlakeForTest
|
from fixtures_flakes import FlakeForTest
|
||||||
|
|
||||||
|
from clan_cli.clan_uri import FlakeId
|
||||||
from clan_cli.config.machine import (
|
from clan_cli.config.machine import (
|
||||||
config_for_machine,
|
config_for_machine,
|
||||||
set_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) == {}
|
assert list_machines(test_flake_minimal.path) == {}
|
||||||
|
|
||||||
create_machine(
|
create_machine(
|
||||||
test_flake_minimal.path,
|
FlakeId(test_flake_minimal.path),
|
||||||
Machine(
|
Machine(
|
||||||
name="foo",
|
name="foo",
|
||||||
system="x86_64-linux",
|
system="x86_64-linux",
|
||||||
|
@ -5,6 +5,7 @@ import pytest
|
|||||||
from fixtures_flakes import FlakeForTest
|
from fixtures_flakes import FlakeForTest
|
||||||
|
|
||||||
from clan_cli.api.modules import list_modules, update_module_instance
|
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.inventory import Machine, Role, Service, ServiceMeta
|
||||||
from clan_cli.machines.create import create_machine
|
from clan_cli.machines.create import create_machine
|
||||||
from clan_cli.nix import nix_eval, run_no_stdout
|
from clan_cli.nix import nix_eval, run_no_stdout
|
||||||
@ -50,7 +51,9 @@ def test_add_module_to_inventory(
|
|||||||
age_keys[0].pubkey,
|
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(
|
update_module_instance(
|
||||||
base_path,
|
base_path,
|
||||||
"borgbackup",
|
"borgbackup",
|
||||||
|
Loading…
Reference in New Issue
Block a user