From 72288bdbcf7c3da181f8868a2dbf45c03e7a73a1 Mon Sep 17 00:00:00 2001 From: Sam Al-Sapti Date: Wed, 26 Apr 2023 19:34:53 +0200 Subject: [PATCH] Add option do bring services down and to dry-run provision.sh --- provision.sh | 57 +++++++++++++++++++++++++++++++++++----------------- 1 file changed, 39 insertions(+), 18 deletions(-) diff --git a/provision.sh b/provision.sh index 39c9a42..539fc2e 100755 --- a/provision.sh +++ b/provision.sh @@ -5,11 +5,11 @@ set -e usage() { printf '%s\n' "Usage:" printf '$ %s\n' "$0 [-h|--help]" - printf '$ %s\n' "$0 os" - printf '$ %s\n' "$0 docker" - printf '$ %s\n' "$0 users [-i|--init]" - printf '$ %s\n' "$0 reboot [-f|--force]" - printf '$ %s\n' "$0 services [SINGLE_SERVICE]" + printf '$ %s\n' "$0 [--dry] os" + printf '$ %s\n' "$0 [--dry] docker" + printf '$ %s\n' "$0 [--dry] users [-i|--init]" + printf '$ %s\n' "$0 [--dry] reboot [-f|--force]" + printf '$ %s\n' "$0 [--dry] services [-d|--down] [SINGLE_SERVICE]" } install_modules() { @@ -19,36 +19,57 @@ install_modules() { } cd "$(dirname "$0")" || exit 255 -BASE_CMD="ansible-playbook playbook.yml --ask-vault-pass --ask-become-pass" +if [ "$1" = "--dry" ]; then + EXEC="echo" + shift +else + EXEC="eval" +fi -case $1 in +BASE_CMD="ansible-playbook playbook.yml --ask-vault-pass --ask-become-pass" +TAG="$1" +shift + +case $TAG in "") install_modules; $BASE_CMD ;; os|docker) - install_modules; $BASE_CMD --tags "$1" ;; + install_modules; $BASE_CMD --tags "$TAG" ;; users) install_modules - if [ "$2" = "-i" ] || [ "$2" = "--init" ]; then - $BASE_CMD --user root --tags "$1" + + if [ "$1" = "-i" ] || [ "$1" = "--init" ]; then + $EXEC "$BASE_CMD --user root --tags '$TAG'" else - $BASE_CMD --tags "$1" + $EXEC "$BASE_CMD --tags '$TAG'" fi ;; reboot) install_modules - if [ "$2" = "-f" ] || [ "$2" = "--force" ]; then - $BASE_CMD --tags "$1" --extra-vars "force_reboot=true" + + if [ "$1" = "-f" ] || [ "$1" = "--force" ]; then + $EXEC "$BASE_CMD --tags '$TAG' --extra-vars 'force_reboot=true'" else - $BASE_CMD --tags "$1" --extra-vars "reboot=true" + $EXEC "$BASE_CMD --tags '$TAG' --extra-vars 'reboot=true'" fi ;; services) install_modules - if [ -z "$2" ]; then - $BASE_CMD --tags "$1" - else - $BASE_CMD --tags "$1" --extra-vars "single_service=$2" + + if [ "$1" = "-d" ] || [ "$1" = "--down" ]; then + DOWN=1 + shift fi + + if [ -z "$DOWN" ] && [ -n "$1" ]; then + VARS="single_service=$1" + elif [ -n "$DOWN" ] && [ -z "$1" ]; then + VARS="stop=true" + elif [ -n "$DOWN" ] && [ -n "$1" ]; then + VARS='{"stop": true, "single_service": "'$1'"}' + fi + + $EXEC "$BASE_CMD --tags '$TAG' $(test -z "$VARS" || echo "--extra-vars '$VARS'")" ;; -h|--help) usage ;;