CLI: init hw-generate command
All checks were successful
buildbot/nix-build .#checks.x86_64-linux.clan-app-no-breakpoints Build done.
buildbot/nix-build .#checks.x86_64-linux.clan-dep-fakeroot Build done.
buildbot/nix-build .#checks.x86_64-linux.clan-dep-bash Build done.
buildbot/nix-build .#checks.x86_64-linux.clan-dep-e2fsprogs Build done.
buildbot/nix-build .#checks.x86_64-linux.clan-dep-age Build done.
buildbot/nix-build .#checks.x86_64-linux.check-for-breakpoints Build done.
buildbot/nix-build .#checks.x86_64-linux.clan-app-pytest Build done.
buildbot/nix-build .#checks.x86_64-linux.nixos-test-backup Build done.
buildbot/nix-build .#checks.x86_64-linux.package-gui-installer-apk Build done.
buildbot/nix-build .#checks.x86_64-linux.package-gui-installer-archlinux Build done.
buildbot/nix-build .#checks.x86_64-linux.borgbackup Build done.
buildbot/nix-build .#checks.aarch64-darwin.nixos-flash-installer Build done.
buildbot/nix-build .#checks.aarch64-darwin.nixos-iso-installer Build done.
buildbot/nix-build .#checks.x86_64-linux.renderClanOptions Build done.
buildbot/nix-build .#checks.aarch64-linux.nixos-iso-installer Build done.
buildbot/nix-build .#checks.x86_64-linux.clan-dep-nix Build done.
buildbot/nix-build .#checks.x86_64-linux."clan-dep-python3.11-qemu" Build done.
buildbot/nix-build .#checks.x86_64-linux.clan-dep-rsync Build done.
buildbot/nix-build .#checks.x86_64-linux.clan-dep-sops Build done.
buildbot/nix-build .#checks.x86_64-linux.clan-dep-sshpass Build done.
buildbot/nix-build .#checks.x86_64-linux."clan-dep-python3.11-mypy" Build done.
buildbot/nix-build .#checks.x86_64-linux.clan-dep-tor Build done.
buildbot/nix-build .#checks.aarch64-darwin.nixos-test-backup Build done.
buildbot/nix-build .#checks.x86_64-linux.devShell-docs Build done.
buildbot/nix-build .#checks.x86_64-linux.package-gui-installer-deb Build done.
buildbot/nix-build .#checks.aarch64-darwin.nixos-test_install_machine Build done.
buildbot/nix-build .#checks.x86_64-linux.package-gui-installer-rpm Build done.
buildbot/nix-build .#checks.x86_64-linux.package-module-docs Build done.
buildbot/nix-build .#checks.aarch64-linux.nixos-test_install_machine Build done.
buildbot/nix-build .#checks.x86_64-linux.clan-dep-git Build done.
buildbot/nix-build .#checks.aarch64-linux.nixos-test-backup Build done.
buildbot/nix-build .#checks.x86_64-linux.devShell-default Build done.
buildbot/nix-build .#checks.x86_64-linux.container Build done.
buildbot/nix-build .#checks.x86_64-linux.package-clan-app Build done.
buildbot/nix-build .#checks.x86_64-linux.test-backups Build done.
checks / checks-impure (pull_request) Successful in 2m8s
buildbot/nix-build .#checks.x86_64-linux.clan-dep-zbar Build done.
buildbot/nix-build .#checks.x86_64-linux.clan-pytest-without-core Build done.
buildbot/nix-build .#checks.aarch64-linux.nixos-flash-installer Build done.
buildbot/nix-build .#checks.x86_64-linux.clan-dep-openssh Build done.
buildbot/nix-build .#checks.x86_64-linux.devShell-webview-ui Build done.
buildbot/nix-build .#checks.x86_64-linux.lib-jsonschema-example-valid Build done.
buildbot/nix-build .#checks.x86_64-linux.deltachat Build done.
buildbot/nix-build .#checks.x86_64-linux.devShell-clan-cli Build done.
buildbot/nix-build .#checks.x86_64-linux.matrix-synapse Build done.
buildbot/nix-build .#checks.x86_64-linux.package-clan-cli Build done.
buildbot/nix-build .#checks.x86_64-linux.package-clan-ts-api Build done.
buildbot/nix-build .#checks.x86_64-linux.package-default Build done.
buildbot/nix-build .#checks.x86_64-linux.package-docs Build done.
buildbot/nix-build .#checks.x86_64-linux.treefmt Build done.
buildbot/nix-build .#checks.x86_64-linux.package-editor Build done.
buildbot/nix-build .#checks.x86_64-linux.module-schema Build done.
buildbot/nix-build .#checks.x86_64-linux.lib-jsonschema-nix-unit-tests Build done.
buildbot/nix-build .#checks.x86_64-linux.package-impure-checks Build done.
buildbot/nix-build .#checks.x86_64-linux.package-merge-after-ci Build done.
buildbot/nix-build .#checks.x86_64-linux.package-clan-cli-docs Build done.
buildbot/nix-build .#checks.x86_64-linux.package-moonlight-sunshine-accept Build done.
buildbot/nix-build .#checks.x86_64-linux.package-pending-reviews Build done.
buildbot/nix-build .#checks.x86_64-linux.package-tea-create-pr Build done.
buildbot/nix-build .#checks.x86_64-linux.package-zerotier-members Build done.
buildbot/nix-build .#checks.x86_64-linux.nixos-test_install_machine Build done.
buildbot/nix-build .#checks.x86_64-linux.package-zerotierone Build done.
buildbot/nix-build .#checks.x86_64-linux.package-zt-tcp-relay Build done.
buildbot/nix-build .#checks.x86_64-linux.nixos-flash-installer Build done.
buildbot/nix-build .#checks.x86_64-linux.package-function-schema Build done.
buildbot/nix-build .#checks.x86_64-linux.nixos-iso-installer Build done.
buildbot/nix-build .#checks.x86_64-linux.postgresql Build done.
buildbot/nix-build .#checks.x86_64-linux.package-module-schema Build done.
buildbot/nix-build .#checks.x86_64-linux.zt-tcp-relay Build done.
buildbot/nix-build .#checks.x86_64-linux.package-iso-installer Build done.
buildbot/nix-build .#checks.x86_64-linux.template-minimal Build done.
buildbot/nix-build .#checks.x86_64-linux.secrets Build done.
buildbot/nix-build .#checks.x86_64-linux.wayland-proxy-virtwl Build done.
buildbot/nix-build .#checks.x86_64-linux.syncthing Build done.
buildbot/nix-build .#checks.x86_64-linux.package-deploy-docs Build done.
buildbot/nix-build .#checks.x86_64-linux.devShell-clan-app Build done.
buildbot/nix-build .#checks.x86_64-linux.package-webview-ui Build done.
buildbot/nix-build .#checks.x86_64-linux.package-gui-install-test-ubuntu-22-04 Build done.
buildbot/nix-build .#checks.x86_64-linux.clan-pytest-with-core Build done.
buildbot/nix-build .#checks.x86_64-linux.test-installation Build done.
buildbot/nix-eval Build done.
buildbot/nix-build .#checks.x86_64-linux.flash Build done.

