Skip to content

Instantly share code, notes, and snippets.

@pesterhazy
Created December 22, 2014 13:09
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 pesterhazy/68a0cb7a07a6dee45d3a to your computer and use it in GitHub Desktop.
Save pesterhazy/68a0cb7a07a6dee45d3a to your computer and use it in GitHub Desktop.
in_vagrant: transparently execute command inside vagrant
#!/bin/bash
#
# Syntax:
#
# ./in_vagrant [command] [args]
#
# Example usage:
#
# Set up a Vagrantfile with a config called "foo"
#
# vconfig.vm.define :foo do |config|
#
# cd /your/project/directory
# npm install # use npm installed on the host
# ./in_vagrant npm install # use npm installed in the guest vm
#
# Modify VAGRANT_NAME and VAGRANT_ROOT below to fit your project structure
VAGRANT_NAME=foo
VAGRANT_ROOT=/vagrant
unset a
for arg; do
printf -v temp %q "$arg"
a+=("$temp")
done
ssh_config_cache=".vagrant-ssh.cache"
if [[ -f "$ssh_config_cache" ]] && test `find $ssh_config_cache -mmin +120`; then
rm -f "$ssh_config_cache"
fi
if [[ -f "$ssh_config_cache" ]] && ! grep -q 'User vagrant' $ssh_config_cache; then
rm -f "$ssh_config_cache"
fi
if [[ ! -f "$ssh_config_cache" ]]; then
vagrant ssh-config > "$ssh_config_cache"
fi
ssh -F "$ssh_config_cache" -t -t "$VAGRANT_NAME" cd "$VAGRANT_ROOT" '&&' "${a[@]}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment