From 1176c65138b8c46b19a48a673aa97422b414ccd1 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Sat, 17 Dec 2011 17:29:52 -0800 Subject: [PATCH] `vagrant ssh-config` --- lib/vagrant.rb | 1 + lib/vagrant/command.rb | 1 + lib/vagrant/command/ssh_config.rb | 57 ++++++++++++++++++++----------- 3 files changed, 39 insertions(+), 20 deletions(-) diff --git a/lib/vagrant.rb b/lib/vagrant.rb index d018c3363..33164a07e 100644 --- a/lib/vagrant.rb +++ b/lib/vagrant.rb @@ -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 } diff --git a/lib/vagrant/command.rb b/lib/vagrant/command.rb index 0544b51e5..cc2a21c96 100644 --- a/lib/vagrant/command.rb +++ b/lib/vagrant/command.rb @@ -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 diff --git a/lib/vagrant/command/ssh_config.rb b/lib/vagrant/command/ssh_config.rb index b79a6e3f7..9d2e0c10e 100644 --- a/lib/vagrant/command/ssh_config.rb +++ b/lib/vagrant/command/ssh_config.rb @@ -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