From f30645a967e3b17d7edf28a9b3c4a1869f7666d9 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Mon, 17 May 2010 10:52:07 -0700 Subject: [PATCH] `vagrant ssh-config` is now multi-vm friendly --- lib/vagrant/commands/ssh_config.rb | 25 +++++++-- templates/strings.yml | 5 ++ test/vagrant/commands/ssh_config_test.rb | 65 ++++++++++++++++-------- 3 files changed, 69 insertions(+), 26 deletions(-) diff --git a/lib/vagrant/commands/ssh_config.rb b/lib/vagrant/commands/ssh_config.rb index 364f42689..4b76a13bd 100644 --- a/lib/vagrant/commands/ssh_config.rb +++ b/lib/vagrant/commands/ssh_config.rb @@ -9,12 +9,27 @@ module Vagrant def execute(args=[]) env.require_root_path - parse_options(args) + args = parse_options(args) + show_single(args[0]) + end + + def show_single(name) + if name.nil? && env.multivm? + error_and_exit(:ssh_config_multivm) + return # for tests + end + + vm = name.nil? ? env.vms.values.first : env.vms[name.to_sym] + if vm.nil? + error_and_exit(:unknown_vm, :vm => name) + return # for tests + end + puts TemplateRenderer.render("ssh_config", { :host_key => options[:host] || "vagrant", - :ssh_user => env.config.ssh.username, - :ssh_port => env.ssh.port, - :private_key_path => env.config.ssh.private_key_path + :ssh_user => vm.env.config.ssh.username, + :ssh_port => vm.ssh.port, + :private_key_path => vm.env.config.ssh.private_key_path }) end @@ -27,4 +42,4 @@ module Vagrant end end end -end \ No newline at end of file +end diff --git a/templates/strings.yml b/templates/strings.yml index d325c5d72..6be52ea14 100644 --- a/templates/strings.yml +++ b/templates/strings.yml @@ -148,6 +148,11 @@ specific VM must be specified. This can be done by calling `vagrant ssh NAME` where NAME is a valid VM represented by your Vagrantfile. +:ssh_config_multivm: |- + Because this Vagrant environment represents multiple VMs, a + specific VM must be specified. This can be done by calling + `vagrant ssh-config NAME` where NAME is a valid VM represented by + your Vagrantfile. :system_invalid_class: |- The specified system does not inherit from `Vagrant::Systems::Base`. The specified system class must inherit from this class. diff --git a/test/vagrant/commands/ssh_config_test.rb b/test/vagrant/commands/ssh_config_test.rb index 5e8673f30..3fab1dd4a 100644 --- a/test/vagrant/commands/ssh_config_test.rb +++ b/test/vagrant/commands/ssh_config_test.rb @@ -4,31 +4,14 @@ class CommandsSSHConfigTest < Test::Unit::TestCase setup do @klass = Vagrant::Commands::SSHConfig - @persisted_vm = mock("persisted_vm") - @persisted_vm.stubs(:execute!) - @env = mock_environment - @env.stubs(:require_persisted_vm) - @env.stubs(:vm).returns(@persisted_vm) - + @env.stubs(:require_root_path) @instance = @klass.new(@env) end context "executing" do setup do - @ssh = mock("ssh") - @ssh.stubs(:port).returns(2197) - @env.stubs(:ssh).returns(@ssh) - @env.stubs(:require_root_path) - - @instance.stubs(:puts) - - @data = { - :host_key => "vagrant", - :ssh_user => @env.config.ssh.username, - :ssh_port => @env.ssh.port, - :private_key_path => @env.config.ssh.private_key_path - } + @instance.stubs(:show_single) end should "require root path" do @@ -36,19 +19,59 @@ class CommandsSSHConfigTest < Test::Unit::TestCase @instance.execute end + should "call show_single with given argument" do + @instance.expects(:show_single).with("foo").once + @instance.execute(["foo"]) + end + end + + context "showing a single entry" do + setup do + @ssh = mock("ssh") + @ssh.stubs(:port).returns(2197) + + @bar = mock("vm") + @bar.stubs(:env).returns(mock_environment) + @bar.stubs(:ssh).returns(@ssh) + + @vms = {:bar => @bar} + @env.stubs(:multivm?).returns(true) + @env.stubs(:vms).returns(@vms) + + @data = { + :host_key => "vagrant", + :ssh_user => @bar.env.config.ssh.username, + :ssh_port => @bar.ssh.port, + :private_key_path => @bar.env.config.ssh.private_key_path + } + + @instance.stubs(:puts) + end + + should "error if name is nil and multivm" do + @env.stubs(:multivm?).returns(true) + @instance.expects(:error_and_exit).with(:ssh_config_multivm).once + @instance.show_single(nil) + end + + should "error if the VM is not found" do + @instance.expects(:error_and_exit).with(:unknown_vm, :vm => "foo").once + @instance.show_single("foo") + end + should "output rendered template" do result = mock("result") Vagrant::Util::TemplateRenderer.expects(:render).with("ssh_config", @data).returns(result) @instance.expects(:puts).with(result).once - @instance.execute + @instance.show_single(:bar) end should "render with the given host name if given" do host = "foo" @data[:host_key] = host Vagrant::Util::TemplateRenderer.expects(:render).with("ssh_config", @data) - @instance.execute(["--host", host]) + @instance.execute(["bar", "--host", host]) end end end