Skip to content

Instantly share code, notes, and snippets.

@kaitwalla
Last active September 10, 2021 20:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kaitwalla/2c8e0766a98d2f93eabf to your computer and use it in GitHub Desktop.
Save kaitwalla/2c8e0766a98d2f93eabf to your computer and use it in GitHub Desktop.
Make Vagrant Great Again, or: Down Is the Frigging Antonym of Up, and Other Useful Things.

Better Vagrant Commmands

The purpose of this shell command is to make the vagrant commands

  1. slightly easier to use if you're not already in the Vagrant directory
  2. parallel

(Not gonna lie, 2 was actually the bigger motivator. Can't tell you how many times I typed vagrant down to have nothing happen.)

The file is structured a little oddly because it's actually part of another, larger command used for deployments and the like. If there are any bugs, let me know, as I probably didn't parse it out very carefully.

Installation

  1. Copy the script somehwere.
  2. Set an alias in your .bash_profile or .bash_rc or w/e. Be sure to include a period before the path to the file, else dir will not work. Example:

alias v=. ~/.scripts/vagrant.sh

Usage:

I have mine aliased as "v". Thus, if I'm in the folder of a box,

v up
v down
v reup

is the same as

vagrant up
vagrant halt
vagrant reload

is the same as

v boot
v deboot
v reboot

and you can use any of those that you like, mixing and matching. You can also use v halt and v reload if you like installing scripts for no reason (or just want to use the aliasing for boxes).

But those are the basics. Let's say you just opened the terminal and want to shutdown the box that's running at /users/you/sites/awesomesite. You can just

v down awesomesite

Want to bring up /users/you/awesomesite2? Just

v up awesomesite2

(Reboot works the same way.)

Oh, and if you want to go the folder where a box is located, just

v dir awesomesite

(I guess you can do that from within the folder without the name, but ... congrats you did nothing?)

status just gets passed, so v status awesomesite gets you the information for awesomesite.

All done for the day? Better shut them all down!

v down all

(Note: If you have a box in a folder called "all," you won't be able to shut it down this way. You will shut down all boxes. Sorry not sorry.)

If for some reason you want ALL OF THE BOXES TO LOAD?

v boot all

"All" works for the uping and downing commands.

You can also v destroy and v kill, either inside the folder (no name) or with the box name. You cannot "all" those commands, because I would definitely accidentally destroy all my boxes every other week.

Also v status all is vagrant global-status.

If you have very generic box or folder titles, it might affect all boxes that match the text you type in. So, y'know, use your brain. For exmaple, v down awesomesite might take down both sites in the previous examples. Not sure. I'd look at that, but I just name stuff more uniquely, so it's not a problem for me.

# 1.0.2
COMMANDS=('v dir [box_folder]' 'v boot [box_folder]' 'v up [box_folder]' 'v reboot [box_folder]' 'v reup [box_folder]' 'v deboot [box_folder]' 'v down [box_folder]' 'v ssh [box_folder]' 'v update [box_folder]' 'v status')
function usage_error {
echo -e "Usage: v [command] [parameters]"
echo -e "Valid commands:"
for ((i=0;i<${#COMMANDS[@]};i++)); do
echo -e ${COMMANDS[${i}]}
done
}
if [ -z "$1" ]; then
usage_error
exit
fi
function find_box {
box_id=$(vagrant global-status | perl -nle "print if m{(\S*)\s*\S*\s\S*\s\S*\s\S*$1 }" | grep -o '^\S*')
if [ -z "$box_id" ]; then
echo -e "No box found for that folder. Please check and try again."
exit
fi
}
function vagrant_status {
if [ -z "$1" ]; then
vagrant status
exit
fi
if [ "$1" = "all" ]; then
vagrant global-status
else
find_box $1
vagrant status $box_id
fi
}
function vagrant_reload {
if [ -z "$1" ]; then
vagrant reload
exit
fi
if [ "$1" = "all" ]; then
find_box "running"
else
find_box $1
fi
vagrant reload $box_id
}
function vagrant_up {
if [ -z "$1" ]; then
vagrant up
exit
fi
if [ "$1" = "all" ]; then
find_box "running"
else
find_box $1
fi
vagrant up $box_id
}
function vagrant_down {
if [ -z "$1" ]; then
vagrant halt
exit
fi
if [ "$1" = "all" ]; then
find_box "running"
else
find_box $1
fi
vagrant halt $box_id
}
function vagrant_update {
if [ -z "$2" ]; then
vagrant box update
exit
fi
if [ "$1" = "all" ]; then
find_box "running"
else
find_box $1
fi
vagrant box update $box_id
}
function vagrant_destroy {
if [ -z "$2" ]; then
vagrant destroy
exit
fi
# no all for this one, that seems like a baaad idea
find_box $1
vagrant destroy $box_id
}
case "$1" in
status)
vagrant_status $2
;;
global-status) vagrant global-status
;;
dir)
if [ -z "$2" ]; then
exit
fi
dir=$(vagrant global-status | perl -nle "print if m{\S*\s*\S*\s\S*\s\S*\s\S*$2 }" | grep -o "\S*$2")
eval "cd $dir"
;;
ssh)
if [ -z "$2" ]; then
vagrant ssh
exit
fi
find_box $2
vagrant ssh $box_id
;;
reload)
#default command
vagrant_reload $2
;;
reup)
vagrant_reload $2
;;
reboot)
vagrant_reload $2
;;
up)
vagrant_up $2
;;
boot)
vagrant_up $2
;;
down)
vagrant_down $2
;;
deboot)
vagrant_down $2
;;
update)
vagrant_update $2
;;
destroy)
vagrant_destroy $2
;;
kill)
vagrant_destroy $2
;;
*) usage_error
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment