forked from clan/clan-core
clan: facts generate
allow regeneration of facts
Add `--regenerate` flag to `clan facts generate` which allows forcing the generation of facts, regardless of their current existence. Examples: ``` clan facts generate [MACHINE] --regenerate ``` or ``` clan facts generate [MACHINE] --service [SERVICE] --regenerate ```
This commit is contained in:
parent
4daf036a3c
commit
81e0700826
@ -33,6 +33,7 @@ def read_multiline_input(prompt: str = "Finish with Ctrl-D") -> str:
|
|||||||
def generate_service_facts(
|
def generate_service_facts(
|
||||||
machine: Machine,
|
machine: Machine,
|
||||||
service: str,
|
service: str,
|
||||||
|
regenerate: bool,
|
||||||
secret_facts_store: SecretStoreBase,
|
secret_facts_store: SecretStoreBase,
|
||||||
public_facts_store: FactStoreBase,
|
public_facts_store: FactStoreBase,
|
||||||
tmpdir: Path,
|
tmpdir: Path,
|
||||||
@ -42,7 +43,7 @@ def generate_service_facts(
|
|||||||
# check if all secrets exist and generate them if at least one is missing
|
# check if all secrets exist and generate them if at least one is missing
|
||||||
needs_regeneration = not check_secrets(machine, service=service)
|
needs_regeneration = not check_secrets(machine, service=service)
|
||||||
log.debug(f"{service} needs_regeneration: {needs_regeneration}")
|
log.debug(f"{service} needs_regeneration: {needs_regeneration}")
|
||||||
if not needs_regeneration:
|
if not (needs_regeneration or regenerate):
|
||||||
return False
|
return False
|
||||||
if not isinstance(machine.flake, Path):
|
if not isinstance(machine.flake, Path):
|
||||||
msg = f"flake is not a Path: {machine.flake}"
|
msg = f"flake is not a Path: {machine.flake}"
|
||||||
@ -136,6 +137,7 @@ def prompt_func(text: str) -> str:
|
|||||||
def _generate_facts_for_machine(
|
def _generate_facts_for_machine(
|
||||||
machine: Machine,
|
machine: Machine,
|
||||||
service: str | None,
|
service: str | None,
|
||||||
|
regenerate: bool,
|
||||||
tmpdir: Path,
|
tmpdir: Path,
|
||||||
prompt: Callable[[str], str] = prompt_func,
|
prompt: Callable[[str], str] = prompt_func,
|
||||||
) -> bool:
|
) -> bool:
|
||||||
@ -164,6 +166,7 @@ def _generate_facts_for_machine(
|
|||||||
machine_updated |= generate_service_facts(
|
machine_updated |= generate_service_facts(
|
||||||
machine=machine,
|
machine=machine,
|
||||||
service=service,
|
service=service,
|
||||||
|
regenerate=regenerate,
|
||||||
secret_facts_store=secret_facts_store,
|
secret_facts_store=secret_facts_store,
|
||||||
public_facts_store=public_facts_store,
|
public_facts_store=public_facts_store,
|
||||||
tmpdir=local_temp,
|
tmpdir=local_temp,
|
||||||
@ -178,6 +181,7 @@ def _generate_facts_for_machine(
|
|||||||
def generate_facts(
|
def generate_facts(
|
||||||
machines: list[Machine],
|
machines: list[Machine],
|
||||||
service: str | None,
|
service: str | None,
|
||||||
|
regenerate: bool,
|
||||||
prompt: Callable[[str], str] = prompt_func,
|
prompt: Callable[[str], str] = prompt_func,
|
||||||
) -> bool:
|
) -> bool:
|
||||||
was_regenerated = False
|
was_regenerated = False
|
||||||
@ -188,7 +192,7 @@ def generate_facts(
|
|||||||
errors = 0
|
errors = 0
|
||||||
try:
|
try:
|
||||||
was_regenerated |= _generate_facts_for_machine(
|
was_regenerated |= _generate_facts_for_machine(
|
||||||
machine, service, tmpdir, prompt
|
machine, service, regenerate, tmpdir, prompt
|
||||||
)
|
)
|
||||||
except Exception as exc:
|
except Exception as exc:
|
||||||
log.error(f"Failed to generate facts for {machine.name}: {exc}")
|
log.error(f"Failed to generate facts for {machine.name}: {exc}")
|
||||||
@ -208,7 +212,7 @@ def generate_command(args: argparse.Namespace) -> None:
|
|||||||
machines = get_all_machines(args.flake)
|
machines = get_all_machines(args.flake)
|
||||||
else:
|
else:
|
||||||
machines = get_selected_machines(args.flake, args.machines)
|
machines = get_selected_machines(args.flake, args.machines)
|
||||||
generate_facts(machines, args.service)
|
generate_facts(machines, args.service, args.regenerate)
|
||||||
|
|
||||||
|
|
||||||
def register_generate_parser(parser: argparse.ArgumentParser) -> None:
|
def register_generate_parser(parser: argparse.ArgumentParser) -> None:
|
||||||
@ -225,4 +229,11 @@ def register_generate_parser(parser: argparse.ArgumentParser) -> None:
|
|||||||
help="service to generate facts for, if empty, generate facts for every service",
|
help="service to generate facts for, if empty, generate facts for every service",
|
||||||
default=None,
|
default=None,
|
||||||
)
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
"--regenerate",
|
||||||
|
type=bool,
|
||||||
|
action=argparse.BooleanOptionalAction,
|
||||||
|
help="whether to regenerate facts for the specified machine",
|
||||||
|
default=None,
|
||||||
|
)
|
||||||
parser.set_defaults(func=generate_command)
|
parser.set_defaults(func=generate_command)
|
||||||
|
@ -35,7 +35,7 @@ def install_nixos(
|
|||||||
target_host = f"{h.user or 'root'}@{h.host}"
|
target_host = f"{h.user or 'root'}@{h.host}"
|
||||||
log.info(f"target host: {target_host}")
|
log.info(f"target host: {target_host}")
|
||||||
|
|
||||||
generate_facts([machine], None)
|
generate_facts([machine], None, False)
|
||||||
|
|
||||||
with TemporaryDirectory() as tmpdir_:
|
with TemporaryDirectory() as tmpdir_:
|
||||||
tmpdir = Path(tmpdir_)
|
tmpdir = Path(tmpdir_)
|
||||||
|
@ -98,7 +98,7 @@ def deploy_nixos(machines: MachineGroup) -> None:
|
|||||||
env = os.environ.copy()
|
env = os.environ.copy()
|
||||||
env["NIX_SSHOPTS"] = ssh_arg
|
env["NIX_SSHOPTS"] = ssh_arg
|
||||||
|
|
||||||
generate_facts([machine], None)
|
generate_facts([machine], None, False)
|
||||||
upload_secrets(machine)
|
upload_secrets(machine)
|
||||||
|
|
||||||
path = upload_sources(".", target)
|
path = upload_sources(".", target)
|
||||||
|
@ -69,7 +69,7 @@ def get_secrets(
|
|||||||
secret_facts_module = importlib.import_module(machine.secret_facts_module)
|
secret_facts_module = importlib.import_module(machine.secret_facts_module)
|
||||||
secret_facts_store = secret_facts_module.SecretStore(machine=machine)
|
secret_facts_store = secret_facts_module.SecretStore(machine=machine)
|
||||||
|
|
||||||
generate_facts([machine], None)
|
generate_facts([machine], None, False)
|
||||||
|
|
||||||
secret_facts_store.upload(secrets_dir)
|
secret_facts_store.upload(secrets_dir)
|
||||||
return secrets_dir
|
return secrets_dir
|
||||||
|
Loading…
Reference in New Issue
Block a user