API: type all services with dict[str,Any] in python to reduce complexity.
This commit is contained in:
@@ -1,62 +1,37 @@
|
|||||||
from clan_cli.errors import ClanError
|
# @API.register
|
||||||
from clan_cli.inventory import (
|
# def set_admin_service(
|
||||||
AdminConfig,
|
# base_url: str,
|
||||||
ServiceAdmin,
|
# allowed_keys: dict[str, str],
|
||||||
ServiceAdminRole,
|
# instance_name: str = "admin",
|
||||||
ServiceAdminRoleDefault,
|
# extra_machines: list[str] | None = None,
|
||||||
ServiceMeta,
|
# ) -> None:
|
||||||
load_inventory_eval,
|
# """
|
||||||
save_inventory,
|
# Set the admin service of a clan
|
||||||
)
|
# Every machine is by default part of the admin service via the 'all' tag
|
||||||
|
# """
|
||||||
|
# if extra_machines is None:
|
||||||
|
# extra_machines = []
|
||||||
|
# inventory = load_inventory_eval(base_url)
|
||||||
|
|
||||||
from . import API
|
# if not allowed_keys:
|
||||||
|
# msg = "At least one key must be provided to ensure access"
|
||||||
|
# raise ClanError(msg)
|
||||||
|
|
||||||
|
# instance = ServiceAdmin(
|
||||||
|
# meta=ServiceMeta(name=instance_name),
|
||||||
|
# roles=ServiceAdminRole(
|
||||||
|
# default=ServiceAdminRoleDefault(
|
||||||
|
# machines=extra_machines,
|
||||||
|
# tags=["all"],
|
||||||
|
# )
|
||||||
|
# ),
|
||||||
|
# config=AdminConfig(allowedKeys=allowed_keys),
|
||||||
|
# )
|
||||||
|
|
||||||
@API.register
|
# inventory.services.admin[instance_name] = instance
|
||||||
def get_admin_service(base_url: str) -> ServiceAdmin | None:
|
|
||||||
"""
|
|
||||||
Return the admin service of a clan.
|
|
||||||
|
|
||||||
There is only one admin service. This might be changed in the future
|
# save_inventory(
|
||||||
"""
|
# inventory,
|
||||||
inventory = load_inventory_eval(base_url)
|
# base_url,
|
||||||
return inventory.services.admin.get("admin")
|
# f"Set admin service: '{instance_name}'",
|
||||||
|
# )
|
||||||
|
|
||||||
@API.register
|
|
||||||
def set_admin_service(
|
|
||||||
base_url: str,
|
|
||||||
allowed_keys: dict[str, str],
|
|
||||||
instance_name: str = "admin",
|
|
||||||
extra_machines: list[str] | None = None,
|
|
||||||
) -> None:
|
|
||||||
"""
|
|
||||||
Set the admin service of a clan
|
|
||||||
Every machine is by default part of the admin service via the 'all' tag
|
|
||||||
"""
|
|
||||||
if extra_machines is None:
|
|
||||||
extra_machines = []
|
|
||||||
inventory = load_inventory_eval(base_url)
|
|
||||||
|
|
||||||
if not allowed_keys:
|
|
||||||
msg = "At least one key must be provided to ensure access"
|
|
||||||
raise ClanError(msg)
|
|
||||||
|
|
||||||
instance = ServiceAdmin(
|
|
||||||
meta=ServiceMeta(name=instance_name),
|
|
||||||
roles=ServiceAdminRole(
|
|
||||||
default=ServiceAdminRoleDefault(
|
|
||||||
machines=extra_machines,
|
|
||||||
tags=["all"],
|
|
||||||
)
|
|
||||||
),
|
|
||||||
config=AdminConfig(allowedKeys=allowed_keys),
|
|
||||||
)
|
|
||||||
|
|
||||||
inventory.services.admin[instance_name] = instance
|
|
||||||
|
|
||||||
save_inventory(
|
|
||||||
inventory,
|
|
||||||
base_url,
|
|
||||||
f"Set admin service: '{instance_name}'",
|
|
||||||
)
|
|
||||||
|
|||||||
@@ -1,67 +1,34 @@
|
|||||||
from clan_cli.inventory import (
|
|
||||||
ServiceMeta,
|
|
||||||
ServiceSingleDisk,
|
|
||||||
ServiceSingleDiskRole,
|
|
||||||
ServiceSingleDiskRoleDefault,
|
|
||||||
SingleDiskConfig,
|
|
||||||
load_inventory_eval,
|
|
||||||
load_inventory_json,
|
|
||||||
save_inventory,
|
|
||||||
)
|
|
||||||
|
|
||||||
from . import API
|
|
||||||
|
|
||||||
|
|
||||||
def get_instance_name(machine_name: str) -> str:
|
def get_instance_name(machine_name: str) -> str:
|
||||||
return f"{machine_name}-single-disk"
|
return f"{machine_name}-single-disk"
|
||||||
|
|
||||||
|
|
||||||
@API.register
|
# @API.register
|
||||||
def set_single_disk_uuid(
|
# def set_single_disk_uuid(
|
||||||
base_path: str,
|
# base_path: str,
|
||||||
machine_name: str,
|
# machine_name: str,
|
||||||
disk_uuid: str,
|
# disk_uuid: str,
|
||||||
) -> None:
|
# ) -> None:
|
||||||
"""
|
# """
|
||||||
Set the disk UUID of single disk machine
|
# Set the disk UUID of single disk machine
|
||||||
"""
|
# """
|
||||||
inventory = load_inventory_json(base_path)
|
# inventory = load_inventory_json(base_path)
|
||||||
|
|
||||||
instance_name = get_instance_name(machine_name)
|
# instance_name = get_instance_name(machine_name)
|
||||||
|
|
||||||
single_disk_config: ServiceSingleDisk = ServiceSingleDisk(
|
# single_disk_config: ServiceSingleDisk = ServiceSingleDisk(
|
||||||
meta=ServiceMeta(name=instance_name),
|
# meta=ServiceMeta(name=instance_name),
|
||||||
roles=ServiceSingleDiskRole(
|
# roles=ServiceSingleDiskRole(
|
||||||
default=ServiceSingleDiskRoleDefault(
|
# default=ServiceSingleDiskRoleDefault(
|
||||||
config=SingleDiskConfig(device=f"/dev/disk/by-id/{disk_uuid}"),
|
# config=SingleDiskConfig(device=f"/dev/disk/by-id/{disk_uuid}"),
|
||||||
machines=[machine_name],
|
# machines=[machine_name],
|
||||||
)
|
# )
|
||||||
),
|
# ),
|
||||||
)
|
# )
|
||||||
|
|
||||||
inventory.services.single_disk[instance_name] = single_disk_config
|
# inventory.services.single_disk[instance_name] = single_disk_config
|
||||||
|
|
||||||
save_inventory(
|
# save_inventory(
|
||||||
inventory,
|
# inventory,
|
||||||
base_path,
|
# base_path,
|
||||||
f"Set disk UUID: '{disk_uuid}' on machine: '{machine_name}'",
|
# f"Set disk UUID: '{disk_uuid}' on machine: '{machine_name}'",
|
||||||
)
|
# )
|
||||||
|
|
||||||
|
|
||||||
@API.register
|
|
||||||
def get_single_disk_uuid(
|
|
||||||
base_path: str,
|
|
||||||
machine_name: str,
|
|
||||||
) -> str | None:
|
|
||||||
"""
|
|
||||||
Get the disk UUID of single disk machine
|
|
||||||
"""
|
|
||||||
inventory = load_inventory_eval(base_path)
|
|
||||||
|
|
||||||
instance_name = get_instance_name(machine_name)
|
|
||||||
|
|
||||||
single_disk_config: ServiceSingleDisk = inventory.services.single_disk[
|
|
||||||
instance_name
|
|
||||||
]
|
|
||||||
|
|
||||||
return single_disk_config.roles.default.config.device
|
|
||||||
|
|||||||
@@ -1,109 +1,67 @@
|
|||||||
from dataclasses import dataclass
|
from dataclasses import dataclass
|
||||||
from pathlib import Path
|
|
||||||
|
|
||||||
from clan_cli.clan_uri import FlakeId
|
|
||||||
from clan_cli.errors import ClanError
|
|
||||||
from clan_cli.facts.generate import generate_facts
|
|
||||||
from clan_cli.inventory import (
|
|
||||||
IwdConfig,
|
|
||||||
IwdConfigNetwork,
|
|
||||||
ServiceIwd,
|
|
||||||
ServiceIwdRole,
|
|
||||||
ServiceIwdRoleDefault,
|
|
||||||
ServiceMeta,
|
|
||||||
load_inventory_eval,
|
|
||||||
save_inventory,
|
|
||||||
)
|
|
||||||
from clan_cli.machines.machines import Machine
|
|
||||||
from clan_cli.secrets.sops import (
|
|
||||||
maybe_get_public_key,
|
|
||||||
maybe_get_user_or_machine,
|
|
||||||
)
|
|
||||||
|
|
||||||
from . import API
|
|
||||||
|
|
||||||
|
|
||||||
def instance_name(machine_name: str) -> str:
|
def instance_name(machine_name: str) -> str:
|
||||||
return f"{machine_name}_wifi_0_"
|
return f"{machine_name}_wifi_0_"
|
||||||
|
|
||||||
|
|
||||||
@API.register
|
|
||||||
def get_iwd_service(base_url: str, machine_name: str) -> ServiceIwd:
|
|
||||||
"""
|
|
||||||
Return the admin service of a clan.
|
|
||||||
|
|
||||||
There is only one admin service. This might be changed in the future
|
|
||||||
"""
|
|
||||||
inventory = load_inventory_eval(base_url)
|
|
||||||
service_config = inventory.services.iwd.get(instance_name(machine_name))
|
|
||||||
if service_config:
|
|
||||||
return service_config
|
|
||||||
|
|
||||||
# Empty service
|
|
||||||
return ServiceIwd(
|
|
||||||
meta=ServiceMeta(name="wifi_0"),
|
|
||||||
roles=ServiceIwdRole(default=ServiceIwdRoleDefault(machines=[machine_name])),
|
|
||||||
config=IwdConfig(networks={}),
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
class NetworkConfig:
|
class NetworkConfig:
|
||||||
ssid: str
|
ssid: str
|
||||||
password: str
|
password: str
|
||||||
|
|
||||||
|
|
||||||
@API.register
|
# @API.register
|
||||||
def set_iwd_service_for_machine(
|
# def set_iwd_service_for_machine(
|
||||||
base_url: str, machine_name: str, networks: dict[str, NetworkConfig]
|
# base_url: str, machine_name: str, networks: dict[str, NetworkConfig]
|
||||||
) -> None:
|
# ) -> None:
|
||||||
"""
|
# """
|
||||||
Set the admin service of a clan
|
# Set the admin service of a clan
|
||||||
Every machine is by default part of the admin service via the 'all' tag
|
# Every machine is by default part of the admin service via the 'all' tag
|
||||||
"""
|
# """
|
||||||
_instance_name = instance_name(machine_name)
|
# _instance_name = instance_name(machine_name)
|
||||||
|
|
||||||
inventory = load_inventory_eval(base_url)
|
# inventory = load_inventory_eval(base_url)
|
||||||
|
|
||||||
instance = ServiceIwd(
|
# instance = ServiceIwd(
|
||||||
meta=ServiceMeta(name="wifi_0"),
|
# meta=ServiceMeta(name="wifi_0"),
|
||||||
roles=ServiceIwdRole(
|
# roles=ServiceIwdRole(
|
||||||
default=ServiceIwdRoleDefault(
|
# default=ServiceIwdRoleDefault(
|
||||||
machines=[machine_name],
|
# machines=[machine_name],
|
||||||
)
|
# )
|
||||||
),
|
# ),
|
||||||
config=IwdConfig(
|
# config=IwdConfig(
|
||||||
networks={k: IwdConfigNetwork(v.ssid) for k, v in networks.items()}
|
# networks={k: IwdConfigNetwork(v.ssid) for k, v in networks.items()}
|
||||||
),
|
# ),
|
||||||
)
|
# )
|
||||||
|
|
||||||
inventory.services.iwd[_instance_name] = instance
|
# inventory.services.iwd[_instance_name] = instance
|
||||||
|
|
||||||
save_inventory(
|
# save_inventory(
|
||||||
inventory,
|
# inventory,
|
||||||
base_url,
|
# base_url,
|
||||||
f"Set iwd service: '{_instance_name}'",
|
# f"Set iwd service: '{_instance_name}'",
|
||||||
)
|
# )
|
||||||
|
|
||||||
pubkey = maybe_get_public_key()
|
# pubkey = maybe_get_public_key()
|
||||||
if not pubkey:
|
# if not pubkey:
|
||||||
# TODO: do this automatically
|
# # TODO: do this automatically
|
||||||
# pubkey = generate_key()
|
# # pubkey = generate_key()
|
||||||
raise ClanError(msg="No public key found. Please initialize your key.")
|
# raise ClanError(msg="No public key found. Please initialize your key.")
|
||||||
|
|
||||||
registered_key = maybe_get_user_or_machine(Path(base_url), pubkey)
|
# registered_key = maybe_get_user_or_machine(Path(base_url), pubkey)
|
||||||
if not registered_key:
|
# if not registered_key:
|
||||||
# TODO: do this automatically
|
# # TODO: do this automatically
|
||||||
# username = os.getlogin()
|
# # username = os.getlogin()
|
||||||
# add_user(Path(base_url), username, pubkey, force=False)
|
# # add_user(Path(base_url), username, pubkey, force=False)
|
||||||
raise ClanError(msg="Your public key is not registered for use with this clan.")
|
# raise ClanError(msg="Your public key is not registered for use with this clan.")
|
||||||
|
|
||||||
password_dict = {f"iwd.{net.ssid}": net.password for net in networks.values()}
|
# password_dict = {f"iwd.{net.ssid}": net.password for net in networks.values()}
|
||||||
for net in networks.values():
|
# for net in networks.values():
|
||||||
generate_facts(
|
# generate_facts(
|
||||||
service=f"iwd.{net.ssid}",
|
# service=f"iwd.{net.ssid}",
|
||||||
machines=[Machine(machine_name, FlakeId(base_url))],
|
# machines=[Machine(machine_name, FlakeId(base_url))],
|
||||||
regenerate=True,
|
# regenerate=True,
|
||||||
# Just returns the password
|
# # Just returns the password
|
||||||
prompt=lambda service, _msg: password_dict[service],
|
# prompt=lambda service, _msg: password_dict[service],
|
||||||
)
|
# )
|
||||||
|
|||||||
@@ -23,35 +23,13 @@ from clan_cli.git import commit_file
|
|||||||
from clan_cli.nix import nix_eval
|
from clan_cli.nix import nix_eval
|
||||||
|
|
||||||
from .classes import (
|
from .classes import (
|
||||||
AdminConfig,
|
|
||||||
Inventory,
|
Inventory,
|
||||||
IwdConfig,
|
|
||||||
IwdConfigNetwork,
|
|
||||||
# Machine classes
|
# Machine classes
|
||||||
Machine,
|
Machine,
|
||||||
MachineDeploy,
|
MachineDeploy,
|
||||||
# General classes
|
# General classes
|
||||||
Meta,
|
Meta,
|
||||||
Service,
|
Service,
|
||||||
# Admin service
|
|
||||||
ServiceAdmin,
|
|
||||||
ServiceAdminRole,
|
|
||||||
ServiceAdminRoleDefault,
|
|
||||||
# Borgbackup service
|
|
||||||
ServiceBorgbackup,
|
|
||||||
ServiceBorgbackupRole,
|
|
||||||
ServiceBorgbackupRoleClient,
|
|
||||||
ServiceBorgbackupRoleServer,
|
|
||||||
# IWD
|
|
||||||
ServiceIwd,
|
|
||||||
ServiceIwdRole,
|
|
||||||
ServiceIwdRoleDefault,
|
|
||||||
ServiceMeta,
|
|
||||||
# Single Disk service
|
|
||||||
ServiceSingleDisk,
|
|
||||||
ServiceSingleDiskRole,
|
|
||||||
ServiceSingleDiskRoleDefault,
|
|
||||||
SingleDiskConfig,
|
|
||||||
)
|
)
|
||||||
|
|
||||||
# Re export classes here
|
# Re export classes here
|
||||||
@@ -64,27 +42,6 @@ __all__ = [
|
|||||||
"Meta",
|
"Meta",
|
||||||
"Inventory",
|
"Inventory",
|
||||||
"MachineDeploy",
|
"MachineDeploy",
|
||||||
"ServiceBorgbackup",
|
|
||||||
"ServiceMeta",
|
|
||||||
"ServiceBorgbackupRole",
|
|
||||||
"ServiceBorgbackupRoleClient",
|
|
||||||
"ServiceBorgbackupRoleServer",
|
|
||||||
# Single Disk service
|
|
||||||
"ServiceSingleDisk",
|
|
||||||
"ServiceSingleDiskRole",
|
|
||||||
"ServiceSingleDiskRoleDefault",
|
|
||||||
"SingleDiskConfig",
|
|
||||||
# Admin service
|
|
||||||
"ServiceAdmin",
|
|
||||||
"ServiceAdminRole",
|
|
||||||
"ServiceAdminRoleDefault",
|
|
||||||
"AdminConfig",
|
|
||||||
# IWD service,
|
|
||||||
"ServiceIwd",
|
|
||||||
"ServiceIwdRole",
|
|
||||||
"ServiceIwdRoleDefault",
|
|
||||||
"IwdConfig",
|
|
||||||
"IwdConfigNetwork",
|
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -30,10 +30,7 @@ class Meta:
|
|||||||
icon: None | str = field(default = None)
|
icon: None | str = field(default = None)
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
Service = dict[str, Any]
|
||||||
class Service:
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
class Inventory:
|
class Inventory:
|
||||||
|
|||||||
@@ -7,18 +7,6 @@ import pytest
|
|||||||
# Functions to test
|
# Functions to test
|
||||||
from clan_cli.api import dataclass_to_dict, from_dict
|
from clan_cli.api import dataclass_to_dict, from_dict
|
||||||
from clan_cli.errors import ClanError
|
from clan_cli.errors import ClanError
|
||||||
from clan_cli.inventory import (
|
|
||||||
Inventory,
|
|
||||||
Machine,
|
|
||||||
MachineDeploy,
|
|
||||||
Meta,
|
|
||||||
Service,
|
|
||||||
ServiceBorgbackup,
|
|
||||||
ServiceBorgbackupRole,
|
|
||||||
ServiceBorgbackupRoleClient,
|
|
||||||
ServiceBorgbackupRoleServer,
|
|
||||||
ServiceMeta,
|
|
||||||
)
|
|
||||||
from clan_cli.machines import machines
|
from clan_cli.machines import machines
|
||||||
|
|
||||||
|
|
||||||
@@ -172,43 +160,6 @@ def test_list() -> None:
|
|||||||
assert result == [Name("John"), Name("Sarah")]
|
assert result == [Name("John"), Name("Sarah")]
|
||||||
|
|
||||||
|
|
||||||
def test_deserialize_extensive_inventory() -> None:
|
|
||||||
# TODO: Make this an abstract test, so it doesn't break the test if the inventory changes
|
|
||||||
data = {
|
|
||||||
"meta": {"name": "superclan", "description": "nice clan"},
|
|
||||||
"services": {
|
|
||||||
"borgbackup": {
|
|
||||||
"instance1": {
|
|
||||||
"meta": {
|
|
||||||
"name": "borg1",
|
|
||||||
},
|
|
||||||
"roles": {
|
|
||||||
"client": {},
|
|
||||||
"server": {},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
},
|
|
||||||
},
|
|
||||||
"machines": {"foo": {"name": "foo", "deploy": {}}},
|
|
||||||
}
|
|
||||||
expected = Inventory(
|
|
||||||
meta=Meta(name="superclan", description="nice clan"),
|
|
||||||
services=Service(
|
|
||||||
borgbackup={
|
|
||||||
"instance1": ServiceBorgbackup(
|
|
||||||
meta=ServiceMeta(name="borg1"),
|
|
||||||
roles=ServiceBorgbackupRole(
|
|
||||||
client=ServiceBorgbackupRoleClient(),
|
|
||||||
server=ServiceBorgbackupRoleServer(),
|
|
||||||
),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
),
|
|
||||||
machines={"foo": Machine(deploy=MachineDeploy(), name="foo")},
|
|
||||||
)
|
|
||||||
assert from_dict(Inventory, data) == expected
|
|
||||||
|
|
||||||
|
|
||||||
def test_alias_field() -> None:
|
def test_alias_field() -> None:
|
||||||
@dataclass
|
@dataclass
|
||||||
class Person:
|
class Person:
|
||||||
|
|||||||
@@ -7,11 +7,6 @@ from clan_cli.clan_uri import FlakeId
|
|||||||
from clan_cli.inventory import (
|
from clan_cli.inventory import (
|
||||||
Machine,
|
Machine,
|
||||||
MachineDeploy,
|
MachineDeploy,
|
||||||
ServiceBorgbackup,
|
|
||||||
ServiceBorgbackupRole,
|
|
||||||
ServiceBorgbackupRoleClient,
|
|
||||||
ServiceBorgbackupRoleServer,
|
|
||||||
ServiceMeta,
|
|
||||||
load_inventory_json,
|
load_inventory_json,
|
||||||
save_inventory,
|
save_inventory,
|
||||||
)
|
)
|
||||||
@@ -67,18 +62,16 @@ def test_add_module_to_inventory(
|
|||||||
|
|
||||||
inventory = load_inventory_json(base_path)
|
inventory = load_inventory_json(base_path)
|
||||||
|
|
||||||
inventory.services.borgbackup = {
|
inventory.services = {
|
||||||
"borg1": ServiceBorgbackup(
|
"borgbackup": {
|
||||||
meta=ServiceMeta(name="borg1"),
|
"borg1": {
|
||||||
roles=ServiceBorgbackupRole(
|
"meta": {"name": "borg1"},
|
||||||
client=ServiceBorgbackupRoleClient(
|
"roles": {
|
||||||
machines=["machine1"],
|
"client": {"machines": ["machine1"]},
|
||||||
),
|
"server": {"machines": ["machine1"]},
|
||||||
server=ServiceBorgbackupRoleServer(
|
},
|
||||||
machines=["machine1"],
|
}
|
||||||
),
|
}
|
||||||
),
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
save_inventory(inventory, base_path, "Add borgbackup service")
|
save_inventory(inventory, base_path, "Add borgbackup service")
|
||||||
|
|||||||
@@ -201,7 +201,7 @@ def generate_dataclass(schema: dict[str, Any], class_name: str = root_class) ->
|
|||||||
nested_classes: list[str] = []
|
nested_classes: list[str] = []
|
||||||
if stop_at and class_name == stop_at:
|
if stop_at and class_name == stop_at:
|
||||||
# Skip generating classes below the stop_at property
|
# Skip generating classes below the stop_at property
|
||||||
return f"@dataclass\nclass {class_name}:\n pass\n"
|
return f"{class_name} = dict[str, Any]"
|
||||||
|
|
||||||
for prop, prop_info in properties.items():
|
for prop, prop_info in properties.items():
|
||||||
field_name = prop.replace("-", "_")
|
field_name = prop.replace("-", "_")
|
||||||
|
|||||||
Reference in New Issue
Block a user