diff --git a/plugins/communicators/ssh/communicator.rb b/plugins/communicators/ssh/communicator.rb index 0648e3cdb..4c3eb40ac 100644 --- a/plugins/communicators/ssh/communicator.rb +++ b/plugins/communicators/ssh/communicator.rb @@ -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 diff --git a/plugins/kernel_v2/config/ssh.rb b/plugins/kernel_v2/config/ssh.rb index c9e0a6695..1dc3d51c9 100644 --- a/plugins/kernel_v2/config/ssh.rb +++ b/plugins/kernel_v2/config/ssh.rb @@ -15,24 +15,25 @@ module VagrantPlugins attr_accessor :ssh_command attr_accessor :pty attr_accessor :sudo_command + attr_accessor :export_command_template attr_reader :default def initialize super - @forward_agent = UNSET_VALUE - @forward_x11 = UNSET_VALUE - @forward_env = UNSET_VALUE - @guest_port = UNSET_VALUE - @keep_alive = UNSET_VALUE - @proxy_command = UNSET_VALUE - @ssh_command = UNSET_VALUE - @pty = UNSET_VALUE - @shell = UNSET_VALUE - @sudo_command = UNSET_VALUE - - @default = SSHConnectConfig.new + @forward_agent = UNSET_VALUE + @forward_x11 = UNSET_VALUE + @forward_env = UNSET_VALUE + @guest_port = UNSET_VALUE + @keep_alive = UNSET_VALUE + @proxy_command = UNSET_VALUE + @ssh_command = UNSET_VALUE + @pty = UNSET_VALUE + @shell = UNSET_VALUE + @sudo_command = UNSET_VALUE + @export_command_template = UNSET_VALUE + @default = SSHConnectConfig.new end def merge(other) @@ -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 diff --git a/test/unit/plugins/communicators/ssh/communicator_test.rb b/test/unit/plugins/communicators/ssh/communicator_test.rb index d38694196..7de5b90d6 100644 --- a/test/unit/plugins/communicators/ssh/communicator_test.rb +++ b/test/unit/plugins/communicators/ssh/communicator_test.rb @@ -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 diff --git a/website/source/docs/vagrantfile/ssh_settings.html.md b/website/source/docs/vagrantfile/ssh_settings.html.md index c45b79a6c..29d1bc8bf 100644 --- a/website/source/docs/vagrantfile/ssh_settings.html.md +++ b/website/source/docs/vagrantfile/ssh_settings.html.md @@ -126,6 +126,17 @@ only affects the shell to use when executing commands internally in Vagrant.