cli/templates: init apply disk
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
# !/usr/bin/env python3
|
# !/usr/bin/env python3
|
||||||
import argparse
|
import argparse
|
||||||
|
|
||||||
|
from .apply import register_apply_parser
|
||||||
from .list import register_list_parser
|
from .list import register_list_parser
|
||||||
|
|
||||||
|
|
||||||
@@ -13,4 +14,8 @@ def register_parser(parser: argparse.ArgumentParser) -> None:
|
|||||||
required=True,
|
required=True,
|
||||||
)
|
)
|
||||||
list_parser = subparser.add_parser("list", help="List avilable templates")
|
list_parser = subparser.add_parser("list", help="List avilable templates")
|
||||||
|
apply_parser = subparser.add_parser(
|
||||||
|
"apply", help="Apply a template of the specified type"
|
||||||
|
)
|
||||||
register_list_parser(list_parser)
|
register_list_parser(list_parser)
|
||||||
|
register_apply_parser(apply_parser)
|
||||||
|
|||||||
15
pkgs/clan-cli/clan_cli/templates/apply.py
Normal file
15
pkgs/clan-cli/clan_cli/templates/apply.py
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
import argparse
|
||||||
|
|
||||||
|
from .apply_disk import register_apply_disk_template_parser
|
||||||
|
|
||||||
|
|
||||||
|
def register_apply_parser(parser: argparse.ArgumentParser) -> None:
|
||||||
|
subparser = parser.add_subparsers(
|
||||||
|
title="template_type",
|
||||||
|
description="the template type to apply",
|
||||||
|
help="the template type to apply",
|
||||||
|
required=True,
|
||||||
|
)
|
||||||
|
disk_parser = subparser.add_parser("disk", help="Apply a disk template")
|
||||||
|
|
||||||
|
register_apply_disk_template_parser(disk_parser)
|
||||||
78
pkgs/clan-cli/clan_cli/templates/apply_disk.py
Normal file
78
pkgs/clan-cli/clan_cli/templates/apply_disk.py
Normal file
@@ -0,0 +1,78 @@
|
|||||||
|
import argparse
|
||||||
|
import logging
|
||||||
|
from collections.abc import Sequence
|
||||||
|
from typing import Any
|
||||||
|
|
||||||
|
from clan_lib.api.disk import set_machine_disk_schema
|
||||||
|
from clan_lib.machines.machines import Machine
|
||||||
|
|
||||||
|
log = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
class AppendSetAction(argparse.Action):
|
||||||
|
def __init__(self, option_strings: str, dest: str, **kwargs: Any) -> None:
|
||||||
|
super().__init__(option_strings, dest, **kwargs)
|
||||||
|
|
||||||
|
def __call__(
|
||||||
|
self,
|
||||||
|
parser: argparse.ArgumentParser,
|
||||||
|
namespace: argparse.Namespace,
|
||||||
|
values: str | Sequence[str] | None,
|
||||||
|
option_string: str | None = None,
|
||||||
|
) -> None:
|
||||||
|
lst = getattr(namespace, self.dest)
|
||||||
|
assert isinstance(values, list), "values must be a list"
|
||||||
|
lst.append((values[0], values[1]))
|
||||||
|
|
||||||
|
|
||||||
|
def apply_command(args: argparse.Namespace) -> None:
|
||||||
|
"""Apply a disk template to a machine."""
|
||||||
|
set_tuples: list[tuple[str, str]] = args.set
|
||||||
|
|
||||||
|
placeholders = dict(set_tuples)
|
||||||
|
|
||||||
|
set_machine_disk_schema(
|
||||||
|
Machine(args.to_machine, args.flake),
|
||||||
|
args.template,
|
||||||
|
placeholders,
|
||||||
|
force=args.force,
|
||||||
|
check_hw=not args.skip_hardware_check,
|
||||||
|
)
|
||||||
|
log.info(f"Applied disk template '{args.template}' to machine '{args.to_machine}' ")
|
||||||
|
|
||||||
|
|
||||||
|
def register_apply_disk_template_parser(parser: argparse.ArgumentParser) -> None:
|
||||||
|
parser.add_argument(
|
||||||
|
"--to-machine",
|
||||||
|
type=str,
|
||||||
|
required=True,
|
||||||
|
help="The machine to apply the template to",
|
||||||
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
"--template",
|
||||||
|
type=str,
|
||||||
|
required=True,
|
||||||
|
help="The name of the disk template to apply",
|
||||||
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
"--set",
|
||||||
|
help="Set a placeholder in the template to a value",
|
||||||
|
nargs=2,
|
||||||
|
metavar=("placeholder", "value"),
|
||||||
|
action=AppendSetAction,
|
||||||
|
default=[],
|
||||||
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
"--force",
|
||||||
|
help="Force apply the template even if the machine already has a disk schema",
|
||||||
|
action="store_true",
|
||||||
|
default=False,
|
||||||
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
"--skip-hardware-check",
|
||||||
|
help="Disables hardware checking. By default this command checks that the facter.json report exists and validates provided options",
|
||||||
|
action="store_true",
|
||||||
|
default=False,
|
||||||
|
)
|
||||||
|
|
||||||
|
parser.set_defaults(func=apply_command)
|
||||||
Reference in New Issue
Block a user