Fix: regression list_machines. Split into multiple functions

list_inventory_machines, list_nixos_machine, list_sops_machines
The caller of the function should specify which machines they wants to see
This commit is contained in:
Johannes Kirschbauer
2024-08-03 12:32:37 +02:00
parent 79e15bff24
commit 91397adbfc
7 changed files with 19 additions and 11 deletions

View File

@@ -6,7 +6,7 @@ from ..clan_uri import FlakeId
from ..cmd import run
from ..dirs import machine_gcroot
from ..errors import ClanError
from ..machines.list import list_machines
from ..machines.list import list_nixos_machines
from ..machines.machines import Machine
from ..nix import nix_add_to_gcroots, nix_build, nix_config, nix_eval, nix_metadata
from ..vms.inspect import VmConfig, inspect_vm
@@ -40,7 +40,7 @@ def inspect_flake(flake_url: str | Path, machine_name: str) -> FlakeConfig:
system = config["system"]
# Check if the machine exists
machines = list_machines(flake_url, False)
machines: list[str] = list_nixos_machines(flake_url, False)
if machine_name not in machines:
raise ClanError(
f"Machine {machine_name} not found in {flake_url}. Available machines: {', '.join(machines)}"

View File

@@ -7,7 +7,7 @@ import logging
from typing import Any
from clan_cli.clan.inspect import FlakeConfig, inspect_flake
from clan_cli.machines.list import list_machines
from clan_cli.machines.list import list_nixos_machines
from ..clan_uri import ClanURI
from ..dirs import user_history_file
@@ -72,7 +72,7 @@ def new_history_entry(url: str, machine: str) -> HistoryEntry:
def add_all_to_history(uri: ClanURI) -> list[HistoryEntry]:
history = list_history()
new_entries: list[HistoryEntry] = []
for machine in list_machines(uri.get_url()):
for machine in list_nixos_machines(uri.get_url()):
new_entry = _add_maschine_to_history_list(uri.get_url(), machine, history)
new_entries.append(new_entry)
write_history_file(history)

View File

@@ -13,7 +13,9 @@ log = logging.getLogger(__name__)
@API.register
def list_machines(flake_url: str | Path, debug: bool = False) -> dict[str, Machine]:
def list_inventory_machines(
flake_url: str | Path, debug: bool = False
) -> dict[str, Machine]:
inventory = load_inventory_eval(flake_url)
return inventory.machines

View File

@@ -47,10 +47,16 @@ def get_machine(flake_dir: Path, name: str) -> str:
def has_machine(flake_dir: Path, name: str) -> bool:
"""
Checks if a machine exists in the sops machines folder
"""
return (sops_machines_folder(flake_dir) / name / "key.json").exists()
def list_machines(flake_dir: Path) -> list[str]:
def list_sops_machines(flake_dir: Path) -> list[str]:
"""
Lists all machines in the sops machines folder
"""
path = sops_machines_folder(flake_dir)
def validate(name: str) -> bool:
@@ -86,7 +92,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(args.flake.path)
lst = list_sops_machines(args.flake.path)
if len(lst) > 0:
print("\n".join(lst))

View File

@@ -113,7 +113,7 @@ class ClanList(Gtk.Box):
# menu_model = Gio.Menu()
# TODO: Make this lazy, blocks UI startup for too long
# for vm in machines.list.list_machines(flake_url=vm.data.flake.flake_url):
# for vm in machines.list.list_nixos_machines(flake_url=vm.data.flake.flake_url):
# if vm not in vm_store:
# menu_model.append(vm, f"app.add::{vm}")

View File

@@ -1,7 +1,7 @@
import { createSignal, Match, Show, Switch } from "solid-js";
import { ErrorData, pyApi, SuccessData } from "../api";
type MachineDetails = SuccessData<"list_machines">["data"][string];
type MachineDetails = SuccessData<"list_inventory_machines">["data"][string];
interface MachineListItemProps {
name: string;

View File

@@ -23,7 +23,7 @@ import { MachineListItem } from "@/src/components/MachineListItem";
// >["data"]["services"];
type MachinesModel = Extract<
OperationResponse<"list_machines">,
OperationResponse<"list_inventory_machines">,
{ status: "success" }
>["data"];
@@ -63,7 +63,7 @@ export const MachineListView: Component = () => {
return;
}
setLoading(true);
const response = await callApi("list_machines", {
const response = await callApi("list_inventory_machines", {
flake_url: uri,
});
setLoading(false);