Add a way to bust the log cache

This commit is contained in:
Reid 'arrdem' McKenzie 2022-07-28 19:24:17 -06:00
parent bbb4f83071
commit b471e2ab82

View file

@ -6,7 +6,6 @@ import os
from pathlib import Path from pathlib import Path
import pickle import pickle
from typing import List from typing import List
import sys
from . import ( from . import (
__author__, __author__,
@ -139,13 +138,15 @@ def simplify(old_fs: Vfs, new_fs: Vfs, /, exec_idempotent=True) -> Vfs:
except ValueError: except ValueError:
pass pass
# Dedupe the new log while preserving order # Dedupe the new log while preserving order.
distinct = set() keys = set()
for txn, idx in zip(new_fs._log, range(len(new_fs._log))): deduped = []
if txn in distinct: for op in new_fs._log:
new_fs._log.pop(idx) key = str(op)
else: if key not in keys:
distinct.add(txn) keys.add(key)
deduped.append(op)
new_fs._log = deduped
return new_fs return new_fs
@ -194,21 +195,20 @@ def cli():
@cli.command("apply") @cli.command("apply")
@click.option("--execute/--dry-run", default=False) @click.option("--execute/--dry-run", default=False)
@click.option("--force/--no-force", default=False)
@click.option("--state-file", default=".cram.log", type=Path) @click.option("--state-file", default=".cram.log", type=Path)
@click.option("--optimize/--no-optimize", default=True) @click.option("--optimize/--no-optimize", default=True)
@click.option("--require", type=str, multiple=True, default=[f"hosts.d/{os.uname()[1].split('.')[0]}", "profiles.d/default"]) @click.option("--require", type=str, multiple=True, default=[f"hosts.d/{os.uname()[1].split('.')[0]}", "profiles.d/default"])
@click.option("--exec-idempotent/--exec-always", "exec_idempotent", default=True) @click.option("--exec-idempotent/--exec-always", "exec_idempotent", default=True)
@click.argument("confdir", type=Path) @click.argument("confdir", type=Path)
@click.argument("destdir", type=Path) @click.argument("destdir", type=Path)
def do_apply(confdir, destdir, state_file, execute, optimize, require, exec_idempotent): def do_apply(confdir, destdir, state_file, execute, optimize, force, require, exec_idempotent):
"""The entry point of cram.""" """The entry point of cram."""
# Resolve the two input paths to absolutes # Resolve the two input paths to absolutes
root = confdir.resolve() root = confdir.resolve()
dest = destdir.resolve() dest = destdir.resolve()
log.info(f"Installing requirements {require}")
if not root.is_dir(): if not root.is_dir():
log.fatal(f"{confdir} does not exist!") log.fatal(f"{confdir} does not exist!")
_exit(1) _exit(1)
@ -216,8 +216,12 @@ def do_apply(confdir, destdir, state_file, execute, optimize, require, exec_idem
if not state_file.is_absolute(): if not state_file.is_absolute():
state_file = root / state_file state_file = root / state_file
old_fs = load_state(state_file) if not force:
log.debug(f"Loaded old state consisting of {len(old_fs._log)} steps") old_fs = load_state(state_file)
log.debug(f"Loaded old state consisting of {len(old_fs._log)} steps")
else:
# Force an empty state
old_fs = Vfs([])
new_fs = build_fs(root, dest, require) new_fs = build_fs(root, dest, require)
log.debug(f"Built new state consisting of {len(new_fs._log)} steps") log.debug(f"Built new state consisting of {len(new_fs._log)} steps")