From a26270286c7e483984683e4b1338c3d885c6cc22 Mon Sep 17 00:00:00 2001 From: songtingxu-okta Date: Tue, 4 Feb 2020 13:09:03 -0800 Subject: [PATCH] refactor lansible and print more info (#26) --- .travis.yml | 2 +- cmd/lansible | 16 ++++++++++++---- lib/ansible.sh | 6 +++--- test/ci/run | 2 +- 4 files changed, 17 insertions(+), 9 deletions(-) diff --git a/.travis.yml b/.travis.yml index cbc4ac2..6a752dc 100644 --- a/.travis.yml +++ b/.travis.yml @@ -39,7 +39,7 @@ jobs: before_install: test/ci/before_install -install: false +install: skip script: test/ci/run diff --git a/cmd/lansible b/cmd/lansible index 3dbba1b..f945ef0 100755 --- a/cmd/lansible +++ b/cmd/lansible @@ -1,6 +1,6 @@ #!/usr/bin/env bash -# Summary: Runs strap to ensure your machine is fully configured -# Usage: strap lansible +# Summary: Runs strap and ansible playbook/role(s) to ensure your machine is fully configured +# Usage: strap lansible [--role|--with-role ] [--playbook|--with-playbook ] set -Eeuo pipefail # https://vaneyckt.io/posts/safer_bash_scripts_with_set_euxo_pipefail/ @@ -38,6 +38,7 @@ STRAP_QUIET_FLAG="-q" Q="$STRAP_QUIET_FLAG" STRAP_ISSUES_URL="https://github.com/ultimatedotfiles/strap/issues/new" +STRAP_LANSIBLE_DEFAULT_PLAYBOOK=".strap/ansible/playbooks/default/main.yml" function array_contains() { local -r arr="${1:-}" value="${2:-}" @@ -122,11 +123,18 @@ EOF done } +function argument_check() { + if ! [[ "$@" == *--role* || "$@" == *--with-role* || "$@" == *--playbook* || "$@" == *--with-playbook* ]] && ! [[ -f "${STRAP_WORKING_DIR}/${STRAP_LANSIBLE_DEFAULT_PLAYBOOK}" ]]; then + strap::abort "Unknown options $@ for strap lansible or no default ansible playbook ${STRAP_LANSIBLE_DEFAULT_PLAYBOOK} is found under current working directory." + fi +} + function main() { # Trap any exit call: trap cleanup SIGINT SIGTERM EXIT + argument_check "$@" [[ "$STRAP_USER" == "root" && "$STRAP_ROOT_ALLOWED" != true ]] && strap::abort "Run 'strap lansible' as yourself, not root." if [[ "$STRAP_OS" == 'mac' ]] && ! groups | grep $Q admin; then strap::abort "Add $STRAP_USER to the admin group."; fi @@ -314,9 +322,9 @@ EOF elif [[ "$@" == *--playbook* ]] || [[ "$@" == *--with-playbook* ]]; then strap::bot "Run Ansible Playbook" strap::ansible::playbook::run "$@" - elif [[ -d "${STRAP_WORKING_DIR}/.strap/ansible/playbooks/default" ]]; then + elif [[ -f "${STRAP_WORKING_DIR}/${STRAP_LANSIBLE_DEFAULT_PLAYBOOK}" ]]; then strap::bot "Run Ansible Playbook" - strap::ansible::playbook::run --with-playbook="${STRAP_WORKING_DIR}/.strap/ansible/playbooks/default" "$@" + strap::ansible::playbook::run --with-playbook="${STRAP_WORKING_DIR}/${STRAP_LANSIBLE_DEFAULT_PLAYBOOK}" "$@" fi # make config/state a little more secure, just in case: diff --git a/lib/ansible.sh b/lib/ansible.sh index aeccd0a..70fc9f2 100644 --- a/lib/ansible.sh +++ b/lib/ansible.sh @@ -287,8 +287,8 @@ strap::ansible::playbook::run() { [[ "$1" == --*=* ]] && set -- "${1%%=*}" "${1#*=}" "${@:2}" # normalize `--foo=bar` into `--foo bar` case "$1" in --playbook|--with-playbook) - playbook_dir="${2:-${STRAP_WORKING_DIR}/.strap/ansible/playbooks/default}" - [[ -d "${playbook_dir}" ]] || strap::abort "strap lansible: $1 needs to be a directory" + playbook_file="${2}" + [[ -f "${playbook_file}" ]] || strap::abort "strap lansible: ${playbook_file} doesn't exist!" shift 2 ;; -i|--inventory|--inventory-file) @@ -316,7 +316,7 @@ strap::ansible::playbook::run() { set -- "${params[@]}" # reset positional arguments fi - playbook_file="${playbook_dir}/main.yml" + playbook_dir="$(dirname $playbook_file)" requirements_file="${playbook_dir}/meta/requirements.yml" [[ -f "${requirements_file}" ]] || requirements_file="${playbook_dir}/requirements.yml" diff --git a/test/ci/run b/test/ci/run index 60128e3..5141e59 100755 --- a/test/ci/run +++ b/test/ci/run @@ -36,6 +36,6 @@ function main() { pushd "${TRAVIS_BUILD_DIR}/test" > /dev/null STRAP_INTERACTIVE=false "${TRAVIS_BUILD_DIR}/bin/strap" lansible popd > /dev/null - STRAP_INTERACTIVE=false bin/strap lansible --playbook="${TRAVIS_BUILD_DIR}/test/.strap/ansible/playbooks/default" + STRAP_INTERACTIVE=false bin/strap lansible --playbook="${TRAVIS_BUILD_DIR}/test/.strap/ansible/playbooks/default/main.yml" } main