diff options
author | Patrick Spek <p.spek@tyil.nl> | 2022-04-17 10:42:09 +0200 |
---|---|---|
committer | Patrick Spek <p.spek@tyil.nl> | 2022-04-17 10:42:09 +0200 |
commit | 97ea86450b3619cca8a0d562a669f5678d5df168 (patch) | |
tree | 237c5c2989dd43e7da2c67529a899a3a8218577b /lib/subcommands | |
parent | e3f5c683a1d2b34be698cb59d77d379d38458cad (diff) |
Add functionality to work with playbooks
Diffstat (limited to 'lib/subcommands')
-rw-r--r-- | lib/subcommands/add.bash | 51 | ||||
-rw-r--r-- | lib/subcommands/del.bash | 51 |
2 files changed, 102 insertions, 0 deletions
diff --git a/lib/subcommands/add.bash b/lib/subcommands/add.bash new file mode 100644 index 0000000..b48ef10 --- /dev/null +++ b/lib/subcommands/add.bash @@ -0,0 +1,51 @@ +#!/usr/bin/env bash + +subcommand() +{ + export BASHTARD_PLAYBOOK="$1" ; shift + + if [[ -z "$BASHTARD_PLAYBOOK" ]] + then + crit "bashtard/add" "No playbook name specified" + return 2 + fi + + local playbook_base="$BASHTARD_ETCDIR/playbooks.d/$BASHTARD_PLAYBOOK" + local 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/add" "'$BASHTARD_PLAYBOOK' is already registered for ${BASHTARD_PLATFORM[fqdn]}" + return 3 + fi + + notice "bashtard/add" "Adding playbook '$BASHTARD_PLAYBOOK' to '${BASHTARD_PLATFORM[fqdn]}'" + + if [[ ! -d "$playbook_base" ]] + then + emerg "bashtard/add" "No such directory: $playbook_base" + return 1 + fi + + if [[ ! -f "$playbook_base/playbook.bash" ]] + then + emerg "bashtard/add" "No such file: $playbook_base/playbook.bash" + return 1 + fi + + . "$playbook_base/playbook.bash" + + if ! playbook_add + then + crit "bashtard/add" "$BASHTARD_PLAYBOOK reported an error" + return 1 + fi + + local buffer="$(tmpfile)" + + # Add the playbook to the registry + cp -- "$playbook_registry" "$buffer" + printf -- "%s\n" "$BASHTARD_PLAYBOOK" >> "$buffer" + sort -- "$buffer" > "$playbook_registry" +} diff --git a/lib/subcommands/del.bash b/lib/subcommands/del.bash new file mode 100644 index 0000000..3896fec --- /dev/null +++ b/lib/subcommands/del.bash @@ -0,0 +1,51 @@ +#!/usr/bin/env bash + +subcommand() +{ + export BASHTARD_PLAYBOOK="$1" ; shift + + if [[ -z "$BASHTARD_PLAYBOOK" ]] + then + crit "bashtard/del" "No playbook name specified" + return + fi + + local playbook_base="$BASHTARD_ETCDIR/playbooks.d/$BASHTARD_PLAYBOOK" + local 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/add" "'$BASHTARD_PLAYBOOK' is not registered for ${BASHTARD_PLATFORM[fqdn]}" + return 3 + fi + + notice "bashtard/del" "Removing playbook '$BASHTARD_PLAYBOOK' from '${BASHTARD_PLATFORM[fqdn]}'" + + if [[ ! -d "$playbook_base" ]] + then + emerg "bashtard/sync" "No such directory: $playbook_base" + return 1 + fi + + if [[ ! -f "$playbook_base/playbook.bash" ]] + then + emerg "bashtard/sync" "No such file: $playbook_base/playbook.bash" + return 1 + fi + + . "$playbook_base/playbook.bash" + + if ! playbook_del + then + crit "bashtard/del" "$BASHTARD_PLAYBOOK reported an error" + return 1 + fi + + local buffer="$(tmpfile)" + + # Remove the playbook from the registry + cp -- "$playbook_registry" "$buffer" + grep -Fqvx "$BASHTARD_PLAYBOOK" "$buffer" \ + | sort > "$playbook_registry" +} |