Skip to content

Instantly share code, notes, and snippets.

@bmcbm
Last active February 1, 2018 13:58
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 bmcbm/a593abbe76cc0e1b6809 to your computer and use it in GitHub Desktop.
Save bmcbm/a593abbe76cc0e1b6809 to your computer and use it in GitHub Desktop.
Add scp and snapshot (ie. shorthand for vboxmanage snapshot) to vagrant command
#!/bin/bash
function vagrant() {
if [ -x /usr/bin/vagrant ] ; then
if [ "$1" = 'scp' ]; then
# Inspired by https://gist.github.com/colindean/5213685
TMPFILE=$(mktemp)
HOST=$( echo "${@: -1}" | cut -d: -f1)
/usr/bin/vagrant ssh-config $HOST > $TMPFILE
if [ $? -eq 0 ] ; then
scp -F $TMPFILE ${*:2}
if [ $? -ne 0 ] ; then
echo "Transfer failed. Available vagrant hosts: "
grep -E "^Host " $TMPFILE | awk '{print $2}'
fi
fi
rm $TMPFILE
elif [ "$1" = 'snapshot' ]; then
# Usage: vagrant snapshot [vagrant-machine] command [commandargs]
# for command and command args see "vboxmanage snapshot help"
machine=default
args=${*:2}
if ! [[ "$2" =~ ^(take|delete|restore(current)?|edit|list|showvminfo)$ ]] ; then
machine=${*:2:1}
args=${*:3}
fi
virtualbox_id=`cat .vagrant/machines/${machine}/virtualbox/id`
vboxmanage snapshot $virtualbox_id $args
else
/usr/bin/vagrant $@
fi
else
echo "Vagrant appears to be missing?"
fi
}
@bmcbm
Copy link
Author

bmcbm commented Nov 4, 2015

Working with vagrant I always miss a vagrant scp command.

Sure you can just copy the files to the directory holding the vagrant image on the host machine and access them through /vagrant folder in the virtual machine, but when you work alot with unix shell it just often feels like "vagrant scp" is missing.

So above is a way to expand the vagrant command to expand the vagrant command with scp functionality.

vagrant scp /path/to/file/on/host default:/path/on/virtualmachine/

Also I work a lot with snapshots, which is a little cumbersome, so I also added a vagrant snapshot command alias to ease the pain.

Normally to take a snapshot you would use a command like:

vboxmanage snapshot `cat .vagrant/machines/default/virtualbox/id` take initial-snapshot

To restore the initial-snapshot you could do:

vboxmanage snapshot `cat .vagrant/machines/default/virtualbox/id` restore initial-snapshot

But who can remember this command anyway?

The shorthand makes it easier:

vagrant snapshot [vagrant-machine] take name-of-snapshot
vagrant snapshot [vagrant-machine] restore name-of-snapshot

My use case for this is testing puppet manifests. When I create a new vagrant vm I make sure it is updated (using mainly Ubuntu I run apt-get update and apt-get upgrade) and provisioned the way I like a new vm to be. This is somewhat time consuming if you have to do it often.

A snapshot of the machine ensures that I can quickly restore it to the initial provisioned state and then do the puppet runs again until they succeed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment