From cff57c8d01e1be63681b8f558bf57393725f4789 Mon Sep 17 00:00:00 2001
From: Mitchell Hashimoto <mitchell.hashimoto@gmail.com>
Date: Wed, 22 Oct 2014 12:07:49 -0700
Subject: [PATCH] core: trigger machine_id_changed for reload [GH-3963]

---
 CHANGELOG.md                                    | 7 +++++++
 lib/vagrant/machine.rb                          | 8 ++++++++
 plugins/providers/docker/action/host_machine.rb | 7 +++++++
 test/unit/vagrant/machine_test.rb               | 5 ++++-
 4 files changed, 26 insertions(+), 1 deletion(-)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index d79518d86..1fe5188bd 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -20,10 +20,17 @@ BUG FIXES:
   - providers/docker: `vagrant share` uses correct IP of proxy VM if it
       exists. [GH-4342]
   - providers/docker: `vagrant\_vagrantfile` expands home directory. [GH-4000]
+  - providers/docker: Fix issue where multiple identical proxy VMs would
+      be created. [GH-3963]
   - providers/virtualbox: Show a human-friendly error if VirtualBox didn't
       clean up an existing VM. [GH-4681]
   - provisioners/docker: Search for docker binary in multiple places. [GH-4580]
 
+PLUGIN AUTHOR CHANGES:
+
+  - `Machine#reload` will now properly trigger the `machine\_id\_changed`
+      callback on providers.
+
 ## 1.6.5 (September 4, 2014)
 
 BUG FIXES:
diff --git a/lib/vagrant/machine.rb b/lib/vagrant/machine.rb
index e76e25730..ed077fd6e 100644
--- a/lib/vagrant/machine.rb
+++ b/lib/vagrant/machine.rb
@@ -342,6 +342,7 @@ module Vagrant
 
     # This reloads the ID of the underlying machine.
     def reload
+      old_id = @id
       @id = nil
 
       if @data_dir
@@ -350,6 +351,13 @@ module Vagrant
         id_file = @data_dir.join("id")
         @id = id_file.read.chomp if id_file.file?
       end
+
+      if @id != old_id && @provider
+        # It changed, notify the provider
+        @provider.machine_id_changed
+      end
+
+      @id
     end
 
     # This returns the SSH info for accessing this machine. This SSH info
diff --git a/plugins/providers/docker/action/host_machine.rb b/plugins/providers/docker/action/host_machine.rb
index 2f38df5b9..c0e518fe8 100644
--- a/plugins/providers/docker/action/host_machine.rb
+++ b/plugins/providers/docker/action/host_machine.rb
@@ -44,6 +44,13 @@ module VagrantPlugins
           proxy_ui.opts[:prefix_spaces] = true
           proxy_ui.opts[:target] = env[:machine].name.to_s
 
+          # Reload the machine so that if it was created while we didn't
+          # hold the lock, we'll see the updated state.
+          host_machine.reload
+
+          p host_machine.id
+          p host_machine.ssh_info
+
           # See if the machine is ready already. If not, start it.
           if host_machine.communicate.ready?
             env[:machine].ui.detail(I18n.t("docker_provider.host_machine_ready"))
diff --git a/test/unit/vagrant/machine_test.rb b/test/unit/vagrant/machine_test.rb
index d8e55e346..063043399 100644
--- a/test/unit/vagrant/machine_test.rb
+++ b/test/unit/vagrant/machine_test.rb
@@ -451,11 +451,12 @@ describe Vagrant::Machine do
   describe "#reload" do
     before do
       allow(provider).to receive(:machine_id_changed)
-
       subject.id = "foo"
     end
 
     it "should read the ID" do
+      expect(provider).to_not receive(:machine_id_changed)
+
       subject.reload
 
       expect(subject.id).to eq("foo")
@@ -464,6 +465,8 @@ describe Vagrant::Machine do
     it "should read the updated ID" do
       new_instance.id = "bar"
 
+      expect(provider).to receive(:machine_id_changed)
+
       subject.reload
 
       expect(subject.id).to eq("bar")