vars: rename: invalidation -> validation

This commit is contained in:
DavHau
2024-11-29 17:13:23 +07:00
parent fbbfcc0aa5
commit 5c5a87d416
7 changed files with 26 additions and 33 deletions

View File

@@ -42,7 +42,7 @@ in
name name
dependencies dependencies
finalScript finalScript
invalidationHash validationHash
migrateFact migrateFact
prompts prompts
share share

View File

@@ -76,7 +76,7 @@ in
example = "my_service"; example = "my_service";
default = null; default = null;
}; };
invalidationData = lib.mkOption { validation = lib.mkOption {
description = '' description = ''
A set of values that invalidate the generated values. A set of values that invalidate the generated values.
If any of these values change, the generated values will be re-generated. If any of these values change, the generated values will be re-generated.
@@ -97,8 +97,8 @@ in
description = "JSON compatible data structure"; description = "JSON compatible data structure";
}; };
}; };
# the invalidationHash is the validation interface to the outside world # the validationHash is the validation interface to the outside world
invalidationHash = lib.mkOption { validationHash = lib.mkOption {
internal = true; internal = true;
description = '' description = ''
A hash of the invalidation data. A hash of the invalidation data.
@@ -108,10 +108,10 @@ in
# TODO: recursively traverse the structure and sort all lists in order to support lists # TODO: recursively traverse the structure and sort all lists in order to support lists
default = default =
# For backwards compat, the hash is null by default in which case the check is omitted # For backwards compat, the hash is null by default in which case the check is omitted
if generator.config.invalidationData == null then if generator.config.validation == null then
null null
else else
hashString "sha256" (toJSON generator.config.invalidationData); hashString "sha256" (toJSON generator.config.validation);
defaultText = "Hash of the invalidation data"; defaultText = "Hash of the invalidation data";
}; };
files = lib.mkOption { files = lib.mkOption {

View File

@@ -119,25 +119,21 @@ class StoreBase(ABC):
all_vars.append(var) all_vars.append(var)
return all_vars return all_vars
def get_invalidation_hash(self, generator: "Generator") -> str | None: def get_validation(self, generator: "Generator") -> str | None:
""" """
Return the invalidation hash that indicates if a generator needs to be re-run Return the invalidation hash that indicates if a generator needs to be re-run
due to a change in its definition due to a change in its definition
""" """
hash_file = ( hash_file = self.machine.flake_dir / "vars" / generator.name / "validation"
self.machine.flake_dir / "vars" / generator.name / "invalidation_hash"
)
if not hash_file.exists(): if not hash_file.exists():
return None return None
return hash_file.read_text().strip() return hash_file.read_text().strip()
def set_invalidation_hash(self, generator: "Generator", hash_str: str) -> None: def set_validation(self, generator: "Generator", hash_str: str) -> None:
""" """
Store the invalidation hash that indicates if a generator needs to be re-run Store the invalidation hash that indicates if a generator needs to be re-run
""" """
hash_file = ( hash_file = self.machine.flake_dir / "vars" / generator.name / "validation"
self.machine.flake_dir / "vars" / generator.name / "invalidation_hash"
)
hash_file.parent.mkdir(parents=True, exist_ok=True) hash_file.parent.mkdir(parents=True, exist_ok=True)
hash_file.write_text(hash_str) hash_file.write_text(hash_str)
@@ -147,8 +143,8 @@ class StoreBase(ABC):
If the hash is not set in nix and hasn't been stored before, it is considered valid If the hash is not set in nix and hasn't been stored before, it is considered valid
-> this provides backward and forward compatibility -> this provides backward and forward compatibility
""" """
stored_hash = self.get_invalidation_hash(generator) stored_hash = self.get_validation(generator)
target_hash = generator.invalidation_hash target_hash = generator.validation
# if the hash is neither set in nix nor on disk, it is considered valid (provides backwards compat) # if the hash is neither set in nix nor on disk, it is considered valid (provides backwards compat)
if target_hash is None and stored_hash is None: if target_hash is None and stored_hash is None:
return True return True

View File

@@ -114,7 +114,7 @@ def check_command(args: argparse.Namespace) -> None:
name=args.machine, name=args.machine,
flake=args.flake, flake=args.flake,
) )
ok = check_vars(machine, generator_name=args.service) ok = check_vars(machine, generator_name=args.generator)
if not ok: if not ok:
raise SystemExit(1) raise SystemExit(1)
@@ -127,7 +127,8 @@ def register_check_parser(parser: argparse.ArgumentParser) -> None:
add_dynamic_completer(machines_parser, complete_machines) add_dynamic_completer(machines_parser, complete_machines)
parser.add_argument( parser.add_argument(
"--service", "--generator",
help="the service to check", "-g",
help="the generator to check",
) )
parser.set_defaults(func=check_command) parser.set_defaults(func=check_command)

