communicator/ssh: Allow custom generation of environment variable exports

This commit is contained in:
Chris Roberts 2016-11-08 09:50:39 -08:00
parent dbbd2d8e36
commit dfc5e0d9a0
4 changed files with 55 additions and 17 deletions

View File

@ -540,7 +540,7 @@ module VagrantPlugins
end
# Set the terminal
ch2.send_data "export TERM=vt100\n"
ch2.send_data generate_environment_export("TERM", "vt100")
# Set SSH_AUTH_SOCK if we are in sudo and forwarding agent.
# This is to work around often misconfigured boxes where
@ -563,7 +563,7 @@ module VagrantPlugins
@logger.warn("No SSH_AUTH_SOCK found despite forward_agent being set.")
else
@logger.info("Setting SSH_AUTH_SOCK remotely: #{auth_socket}")
ch2.send_data "export SSH_AUTH_SOCK=#{auth_socket}\n"
ch2.send_data generate_environment_export("SSH_AUTH_SOCK", auth_socket)
end
end
@ -572,9 +572,9 @@ module VagrantPlugins
# without the cruft added from pty mode.
if pty
data = "stty raw -echo\n"
data += "export PS1=\n"
data += "export PS2=\n"
data += "export PROMPT_COMMAND=\n"
data += generate_environment_export("PS1", "")
data += generate_environment_export("PS2", "")
data += generate_environment_export("PROMPT_COMMAND", "")
data += "printf #{PTY_DELIM_START}\n"
data += "#{command}\n"
data += "exitcode=$?\n"
@ -670,6 +670,11 @@ module VagrantPlugins
source_path = Vagrant.source_root.join("keys", "vagrant")
return File.read(path).chomp == source_path.read.chomp
end
def generate_environment_export(env_key, env_value)
template = @machine.config.ssh.export_command_template
template.sub("%ENV_KEY%", env_key).sub("%ENV_VALUE%", env_value) + "\n"
end
end
end
end

View File

@ -15,6 +15,7 @@ module VagrantPlugins
attr_accessor :ssh_command
attr_accessor :pty
attr_accessor :sudo_command
attr_accessor :export_command_template
attr_reader :default
@ -31,7 +32,7 @@ module VagrantPlugins
@pty = UNSET_VALUE
@shell = UNSET_VALUE
@sudo_command = UNSET_VALUE
@export_command_template = UNSET_VALUE
@default = SSHConnectConfig.new
end
@ -55,6 +56,10 @@ module VagrantPlugins
@pty = false if @pty == UNSET_VALUE
@shell = "bash -l" if @shell == UNSET_VALUE
if @export_command_template == UNSET_VALUE
@export_command_template = 'export %ENV_KEY%="%ENV_VALUE%"'
end
if @sudo_command == UNSET_VALUE
@sudo_command = "sudo -E -H %c"
end

View File

@ -5,6 +5,8 @@ require Vagrant.source_root.join("plugins/communicators/ssh/communicator")
describe VagrantPlugins::CommunicatorSSH::Communicator do
include_context "unit"
let(:export_command_template){ 'export %ENV_KEY%="%ENV_VALUE%"' }
# SSH configuration information mock
let(:ssh) do
double("ssh",
@ -15,6 +17,7 @@ describe VagrantPlugins::CommunicatorSSH::Communicator do
pty: false,
keep_alive: false,
insert_key: false,
export_command_template: export_command_template,
shell: 'bash -l'
)
end
@ -509,4 +512,18 @@ describe VagrantPlugins::CommunicatorSSH::Communicator do
end
end
end
describe ".generate_environment_export" do
it "should generate bourne shell compatible export" do
communicator.send(:generate_environment_export, "TEST", "value").should eq("export TEST=\"value\"\n")
end
context "with custom template defined" do
let(:export_command_template){ "setenv %ENV_KEY% %ENV_VALUE%" }
it "should generate custom export based on template" do
communicator.send(:generate_environment_export, "TEST", "value").should eq("setenv TEST value\n")
end
end
end
end

View File

@ -126,6 +126,17 @@ only affects the shell to use when executing commands internally in Vagrant.
<hr>
`config.ssh.export_command_template` - The template used to generate
exported environment variables in the active session. This can be useful
when using a Bourne incompatible shell like C shell. The template supports
two variables which are replaced with the desired environment variable key and
environment variable value: `%ENV_KEY%` and `%ENV_VALUE%`. The default template
is:
```ruby
config.ssh.export_command_template = 'export %ENV_KEY%="%ENV_VALUE%"'
```
`config.ssh.sudo_command` - The command to use when executing a command
with `sudo`. This defaults to `sudo -E -H %c`. The `%c` will be replaced by
the command that is being executed.