From 58ddc66b9cccfb123225b4235c57c3de8f926dc6 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Thu, 17 Apr 2014 18:33:46 -0700 Subject: [PATCH] providers/docker: can specify links --- plugins/providers/docker/action/create.rb | 10 +++++ plugins/providers/docker/config.rb | 25 +++++++++++ plugins/providers/docker/driver.rb | 2 + templates/locales/providers_docker.yml | 4 +- .../plugins/providers/docker/config_spec.rb | 41 ++++++++++++++++--- 5 files changed, 75 insertions(+), 7 deletions(-) diff --git a/plugins/providers/docker/action/create.rb b/plugins/providers/docker/action/create.rb index 0a41239a3..d0d65f7e8 100644 --- a/plugins/providers/docker/action/create.rb +++ b/plugins/providers/docker/action/create.rb @@ -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, diff --git a/plugins/providers/docker/config.rb b/plugins/providers/docker/config.rb index 75363d9aa..a97ce250a 100644 --- a/plugins/providers/docker/config.rb +++ b/plugins/providers/docker/config.rb @@ -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 diff --git a/plugins/providers/docker/driver.rb b/plugins/providers/docker/driver.rb index d624547c7..116db4700 100644 --- a/plugins/providers/docker/driver.rb +++ b/plugins/providers/docker/driver.rb @@ -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] diff --git a/templates/locales/providers_docker.yml b/templates/locales/providers_docker.yml index d45d8b1bc..255ec44e0 100644 --- a/templates/locales/providers_docker.yml +++ b/templates/locales/providers_docker.yml @@ -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 diff --git a/test/unit/plugins/providers/docker/config_spec.rb b/test/unit/plugins/providers/docker/config_spec.rb index e89b477e5..9f05637d5 100644 --- a/test/unit/plugins/providers/docker/config_spec.rb +++ b/test/unit/plugins/providers/docker/config_spec.rb @@ -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