Support environment variable forwarding, fixes #4131

Signed-off-by: Eli Skeggs <eskeggs@globesherpa.com>
This commit is contained in:
Eli Skeggs 2014-06-30 16:41:31 -07:00 committed by Seth Vargo
parent 6a147e1146
commit 2b9173e15a
7 changed files with 43 additions and 1 deletions

View File

@ -442,6 +442,7 @@ module Vagrant
# We also set some fields that are purely controlled by Varant
info[:forward_agent] = @config.ssh.forward_agent
info[:forward_x11] = @config.ssh.forward_x11
info[:forward_env] = @config.ssh.forward_env
info[:ssh_command] = @config.ssh.ssh_command if @config.ssh.ssh_command

View File

@ -138,6 +138,10 @@ module Vagrant
command_options += ["-o", "ProxyCommand=#{ssh_info[:proxy_command]}"]
end
if ssh_info[:forward_env]
command_options += ["-o", "SendEnv=" + ssh_info[:forward_env].keys().join(" ")]
end
# Configurables -- extra_args should always be last due to the way the
# ssh args parser works. e.g. if the user wants to use the -t option,
# any shell command(s) she'd like to run on the remote server would
@ -172,6 +176,14 @@ module Vagrant
LOGGER.info("Executing SSH in subprocess: #{ssh} #{command_options.inspect}")
process = ChildProcess.build(ssh, *command_options)
process.io.inherit!
# Forward configured environment variables.
if ssh_info[:forward_env]
ssh_info[:forward_env].each do |host_var, guest_var|
process.environment[guest_var] = ENV[guest_var]
end
end
process.start
process.wait
return process.exit_code

View File

@ -41,7 +41,8 @@ module VagrantPlugins
forward_agent: ssh_info[:forward_agent],
forward_x11: ssh_info[:forward_x11],
proxy_command: ssh_info[:proxy_command],
ssh_command: ssh_info[:ssh_command]
ssh_command: ssh_info[:ssh_command],
forward_env: ssh_info[:forward_env],
}
# Render the template and output directly to STDOUT

View File

@ -328,6 +328,17 @@ module VagrantPlugins
auth_methods << "publickey" if ssh_info[:private_key_path]
auth_methods << "password" if ssh_info[:password]
# Build the environment
env_revert = {}
send_env_array = nil
if ssh_info[:forward_agent]
send_env_array = ssh_info[:forward_env].values()
ssh_info[:forward_env].each do |host_var, guest_var|
env_revert[guest_var] = ENV[guest_var]
ENV[guest_var] = ENV[host_var]
end
end
# Build the options we'll use to initiate the connection via Net::SSH
common_connect_opts = {
auth_methods: auth_methods,
@ -338,6 +349,7 @@ module VagrantPlugins
paranoid: false,
password: ssh_info[:password],
port: ssh_info[:port],
send_env: send_env_array,
timeout: 15,
user_known_hosts_file: [],
verbose: :debug,
@ -430,6 +442,11 @@ module VagrantPlugins
# This is raised if a private key type that Net-SSH doesn't support
# is used. Show a nicer error.
raise Vagrant::Errors::SSHKeyTypeNotSupported
ensure
# TODO: will this leak?
env_revert.each do |guest_var, guest_value|
ENV[guest_var] = guest_value
end
end
@connection = connection

View File

@ -13,6 +13,7 @@ module VagrantPlugins
attr_accessor :private_key_path
attr_accessor :forward_agent
attr_accessor :forward_x11
attr_accessor :forward_env
attr_accessor :shell
def initialize
@ -26,6 +27,7 @@ module VagrantPlugins
@private_key_path = UNSET_VALUE
@forward_agent = UNSET_VALUE
@forward_x11 = UNSET_VALUE
@forward_env = UNSET_VALUE
@shell = UNSET_VALUE
end
@ -37,6 +39,7 @@ module VagrantPlugins
new.ssh.private_key_path = @private_key_path if @private_key_path != UNSET_VALUE
new.ssh.forward_agent = @forward_agent if @forward_agent != UNSET_VALUE
new.ssh.forward_x11 = @forward_x11 if @forward_x11 != UNSET_VALUE
new.ssh.forward_env = @forward_env if @forward_env != UNSET_VALUE
new.ssh.shell = @shell if @shell != UNSET_VALUE
end
end

View File

@ -7,6 +7,7 @@ module VagrantPlugins
class SSHConfig < SSHConnectConfig
attr_accessor :forward_agent
attr_accessor :forward_x11
attr_accessor :forward_env
attr_accessor :guest_port
attr_accessor :keep_alive
attr_accessor :shell
@ -22,6 +23,7 @@ module VagrantPlugins
@forward_agent = UNSET_VALUE
@forward_x11 = UNSET_VALUE
@forward_env = UNSET_VALUE
@guest_port = UNSET_VALUE
@keep_alive = UNSET_VALUE
@proxy_command = UNSET_VALUE
@ -45,6 +47,7 @@ module VagrantPlugins
@forward_agent = false if @forward_agent == UNSET_VALUE
@forward_x11 = false if @forward_x11 == UNSET_VALUE
@forward_env = false if @forward_env == UNSET_VALUE
@guest_port = 22 if @guest_port == UNSET_VALUE
@keep_alive = true if @keep_alive == UNSET_VALUE
@proxy_command = nil if @proxy_command == UNSET_VALUE

View File

@ -67,6 +67,11 @@ is enabled. Defaults to false.
<hr>
`config.ssh.forward_env` - A hash of host environment variable names to guest
environment variable names.
<hr>
`config.ssh.insert_key` - If `true`, Vagrant will automatically insert
a keypair to use for SSH, replacing Vagrant's default insecure key
inside the machine if detected. By default, this is true.