Skip to content

Instantly share code, notes, and snippets.

@samukasmk
Last active August 29, 2015 14:13
Show Gist options
  • Save samukasmk/1caf8c311d93f16574e3 to your computer and use it in GitHub Desktop.
Save samukasmk/1caf8c311d93f16574e3 to your computer and use it in GitHub Desktop.
Alias for vagrant commands (like: git alias)
#!/bin/bash
#
# vagrant_aliases.sh
# By: Samuel Maciel Sampaio <[email protected]>
#
# Installation instructions:
#
# 1.) put this file in folder /usr/bin with name 'v', eg: '/usr/bin/v'
#
# 2.) to exec vagrant snap or vagrant sandbox please install this plugins:
# $ vagrant plugin install sahara
# $ vagrant plugin install vagrant-multiprovider-snap
function vagrant_alias() {
case $1 in
### options for (default vagrant)
's'|'ss' ) vagrant_ssh_customized ${@:2};;
'u' ) vagrant up "${@:2}" ;;
'h' ) confirm_before "vagrant halt ${@:2}" "halt this machines $2";;
'r'|'rl' ) vagrant reload "${@:2}" ;;
'rs' ) vagrant resume "${@:2}" ;;
'sus' ) confirm_before "vagrant suspend ${@:2}" "suspend this machines $2";;
'd' ) confirm_before "vagrant destroy --force ${@:2}" "destroy this machines $2" "rm -fr .vagrant/machines/$2";;
'p'|'pr' ) vagrant provision "${@:2}" ;;
'st' ) vagrant status "${@:2}" ;;
'b' ) vagrant box "${@:2}" ;;
'i' ) vagrant init "${@:2}" ;;
'l' ) vagrant login "${@:2}" ;;
'pk' ) vagrant package "${@:2}" ;;
'pl' ) vagrant plugin "${@:2}" ;;
### options for (sandbox) plugin
'sd' ) vagrant sandbox "${@:2}" ;;
# create some specific state of a machine and it will be able to easily
# restore with 'vagrant sandbox rollback' or 'vagrant snap rollback'
'sdo'|'sd'|'save'|'sa' ) vagrant sandbox on "${@:2}" ;;
# save new state of a machine created before with command 'vagrant sandbox on'
'sdc' ) vagrant sandbox commit "${@:2}" ;;
### options for (vagrant-multiprovider-snap) plugin
'sn' ) vagrant snap "${@:2}" ;;
# to salve the specific state of machine an can restore after
'snt' ) vagrant snap take "${@:2}" ;;
# to restore the specific state of machine saved before
'sdr'|'snr' ) confirm_before "vagrant snap rollback ${@:2}" "rollback to snapshot $2";;
# to list the specific state of machines saved
'snl' ) vagrant snap list "${@:2}" ;;
### options for (internal uses)
'c'|'console') console_term $2;;
'q'|'quit' ) echo "skipping command..."; exit;;
'm'|'machine') console_term $2;;
'cl'|'clean'|'clear') reset;;
'cd') go_cwd "${@:2}";;
'pwd') echo $new_cwd;;
### other arguments passed
* ) vagrant "$@" ;;
esac
}
function go_cwd() {
new_cwd="$1"
if [ -n "$new_cwd" ]; then
echo "new cwd: $new_cwd"
cwd_arg="cd $new_cwd;"
fi
}
# Customized alias
function vagrant_ssh_customized() {
if [ "$console_mode" != "on" ]; then
local ssh_host=$1
local ssh_extra_args=${@:2}
else
local ssh_host=$machine_name
local ssh_extra_args=$(echo ${@:1} | sed s/'\w*$'//)
fi
if [ -n "$ssh_extra_args" -a -n "$new_cwd" ]; then
ssh_arg="sudo su -c \"$cwd_arg bash -c '$ssh_extra_args'\""
elif [ -n "$ssh_extra_args" -a -z "$new_cwd" ]; then
ssh_arg="sudo su -c \"$ssh_extra_args\""
elif [ -n "$new_cwd" ]; then
ssh_arg="sudo su -c '$cwd_arg bash'"
else
ssh_arg="sudo su"
fi
vagrant ssh -c "$ssh_arg" $ssh_host
}
function confirm_before() {
local action=$1
local message=$2
local post_action=$3
echo -n " Are you sure you want to $message? [y/N] ";
read ask_opt;
if [ "$ask_opt" == "y" ]; then
$action;
$post_action;
else
echo " skipping action $message by user..."
fi
}
function console_term() {
machine_name=$1
console_mode="on"
while :;
do
if [ -n "$machine_name" ]; then
echo -n "($machine_name) type vagrant command> "
else
echo -n "type vagrant command> "
fi
read entry_cmd
if [ -n "$entry_cmd" ]; then
vagrant_alias $entry_cmd $machine_name
fi
done
}
vagrant_alias $@
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment