Skip to content

Commit

Permalink
Added 'vc' script to run commands in a vagrant box
Browse files Browse the repository at this point in the history
  • Loading branch information
reinout committed Sep 17, 2012
1 parent b1f1a1e commit 8f09f5d
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 0 deletions.
1 change: 1 addition & 0 deletions setup.py
Expand Up @@ -40,6 +40,7 @@
'latestentries = tools.blog:list_todays_entries', 'latestentries = tools.blog:list_todays_entries',
'makedocs = tools.blog:makedocs', 'makedocs = tools.blog:makedocs',
'sommen = tools.sommen:main', 'sommen = tools.sommen:main',
'vc = tools.vagrant:main',
], ],
}, },
) )
36 changes: 36 additions & 0 deletions tools/README.rst
Expand Up @@ -141,3 +141,39 @@ This script removes the holes.


(See `source code on github <https://github.com/reinout/tools/blob/master/tools/thunderbird.py>`_). (See `source code on github <https://github.com/reinout/tools/blob/master/tools/thunderbird.py>`_).




vagrant.py
------------------------------------------------------------------------


Script to run a command via ssh inside vagrant.

What it does: we're inside a directory that we know has been mounted in a
local vagrant box. We ``cd`` to the corresponding directory and run the
command there.

There are quite some assumptions in here, they match the way I (Reinout) has
set it all up:

- Virtual machines are inside ``~/vm/VM_NAME/``.

- That ``~/vm/VM_NAME/`` directory is mounted as ``/vagrant/`` inside the VM.

- The vm name is "django" for a vm inside ~/vm/django/`` and it has a
corresponding alias inside your ssh config file. So ssh'ing to "django"
means you connect just fine to the right VM with the vagrant user. An
example of such a config that ought to go inside ``~/.ssh/config`` ::

Host django
HostName 33.33.33.20
User vagrant

Oh, and make sure you use ``ssh-copy-id`` to copy your ssh key to the
vagrant box, otherwise you'll go mad typing your password all the time.




(See `source code on github <https://github.com/reinout/tools/blob/master/tools/vagrant.py>`_).

68 changes: 68 additions & 0 deletions tools/vagrant.py
@@ -0,0 +1,68 @@
"""
Script to run a command via ssh inside vagrant.
What it does: we're inside a directory that we know has been mounted in a
local vagrant box. We ``cd`` to the corresponding directory and run the
command there.
There are quite some assumptions in here, they match the way I (Reinout) has
set it all up:
- Virtual machines are inside ``~/vm/VM_NAME/``.
- That ``~/vm/VM_NAME/`` directory is mounted as ``/vagrant/`` inside the VM.
- The vm name is "django" for a vm inside ~/vm/django/`` and it has a
corresponding alias inside your ssh config file. So ssh'ing to "django"
means you connect just fine to the right VM with the vagrant user. An
example of such a config that ought to go inside ``~/.ssh/config`` ::
Host django
HostName 33.33.33.20
User vagrant
Oh, and make sure you use ``ssh-copy-id`` to copy your ssh key to the
vagrant box, otherwise you'll go mad typing your password all the time.
"""
import os
import sys

from fabric.api import cd
from fabric.api import env
from fabric.api import execute
from fabric.api import run

HOMEDIR = os.path.expanduser('~')
VM_BASEDIR = os.path.join(HOMEDIR, 'vm')
BASEDIR_IN_VAGRANT = '/vagrant/'


def vm_and_path():
"""Return vm name and path inside vm."""
cur_dir = os.getcwd()
if not cur_dir.startswith(VM_BASEDIR):
raise RuntimeError("We're not inside {}".format(VM_BASEDIR))
remainder = cur_dir.lstrip(VM_BASEDIR)
parts = remainder.split('/')
if not parts:
raise RuntimeError("We're not inside {}/VM_NAME".format(VM_BASEDIR))
vm_name = parts[0]
vagrant_path = BASEDIR_IN_VAGRANT + '/'.join(parts[1:])
return vm_name, vagrant_path


def run_cmd():
with cd(env.my_path):
run(env.my_cmd)


def main():
env.use_ssh_config = True
vm_name, path = vm_and_path()
cmd = ' '.join(sys.argv[1:])
env.hosts = [vm_name]
env.my_cmd = cmd
env.my_path = path
execute(run_cmd)

0 comments on commit 8f09f5d

Please sign in to comment.