Skip to content

Instantly share code, notes, and snippets.

@djfdyuruiry
Created June 17, 2018 16:25
Show Gist options
  • Save djfdyuruiry/9ee3956f3c858d05cefaf5e0f1e2f99a to your computer and use it in GitHub Desktop.
Save djfdyuruiry/9ee3956f3c858d05cefaf5e0f1e2f99a to your computer and use it in GitHub Desktop.
Bash function 'library' which contains handy vagrant utils, including provisioning, suspending and resuming multiple machines in parallel.
#!/usr/bin/env bash
if [ ! `command -v "vagrant"` ]; then
>&2 echo "ERROR: Script $0 requires vagrant, it was not found in the path"
exit 1
fi
vagrantProvider=${VAGRANT_PROVIDER:-virtualbox}
providerAwkString="[${vagrantProvider:0:1}]${vagrantProvider:1}"
vagrant-status() {
vagrant global-status --prune 2> /dev/null | \
awk "/${providerAwkString}/ {print \$2\" \"\$4\" \"\$1}"
}
# provision a machine, if up, otherwise up it and provision
vagrant-provision-machine() {
machine=$1
if [ -z "${VAGRANT_DEFAULT_DIRECTORY}" ]; then
>&2 echo "ERROR: Please define the VAGRANT_DEFAULT_DIRECTORY env var before running $0"
exit 1
fi
currentDir="$(pwd)"
cd "${VAGRANT_DEFAULT_DIRECTORY}"
machineIsUp=true
vagrant-status | grep ${machine} || {
machineIsUp=false
}
if [ "${machineIsUp}" = true ]; then
vagrant provision ${machine}
else
vagrant up ${machine} --provision
fi
cd "${currentDir}"
}
# provision a comma/space seperated list of machines in parallel
vagrant-provision() {
IFS=', ' read -r -a machines <<< "$1"
if [ -z "${VAGRANT_DEFAULT_DIRECTORY}" ]; then
>&2 echo "ERROR: Please define the VAGRANT_DEFAULT_DIRECTORY env var before running $0"
exit 1
fi
for machine in "${machines[@]}"; do
vagrant-provision-machine ${machine} &
done
wait
}
vagrant-query() {
query="$1"
vagrant-status | grep "${query}"
}
vagrant-query-for-ids() {
query="$1"
vagrant-query "${query}" | cut -d' ' -f3
}
# suspend all machines in parallel
vagrant-supend() {
IFS=$'\r\n'; machinesToHibernate=($(vagrant-query-for-ids "running"))
for machine in "${machinesToHibernate[@]}"; do
vagrant suspend ${machine} &
done
wait
}
# resume/power on all machines in parallel
vagrant-resume() {
IFS=$'\r\n';machinesToResume=($(vagrant-query-for-ids "saved"))
for machine in "${machinesToResume[@]}"; do
vagrant resume ${machine} &
done
IFS=$'\r\n';machinesToBoot=($(vagrant-query-for-ids "poweroff"))
for machine in "${machinesToBoot[@]}"; do
vagrant up ${machine} --no-provision &
done
wait
}
# destroy a machines then provision it again
vagrant-reset() {
machine=$1
machineToReset=$($(vagrant-query-for-ids ${machine}))
if [ ! -z "${machineToReset}" ]; then
vagrant destroy ${machineToReset} -f
fi
vagrant-provision ${machine}
}
vagrant-ssh() {
machine=$1
command="$2"
if [ -z "${VAGRANT_DEFAULT_DIRECTORY}" ]; then
>&2 echo "ERROR: Please define the VAGRANT_DEFAULT_DIRECTORY env var before running $0"
return
fi
currentDir="$(pwd)"
cd "${VAGRANT_DEFAULT_DIRECTORY}"
if [ -z "${command}" ]; then
vagrant ssh ${machine}
else
vagrant ssh ${machine} -c "${command}"
fi
cd "${currentDir}"
}
# used for a repetitive chore that you run often on machines
vagrant-chore() {
machine=$1
if [ -z "${VAGRANT_CHORE_COMMAND}" ]; then
>&2 echo "ERROR: Please define the VAGRANT_CHORE_COMMAND env var before running $0"
return
fi
machineToRunChoreOn=$(vagrant-query-for-ids ${machine})
if [ ! -z "${machineToRunChoreOn}" ]; then
vagrant-ssh ${machineToRunChoreOn} "${VAGRANT_CHORE_COMMAND}"
else
>&2 echo "ERROR: Machine ${machine} does not exist"
return
fi
}
# destroy all machines vagrant is aware of in parallel
vagrant-nuke() {
IFS=$'\r\n';machinesToDestroy=($(vagrant-query | cut -d' ' -f3))
for machine in "${machinesToDestroy[@]}"; do
vagrant destroy ${machine} -f &
done
wait
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment