Merge pull request 'clan-cli: Unify list_machines and use flake caching' (#3673) from Qubasa/clan-core:fix_ui_stuff into main
Reviewed-on: https://git.clan.lol/clan/clan-core/pulls/3673
This commit is contained in:
@@ -180,8 +180,8 @@ in
|
|||||||
meta = lib.mkOption { type = lib.types.raw; };
|
meta = lib.mkOption { type = lib.types.raw; };
|
||||||
secrets = lib.mkOption { type = lib.types.raw; };
|
secrets = lib.mkOption { type = lib.types.raw; };
|
||||||
clanLib = lib.mkOption { type = lib.types.raw; };
|
clanLib = lib.mkOption { type = lib.types.raw; };
|
||||||
all-machines-json = lib.mkOption { type = lib.types.raw; };
|
|
||||||
machines = lib.mkOption { type = lib.types.raw; };
|
machines = lib.mkOption { type = lib.types.raw; };
|
||||||
|
all-machines-json = lib.mkOption { type = lib.types.raw; };
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -231,11 +231,14 @@ in
|
|||||||
|
|
||||||
# machine specifics
|
# machine specifics
|
||||||
machines = configsPerSystem;
|
machines = configsPerSystem;
|
||||||
all-machines-json = lib.mapAttrs (
|
all-machines-json =
|
||||||
system: configs:
|
lib.trace "Your clan-cli and the clan-core input have incompatible versions" lib.mapAttrs
|
||||||
nixpkgs.legacyPackages.${system}.writers.writeJSON "machines.json" (
|
(
|
||||||
lib.mapAttrs (_: m: m.config.system.clan.deployment.data) configs
|
system: configs:
|
||||||
)
|
nixpkgs.legacyPackages.${system}.writers.writeJSON "machines.json" (
|
||||||
) configsPerSystem;
|
lib.mapAttrs (_: m: m.config.system.clan.deployment.data) configs
|
||||||
|
)
|
||||||
|
)
|
||||||
|
configsPerSystem;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -23,6 +23,9 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"path": "../clan-cli/clan_lib"
|
"path": "../clan-cli/clan_lib"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"path": "../webview-ui"
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"settings": {
|
"settings": {
|
||||||
|
|||||||
10
pkgs/clan-app/ui/src/api/manual_types.tsx
Normal file
10
pkgs/clan-app/ui/src/api/manual_types.tsx
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
export interface Machine {
|
||||||
|
machine: {
|
||||||
|
name: string;
|
||||||
|
flake: {
|
||||||
|
identifier: string;
|
||||||
|
};
|
||||||
|
override_target_host: string | null;
|
||||||
|
private_key: string | null;
|
||||||
|
};
|
||||||
|
}
|
||||||
@@ -10,7 +10,7 @@ import { Filter } from "../../routes/machines";
|
|||||||
import { Typography } from "../Typography";
|
import { Typography } from "../Typography";
|
||||||
import "./css/index.css";
|
import "./css/index.css";
|
||||||
|
|
||||||
type MachineDetails = SuccessQuery<"list_machines">["data"][string];
|
type MachineDetails = SuccessQuery<"list_inv_machines">["data"][string];
|
||||||
|
|
||||||
interface MachineListItemProps {
|
interface MachineListItemProps {
|
||||||
name: string;
|
name: string;
|
||||||
|
|||||||
@@ -61,7 +61,7 @@ export function CreateMachine() {
|
|||||||
reset(formStore);
|
reset(formStore);
|
||||||
|
|
||||||
queryClient.invalidateQueries({
|
queryClient.invalidateQueries({
|
||||||
queryKey: [activeURI(), "list_machines"],
|
queryKey: [activeURI(), "list_inv_machines"],
|
||||||
});
|
});
|
||||||
navigate("/machines");
|
navigate("/machines");
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -391,12 +391,14 @@ const MachineForm = (props: MachineDetailsProps) => {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const machine_response = await callApi("set_machine", {
|
const machine_response = await callApi("set_inv_machine", {
|
||||||
flake: {
|
|
||||||
identifier: curr_uri,
|
|
||||||
},
|
|
||||||
machine_name: props.initialData.machine.name || "My machine",
|
|
||||||
machine: {
|
machine: {
|
||||||
|
name: props.initialData.machine.name || "My machine",
|
||||||
|
flake: {
|
||||||
|
identifier: curr_uri,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
inventory_machine: {
|
||||||
...values.machine,
|
...values.machine,
|
||||||
// TODO: Remove this workaround
|
// TODO: Remove this workaround
|
||||||
tags: Array.from(
|
tags: Array.from(
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ import { Header } from "@/src/layout/header";
|
|||||||
import { makePersisted } from "@solid-primitives/storage";
|
import { makePersisted } from "@solid-primitives/storage";
|
||||||
|
|
||||||
type MachinesModel = Extract<
|
type MachinesModel = Extract<
|
||||||
OperationResponse<"list_machines">,
|
OperationResponse<"list_inv_machines">,
|
||||||
{ status: "success" }
|
{ status: "success" }
|
||||||
>["data"];
|
>["data"];
|
||||||
|
|
||||||
@@ -32,13 +32,13 @@ export const MachineListView: Component = () => {
|
|||||||
const [filter, setFilter] = createSignal<Filter>({ tags: [] });
|
const [filter, setFilter] = createSignal<Filter>({ tags: [] });
|
||||||
|
|
||||||
const inventoryQuery = createQuery<MachinesModel>(() => ({
|
const inventoryQuery = createQuery<MachinesModel>(() => ({
|
||||||
queryKey: [activeURI(), "list_machines"],
|
queryKey: [activeURI(), "list_inv_machines"],
|
||||||
placeholderData: {},
|
placeholderData: {},
|
||||||
enabled: !!activeURI(),
|
enabled: !!activeURI(),
|
||||||
queryFn: async () => {
|
queryFn: async () => {
|
||||||
const uri = activeURI();
|
const uri = activeURI();
|
||||||
if (uri) {
|
if (uri) {
|
||||||
const response = await callApi("list_machines", {
|
const response = await callApi("list_inv_machines", {
|
||||||
flake: {
|
flake: {
|
||||||
identifier: uri,
|
identifier: uri,
|
||||||
},
|
},
|
||||||
@@ -56,7 +56,7 @@ export const MachineListView: Component = () => {
|
|||||||
const refresh = async () => {
|
const refresh = async () => {
|
||||||
queryClient.invalidateQueries({
|
queryClient.invalidateQueries({
|
||||||
// Invalidates the cache for of all types of machine list at once
|
// Invalidates the cache for of all types of machine list at once
|
||||||
queryKey: [activeURI(), "list_machines"],
|
queryKey: [activeURI(), "list_inv_machines"],
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ from clan_cli.cmd import run
|
|||||||
from clan_cli.dirs import machine_gcroot
|
from clan_cli.dirs import machine_gcroot
|
||||||
from clan_cli.errors import ClanError
|
from clan_cli.errors import ClanError
|
||||||
from clan_cli.flake import Flake
|
from clan_cli.flake import Flake
|
||||||
from clan_cli.machines.list import list_nixos_machines
|
from clan_cli.machines.list import list_machines
|
||||||
from clan_cli.machines.machines import Machine
|
from clan_cli.machines.machines import Machine
|
||||||
from clan_cli.nix import (
|
from clan_cli.nix import (
|
||||||
nix_add_to_gcroots,
|
nix_add_to_gcroots,
|
||||||
@@ -57,7 +57,7 @@ def inspect_flake(flake_url: str | Path, machine_name: str) -> FlakeConfig:
|
|||||||
system = config["system"]
|
system = config["system"]
|
||||||
|
|
||||||
# Check if the machine exists
|
# Check if the machine exists
|
||||||
machines: list[str] = list_nixos_machines(flake_url)
|
machines: dict[str, Machine] = list_machines(Flake(str(flake_url)))
|
||||||
if machine_name not in machines:
|
if machine_name not in machines:
|
||||||
msg = f"Machine {machine_name} not found in {flake_url}. Available machines: {', '.join(machines)}"
|
msg = f"Machine {machine_name} not found in {flake_url}. Available machines: {', '.join(machines)}"
|
||||||
raise ClanError(msg)
|
raise ClanError(msg)
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ from clan_cli.completions import (
|
|||||||
)
|
)
|
||||||
from clan_cli.errors import ClanError
|
from clan_cli.errors import ClanError
|
||||||
from clan_cli.git import commit_files
|
from clan_cli.git import commit_files
|
||||||
from clan_cli.machines.inventory import get_all_machines, get_selected_machines
|
from clan_cli.machines.list import list_machines
|
||||||
from clan_cli.machines.machines import Machine
|
from clan_cli.machines.machines import Machine
|
||||||
from clan_cli.nix import nix_shell
|
from clan_cli.nix import nix_shell
|
||||||
|
|
||||||
@@ -225,10 +225,15 @@ def generate_command(args: argparse.Namespace) -> None:
|
|||||||
if args.flake is None:
|
if args.flake is None:
|
||||||
msg = "Could not find clan flake toplevel directory"
|
msg = "Could not find clan flake toplevel directory"
|
||||||
raise ClanError(msg)
|
raise ClanError(msg)
|
||||||
if len(args.machines) == 0:
|
|
||||||
machines = get_all_machines(args.flake, args.option)
|
machines: list[Machine] = list(list_machines(args.flake).values())
|
||||||
else:
|
if len(args.machines) > 0:
|
||||||
machines = get_selected_machines(args.flake, args.option, args.machines)
|
machines = list(
|
||||||
|
filter(
|
||||||
|
lambda m: m.name in args.machines,
|
||||||
|
machines,
|
||||||
|
)
|
||||||
|
)
|
||||||
generate_facts(machines, args.service, args.regenerate)
|
generate_facts(machines, args.service, args.regenerate)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -25,11 +25,9 @@ from clan_lib.persist.util import (
|
|||||||
determine_writeability,
|
determine_writeability,
|
||||||
)
|
)
|
||||||
|
|
||||||
from clan_cli.cmd import run
|
|
||||||
from clan_cli.errors import ClanError
|
from clan_cli.errors import ClanError
|
||||||
from clan_cli.flake import Flake
|
from clan_cli.flake import Flake
|
||||||
from clan_cli.git import commit_file
|
from clan_cli.git import commit_file
|
||||||
from clan_cli.nix import nix_eval
|
|
||||||
|
|
||||||
|
|
||||||
def get_inventory_path(flake: Flake) -> Path:
|
def get_inventory_path(flake: Flake) -> Path:
|
||||||
@@ -40,7 +38,7 @@ def get_inventory_path(flake: Flake) -> Path:
|
|||||||
return inventory_file
|
return inventory_file
|
||||||
|
|
||||||
|
|
||||||
def load_inventory_eval(flake_dir: Flake) -> Inventory:
|
def load_inventory_eval(flake: Flake) -> Inventory:
|
||||||
"""
|
"""
|
||||||
Loads the evaluated inventory.
|
Loads the evaluated inventory.
|
||||||
After all merge operations with eventual nix code in buildClan.
|
After all merge operations with eventual nix code in buildClan.
|
||||||
@@ -51,18 +49,9 @@ def load_inventory_eval(flake_dir: Flake) -> Inventory:
|
|||||||
- Contains all machines
|
- Contains all machines
|
||||||
- and more
|
- and more
|
||||||
"""
|
"""
|
||||||
cmd = nix_eval(
|
data = flake.select("clanInternals.inventory")
|
||||||
[
|
|
||||||
f"{flake_dir}#clanInternals.inventory",
|
|
||||||
"--json",
|
|
||||||
]
|
|
||||||
)
|
|
||||||
|
|
||||||
proc = run(cmd)
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
res = proc.stdout.strip()
|
|
||||||
data: dict = json.loads(res)
|
|
||||||
inventory = Inventory(data) # type: ignore
|
inventory = Inventory(data) # type: ignore
|
||||||
except json.JSONDecodeError as e:
|
except json.JSONDecodeError as e:
|
||||||
msg = f"Error decoding inventory from flake: {e}"
|
msg = f"Error decoding inventory from flake: {e}"
|
||||||
@@ -89,18 +78,9 @@ def get_inventory_current_priority(flake: Flake) -> dict:
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
"""
|
"""
|
||||||
cmd = nix_eval(
|
|
||||||
[
|
|
||||||
f"{flake}#clanInternals.inventoryClass.introspection",
|
|
||||||
"--json",
|
|
||||||
]
|
|
||||||
)
|
|
||||||
|
|
||||||
proc = run(cmd)
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
res = proc.stdout.strip()
|
data = flake.select("clanInternals.inventoryClass.introspection")
|
||||||
data = json.loads(res)
|
|
||||||
except json.JSONDecodeError as e:
|
except json.JSONDecodeError as e:
|
||||||
msg = f"Error decoding inventory from flake: {e}"
|
msg = f"Error decoding inventory from flake: {e}"
|
||||||
raise ClanError(msg) from e
|
raise ClanError(msg) from e
|
||||||
|
|||||||
@@ -21,7 +21,7 @@ from clan_cli.git import commit_file
|
|||||||
from clan_cli.inventory import (
|
from clan_cli.inventory import (
|
||||||
patch_inventory_with,
|
patch_inventory_with,
|
||||||
)
|
)
|
||||||
from clan_cli.machines.list import list_nixos_machines
|
from clan_cli.machines.list import list_machines
|
||||||
from clan_cli.templates import (
|
from clan_cli.templates import (
|
||||||
InputPrio,
|
InputPrio,
|
||||||
TemplateName,
|
TemplateName,
|
||||||
@@ -62,9 +62,9 @@ def create_machine(opts: CreateOptions, commit: bool = True) -> None:
|
|||||||
log.info(f"Found template '{template.name}' in '{template.input_variant}'")
|
log.info(f"Found template '{template.name}' in '{template.input_variant}'")
|
||||||
|
|
||||||
machine_name = opts.machine.get("name")
|
machine_name = opts.machine.get("name")
|
||||||
if opts.template_name in list_nixos_machines(clan_dir) and not opts.machine.get(
|
if opts.template_name in list_machines(
|
||||||
"name"
|
Flake(str(clan_dir))
|
||||||
):
|
) and not opts.machine.get("name"):
|
||||||
msg = f"{opts.template_name} is already defined in {clan_dir}"
|
msg = f"{opts.template_name} is already defined in {clan_dir}"
|
||||||
description = (
|
description = (
|
||||||
"Please add the --rename option to import the machine with a different name"
|
"Please add the --rename option to import the machine with a different name"
|
||||||
|
|||||||
@@ -1,45 +1,34 @@
|
|||||||
import json
|
from clan_lib.api import API
|
||||||
from pathlib import Path
|
from clan_lib.nix_models.inventory import (
|
||||||
|
Machine as InventoryMachine,
|
||||||
|
)
|
||||||
|
from clan_lib.persist.inventory_store import InventoryStore
|
||||||
|
from clan_lib.persist.util import apply_patch
|
||||||
|
|
||||||
from clan_cli.cmd import run
|
from clan_cli.errors import ClanError
|
||||||
from clan_cli.flake import Flake
|
from clan_cli.machines.machines import Machine
|
||||||
from clan_cli.nix import nix_build, nix_config, nix_test_store
|
|
||||||
|
|
||||||
from .machines import Machine
|
|
||||||
|
|
||||||
|
|
||||||
# function to speedup eval if we want to evaluate all machines
|
@API.register
|
||||||
def get_all_machines(flake: Flake, nix_options: list[str]) -> list[Machine]:
|
def get_inv_machine(machine: Machine) -> InventoryMachine:
|
||||||
config = nix_config()
|
inventory_store = InventoryStore(flake=machine.flake)
|
||||||
system = config["system"]
|
inventory = inventory_store.read()
|
||||||
json_path = Path(
|
|
||||||
run(
|
machine_inv = inventory.get("machines", {}).get(machine.name)
|
||||||
nix_build([f'{flake}#clanInternals.all-machines-json."{system}"'])
|
if machine_inv is None:
|
||||||
).stdout.rstrip()
|
msg = f"Machine {machine.name} not found in inventory"
|
||||||
|
raise ClanError(msg)
|
||||||
|
|
||||||
|
return InventoryMachine(**machine_inv)
|
||||||
|
|
||||||
|
|
||||||
|
@API.register
|
||||||
|
def set_inv_machine(machine: Machine, inventory_machine: InventoryMachine) -> None:
|
||||||
|
assert machine.name == inventory_machine["name"], "Machine name mismatch"
|
||||||
|
inventory_store = InventoryStore(flake=machine.flake)
|
||||||
|
inventory = inventory_store.read()
|
||||||
|
|
||||||
|
apply_patch(inventory, f"machines.{machine.name}", inventory_machine)
|
||||||
|
inventory_store.write(
|
||||||
|
inventory, message=f"Update information about machine {machine.name}"
|
||||||
)
|
)
|
||||||
|
|
||||||
if test_store := nix_test_store():
|
|
||||||
json_path = test_store.joinpath(*json_path.parts[1:])
|
|
||||||
|
|
||||||
machines_json = json.loads(json_path.read_text())
|
|
||||||
|
|
||||||
machines = []
|
|
||||||
for name, machine_data in machines_json.items():
|
|
||||||
machines.append(
|
|
||||||
Machine(
|
|
||||||
name=name,
|
|
||||||
flake=flake,
|
|
||||||
cached_deployment=machine_data,
|
|
||||||
nix_options=nix_options,
|
|
||||||
)
|
|
||||||
)
|
|
||||||
return machines
|
|
||||||
|
|
||||||
|
|
||||||
def get_selected_machines(
|
|
||||||
flake: Flake, nix_options: list[str], machine_names: list[str]
|
|
||||||
) -> list[Machine]:
|
|
||||||
machines = []
|
|
||||||
for name in machine_names:
|
|
||||||
machines.append(Machine(name=name, flake=flake, nix_options=nix_options))
|
|
||||||
return machines
|
|
||||||
|
|||||||
@@ -1,47 +1,71 @@
|
|||||||
import argparse
|
import argparse
|
||||||
import json
|
|
||||||
import logging
|
import logging
|
||||||
import re
|
import re
|
||||||
import time
|
|
||||||
from dataclasses import dataclass
|
from dataclasses import dataclass
|
||||||
from pathlib import Path
|
|
||||||
from typing import Literal
|
|
||||||
|
|
||||||
from clan_lib.api import API
|
from clan_lib.api import API
|
||||||
from clan_lib.api.disk import MachineDiskMatter
|
from clan_lib.api.disk import MachineDiskMatter
|
||||||
from clan_lib.api.modules import parse_frontmatter
|
from clan_lib.api.modules import parse_frontmatter
|
||||||
from clan_lib.nix_models.inventory import Machine as InventoryMachine
|
from clan_lib.nix_models.inventory import Machine as InventoryMachine
|
||||||
from clan_lib.persist.inventory_store import InventoryStore
|
from clan_lib.persist.inventory_store import InventoryStore
|
||||||
from clan_lib.persist.util import apply_patch
|
|
||||||
|
|
||||||
from clan_cli.cmd import RunOpts, run
|
|
||||||
from clan_cli.completions import add_dynamic_completer, complete_tags
|
from clan_cli.completions import add_dynamic_completer, complete_tags
|
||||||
from clan_cli.dirs import specific_machine_dir
|
from clan_cli.dirs import specific_machine_dir
|
||||||
from clan_cli.errors import ClanError
|
|
||||||
from clan_cli.flake import Flake
|
from clan_cli.flake import Flake
|
||||||
from clan_cli.machines.hardware import HardwareConfig
|
from clan_cli.machines.hardware import HardwareConfig
|
||||||
|
from clan_cli.machines.inventory import get_inv_machine
|
||||||
from clan_cli.machines.machines import Machine
|
from clan_cli.machines.machines import Machine
|
||||||
from clan_cli.nix import nix_eval
|
|
||||||
from clan_cli.tags import list_nixos_machines_by_tags
|
|
||||||
|
|
||||||
log = logging.getLogger(__name__)
|
log = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
@API.register
|
@API.register
|
||||||
def set_machine(flake: Flake, machine_name: str, machine: InventoryMachine) -> None:
|
def list_inv_machines(flake: Flake) -> dict[str, InventoryMachine]:
|
||||||
|
"""
|
||||||
|
List machines in the inventory for the UI.
|
||||||
|
"""
|
||||||
inventory_store = InventoryStore(flake=flake)
|
inventory_store = InventoryStore(flake=flake)
|
||||||
inventory = inventory_store.read()
|
inventory = inventory_store.read()
|
||||||
apply_patch(inventory, f"machines.{machine_name}", machine)
|
|
||||||
inventory_store.write(
|
res = inventory.get("machines", {})
|
||||||
inventory, message=f"Update information about machine {machine_name}"
|
return res
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
@API.register
|
def list_machines(
|
||||||
def list_machines(flake: Flake) -> dict[str, InventoryMachine]:
|
flake: Flake, nix_options: list[str] | None = None
|
||||||
|
) -> dict[str, Machine]:
|
||||||
inventory_store = InventoryStore(flake=flake)
|
inventory_store = InventoryStore(flake=flake)
|
||||||
inventory = inventory_store.read()
|
inventory = inventory_store.read()
|
||||||
return inventory.get("machines", {})
|
res = {}
|
||||||
|
|
||||||
|
if nix_options is None:
|
||||||
|
nix_options = []
|
||||||
|
|
||||||
|
for inv_machine in inventory.get("machines", {}).values():
|
||||||
|
machine = Machine(
|
||||||
|
name=inv_machine["name"],
|
||||||
|
flake=flake,
|
||||||
|
nix_options=nix_options,
|
||||||
|
)
|
||||||
|
res[machine.name] = machine
|
||||||
|
return res
|
||||||
|
|
||||||
|
|
||||||
|
def query_machines_by_tags(flake: Flake, tags: list[str]) -> dict[str, Machine]:
|
||||||
|
"""
|
||||||
|
Query machines by their respective tags, if multiple tags are specified
|
||||||
|
then only machines that have those respective tags specified will be listed.
|
||||||
|
It is an intersection of the tags and machines.
|
||||||
|
"""
|
||||||
|
machines = list_machines(flake)
|
||||||
|
|
||||||
|
filtered_machines = {}
|
||||||
|
for machine in machines.values():
|
||||||
|
inv_machine = get_inv_machine(machine)
|
||||||
|
if all(tag in inv_machine["tags"] for tag in tags):
|
||||||
|
filtered_machines[machine.name] = machine
|
||||||
|
|
||||||
|
return filtered_machines
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
@@ -64,13 +88,7 @@ def extract_header(c: str) -> str:
|
|||||||
|
|
||||||
@API.register
|
@API.register
|
||||||
def get_machine_details(machine: Machine) -> MachineDetails:
|
def get_machine_details(machine: Machine) -> MachineDetails:
|
||||||
inventory_store = InventoryStore(flake=machine.flake)
|
machine_inv = get_inv_machine(machine)
|
||||||
inventory = inventory_store.read()
|
|
||||||
machine_inv = inventory.get("machines", {}).get(machine.name)
|
|
||||||
if machine_inv is None:
|
|
||||||
msg = f"Machine {machine.name} not found in inventory"
|
|
||||||
raise ClanError(msg)
|
|
||||||
|
|
||||||
hw_config = HardwareConfig.detect_type(machine)
|
hw_config = HardwareConfig.detect_type(machine)
|
||||||
|
|
||||||
machine_dir = specific_machine_dir(machine)
|
machine_dir = specific_machine_dir(machine)
|
||||||
@@ -85,73 +103,21 @@ def get_machine_details(machine: Machine) -> MachineDetails:
|
|||||||
disk_schema = data # type: ignore
|
disk_schema = data # type: ignore
|
||||||
|
|
||||||
return MachineDetails(
|
return MachineDetails(
|
||||||
machine=machine_inv, hw_config=hw_config, disk_schema=disk_schema
|
machine=machine_inv,
|
||||||
|
hw_config=hw_config,
|
||||||
|
disk_schema=disk_schema,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def list_nixos_machines(flake_url: str | Path) -> list[str]:
|
|
||||||
cmd = nix_eval(
|
|
||||||
[
|
|
||||||
f"{flake_url}#clanInternals.machines.x86_64-linux",
|
|
||||||
"--apply",
|
|
||||||
"builtins.attrNames",
|
|
||||||
"--json",
|
|
||||||
]
|
|
||||||
)
|
|
||||||
|
|
||||||
proc = run(cmd)
|
|
||||||
|
|
||||||
try:
|
|
||||||
res = proc.stdout.strip()
|
|
||||||
data = json.loads(res)
|
|
||||||
except json.JSONDecodeError as e:
|
|
||||||
msg = f"Error decoding machines from flake: {e}"
|
|
||||||
raise ClanError(msg) from e
|
|
||||||
else:
|
|
||||||
return data
|
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
|
||||||
class ConnectionOptions:
|
|
||||||
timeout: int = 2
|
|
||||||
retries: int = 10
|
|
||||||
|
|
||||||
|
|
||||||
from clan_cli.machines.machines import Machine
|
|
||||||
|
|
||||||
|
|
||||||
@API.register
|
|
||||||
def check_machine_online(
|
|
||||||
machine: Machine, opts: ConnectionOptions | None = None
|
|
||||||
) -> Literal["Online", "Offline"]:
|
|
||||||
hostname = machine.target_host_address
|
|
||||||
if not hostname:
|
|
||||||
msg = f"Machine {machine.name} does not specify a targetHost"
|
|
||||||
raise ClanError(msg)
|
|
||||||
|
|
||||||
timeout = opts.timeout if opts and opts.timeout else 2
|
|
||||||
|
|
||||||
for _ in range(opts.retries if opts and opts.retries else 10):
|
|
||||||
with machine.target_host() as target:
|
|
||||||
res = target.run(
|
|
||||||
["true"],
|
|
||||||
RunOpts(timeout=timeout, check=False, needs_user_terminal=True),
|
|
||||||
)
|
|
||||||
|
|
||||||
if res.returncode == 0:
|
|
||||||
return "Online"
|
|
||||||
time.sleep(timeout)
|
|
||||||
|
|
||||||
return "Offline"
|
|
||||||
|
|
||||||
|
|
||||||
def list_command(args: argparse.Namespace) -> None:
|
def list_command(args: argparse.Namespace) -> None:
|
||||||
flake_path = args.flake.path
|
flake: Flake = args.flake
|
||||||
|
|
||||||
if args.tags:
|
if args.tags:
|
||||||
list_nixos_machines_by_tags(flake_path, args.tags)
|
for name in query_machines_by_tags(flake, args.tags):
|
||||||
return
|
print(name)
|
||||||
for name in list_nixos_machines(flake_path):
|
else:
|
||||||
print(name)
|
for name in list_machines(flake):
|
||||||
|
print(name)
|
||||||
|
|
||||||
|
|
||||||
def register_list_parser(parser: argparse.ArgumentParser) -> None:
|
def register_list_parser(parser: argparse.ArgumentParser) -> None:
|
||||||
|
|||||||
@@ -30,8 +30,8 @@ if TYPE_CHECKING:
|
|||||||
class Machine:
|
class Machine:
|
||||||
name: str
|
name: str
|
||||||
flake: Flake
|
flake: Flake
|
||||||
|
|
||||||
nix_options: list[str] = field(default_factory=list)
|
nix_options: list[str] = field(default_factory=list)
|
||||||
cached_deployment: None | dict[str, Any] = None
|
|
||||||
override_target_host: None | str = None
|
override_target_host: None | str = None
|
||||||
override_build_host: None | str = None
|
override_build_host: None | str = None
|
||||||
private_key: Path | None = None
|
private_key: Path | None = None
|
||||||
|
|||||||
@@ -1,58 +0,0 @@
|
|||||||
import json
|
|
||||||
from pathlib import Path
|
|
||||||
from typing import Any
|
|
||||||
|
|
||||||
from clan_cli.cmd import run
|
|
||||||
from clan_cli.errors import ClanError
|
|
||||||
from clan_cli.nix import nix_eval
|
|
||||||
|
|
||||||
|
|
||||||
def list_tagged_machines(flake_url: str | Path) -> dict[str, Any]:
|
|
||||||
"""
|
|
||||||
Query machines from the inventory with their meta information intact.
|
|
||||||
The meta information includes tags.
|
|
||||||
"""
|
|
||||||
cmd = nix_eval(
|
|
||||||
[
|
|
||||||
f"{flake_url}#clanInternals.inventory.machines",
|
|
||||||
"--json",
|
|
||||||
]
|
|
||||||
)
|
|
||||||
proc = run(cmd)
|
|
||||||
|
|
||||||
try:
|
|
||||||
res = proc.stdout.strip()
|
|
||||||
data = json.loads(res)
|
|
||||||
except json.JSONDecodeError as e:
|
|
||||||
msg = f"Error decoding tagged inventory machines from flake: {e}"
|
|
||||||
raise ClanError(msg) from e
|
|
||||||
else:
|
|
||||||
return data
|
|
||||||
|
|
||||||
|
|
||||||
def query_machines_by_tags(
|
|
||||||
flake_path: str | Path, tags: list[str] | None = None
|
|
||||||
) -> list[str]:
|
|
||||||
"""
|
|
||||||
Query machines by their respective tags, if multiple tags are specified
|
|
||||||
then only machines that have those respective tags specified will be listed.
|
|
||||||
It is an intersection of the tags and machines.
|
|
||||||
"""
|
|
||||||
machines = list_tagged_machines(flake_path)
|
|
||||||
|
|
||||||
if not tags:
|
|
||||||
return list(machines.keys())
|
|
||||||
|
|
||||||
filtered_machines = []
|
|
||||||
for machine_id, machine_values in machines.items():
|
|
||||||
if all(tag in machine_values["tags"] for tag in tags):
|
|
||||||
filtered_machines.append(machine_id)
|
|
||||||
|
|
||||||
return filtered_machines
|
|
||||||
|
|
||||||
|
|
||||||
def list_nixos_machines_by_tags(
|
|
||||||
flake_path: str | Path, tags: list[str] | None = None
|
|
||||||
) -> None:
|
|
||||||
for name in query_machines_by_tags(flake_path, tags):
|
|
||||||
print(name)
|
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
from clan_cli.flake import Flake
|
||||||
|
|
||||||
# Functions to test
|
# Functions to test
|
||||||
from clan_cli.inventory import load_inventory_eval
|
from clan_cli.inventory import load_inventory_eval
|
||||||
@@ -43,7 +44,7 @@ def test_inventory_deserialize_variants(
|
|||||||
Testing different inventory deserializations
|
Testing different inventory deserializations
|
||||||
Inventory should always be deserializable to a dict
|
Inventory should always be deserializable to a dict
|
||||||
"""
|
"""
|
||||||
inventory: dict[str, Any] = load_inventory_eval(test_flake_with_core.path) # type: ignore
|
inventory: dict[str, Any] = load_inventory_eval(Flake(test_flake_with_core.path)) # type: ignore
|
||||||
# Check that the inventory is a dict
|
# Check that the inventory is a dict
|
||||||
assert isinstance(inventory, dict)
|
assert isinstance(inventory, dict)
|
||||||
|
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ from clan_cli.completions import (
|
|||||||
from clan_cli.errors import ClanError
|
from clan_cli.errors import ClanError
|
||||||
from clan_cli.flake import Flake
|
from clan_cli.flake import Flake
|
||||||
from clan_cli.git import commit_files
|
from clan_cli.git import commit_files
|
||||||
from clan_cli.machines.inventory import get_all_machines, get_selected_machines
|
from clan_cli.machines.list import list_machines
|
||||||
from clan_cli.nix import nix_config, nix_shell, nix_test_store
|
from clan_cli.nix import nix_config, nix_shell, nix_test_store
|
||||||
from clan_cli.vars._types import StoreBase
|
from clan_cli.vars._types import StoreBase
|
||||||
from clan_cli.vars.migration import check_can_migrate, migrate_files
|
from clan_cli.vars.migration import check_can_migrate, migrate_files
|
||||||
@@ -481,10 +481,16 @@ def generate_command(args: argparse.Namespace) -> None:
|
|||||||
if args.flake is None:
|
if args.flake is None:
|
||||||
msg = "Could not find clan flake toplevel directory"
|
msg = "Could not find clan flake toplevel directory"
|
||||||
raise ClanError(msg)
|
raise ClanError(msg)
|
||||||
if len(args.machines) == 0:
|
|
||||||
machines = get_all_machines(args.flake, args.option)
|
machines: list[Machine] = list(list_machines(args.flake, args.option).values())
|
||||||
else:
|
|
||||||
machines = get_selected_machines(args.flake, args.option, args.machines)
|
if len(args.machines) > 0:
|
||||||
|
machines = list(
|
||||||
|
filter(
|
||||||
|
lambda m: m.name in args.machines,
|
||||||
|
machines,
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
# prefetch all vars
|
# prefetch all vars
|
||||||
config = nix_config()
|
config = nix_config()
|
||||||
|
|||||||
43
pkgs/clan-cli/clan_lib/api/network.py
Normal file
43
pkgs/clan-cli/clan_lib/api/network.py
Normal file
@@ -0,0 +1,43 @@
|
|||||||
|
import logging
|
||||||
|
import time
|
||||||
|
from dataclasses import dataclass
|
||||||
|
from typing import Literal
|
||||||
|
|
||||||
|
from clan_cli.cmd import RunOpts
|
||||||
|
from clan_cli.errors import ClanError
|
||||||
|
from clan_cli.machines.machines import Machine
|
||||||
|
|
||||||
|
from clan_lib.api import API
|
||||||
|
|
||||||
|
log = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class ConnectionOptions:
|
||||||
|
timeout: int = 2
|
||||||
|
retries: int = 10
|
||||||
|
|
||||||
|
|
||||||
|
@API.register
|
||||||
|
def check_machine_online(
|
||||||
|
machine: Machine, opts: ConnectionOptions | None = None
|
||||||
|
) -> Literal["Online", "Offline"]:
|
||||||
|
hostname = machine.target_host_address
|
||||||
|
if not hostname:
|
||||||
|
msg = f"Machine {machine.name} does not specify a targetHost"
|
||||||
|
raise ClanError(msg)
|
||||||
|
|
||||||
|
timeout = opts.timeout if opts and opts.timeout else 2
|
||||||
|
|
||||||
|
for _ in range(opts.retries if opts and opts.retries else 10):
|
||||||
|
with machine.target_host() as target:
|
||||||
|
res = target.run(
|
||||||
|
["true"],
|
||||||
|
RunOpts(timeout=timeout, check=False, needs_user_terminal=True),
|
||||||
|
)
|
||||||
|
|
||||||
|
if res.returncode == 0:
|
||||||
|
return "Online"
|
||||||
|
time.sleep(timeout)
|
||||||
|
|
||||||
|
return "Offline"
|
||||||
@@ -15,7 +15,6 @@ from clan_cli.flake import Flake
|
|||||||
from clan_cli.inventory import patch_inventory_with
|
from clan_cli.inventory import patch_inventory_with
|
||||||
from clan_cli.machines.create import CreateOptions as ClanCreateOptions
|
from clan_cli.machines.create import CreateOptions as ClanCreateOptions
|
||||||
from clan_cli.machines.create import create_machine
|
from clan_cli.machines.create import create_machine
|
||||||
from clan_cli.machines.list import check_machine_online
|
|
||||||
from clan_cli.machines.machines import Machine
|
from clan_cli.machines.machines import Machine
|
||||||
from clan_cli.nix import nix_command
|
from clan_cli.nix import nix_command
|
||||||
from clan_cli.secrets.key import generate_key
|
from clan_cli.secrets.key import generate_key
|
||||||
@@ -26,6 +25,7 @@ from clan_cli.ssh.host_key import HostKeyCheck
|
|||||||
from clan_cli.vars.generate import generate_vars_for_machine, get_generators_closure
|
from clan_cli.vars.generate import generate_vars_for_machine, get_generators_closure
|
||||||
|
|
||||||
from clan_lib.api.disk import hw_main_disk_options, set_machine_disk_schema
|
from clan_lib.api.disk import hw_main_disk_options, set_machine_disk_schema
|
||||||
|
from clan_lib.api.network import check_machine_online
|
||||||
from clan_lib.nix_models.inventory import Machine as InventoryMachine
|
from clan_lib.nix_models.inventory import Machine as InventoryMachine
|
||||||
from clan_lib.nix_models.inventory import MachineDeploy
|
from clan_lib.nix_models.inventory import MachineDeploy
|
||||||
|
|
||||||
|
|||||||
@@ -8,8 +8,9 @@ from typing import Any
|
|||||||
from clan_cli.clan.inspect import FlakeConfig, inspect_flake
|
from clan_cli.clan.inspect import FlakeConfig, inspect_flake
|
||||||
from clan_cli.dirs import user_history_file
|
from clan_cli.dirs import user_history_file
|
||||||
from clan_cli.errors import ClanError
|
from clan_cli.errors import ClanError
|
||||||
|
from clan_cli.flake import Flake
|
||||||
from clan_cli.locked_open import read_history_file, write_history_file
|
from clan_cli.locked_open import read_history_file, write_history_file
|
||||||
from clan_cli.machines.list import list_nixos_machines
|
from clan_cli.machines.list import list_machines
|
||||||
|
|
||||||
from clan_vm_manager.clan_uri import ClanURI
|
from clan_vm_manager.clan_uri import ClanURI
|
||||||
|
|
||||||
@@ -75,7 +76,7 @@ def new_history_entry(url: str, machine: str) -> HistoryEntry:
|
|||||||
def add_all_to_history(uri: ClanURI) -> list[HistoryEntry]:
|
def add_all_to_history(uri: ClanURI) -> list[HistoryEntry]:
|
||||||
history = list_history()
|
history = list_history()
|
||||||
new_entries: list[HistoryEntry] = []
|
new_entries: list[HistoryEntry] = []
|
||||||
for machine in list_nixos_machines(uri.get_url()):
|
for machine in list_machines(Flake(uri.get_url())):
|
||||||
new_entry = _add_maschine_to_history_list(uri.get_url(), machine, history)
|
new_entry = _add_maschine_to_history_list(uri.get_url(), machine, history)
|
||||||
new_entries.append(new_entry)
|
new_entries.append(new_entry)
|
||||||
write_history_file(history)
|
write_history_file(history)
|
||||||
|
|||||||
Reference in New Issue
Block a user