View File

@@ -145,10 +145,10 @@ Examples:
$ clan vars generate [MACHINE] $ clan vars generate [MACHINE]
Will generate vars for the specified machine. Will generate vars for the specified machine.
$ clan vars generate [MACHINE] --service [SERVICE] $ clan vars generate [MACHINE] --generator [SERVICE]
Will generate vars for the specified machine for the specified service. Will generate vars for the specified machine for the specified service.
$ clan vars generate --service [SERVICE] --regenerate $ clan vars generate --generator [SERVICE] --regenerate
Will regenerate vars, if they are already generated for a specific service. Will regenerate vars, if they are already generated for a specific service.
This is especially useful for resetting certain passwords while leaving the rest This is especially useful for resetting certain passwords while leaving the rest
of the vars for a machine in place. of the vars for a machine in place.

View File

@@ -40,7 +40,7 @@ class Generator:
name: str name: str
files: list[Var] = field(default_factory=list) files: list[Var] = field(default_factory=list)
share: bool = False share: bool = False
invalidation_hash: str | None = None validation: str | None = None
final_script: str = "" final_script: str = ""
prompts: list[Prompt] = field(default_factory=list) prompts: list[Prompt] = field(default_factory=list)
dependencies: list[str] = field(default_factory=list) dependencies: list[str] = field(default_factory=list)
@@ -65,7 +65,7 @@ class Generator:
share=data["share"], share=data["share"],
final_script=data["finalScript"], final_script=data["finalScript"],
files=[Var.from_json(data["name"], f) for f in data["files"].values()], files=[Var.from_json(data["name"], f) for f in data["files"].values()],
invalidation_hash=data["invalidationHash"], validation=data["validationHash"],
dependencies=data["dependencies"], dependencies=data["dependencies"],
migrate_fact=data["migrateFact"], migrate_fact=data["migrateFact"],
prompts=[Prompt.from_json(p) for p in data["prompts"].values()], prompts=[Prompt.from_json(p) for p in data["prompts"].values()],
@@ -220,15 +220,11 @@ def execute_generator(
public_changed = True public_changed = True
if file_path: if file_path:
files_to_commit.append(file_path) files_to_commit.append(file_path)
if generator.invalidation_hash is not None: if generator.validation is not None:
if public_changed: if public_changed:
public_vars_store.set_invalidation_hash( public_vars_store.set_validation(generator, generator.validation)
generator, generator.invalidation_hash
)
if secret_changed: if secret_changed:
secret_vars_store.set_invalidation_hash( secret_vars_store.set_validation(generator, generator.validation)
generator, generator.invalidation_hash
)
commit_files( commit_files(
files_to_commit, files_to_commit,
machine.flake_dir, machine.flake_dir,

View File

@@ -659,7 +659,7 @@ def test_commit_message(
"--flake", "--flake",
str(flake.path), str(flake.path),
"my_machine", "my_machine",
"--service", "--generator",
"my_generator", "my_generator",
] ]
) )
@@ -678,7 +678,7 @@ def test_commit_message(
"--flake", "--flake",
str(flake.path), str(flake.path),
"my_machine", "my_machine",
"--service", "--generator",
"my_secret_generator", "my_secret_generator",
] ]
) )
@@ -981,7 +981,7 @@ def test_invalidation(
value1_new = get_var(machine, "my_generator/my_value").printable_value value1_new = get_var(machine, "my_generator/my_value").printable_value
assert value1 == value1_new assert value1 == value1_new
# set the invalidation data of the generator # set the invalidation data of the generator
my_generator["invalidationData"] = 1 my_generator["validation"] = 1
flake.refresh() flake.refresh()
# generate again and make sure the value changes # generate again and make sure the value changes
cli.run(["vars", "generate", "--flake", str(flake.path), "my_machine"]) cli.run(["vars", "generate", "--flake", str(flake.path), "my_machine"])