`vagrant ssh-config` outputs .ssh/config ready entry for current environment

This commit is contained in:
Mitchell Hashimoto 2010-04-12 21:00:42 -07:00
parent d5e2a64fd3
commit c630b028fc
4 changed files with 68 additions and 0 deletions

28
bin/vagrant-ssh-config Executable file
View File

@ -0,0 +1,28 @@
#!/usr/bin/env ruby
begin
require File.expand_path('../../.bundle/environment', __FILE__)
rescue LoadError
# Fallback on rubygems
require "rubygems"
end
require 'git-style-binary/command'
# Get library
libdir = File.join(File.dirname(__FILE__), '..', 'lib')
require File.expand_path('vagrant', libdir)
GitStyleBinary.command do
short_desc "outputs .ssh/config valid syntax for connecting to this environment via ssh"
banner <<-EOS
Usage: #{command.full_name} #{all_options_string}
Outputs a .ssh/config valid entry for connecting (via SCP, git, etc.)
to the current environment.
EOS
run do |command|
Vagrant::Commands.execute(:ssh_config)
end
end

View File

@ -61,6 +61,18 @@ module Vagrant
env.ssh.connect
end
# Outputs a valid entry for .ssh/config which can be used to connect
# to this environment.
def ssh_config
env.require_persisted_vm
puts TemplateRenderer.render("ssh_config", {
:host_key => "vagrant",
:ssh_user => env.config.ssh.username,
:ssh_port => env.ssh.port,
:private_key_path => env.config.ssh.private_key_path
})
end
# Halts a running vagrant instance. This forcibly halts the instance;
# it is the equivalent of pulling the power on a machine. The instance
# can be restarted again with {up}.

7
templates/ssh_config.erb Normal file
View File

@ -0,0 +1,7 @@
Host <%= host_key %>
HostName localhost
User <%= ssh_user %>
Port <%= ssh_port %>
UserKnownHostsFile /dev/null
StrictHostKeyChecking no
IdentityFile <%= private_key_path %>

View File

@ -108,6 +108,27 @@ class CommandsTest < Test::Unit::TestCase
end
end
context "ssh-config" do
setup do
@ssh = mock("ssh")
@ssh.stubs(:port).returns(2197)
@env.stubs(:ssh).returns(@ssh)
end
should "output rendered template" do
result = mock("result")
Vagrant::Util::TemplateRenderer.expects(:render).with("ssh_config", {
:host_key => "vagrant",
:ssh_user => @env.config.ssh.username,
:ssh_port => @env.ssh.port,
:private_key_path => @env.config.ssh.private_key_path
}).returns(result)
@commands.expects(:puts).with(result).once
@commands.ssh_config
end
end
context "suspend" do
setup do
@persisted_vm.stubs(:suspend)