Merge pull request #6562 from mitchellh/sethvargo/pr-4132

Add support for forwarding host environment variables via SSH
This commit is contained in:
Seth Vargo 2015-11-19 16:38:07 -08:00
commit 94f2f68f88
7 changed files with 32 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].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 |key|
process.environment[key] = ENV[key]
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

@ -333,6 +333,7 @@ module VagrantPlugins
auth_methods: auth_methods,
config: false,
forward_agent: ssh_info[:forward_agent],
forward_env: ssh_info[:forward_env],
keys: ssh_info[:private_key_path],
keys_only: true,
paranoid: false,

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,16 @@ is enabled. Defaults to false.
<hr>
`config.ssh.forward_env` - An array of host environment variables to forward to
the guest. If you are familiar with OpenSSH, this corresponds to the `SendEnv`
paramter.
```ruby
config.ssh.forward_env = ["CUSTOM_VAR"]
```
<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.