Skip to content

Commit

Permalink
Support vars sourcing, passing raw ssh/docker opts, prerun vars expan…
Browse files Browse the repository at this point in the history
…sion

This commit adds the ability to expand bash variables before spec
execution (default is during execution on the target host) and
optionally source a variable file to use during expansion. This commit
also adds the ability to pass raw SSH/docker options to the commands.
Additional bug fixes include docker container checking and support '--help'
flag.

Closes #2
  • Loading branch information
nextrevision committed Aug 5, 2015
1 parent d99b2f3 commit 1bd023f
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 12 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
*.swp
test/tmp
examples_spec.sh
53 changes: 42 additions & 11 deletions shrift
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@ function _usage() {
echo "usage: $0 [args] specfiles..."
echo
echo "-h Help"
echo "-f [vars_file] Variables file to source before expanding (implies -e)"
echo "-d [container] Target running docker container"
echo "-s [hostname] Target remote host via ssh"
echo "-o '[opts]' Raw CLI options to pass to SSH/Docker (must be in quotes)"
echo "-e Expand variables in spec before running"
echo "-v Enable verbose output"
echo
}
Expand All @@ -20,6 +23,10 @@ function _error() {
exit 1
}

function _mktemp() {
mktemp /tmp/$(basename ${0//-/}).XXX
}

function _print_cmd_summary() {
local rc=$1
local cmd=$2
Expand All @@ -44,8 +51,16 @@ function _print_dot() {
fi
}

function _expand_vars() {
local tmp=$(_mktemp)
eval "echo \"$(cat $1)\"" > $tmp
echo $tmp
}

function _run_file() {
local file=$1
[ $verbose -eq 1 ] && echo "# ${1}"
[ $expand_vars -eq 1 ] && file=$(_expand_vars $1)
# loop through specs
while read spec; do
# skip comments
Expand All @@ -55,8 +70,8 @@ function _run_file() {

# form command string
case $driver in
docker) cmd="docker exec ${target} ${spec}";;
ssh) cmd="ssh -nx ${target} ${spec}";;
docker) cmd="docker ${opts} exec ${target} ${spec}";;
ssh) cmd="ssh ${opts} -nx ${target} ${spec}";;
local) cmd="eval ${spec}";;
esac

Expand All @@ -76,22 +91,29 @@ function _run_file() {
_print_cmd_output $cmd_result
fi
result_summary+=$(_print_dot $cmd_rc)
done < $1
done < $file
[ $expand_vars -eq 1 ] && rm $file
}

function _main() {
driver='local'
opts=''
vars_file=''
expand_vars=0
verbose=0
spec_files=()

# parse cli args
while [ ! -z $1 ]; do
case $1 in
-h) _usage; exit 0;;
-d) shift; driver='docker'; target=$1;;
-s) shift; driver='ssh'; target=$1;;
-v) verbose=1;;
*) spec_files+=("$1");;
-h|--help) _usage; exit 0;;
-f) shift; vars_file=$1; expand_vars=1;;
-d) shift; driver='docker'; target=$1;;
-s) shift; driver='ssh'; target=$1;;
-o) shift; opts="${1}";;
-e) expand_vars=1;;
-v) verbose=1;;
*) spec_files+=("$1");;
esac
shift
done
Expand All @@ -107,16 +129,25 @@ function _main() {
if ! docker info &> /dev/null; then
_error "Cannot connect to docker daemon"
fi
if ! docker ps | grep -q $target; then
if ! docker inspect $target &> /dev/null; then
_error "No such container ${target}"
fi
fi

# source variable file
if [ ! -z $vars_file ]; then
if [ ! -f $vars_file ]; then
_error "No such vars_file ${vars_file}"
else
source ${vars_file}
fi
fi

# global run state
total_specs=0
failed_specs=0
result_summary=""
cmd_result=$(mktemp /tmp/$0.XXX)
cmd_result=$(_mktemp)
exit_status=0

# exec all files
Expand All @@ -142,5 +173,5 @@ function _main() {

# test if script is being called or sourced
if [[ $(basename ${0//-/}) == "shrift" ]]; then
_main $@
_main "$@"
fi
14 changes: 13 additions & 1 deletion shrift_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,18 @@ assert "_usage | head -n1 | cut -d ' ' -f1" "usage:"
# test _error
assert "_error test" "${RED}ERROR${END}: test"

# test _mktemp
t=$(_mktemp)
assert_raises "test -f ${t}" 0
rm $t

# test _print_cmd_summary
assert "_print_cmd_summary 0 'good'" "${GREEN}Success 0${END}: good"
assert "_print_cmd_summary 1 'bad'" "${RED}Failure 1${END}: bad"
assert "_print_cmd_summary 255 'also bad'" "${RED}Failure 255${END}: also bad"

# test _print_cmd_output
t=$(mktemp test/tmp.XXX)
t=$(_mktemp)
echo -e "command not found: banana" > $t
assert "_print_cmd_output $t" " command not found: banana\n"
echo -e "command not found: barnana" >> $t
Expand All @@ -27,6 +32,12 @@ assert "_print_dot 0" "${GREEN}.${END}"
assert "_print_dot 1" "${RED}F${END}"
assert "_print_dot 255" "${RED}F${END}"

# test _expand_vars
t=$(_expand_vars test/fixtures/vars_test)
assert_raises "test -f ${t}"
assert "cat ${t}" "echo ${USER}"
rm $t

# test _main output
assert "_main test/fixtures/pass_spec.sh" "\n${GREEN}.${END}${GREEN}.${END}\n2 tests, 0 failed"
assert "_main test/fixtures/fail_spec.sh" "${RED}Failure 1${END}: test -f notpresent\n\n${RED}F${END}\n1 tests, 1 failed"
Expand All @@ -43,6 +54,7 @@ assert_raises "./shrift -h | grep -q 'usage: '" 0
assert "./shrift test/fixtures/pass_spec.sh" "\n${GREEN}.${END}${GREEN}.${END}\n2 tests, 0 failed\n"
assert "./shrift test/fixtures/fail_spec.sh" "${RED}Failure 1${END}: test -f notpresent\n\n${RED}F${END}\n1 tests, 1 failed\n"
assert "./shrift -v test/fixtures/pass_spec.sh" "# test/fixtures/pass_spec.sh\n${GREEN}Success 0${END}: test -f ./shrift\n${GREEN}Success 0${END}: test -f ./shrift_test.sh\n\n${GREEN}.${END}${GREEN}.${END}\n2 tests, 0 failed\n"
assert "./shrift -v -f test/fixtures/vars_file test/fixtures/vars_test" "# test/fixtures/vars_test\n${GREEN}Success 0${END}: echo notarealuser\n notarealuser\n\n${GREEN}.${END}\n1 tests, 0 failed"

# test runtime return code
assert_raises "./shrift test/fixtures/pass_spec.sh" 0
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/vars_file
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
USER=notarealuser
1 change: 1 addition & 0 deletions test/fixtures/vars_test
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
echo ${USER}

0 comments on commit 1bd023f

Please sign in to comment.