api: refactor create flake into create clan

This commit is contained in:
Johannes Kirschbauer
2024-06-11 12:10:55 +02:00
parent b72fa7ae3c
commit 81b1487057
8 changed files with 83 additions and 44 deletions

View File

@@ -0,0 +1,20 @@
# !/usr/bin/env python3
import argparse
from clan_cli.clan.inspect import register_inspect_parser
from .create import register_create_parser
# takes a (sub)parser and configures it
def register_parser(parser: argparse.ArgumentParser) -> None:
subparser = parser.add_subparsers(
title="command",
description="the command to run",
help="the command to run",
required=True,
)
create_parser = subparser.add_parser("create", help="Create a clan")
register_create_parser(create_parser)
inspect_parser = subparser.add_parser("inspect", help="Inspect a clan ")
register_inspect_parser(inspect_parser)

View File

@@ -0,0 +1,134 @@
# !/usr/bin/env python3
import argparse
import json
from dataclasses import dataclass, fields
from pathlib import Path
from clan_cli.api import API
from clan_cli.arg_actions import AppendOptionAction
from ..cmd import CmdOut, run
from ..errors import ClanError
from ..nix import nix_command, nix_shell
default_template_url: str = "git+https://git.clan.lol/clan/clan-core"
minimal_template_url: str = "git+https://git.clan.lol/clan/clan-core#templates.minimal"
@dataclass
class CreateClanResponse:
git_init: CmdOut
git_add: CmdOut
git_config: CmdOut
flake_update: CmdOut
@dataclass
class ClanMetaInfo:
name: str
description: str | None
icon: str | None
@dataclass
class CreateOptions:
directory: Path
# Metadata for the clan
# Metadata can be shown with `clan show`
meta: ClanMetaInfo | None = None
# URL to the template to use. Defaults to the "minimal" template
template_url: str = minimal_template_url
@API.register
def create_clan(options: CreateOptions) -> CreateClanResponse:
directory = options.directory
template_url = options.template_url
if not directory.exists():
directory.mkdir()
else:
raise ClanError(
location=f"{directory.resolve()}",
msg="Cannot create clan",
description="Directory already exists",
)
cmd_responses = {}
command = nix_command(
[
"flake",
"init",
"-t",
template_url,
]
)
out = run(command, cwd=directory)
# Write meta.json file if meta is provided
if options.meta is not None:
meta_file = Path(directory / "clan/meta.json")
meta_file.parent.mkdir(parents=True, exist_ok=True)
with open(meta_file, "w") as f:
json.dump(options.meta.__dict__, f)
command = nix_shell(["nixpkgs#git"], ["git", "init"])
out = run(command, cwd=directory)
cmd_responses["git init"] = out
command = nix_shell(["nixpkgs#git"], ["git", "add", "."])
out = run(command, cwd=directory)
cmd_responses["git add"] = out
command = nix_shell(["nixpkgs#git"], ["git", "config", "user.name", "clan-tool"])
out = run(command, cwd=directory)
cmd_responses["git config"] = out
command = nix_shell(
["nixpkgs#git"], ["git", "config", "user.email", "clan@example.com"]
)
out = run(command, cwd=directory)
cmd_responses["git config"] = out
command = ["nix", "flake", "update"]
out = run(command, cwd=directory)
cmd_responses["flake update"] = out
response = CreateClanResponse(
git_init=cmd_responses["git init"],
git_add=cmd_responses["git add"],
git_config=cmd_responses["git config"],
flake_update=cmd_responses["flake update"],
)
return response
def register_create_parser(parser: argparse.ArgumentParser) -> None:
parser.add_argument(
"--url",
type=str,
help="url to the clan template",
default=default_template_url,
)
parser.add_argument(
"--meta",
help=f"""Metadata to set for the clan. Available options are: {", ".join([f.name for f in fields(ClanMetaInfo)]) }""",
nargs=2,
metavar=("name", "value"),
action=AppendOptionAction,
default=[],
)
parser.add_argument(
"path", type=Path, help="Path to the clan directory", default=Path(".")
)
def create_flake_command(args: argparse.Namespace) -> None:
create_clan(
CreateOptions(
directory=args.path,
template_url=args.url,
)
)
parser.set_defaults(func=create_flake_command)

View File

@@ -0,0 +1,125 @@
import argparse
from dataclasses import dataclass
from pathlib import Path
from ..cmd import run
from ..dirs import machine_gcroot
from ..errors import ClanError
from ..machines.list import list_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
@dataclass
class FlakeConfig:
flake_url: str | Path
flake_attr: str
clan_name: str
nar_hash: str
icon: str | None
description: str | None
last_updated: str
revision: str | None
vm: VmConfig
def __post_init__(self) -> None:
if isinstance(self.vm, dict):
self.vm = VmConfig(**self.vm)
def run_cmd(cmd: list[str]) -> str:
proc = run(cmd)
return proc.stdout.strip()
def inspect_flake(flake_url: str | Path, machine_name: str) -> FlakeConfig:
config = nix_config()
system = config["system"]
# Check if the machine exists
machines = list_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)}"
)
machine = Machine(machine_name, flake_url)
vm = inspect_vm(machine)
# Make symlink to gcroots from vm.machine_icon
if vm.machine_icon:
gcroot_icon: Path = machine_gcroot(flake_url=str(flake_url)) / vm.machine_name
nix_add_to_gcroots(vm.machine_icon, gcroot_icon)
# Get the Clan name
cmd = nix_eval(
[
f'{flake_url}#clanInternals.machines."{system}"."{machine_name}".config.clanCore.clanName'
]
)
res = run_cmd(cmd)
clan_name = res.strip('"')
# Get the clan icon path
cmd = nix_eval(
[
f'{flake_url}#clanInternals.machines."{system}"."{machine_name}".config.clanCore.clanIcon'
]
)
res = run_cmd(cmd)
# If the icon is null, no icon is set for this Clan
if res == "null":
icon_path = None
else:
icon_path = res.strip('"')
cmd = nix_build(
[
f'{flake_url}#clanInternals.machines."{system}"."{machine_name}".config.clanCore.clanIcon'
],
machine_gcroot(flake_url=str(flake_url)) / "clanIcon",
)
run_cmd(cmd)
# Get the flake metadata
meta = nix_metadata(flake_url)
return FlakeConfig(
vm=vm,
flake_url=flake_url,
clan_name=clan_name,
flake_attr=machine_name,
nar_hash=meta["locked"]["narHash"],
icon=icon_path,
description=meta.get("description"),
last_updated=meta["lastModified"],
revision=meta.get("revision"),
)
@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_flake(
flake_url=inspect_options.flake, machine_name=inspect_options.machine
)
print("Clan name:", res.clan_name)
print("Icon:", res.icon)
print("Description:", res.description)
print("Last updated:", res.last_updated)
print("Revision:", res.revision)
def register_inspect_parser(parser: argparse.ArgumentParser) -> None:
parser.add_argument("--machine", type=str, default="defaultVM")
parser.set_defaults(func=inspect_command)

View File

@@ -0,0 +1,57 @@
import argparse
import json
import logging
from pathlib import Path
from clan_cli.api import API
from clan_cli.clan.create import ClanMetaInfo
from clan_cli.errors import ClanCmdError, ClanError
from ..cmd import run_no_stdout
from ..nix import nix_eval
log = logging.getLogger(__name__)
@API.register
def show_clan_meta(uri: str | Path) -> ClanMetaInfo:
cmd = nix_eval(
[
f"{uri}#clanInternals.meta",
"--json",
]
)
res = "{}"
try:
proc = run_no_stdout(cmd)
res = proc.stdout.strip()
except ClanCmdError:
raise ClanError(
"Clan might not have meta attributes",
location=f"show_clan {uri}",
description="Evaluation failed on clanInternals.meta attribute",
)
clan_meta = json.loads(res)
return ClanMetaInfo(
name=clan_meta.get("name"),
description=clan_meta.get("description", None),
icon=clan_meta.get("icon", None),
)
def show_command(args: argparse.Namespace) -> None:
flake_path = Path(args.flake).resolve()
meta = show_clan_meta(flake_path)
print(f"Name: {meta.name}")
print(f"Description: {meta.description or ''}")
print(f"Icon: {meta.icon or ''}")
def register_parser(parser: argparse.ArgumentParser) -> None:
parser.set_defaults(func=show_command)
parser.add_argument(
"show",
help="Show",
)