From 59fd245a3fcefe59d2c68afbf0883a1105e6a3fa Mon Sep 17 00:00:00 2001 From: Franziska Kunsmann Date: Fri, 1 Sep 2023 06:16:09 +0200 Subject: [PATCH] add dynamic node attribute for last apply so we can check if something has changed in the repo since the last apply --- hooks/deploy_commit_hash_to_node.py | 5 +++++ nodes/attributes.py | 24 ++++++++++++++++++++++++ 2 files changed, 29 insertions(+) create mode 100644 hooks/deploy_commit_hash_to_node.py create mode 100644 nodes/attributes.py diff --git a/hooks/deploy_commit_hash_to_node.py b/hooks/deploy_commit_hash_to_node.py new file mode 100644 index 0000000..caddaad --- /dev/null +++ b/hooks/deploy_commit_hash_to_node.py @@ -0,0 +1,5 @@ +def node_apply_end(repo, node, duration, interactive, result, **kwargs): + if not node.os in node.OS_FAMILY_UNIX: + return + + node.run(f'echo "{repo.revision}" > /var/lib/bundlewrap/last_apply_commit_id') diff --git a/nodes/attributes.py b/nodes/attributes.py new file mode 100644 index 0000000..8460bc9 --- /dev/null +++ b/nodes/attributes.py @@ -0,0 +1,24 @@ +from bundlewrap.utils.ui import io +from bundlewrap.utils.scm import get_rev +from bundlewrap.utils.text import red, bold + +@node_attribute +def needs_apply(node): + if node.dummy: + return False + + if node.os not in node.OS_FAMILY_UNIX: + return True + + try: + applied = node.run( + 'cat /var/lib/bundlewrap/last_apply_commit_id', + may_fail=True, + ).stdout.decode().strip() + + if not applied or applied != get_rev(): + return True + except Exception as e: + io.stderr(f'{red("!!!")} {bold(node.name)} {e!r}') + + return False