Files
clan-core/pkgs/clan-cli/clan_cli/vms/inspect.py
DavHau 4bdf769075 VMs: persist state folders on host
Done:
- move vm inspect attrs from system.clan.vm.config to clanCore.vm.inspect. This gives us proper name and type checking. everything in `system` is basically freeform, so the previous option definitions were never enforced
- when running VMs, mount state directory from ~/.config/clan/vmstate/{...} from the host to /var/vmstate inside the vm
- create bind mount inside the VM from /var/vmstate/{folder} to / for all folders defined in clanCore.state.<name>.folders

TODOs:
- make sure directories in ~/.config/clan/vmstate never collide (include hash of clan-url, etc.)
- port impure test to python
2024-01-08 18:38:07 +07:00

60 lines
1.3 KiB
Python

import argparse
import json
from dataclasses import dataclass
from pathlib import Path
from ..cmd import run
from ..nix import nix_config, nix_eval
@dataclass
class VmConfig:
clan_name: str
flake_url: str | Path
flake_attr: str
cores: int
memory_size: int
graphics: bool
wayland: bool = False
def inspect_vm(flake_url: str | Path, flake_attr: str) -> VmConfig:
config = nix_config()
system = config["system"]
cmd = nix_eval(
[
f'{flake_url}#clanInternals.machines."{system}"."{flake_attr}".config.clanCore.vm.inspect'
]
)
proc = run(cmd)
data = json.loads(proc.stdout)
return VmConfig(flake_url=flake_url, flake_attr=flake_attr, **data)
@dataclass
class InspectOptions:
machine: str
flake: Path
def inspect_command(args: argparse.Namespace) -> None:
inspect_options = InspectOptions(
machine=args.machine,
flake=args.flake or Path.cwd(),
)
res = inspect_vm(
flake_url=inspect_options.flake, flake_attr=inspect_options.machine
)
print("Cores:", res.cores)
print("Memory size:", res.memory_size)
print("Graphics:", res.graphics)
def register_inspect_parser(parser: argparse.ArgumentParser) -> None:
parser.add_argument("machine", type=str, default="defaultVM")
parser.set_defaults(func=inspect_command)