From 5ae54c278477e9d975410892d9f6b0995f9a2dcc Mon Sep 17 00:00:00 2001 From: Patrick Spek Date: Tue, 2 Apr 2024 09:32:25 +0200 Subject: Implement zap --- CHANGELOG.md | 4 ++++ lib/main.bash | 3 +++ lib/subcommands/zap.bash | 39 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 46 insertions(+) create mode 100644 lib/subcommands/zap.bash diff --git a/CHANGELOG.md b/CHANGELOG.md index e2f9903..157dc0e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Configuration variables can be assigned values of other variables with the `&=` assignment. This allows a single value to be re-used dynamically, rather than having to explicitly set the same value several times. +- A `zap` command has been added to remove a playbook from the registry without + running the playbook's `playbook_del()` function. This is intended to easily + remove registry entries when a playbook itself has been deleted or is + otherwise broken in a way that the regular `del` subcommand cannot fix. ### Fixed diff --git a/lib/main.bash b/lib/main.bash index 57f1559..196bbbf 100644 --- a/lib/main.bash +++ b/lib/main.bash @@ -89,6 +89,7 @@ Usage: $BASHTARD_NAME top $BASHTARD_NAME var [-p ] $BASHTARD_NAME var [-s] + $BASHTARD_NAME zap Perform maintenance on your infra. @@ -105,6 +106,8 @@ Commands: sysinfo Show gathered information about this system. top Show resource information about all known hosts. var Show or set the value of a given configuration key. + zap Remove a playbook from the registry without attempting to run + the delete step from the playbook. EOF if [[ ! -d "$BASHTARD_ETCDIR/playbooks.d" ]] diff --git a/lib/subcommands/zap.bash b/lib/subcommands/zap.bash new file mode 100644 index 0000000..6864a33 --- /dev/null +++ b/lib/subcommands/zap.bash @@ -0,0 +1,39 @@ +#!/usr/bin/env bash + +# SPDX-FileCopyrightText: 2023 Patrick Spek +# +# SPDX-License-Identifier: AGPL-3.0-or-later + +subcommand() +{ + local buffer + local playbook_base + local playbook_registry + + export BASHTARD_PLAYBOOK="$1" ; shift + + if [[ -z "$BASHTARD_PLAYBOOK" ]] + then + crit "bashtard/zap" "No playbook name specified" + return + fi + + playbook_base="$(playbook_path "base")" + playbook_registry="$BASHTARD_ETCDIR/registry.d/${BASHTARD_PLATFORM[fqdn]}" + + # Make sure we only run add if the playbook is not in the registry yet + if ! grep -Fqx "$BASHTARD_PLAYBOOK" "$playbook_registry" + then + crit "bashtard/zap" "'$BASHTARD_PLAYBOOK' is not registered for ${BASHTARD_PLATFORM[fqdn]}" + return 3 + fi + + notice "bashtard/zap" "Removing playbook '$BASHTARD_PLAYBOOK' from '${BASHTARD_PLATFORM[fqdn]}'" + + buffer="$(tmpfile)" + + # Remove the playbook from the registry + cp -- "$playbook_registry" "$buffer" + grep -Fvx "$BASHTARD_PLAYBOOK" "$buffer" \ + | sort > "$playbook_registry" +} -- cgit v1.1