ssh-config subcommand

This commit is contained in:
Mitchell Hashimoto 2010-04-13 16:31:08 -07:00
parent 4674b1983e
commit fa3ca57c9f
2 changed files with 87 additions and 0 deletions

View File

@ -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

View File

@ -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