`vagrant ssh-config`

This commit is contained in:
Mitchell Hashimoto 2011-12-17 17:29:52 -08:00
parent e34f0a8af7
commit 1176c65138
3 changed files with 39 additions and 20 deletions

View File

@ -101,6 +101,7 @@ Vagrant.commands.register(:provision) { Vagrant::Command::Provision }
Vagrant.commands.register(:reload) { Vagrant::Command::Reload }
Vagrant.commands.register(:resume) { Vagrant::Command::Resume }
Vagrant.commands.register(:ssh) { Vagrant::Command::SSH }
Vagrant.commands.register(:"ssh-config") { Vagrant::Command::SSHConfig }
Vagrant.commands.register(:suspend) { Vagrant::Command::Suspend }
Vagrant.commands.register(:up) { Vagrant::Command::Up }

View File

@ -9,6 +9,7 @@ module Vagrant
autoload :Reload, 'vagrant/command/reload'
autoload :Resume, 'vagrant/command/resume'
autoload :SSH, 'vagrant/command/ssh'
autoload :SSHConfig, 'vagrant/command/ssh_config'
autoload :Suspend, 'vagrant/command/suspend'
autoload :Up, 'vagrant/command/up'
end

View File

@ -1,28 +1,45 @@
require 'optparse'
module Vagrant
module Command
class SSHConfigCommand < NamedBase
class_option :host, :type => :string, :default => nil, :aliases => "-h"
register "ssh_config", "outputs .ssh/config valid syntax for connecting to this environment via ssh"
class SSHConfig < Base
def execute
raise Errors::MultiVMTargetRequired, :command => "ssh_config" if target_vms.length > 1
vm = target_vms.first
raise Errors::VMNotCreatedError if !vm.created?
raise Errors::VMInaccessible if !vm.vm.accessible?
options = {}
# We need to fix the file permissions of the key if they aren't set
# properly, otherwise if the user attempts to SSH in, it won't work!
vm.ssh.check_key_permissions(vm.env.config.ssh.private_key_path)
opts = OptionParser.new do |opts|
opts.banner = "Usage: vagrant ssh-config [vm-name] [-h name]"
$stdout.puts(Util::TemplateRenderer.render("ssh_config", {
:host_key => options[:host] || vm.name || "vagrant",
:ssh_host => vm.env.config.ssh.host,
:ssh_user => vm.env.config.ssh.username,
:ssh_port => vm.ssh.port,
:private_key_path => vm.env.config.ssh.private_key_path,
:forward_agent => vm.env.config.ssh.forward_agent,
:forward_x11 => vm.env.config.ssh.forward_x11
}))
opts.separator ""
opts.on("-h", "--host COMMAND", "Name the host for the config..") do |h|
options[:host] = h
end
end
argv = parse_options(opts)
return if !argv
# SSH-config always requires a target VM
raise Errors::MultiVMTargetRequired, :command => "ssh_config" if @env.multivm? && !argv[0]
with_target_vms(argv[0]) do |vm|
raise Errors::VMNotCreatedError if !vm.created?
raise Errors::VMInaccessible if !vm.vm.accessible?
# We need to fix the file permissions of the key if they aren't set
# properly, otherwise if the user attempts to SSH in, it won't work!
vm.ssh.check_key_permissions(vm.ssh.private_key_path)
$stdout.puts(Util::TemplateRenderer.render("ssh_config", {
:host_key => options[:host] || vm.name || "vagrant",
:ssh_host => vm.config.ssh.host,
:ssh_user => vm.config.ssh.username,
:ssh_port => vm.ssh.port,
:private_key_path => vm.config.ssh.private_key_path,
:forward_agent => vm.config.ssh.forward_agent,
:forward_x11 => vm.config.ssh.forward_x11
}))
end
end
end
end