From 97ea86450b3619cca8a0d562a669f5678d5df168 Mon Sep 17 00:00:00 2001 From: Patrick Spek Date: Sun, 17 Apr 2022 10:42:09 +0200 Subject: Add functionality to work with playbooks --- lib/main.bash | 10 +++--- lib/subcommands/add.bash | 51 +++++++++++++++++++++++++++ lib/subcommands/del.bash | 51 +++++++++++++++++++++++++++ lib/util.bash | 90 ++++++++++++++++++++++++++++++++++++++++++------ 4 files changed, 186 insertions(+), 16 deletions(-) create mode 100644 lib/subcommands/add.bash create mode 100644 lib/subcommands/del.bash (limited to 'lib') diff --git a/lib/main.bash b/lib/main.bash index ce41b92..063b05f 100644 --- a/lib/main.bash +++ b/lib/main.bash @@ -13,12 +13,11 @@ main() { [[ -z $1 ]] && usage && exit 2 - local subcommand="$1" - shift + export BASHTARD_COMMAND="$1" ; shift - debug "$BASHTARD_NAME/main" "Handling subcommand '$subcommand'" + debug "$BASHTARD_NAME/main" "Handling subcommand '$BASHTARD_COMMAND'" - subcommand_src="$BASHTARD_BASEDIR/lib/subcommands/$subcommand.bash" + subcommand_src="$BASHTARD_BASEDIR/lib/subcommands/$BASHTARD_COMMAND.bash" debug "$BASHTARD_NAME/main" "Checking $subcommand_src" @@ -38,7 +37,6 @@ main() { # Export BASHTARD_ variables export BASHTARD_PLATFORM - export BASHTARD_SYSTEM="${subcommand%%/*}" # Source the file defining the subcommand. debug "$BASHTARD_NAME/main" "Sourcing $subcommand_src" @@ -50,7 +48,7 @@ main() { debug "$BASHTARD_NAME/main" "\$TMPDIR set to $TMPDIR" # Actually perform the subcommand - debug "$BASHTARD_NAME/main" "Running subcommand '$subcommand'" + debug "$BASHTARD_NAME/main" "Running subcommand '$BASHTARD_COMMAND'" subcommand "$@" local subcommand_exit=$? 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" +} diff --git a/lib/util.bash b/lib/util.bash index 32d61c6..72a41a8 100644 --- a/lib/util.bash +++ b/lib/util.bash @@ -187,13 +187,11 @@ pkg() { local system="bashtard/pkg" local action=$1 ; shift - local app="$(config "pkg.$1" "$(config "app.$1")")" ; shift + local pkg="$(config "pkg.$1")" ; shift - local type - - if [[ -z $app ]] + if [[ -z $pkg ]] then - crit "$system" "No package name for $app" + crit "$system" "No package name for $pkg" return 1 fi @@ -203,7 +201,7 @@ pkg() { return 1 fi - "pkg_$action" "$app" + "pkg_$action" "$pkg" } pkg_install() { @@ -214,7 +212,26 @@ pkg_install() { case "${BASHTARD_PLATFORM[key]}" in freebsd) set -- /usr/sbin/pkg install -y "$app" ;; linux-debian*) set -- apt install -y "$app" ;; - linux-gentoo) set -- emerge --ask=n "$app" ;; + linux-gentoo) set -- emerge --ask=n --update "$app" ;; + *) + crit "$system" "No package manager configured for ${BASHTARD_PLATFORM[key]}" + return 1 + ;; + esac + + notice "$system" "$*" + $@ +} + +pkg_uninstall() { + local system="bashtard/pkg/uninstall" + + local app=$1 ; shift + + case "${BASHTARD_PLATFORM[key]}" in + freebsd) set -- /usr/sbin/pkg uninstall -y "$app" ;; + linux-debian*) set -- apt remove -y "$app" ;; + linux-gentoo) set -- emerge --ask=n --unmerge "$app" ;; *) crit "$system" "No package manager configured for ${BASHTARD_PLATFORM[key]}" return 1 @@ -232,8 +249,8 @@ svc() { local service local action - service="$(config svc.$1)" ; shift action=$1 ; shift + service="$(config svc.$1)" ; shift if [[ -z $service ]] then @@ -250,6 +267,23 @@ svc() { "svc_$action" "$service" } +svc_disable() { + local system="bashtard/svc/disable" + + local service=$1 + + case "${BASHTARD_PLATFORM[key]}" in + linux-gentoo) set -- /sbin/rc-update del "$service" ;; + linux-*) set -- systemctl disable "$service" ;; + *) + crit "$system" "No service manager configured for ${BASHTARD_PLATFORM[key]}" + return 1 + esac + + notice "$system" "$*" + $@ +} + svc_enable() { local system="bashtard/svc/enable" @@ -267,6 +301,24 @@ svc_enable() { $@ } +svc_reload() { + local system="bashtard/svc/reload" + + local service=$1 + + case "${BASHTARD_PLATFORM[key]}" in + freebsd) set -- service "$service" reload ;; + linux-gentoo) set -- /sbin/rc-service "$service" reload ;; + linux-*) set -- systemctl reload "$service" ;; + *) + crit "$system" "No service manager configured for ${BASHTARD_PLATFORM[key]}" + return 1 + esac + + notice "$system" "$*" + $@ +} + svc_restart() { local system="bashtard/svc/restart" @@ -303,9 +355,27 @@ svc_start() { $@ } -template() +svc_stop() { + local system="bashtard/svc/stop" + + local service=$1 + + case "${BASHTARD_PLATFORM[key]}" in + freebsd) set -- service "$service" stop ;; + linux-gentoo) set -- /sbin/rc-service "$service" stop ;; + linux-*) set -- systemctl stop "$service" ;; + *) + crit "$system" "No service manager configured for ${BASHTARD_PLATFORM[key]}" + return 1 + esac + + notice "$system" "$*" + $@ +} + +file_template() { - local file="$BASEDIR/etc/templates/$1" ; shift + local file="$BASHTARD_ETCDIR/playbooks.d/$BASHTARD_PLAYBOOK/share/$1" ; shift local sedfile="$(tmpfile)" if [[ ! -f $file ]] -- cgit v1.1