diff --git a/lib/vagrant/commands/ssh_config.rb b/lib/vagrant/commands/ssh_config.rb new file mode 100644 index 000000000..fdbb33e7e --- /dev/null +++ b/lib/vagrant/commands/ssh_config.rb @@ -0,0 +1,33 @@ +module Vagrant + class Commands + # Reload the environment. This is almost equivalent to the {up} command + # except that it doesn't import the VM and do the initialize bootstrapping + # of the instance. Instead, it forces a shutdown (if its running) of the + # VM, updates the metadata (shared folders, forwarded ports), restarts + # the VM, and then reruns the provisioning if enabled. + class SSHConfig < Base + Base.subcommand "ssh-config", self + description "outputs .ssh/config valid syntax for connecting to this environment via ssh" + + def execute(args=[]) + env.require_root_path + + parse_options(args) + 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 + }) + end + + def options_spec(opts) + opts.banner = "Usage: vagrant ssh-config [--host NAME]" + + opts.on("-h", "--host [HOST]", "Host name for the SSH config") do |h| + options[:host] = h + end + end + end + end +end \ No newline at end of file diff --git a/test/vagrant/commands/ssh_config_test.rb b/test/vagrant/commands/ssh_config_test.rb new file mode 100644 index 000000000..5e8673f30 --- /dev/null +++ b/test/vagrant/commands/ssh_config_test.rb @@ -0,0 +1,54 @@ +require File.join(File.dirname(__FILE__), '..', '..', 'test_helper') + +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) + + @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 + } + end + + should "require root path" do + @env.expects(:require_root_path).once + @instance.execute + 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 + 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]) + end + end +end