vagrant/plugins/commands/ssh_config/command.rb

74 lines
2.2 KiB
Ruby
Raw Normal View History

2011-12-18 01:29:52 +00:00
require 'optparse'
2012-03-18 23:41:26 +00:00
require "vagrant/util/safe_puts"
require "vagrant/util/platform"
2012-03-18 23:41:26 +00:00
2012-04-19 20:59:48 +00:00
module VagrantPlugins
module CommandSSHConfig
2012-11-07 05:05:14 +00:00
class Command < Vagrant.plugin("2", :command)
2012-04-19 20:59:48 +00:00
include Vagrant::Util::SafePuts
2012-03-18 23:41:26 +00:00
def self.synopsis
"outputs OpenSSH valid configuration to connect to the machine"
end
def convert_win_paths(paths)
paths.map! { |path| Vagrant::Util::Platform.format_windows_path(path, :disable_unc) }
end
2010-08-25 06:55:53 +00:00
def execute
2011-12-18 01:29:52 +00:00
options = {}
opts = OptionParser.new do |o|
2016-05-29 05:06:51 +00:00
o.banner = "Usage: vagrant ssh-config [options] [name|id]"
o.separator ""
2014-02-08 08:20:50 +00:00
o.separator "Options:"
o.separator ""
2011-12-18 01:29:52 +00:00
2014-02-08 08:20:50 +00:00
o.on("--host NAME", "Name the host for the config") do |h|
2011-12-18 01:29:52 +00:00
options[:host] = h
end
end
argv = parse_options(opts)
return if !argv
with_target_vms(argv) do |machine|
ssh_info = machine.ssh_info
raise Vagrant::Errors::SSHNotReady if ssh_info.nil?
2011-12-18 01:29:52 +00:00
if Vagrant::Util::Platform.windows?
ssh_info[:private_key_path] = convert_win_paths(ssh_info[:private_key_path])
end
variables = {
host_key: options[:host] || machine.name || "vagrant",
ssh_host: ssh_info[:host],
ssh_port: ssh_info[:port],
ssh_user: ssh_info[:username],
2016-07-22 01:15:21 +00:00
keys_only: ssh_info[:keys_only],
verify_host_key: ssh_info[:verify_host_key],
private_key_path: ssh_info[:private_key_path],
log_level: ssh_info[:log_level],
forward_agent: ssh_info[:forward_agent],
forward_x11: ssh_info[:forward_x11],
proxy_command: ssh_info[:proxy_command],
ssh_command: ssh_info[:ssh_command],
forward_env: ssh_info[:forward_env],
}
# Render the template and output directly to STDOUT
2012-01-08 05:56:14 +00:00
template = "commands/ssh_config/config"
config = Vagrant::Util::TemplateRenderer.render(template, variables)
machine.ui.machine("ssh-config", config)
safe_puts(config)
safe_puts
2011-12-18 01:29:52 +00:00
end
# Success, exit status 0
0
end
2010-08-25 06:55:53 +00:00
end
end
end