Skip to content

Commit

Permalink
refactor lansible and print more info (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
songtingxu-okta authored Feb 4, 2020
1 parent 14de12b commit a262702
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ jobs:

before_install: test/ci/before_install

install: false
install: skip

script: test/ci/run

Expand Down
16 changes: 12 additions & 4 deletions cmd/lansible
Original file line number Diff line number Diff line change
@@ -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 <ansible_role>] [--playbook|--with-playbook <ansible_playbook>]

set -Eeuo pipefail # https://vaneyckt.io/posts/safer_bash_scripts_with_set_euxo_pipefail/

Expand Down Expand Up @@ -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:-}"
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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:
Expand Down
6 changes: 3 additions & 3 deletions lib/ansible.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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"

Expand Down
2 changes: 1 addition & 1 deletion test/ci/run
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit a262702

Please sign in to comment.