providers/docker: can specify links

This commit is contained in:
Mitchell Hashimoto 2014-04-17 18:33:46 -07:00
parent 71d615212d
commit 58ddc66b9c
5 changed files with 75 additions and 7 deletions

View File

@ -29,6 +29,9 @@ module VagrantPlugins
params[:ports].each do |pair|
env[:ui].detail(" Port: #{pair}")
end
params[:links].each do |name, other|
env[:ui].detail(" Link: #{name}:#{other}")
end
cid = @driver.create(params)
end
@ -44,12 +47,19 @@ module VagrantPlugins
container_name.gsub!(/[^-a-z0-9_]/i, "")
container_name << "_#{Time.now.to_i}"
links = {}
@provider_config._links.each do |link|
parts = link.split(":", 2)
links[parts[0]] = parts[1]
end
{
cmd: @provider_config.cmd,
env: @provider_config.env,
extra_args: @provider_config.create_args,
hostname: @machine_config.vm.hostname,
image: @provider_config.image,
links: links,
name: container_name,
ports: forwarded_ports,
privileged: @provider_config.privileged,

View File

@ -53,6 +53,7 @@ module VagrantPlugins
@env = {}
@has_ssh = UNSET_VALUE
@image = UNSET_VALUE
@links = []
@ports = []
@privileged = UNSET_VALUE
@remains_running = UNSET_VALUE
@ -61,12 +62,20 @@ module VagrantPlugins
@vagrant_vagrantfile = UNSET_VALUE
end
def link(name)
@links << name
end
def merge(other)
super.tap do |result|
env = {}
env.merge!(@env) if @env
env.merge!(other.env) if other.env
result.env = env
links = _links.dup
links += other._links
result.instance_variable_set(:@links, links)
end
end
@ -85,11 +94,27 @@ module VagrantPlugins
def validate(machine)
errors = _detected_errors
@links.each do |link|
parts = link.split(":")
if parts.length != 2 || parts[0] == "" || parts[1] == ""
errors << I18n.t(
"docker_provider.errors.config.invalid_link", link: link)
end
end
# TODO: Detect if base image has a CMD / ENTRYPOINT set before erroring out
errors << I18n.t("docker_provider.errors.config.cmd_not_set") if @cmd == UNSET_VALUE
{ "docker provider" => errors }
end
#--------------------------------------------------------------
# Functions below should not be called by config files
#--------------------------------------------------------------
def _links
@links
end
end
end
end

View File

@ -16,6 +16,7 @@ module VagrantPlugins
def create(params)
image = params.fetch(:image)
links = params.fetch(:links)
ports = Array(params[:ports])
volumes = Array(params[:volumes])
name = params.fetch(:name)
@ -24,6 +25,7 @@ module VagrantPlugins
run_cmd = %W(docker run --name #{name} -d)
run_cmd += env.map { |k,v| ['-e', "#{k}=#{v}"] }
run_cmd += links.map { |k, v| ['--link', "#{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

@ -86,8 +86,8 @@ en:
and notify them to not use this communicator for anything except the
"docker" provider.
config:
cmd_not_set: |-
The Docker command has not been set!
invalid_link: |-
Invalid link (should be 'name:alias'): "%{link}"
docker_provider_nfs_without_privileged: |-
You've configured a NFS synced folder but didn't enable privileged
mode for the container. Please set the `privileged` option to true

View File

@ -42,13 +42,34 @@ describe VagrantPlugins::DockerProvider::Config do
assert_valid
end
describe "#link" do
it "should be valid with good links" do
subject.link "foo:bar"
subject.link "db:blah"
subject.finalize!
assert_valid
end
it "should be invalid if not name:alias" do
subject.link "foo"
subject.finalize!
assert_invalid
end
it "should be invalid if too many colons" do
subject.link "foo:bar:baz"
subject.finalize!
assert_invalid
end
end
describe "#merge" do
let(:one) { described_class.new }
let(:two) { described_class.new }
subject { one.merge(two) }
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"
@ -59,5 +80,15 @@ describe VagrantPlugins::DockerProvider::Config do
})
end
end
context "links" do
it "should merge the links" do
one.link "foo"
two.link "bar"
expect(subject._links).to eq([
"foo", "bar"])
end
end
end
end