Skip to content

Instantly share code, notes, and snippets.

@westonruter
Last active April 2, 2019 20:18
Show Gist options
  • Save westonruter/ac0f964716d95cdee301880f1865ba30 to your computer and use it in GitHub Desktop.
Save westonruter/ac0f964716d95cdee301880f1865ba30 to your computer and use it in GitHub Desktop.
vvv-ssh: Seamlessly and quickly connect to VVV over SSH

vvv-ssh

Opens SSH connections to VVV with initial working directory corresponding to current synced directory on host machine. Wraps commands to Vagrant machine wp, phpunit, grunt, and npm. Supersedes the vassh project. Also speeds up SSH connection time by re-using the cached result of vagrant ssh-config.

Author: Weston Ruter (@westonruter), XWP
License: GPLv2

Usage

Important: This currently depends on VVV PR #1311 to be merged or checked out.

Clone repo to system in any directory.

$ git clone https://gist.github.com/ac0f964716d95cdee301880f1865ba30.git vvv-ssh-gist

Include repo on path when starting work on a VVV project in a given Bash session:

$ export PATH=~/vvv-ssh-gist:$PATH

You'll probably want to add a Bash function to your .bashrc that you invoke whenever you're going to start working with VVV. For example:

function workon_vvv {
	export PATH="~/vvv-ssh-gist:$PATH"
}

Change to any directory in a synced folder on your host machine.

$ cd www/wordpress-develop/public_html/src/wp-content/plugins

Start interactive session with initial working directory matching host machine:

$ vvv-ssh
> Last login: Sat Oct 21 20:56:05 2017 from 10.0.2.2
$ pwd
> /srv/www/wordpress-develop/public_html/src/wp-content/plugins

Start interactive TTY session over SSH connection to monitor VM performance.

$ vvv-ssh top

Connect to WP-CLI as if was on your host machine (shortcut for vvv-ssh wp post list).

$ wp post list

Pipe output into WP-CLI on the VM:

$ cat export.sql | wp db import -

Run PHPUnit in the VM.

$ phpunit
#!/bin/bash
$( dirname $0 )/vvv-ssh $( basename $0 ) $@
forward-vvv-ssh-command.sh
forward-vvv-ssh-command.sh
forward-vvv-ssh-command.sh
#!/bin/bash
# vvv-ssh
# Open SSH connections to VVV with intial working directory corresponding to
# current synced directory on host machine. Proxy commands to Vagrant machine
# with aliases for wp, phpunit, grunt, and npm. Supersedes the 'vassh' project.
# Author: Weston Ruter, XWP
# URL: https://gist.github.com/westonruter/ac0f964716d95cdee301880f1865ba30
# Depends on https://github.com/Varying-Vagrant-Vagrants/VVV/pull/1311
#
# USAGE:
#
# Clone repo to system in any directory.
# $ git clone https://gist.github.com/ac0f964716d95cdee301880f1865ba30.git vvv-ssh-gist
#
# Include repo on path when starting work on a VVV project in a given Bash session.
# $ export PATH="$( pwd )/vvv-ssh-gist:$PATH"
#
# Change to any directory in a synced folder on your host machine.
# $ cd www/wordpress-develop/public_html/src/wp-content/plugins
#
# # Start interactive session with initial working directory matching host machine.
# $ vvv-ssh
# $ pwd
# > /srv/www/wordpress-develop/public_html/src/wp-content/plugins
#
# Start interactive TTY session over SSH connection to monitor VM performance.
# $ vvv-ssh top
#
# Connect to WP-CLI as if was on your host machine (shortcut for vvv-ssh wp post list).
# $ wp post list
#
# Run PHPUnit in the VM.
# $ phpunit
set -e
vvv_root=''
cwd=$( pwd )
while [ 1 ]; do
if [ -e Vagrantfile ]; then
vvv_root="$( pwd )"
break
else
cd ..
if [[ "$OLDPWD" == "$PWD" ]]; then
break
fi
fi
done
cd "$cwd"
if [ -z "$vvv_root" ]; then
echo "Unable to find Vagrant root"
exit 1
fi
config_dir="$vvv_root/config"
if [[ ! -e "$config_dir" ]]; then
echo "Unable to find vvv config dir"
exit 2
fi
vagrant_path=$( sed "s#^$vvv_root#/srv#" <<< "$cwd" )
if [[ "$vagrant_path" == "$( pwd )" ]]; then
echo "Unable to determine vagrant path"
exit 1
fi
echo "$vagrant_path" > "$config_dir/custom/initial_cwd_once"
if [ ! -e /tmp/vvv-ssh-config ]; then
VVV_SKIP_LOGO=1 vagrant ssh-config > /tmp/vvv-ssh-config
fi
if [[ 0 == "$#" ]] || ( [[ -t 0 ]] && [ -t 1 ] ); then
tty_arg='-t'
else
tty_arg='-T'
fi
ssh -q -F /tmp/vvv-ssh-config $tty_arg default -- $@
forward-vvv-ssh-command.sh
@tomjn
Copy link

tomjn commented Oct 22, 2017

Keep in mind that this will only work with sites configured with the default location, sites that specify the vm_dir and local_dir keys aren't accounted for

@Mte90
Copy link

Mte90 commented Oct 26, 2017

Uhm maybe a parser of the yml file to get the location can avoid every issue for this problem of paths

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