This commit is contained in:
Johannes Kirschbauer 2024-06-15 17:35:50 +02:00
parent b28950f310
commit 5e39514251
Signed by: hsjobeki
SSH Key Fingerprint: SHA256:vX3utDqig7Ph5L0JPv87ZTPb/w7cMzREKVZzzLFg9qU
6 changed files with 163 additions and 7 deletions

View File

@ -58,9 +58,9 @@ class ClanError(Exception):
self.location = location or "Unknown location"
self.msg = msg or ""
if self.description:
exception_msg = f"{self.location}: {self.msg} - {self.description}"
exception_msg = f"{self.location}: \n{self.msg} - {self.description}"
else:
exception_msg = f"{self.location}: {self.msg}"
exception_msg = f"{self.location}: \n{self.msg}"
super().__init__(exception_msg)

View File

@ -3,6 +3,7 @@ import argparse
from .create import register_create_parser
from .delete import register_delete_parser
from .hardware import register_hw_generate
from .install import register_install_parser
from .list import register_list_parser
from .show import register_show_parser
@ -63,6 +64,25 @@ Examples:
)
register_list_parser(list_parser)
generate_hw_parser = subparser.add_parser(
"hw-generate",
help="Generate hardware specifics for a machine",
epilog=(
"""
This subcommand generates hardware specifics for a machine. Such as the host platform, available kernel modules, etc.
Examples:
$ clan machines hw-generate [MACHINE] [TARGET_HOST]
Will generate hardware specifics for the the specified `[TARGET_HOST]` and place the result in hardware.nix for the given machine `[MACHINE]`.
For more detailed information, visit: https://docs.clan.lol/getting-started/configure/#machine-configuration
"""
),
)
register_hw_generate(generate_hw_parser)
show_parser = subparser.add_parser(
"show",
help="Show a machine",
@ -92,7 +112,7 @@ Examples:
Will install the specified machine [MACHINE], to the specified [TARGET_HOST].
$ clan machines install [MACHINE] --json [JSON]
Will install the specified machine [MACHINE] to the host exposed by
Will install the specified machine [MACHINE] to the host exposed by
the deployment information of the [JSON] deployment string.
For information on how to set up the installer see: https://docs.clan.lol/getting-started/installer/

View File

@ -0,0 +1,137 @@
import argparse
import dataclasses
import json
import logging
from pathlib import Path
from clan_cli.api import API
from clan_cli.errors import ClanError
from ..cmd import run, run_no_stdout
from ..completions import add_dynamic_completer, complete_machines
from ..nix import nix_config, nix_eval, nix_shell
from .types import machine_name_type
log = logging.getLogger(__name__)
@dataclasses.dataclass
class HardwareInfo:
system: str | None
@API.register
def generate_machine_hardware_info(
clan_dir: str | Path,
machine_name: str,
hostname: str,
password: str | None = None,
force: bool | None = False,
) -> HardwareInfo:
"""
Generate hardware information for a machine
and place the resulting *.nix file in the machine's directory.
"""
cmd = nix_shell(
[
"nixpkgs#openssh",
"nixpkgs#sshpass",
# Provides nixos-generate-config on non-NixOS systems
"nixpkgs#nixos-install-tools",
],
[
*(["sshpass", "-p", f"{password}"] if password else []),
"ssh",
# Disable strict host key checking
"-o StrictHostKeyChecking=no",
# Disable known hosts file
"-o UserKnownHostsFile=/dev/null",
f"root@{hostname}",
"nixos-generate-config",
# Filesystems are managed by disko
"--no-filesystems",
"--show-hardware-config",
],
)
out = run(cmd)
if out.returncode != 0:
log.error(f"Failed to inspect {machine_name}. Address: {hostname}")
log.error(out)
raise ClanError(f"Failed to inspect {machine_name}. Address: {hostname}")
hw_file = Path(f"{clan_dir}/machines/{machine_name}/hardware-configuration.nix")
hw_file.parent.mkdir(parents=True, exist_ok=True)
# Check if the hardware-configuration.nix file is a template
is_template = False
if hw_file.exists():
is_template = "throw" in hw_file.read_text()
if hw_file.exists() and not force and not is_template:
raise ClanError(
"File exists.",
description="Hardware file already exists. To force overwrite the existing configuration use '--force'.",
location=f"{__name__} {hw_file}",
)
with open(hw_file, "w") as f:
f.write(out.stdout)
print(f"Successfully generated: {hw_file}")
# TODO: This could be its own API function?
config = nix_config()
system = config["system"]
cmd = nix_eval(
[
f"{clan_dir}#clanInternals.machines.{system}.{machine_name}",
"--apply",
"machine: { inherit (machine.config.nixpkgs.hostPlatform) system; }",
"--json",
]
)
proc = run_no_stdout(cmd)
res = proc.stdout.strip()
host_platform = json.loads(res)
return HardwareInfo(
system=host_platform.get("system", None),
)
def hw_generate_command(args: argparse.Namespace) -> None:
flake_path = Path(args.flake).resolve()
hw_info = generate_machine_hardware_info(
flake_path, args.machine, args.hostname, args.password, args.force
)
print("----")
print("Successfully generated hardware information.")
print(f"Target: {args.machine} ({args.hostname})")
print(f"System: {hw_info.system}")
print("----")
def register_hw_generate(parser: argparse.ArgumentParser) -> None:
parser.set_defaults(func=hw_generate_command)
machine_parser = parser.add_argument(
"machine",
help="the name of the machine",
type=machine_name_type,
)
machine_parser = parser.add_argument(
"hostname",
help="hostname of the machine",
type=str,
)
machine_parser = parser.add_argument(
"--password",
help="Pre-provided password the cli will prompt otherwise if needed.",
type=str,
required=False,
)
machine_parser = parser.add_argument(
"--force",
help="Will overwrite the hardware-configuration.nix file.",
action="store_true",
)
add_dynamic_completer(machine_parser, complete_machines)

View File

@ -6,7 +6,7 @@ from ..nix import nix_build, nix_config
from .machines import Machine
# function to speedup eval if we want to evauluate all machines
# function to speedup eval if we want to evaluate all machines
def get_all_machines(flake_dir: Path, nix_options: list[str]) -> list[Machine]:
config = nix_config()
system = config["system"]

View File

@ -22,7 +22,7 @@ class MachineInfo:
@API.register
def show_machine(flake_url: str | Path, machine_name: str, debug: bool) -> MachineInfo:
def show_machine(flake_url: str | Path, machine_name: str) -> MachineInfo:
config = nix_config()
system = config["system"]
cmd = nix_eval(
@ -46,7 +46,7 @@ def show_machine(flake_url: str | Path, machine_name: str, debug: bool) -> Machi
def show_command(args: argparse.Namespace) -> None:
flake_path = Path(args.flake).resolve()
machine = show_machine(flake_path, args.machine, args.debug)
machine = show_machine(flake_path, args.machine)
print(f"Name: {machine.machine_name}")
print(f"Description: {machine.machine_description or ''}")
print(f"Icon: {machine.machine_icon or ''}")

View File

@ -32,7 +32,6 @@ export const MachineListItem = (props: MachineListItemProps) => {
pyApi.show_machine.dispatch({
op_key: name,
machine_name: name,
debug: false,
flake_url: currClanURI(),
});