Last active
February 1, 2018 13:58
-
-
Save bmcbm/a593abbe76cc0e1b6809 to your computer and use it in GitHub Desktop.
Add scp and snapshot (ie. shorthand for vboxmanage snapshot) to vagrant command
This file contains hidden or 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 | |
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 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
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:
To restore the initial-snapshot you could do:
But who can remember this command anyway?
The shorthand makes it easier:
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.