providers/docker: can set environmental variables

This commit is contained in:
Mitchell Hashimoto 2014-04-17 18:07:57 -07:00
parent 0a3346c918
commit 71d615212d
4 changed files with 39 additions and 0 deletions

View File

@ -46,6 +46,7 @@ module VagrantPlugins
{
cmd: @provider_config.cmd,
env: @provider_config.env,
extra_args: @provider_config.create_args,
hostname: @machine_config.vm.hostname,
image: @provider_config.image,

View File

@ -9,6 +9,11 @@ module VagrantPlugins
# @return [Array<String>]
attr_accessor :create_args
# Environmental variables to set in the container.
#
# @return [Hash]
attr_accessor :env
# True if the Docker container exposes SSH access. If this is true,
# then Vagrant can do a bunch more things like setting the hostname,
# provisioning, etc.
@ -45,6 +50,7 @@ module VagrantPlugins
def initialize
@cmd = UNSET_VALUE
@create_args = []
@env = {}
@has_ssh = UNSET_VALUE
@image = UNSET_VALUE
@ports = []
@ -55,9 +61,19 @@ module VagrantPlugins
@vagrant_vagrantfile = UNSET_VALUE
end
def merge(other)
super.tap do |result|
env = {}
env.merge!(@env) if @env
env.merge!(other.env) if other.env
result.env = env
end
end
def finalize!
@cmd = [] if @cmd == UNSET_VALUE
@create_args = [] if @create_args == UNSET_VALUE
@env ||= {}
@has_ssh = false if @has_ssh == UNSET_VALUE
@image = nil if @image == UNSET_VALUE
@privileged = false if @privileged == UNSET_VALUE

View File

@ -20,8 +20,10 @@ module VagrantPlugins
volumes = Array(params[:volumes])
name = params.fetch(:name)
cmd = Array(params.fetch(:cmd))
env = params.fetch(:env)
run_cmd = %W(docker run --name #{name} -d)
run_cmd += env.map { |k,v| ['-e', "#{k}=#{v}"] }
run_cmd += ports.map { |p| ['-p', p.to_s] }
run_cmd += volumes.map { |v| ['-v', v.to_s] }
run_cmd += %W(--privileged) if params[:privileged]

View File

@ -25,6 +25,7 @@ describe VagrantPlugins::DockerProvider::Config do
before { subject.finalize! }
its(:cmd) { should eq([]) }
its(:env) { should eq({}) }
its(:image) { should be_nil }
its(:privileged) { should be_false }
its(:vagrant_machine) { should be_nil }
@ -40,4 +41,23 @@ describe VagrantPlugins::DockerProvider::Config do
subject.finalize!
assert_valid
end
describe "#merge" do
context "env vars" do
let(:one) { described_class.new }
let(:two) { described_class.new }
subject { one.merge(two) }
it "should merge the values" do
one.env["foo"] = "bar"
two.env["bar"] = "baz"
expect(subject.env).to eq({
"foo" => "bar",
"bar" => "baz",
})
end
end
end
end