diff --git a/lib/vagrant/commands/ssh.rb b/lib/vagrant/commands/ssh.rb index 9a01edb26..3a8c6d243 100644 --- a/lib/vagrant/commands/ssh.rb +++ b/lib/vagrant/commands/ssh.rb @@ -16,11 +16,13 @@ module Vagrant def ssh_connect(name) if name.nil? && env.multivm? - error_and_exit(:ssh_multivm) - return # for tests + if env.primary_vm.nil? + error_and_exit(:ssh_multivm) + return # for tests + end end - vm = name.nil? ? env.vms.values.first : env.vms[name.to_sym] + vm = name.nil? ? env.primary_vm : env.vms[name.to_sym] if vm.nil? error_and_exit(:unknown_vm, :vm => name) return # for tests diff --git a/templates/strings.yml b/templates/strings.yml index 662391b36..06667f97a 100644 --- a/templates/strings.yml +++ b/templates/strings.yml @@ -153,6 +153,14 @@ specific VM must be specified. This can be done by calling `vagrant ssh NAME` where NAME is a valid VM represented by your Vagrantfile. + + Alternatively, if you mark one of your VMs as 'primary,' + then Vagrant will default to that VM. This can be done by + specifying `:primary => true` when defining the VM. Example: + + config.vm.define(:foo, :primary => true) do |config| + ... + end :ssh_config_multivm: |- Because this Vagrant environment represents multiple VMs, a specific VM must be specified. This can be done by calling diff --git a/test/vagrant/commands/ssh_test.rb b/test/vagrant/commands/ssh_test.rb index e12c3ea75..84d80e255 100644 --- a/test/vagrant/commands/ssh_test.rb +++ b/test/vagrant/commands/ssh_test.rb @@ -30,12 +30,25 @@ class CommandsSSHTest < Test::Unit::TestCase @env.stubs(:multivm?).returns(false) end - should "error and exit if no VM is specified and multivm" do + should "error and exit if no VM is specified and multivm and no primary VM" do @env.stubs(:multivm?).returns(true) + @env.stubs(:primary_vm).returns(nil) @instance.expects(:error_and_exit).with(:ssh_multivm).once @instance.ssh_connect(nil) end + should "use the primary VM if it exists and no name is specified" do + vm = mock("vm") + ssh = mock("ssh") + vm.stubs(:created?).returns(true) + vm.stubs(:ssh).returns(ssh) + + @env.stubs(:multivm?).returns(true) + @env.stubs(:primary_vm).returns(vm) + ssh.expects(:connect).once + @instance.ssh_connect(nil) + end + should "error and exit if VM is nil" do @instance.expects(:error_and_exit).with(:unknown_vm, :vm => :foo).once @instance.ssh_connect(:foo)