Skip to content

Commit 00e83dc

Browse files
author
Todd DeLuca
committed
Add ssh command to Vagrant
Add tests as well.
1 parent 9c954b7 commit 00e83dc

File tree

3 files changed

+43
-0
lines changed

3 files changed

+43
-0
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@
44
This document lists the changes (and individuals who contributed to those
55
changes) for each release of python-vagrant.
66

7+
## To be released
8+
9+
- Pull Request #54: Create ssh() method to run shell commands in a VM
10+
Authors: Parker Thompson (https://github.com/mothran) and Todd DeLuca
11+
(https://github.com/todddeluca)
12+
713
## 0.5.14
814

915
- Pull Request #51: Add support for the vagrant package command.

tests/test_vagrant.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -520,6 +520,30 @@ def test_multivm_config():
520520
eq_(keyfile, parsed_config["IdentityFile"].lstrip('"').rstrip('"'))
521521

522522

523+
@with_setup(make_setup_vm(), teardown_vm)
524+
def test_ssh_command():
525+
'''
526+
Test executing a command via ssh on a vm.
527+
'''
528+
v = vagrant.Vagrant(TD)
529+
v.up()
530+
output = v.ssh(command='echo hello')
531+
assert output.strip() == 'hello'
532+
533+
534+
@with_setup(make_setup_vm(MULTIVM_VAGRANTFILE), teardown_vm)
535+
def test_ssh_command_multivm():
536+
'''
537+
Test executing a command via ssh on a specific vm
538+
'''
539+
v = vagrant.Vagrant(TD)
540+
v.up()
541+
output = v.ssh(vm_name=VM_1, command='echo hello')
542+
assert output.strip() == 'hello'
543+
output = v.ssh(vm_name=VM_2, command='echo I like your hat')
544+
assert output.strip() == 'I like your hat'
545+
546+
523547
def test_make_file_cm():
524548
filename = os.path.join(TD, 'test.log')
525549
if os.path.exists(filename):

vagrant/__init__.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -728,6 +728,19 @@ def snapshot_delete(self, name):
728728
'''
729729
self._call_vagrant_command(['snapshot', 'delete', name])
730730

731+
def ssh(self, vm_name=None, command=None, extra_ssh_args=None):
732+
'''
733+
Execute a command via ssh on the vm specified.
734+
command: The command to execute via ssh.
735+
extra_ssh_args: Corresponds to '--' option in the vagrant ssh command
736+
Returns the output of running the command.
737+
'''
738+
cmd = ['ssh', vm_name, '--command', command]
739+
if extra_ssh_args is not None:
740+
cmd += ['--', extra_ssh_args]
741+
742+
return self._run_vagrant_command(cmd)
743+
731744
def _parse_box_list(self, output):
732745
'''
733746
Remove Vagrant usage for unit testing

0 commit comments

Comments
 (0)