communicator/ssh: Allow custom generation of environment variable exports
This commit is contained in:
parent
dbbd2d8e36
commit
dfc5e0d9a0
|
@ -540,7 +540,7 @@ module VagrantPlugins
|
||||||
end
|
end
|
||||||
|
|
||||||
# Set the terminal
|
# 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.
|
# Set SSH_AUTH_SOCK if we are in sudo and forwarding agent.
|
||||||
# This is to work around often misconfigured boxes where
|
# 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.")
|
@logger.warn("No SSH_AUTH_SOCK found despite forward_agent being set.")
|
||||||
else
|
else
|
||||||
@logger.info("Setting SSH_AUTH_SOCK remotely: #{auth_socket}")
|
@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
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -572,9 +572,9 @@ module VagrantPlugins
|
||||||
# without the cruft added from pty mode.
|
# without the cruft added from pty mode.
|
||||||
if pty
|
if pty
|
||||||
data = "stty raw -echo\n"
|
data = "stty raw -echo\n"
|
||||||
data += "export PS1=\n"
|
data += generate_environment_export("PS1", "")
|
||||||
data += "export PS2=\n"
|
data += generate_environment_export("PS2", "")
|
||||||
data += "export PROMPT_COMMAND=\n"
|
data += generate_environment_export("PROMPT_COMMAND", "")
|
||||||
data += "printf #{PTY_DELIM_START}\n"
|
data += "printf #{PTY_DELIM_START}\n"
|
||||||
data += "#{command}\n"
|
data += "#{command}\n"
|
||||||
data += "exitcode=$?\n"
|
data += "exitcode=$?\n"
|
||||||
|
@ -670,6 +670,11 @@ module VagrantPlugins
|
||||||
source_path = Vagrant.source_root.join("keys", "vagrant")
|
source_path = Vagrant.source_root.join("keys", "vagrant")
|
||||||
return File.read(path).chomp == source_path.read.chomp
|
return File.read(path).chomp == source_path.read.chomp
|
||||||
end
|
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
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -15,24 +15,25 @@ module VagrantPlugins
|
||||||
attr_accessor :ssh_command
|
attr_accessor :ssh_command
|
||||||
attr_accessor :pty
|
attr_accessor :pty
|
||||||
attr_accessor :sudo_command
|
attr_accessor :sudo_command
|
||||||
|
attr_accessor :export_command_template
|
||||||
|
|
||||||
attr_reader :default
|
attr_reader :default
|
||||||
|
|
||||||
def initialize
|
def initialize
|
||||||
super
|
super
|
||||||
|
|
||||||
@forward_agent = UNSET_VALUE
|
@forward_agent = UNSET_VALUE
|
||||||
@forward_x11 = UNSET_VALUE
|
@forward_x11 = UNSET_VALUE
|
||||||
@forward_env = UNSET_VALUE
|
@forward_env = UNSET_VALUE
|
||||||
@guest_port = UNSET_VALUE
|
@guest_port = UNSET_VALUE
|
||||||
@keep_alive = UNSET_VALUE
|
@keep_alive = UNSET_VALUE
|
||||||
@proxy_command = UNSET_VALUE
|
@proxy_command = UNSET_VALUE
|
||||||
@ssh_command = UNSET_VALUE
|
@ssh_command = UNSET_VALUE
|
||||||
@pty = UNSET_VALUE
|
@pty = UNSET_VALUE
|
||||||
@shell = UNSET_VALUE
|
@shell = UNSET_VALUE
|
||||||
@sudo_command = UNSET_VALUE
|
@sudo_command = UNSET_VALUE
|
||||||
|
@export_command_template = UNSET_VALUE
|
||||||
@default = SSHConnectConfig.new
|
@default = SSHConnectConfig.new
|
||||||
end
|
end
|
||||||
|
|
||||||
def merge(other)
|
def merge(other)
|
||||||
|
@ -55,6 +56,10 @@ module VagrantPlugins
|
||||||
@pty = false if @pty == UNSET_VALUE
|
@pty = false if @pty == UNSET_VALUE
|
||||||
@shell = "bash -l" if @shell == 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
|
if @sudo_command == UNSET_VALUE
|
||||||
@sudo_command = "sudo -E -H %c"
|
@sudo_command = "sudo -E -H %c"
|
||||||
end
|
end
|
||||||
|
|
|
@ -5,6 +5,8 @@ require Vagrant.source_root.join("plugins/communicators/ssh/communicator")
|
||||||
describe VagrantPlugins::CommunicatorSSH::Communicator do
|
describe VagrantPlugins::CommunicatorSSH::Communicator do
|
||||||
include_context "unit"
|
include_context "unit"
|
||||||
|
|
||||||
|
let(:export_command_template){ 'export %ENV_KEY%="%ENV_VALUE%"' }
|
||||||
|
|
||||||
# SSH configuration information mock
|
# SSH configuration information mock
|
||||||
let(:ssh) do
|
let(:ssh) do
|
||||||
double("ssh",
|
double("ssh",
|
||||||
|
@ -15,6 +17,7 @@ describe VagrantPlugins::CommunicatorSSH::Communicator do
|
||||||
pty: false,
|
pty: false,
|
||||||
keep_alive: false,
|
keep_alive: false,
|
||||||
insert_key: false,
|
insert_key: false,
|
||||||
|
export_command_template: export_command_template,
|
||||||
shell: 'bash -l'
|
shell: 'bash -l'
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
@ -509,4 +512,18 @@ describe VagrantPlugins::CommunicatorSSH::Communicator do
|
||||||
end
|
end
|
||||||
end
|
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
|
end
|
||||||
|
|
|
@ -126,6 +126,17 @@ only affects the shell to use when executing commands internally in Vagrant.
|
||||||
|
|
||||||
<hr>
|
<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
|
`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
|
with `sudo`. This defaults to `sudo -E -H %c`. The `%c` will be replaced by
|
||||||
the command that is being executed.
|
the command that is being executed.
|
||||||
|
|
Loading…
Reference in New Issue