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
dependencies
finalScript
invalidationHash
validationHash
migrateFact
prompts
share

View File

@@ -76,7 +76,7 @@ in
example = "my_service";
default = null;
};
invalidationData = lib.mkOption {
validation = lib.mkOption {
description = ''
A set of values that invalidate the generated values.
If any of these values change, the generated values will be re-generated.
@@ -97,8 +97,8 @@ in
description = "JSON compatible data structure";
};
};
# the invalidationHash is the validation interface to the outside world
invalidationHash = lib.mkOption {
# the validationHash is the validation interface to the outside world
validationHash = lib.mkOption {
internal = true;
description = ''
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
default =
# 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
else
hashString "sha256" (toJSON generator.config.invalidationData);
hashString "sha256" (toJSON generator.config.validation);
defaultText = "Hash of the invalidation data";
};
files = lib.mkOption {

View File

@@ -119,25 +119,21 @@ class StoreBase(ABC):
all_vars.append(var)
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
due to a change in its definition
"""
hash_file = (
self.machine.flake_dir / "vars" / generator.name / "invalidation_hash"
)
hash_file = self.machine.flake_dir / "vars" / generator.name / "validation"
if not hash_file.exists():
return None
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
"""
hash_file = (
self.machine.flake_dir / "vars" / generator.name / "invalidation_hash"
)
hash_file = self.machine.flake_dir / "vars" / generator.name / "validation"
hash_file.parent.mkdir(parents=True, exist_ok=True)
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
-> this provides backward and forward compatibility
"""
stored_hash = self.get_invalidation_hash(generator)
target_hash = generator.invalidation_hash
stored_hash = self.get_validation(generator)
target_hash = generator.validation
# 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:
return True

View File

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

View File

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

View File

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

View File

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