Created
October 19, 2018 12:52
-
-
Save NFarrington/7d2b8842e18480c55851638ac499e9ad to your computer and use it in GitHub Desktop.
Runs any Vagrant command against multiple Vagrant boxes in parallel.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
### DEFAULT ARGUMENTS ### | |
ARGS=() | |
VAGRANT_ARGS=() | |
DEBUG=false | |
USAGE=" | |
Usage: $0 [OPTIONS] [VAGRANT_COMMAND] [VAGRANT_BOX]... | |
Runs VAGRANT_COMMAND against all specified boxes in parallel. | |
Options: | |
-d|-debug Run the script in debug mode. | |
-h|--help Print this help dialogue. | |
Vagrant options: | |
--varg[-|--][ARG] [VALUE] Use the specified argument-value pair when | |
running the vagrant command. | |
--vflag[-|--][FLAG] Use the specified flag when running the vagrant | |
command. | |
Examples: | |
vagrant ssh -c 'sudo puppet agent -t' box1 box2 | |
$0 ssh --varg-c 'sudo puppet agent -t' box1 box2 | |
vagrant up --provision box1 box2 | |
$0 up --vflag--provision box1 box2 | |
vagrant destroy -f box1 box2 | |
$0 destroy --vflag-f box1 box2 | |
" | |
function getArgs() { | |
while [[ $# -gt 0 ]]; do | |
if [[ "$1" == --* ]]; then | |
setOption ${1:2:${#1}} "$2" || shift | |
elif [[ "$1" == -* ]]; then | |
for (( i=1; i<${#1}; i++ )); do | |
setOption ${1:$i:1} | |
done | |
else | |
ARGS+=("$1") | |
fi | |
shift | |
done | |
} | |
function setOption() { | |
case "$1" in | |
d|debug) | |
set -x | |
DEBUG=true | |
;; | |
h|help) | |
echo "$USAGE" | |
exit | |
;; | |
varg-*) | |
VAGRANT_ARGS+=("${1:4:${#1}}") | |
VAGRANT_ARGS+=("$2") | |
return 1 # indicates $2 was also used | |
;; | |
vflag-*) | |
VAGRANT_ARGS+=("${1:5:${#1}}") | |
;; | |
*) | |
# unknown option | |
error "$USAGE" | |
fatal "Argument '$1' does not exist." | |
;; | |
esac | |
} | |
function error() { | |
echo -e "$1" >&2 | |
} | |
function fatal() { | |
error "FATAL: $1 Exiting..." | |
exit 1 | |
} | |
getArgs "$@" | |
PIDS=() | |
for box in ${ARGS[@]:1}; do | |
( vagrant "${ARGS[0]}" "${VAGRANT_ARGS[@]}" "$box" \ | |
> >(awk "{print \"[$box] \" \$0}") \ | |
2> >(awk "{print \"[$box] \" \$0 > \"/dev/stderr\"}") | |
) & PIDS+=($!) | |
done | |
trap '' INT TERM # ensure signals are passed to sub-processes | |
wait "${PIDS[@]}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment