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