Merge pull request #7190 from electrofelix/ansible-parallel-race

Fix a race condition in the concurrent generations of the ansible inventory file, while running `vagrant up --parallel`.

Closes GH-6526
This commit is contained in:
Gilles Cornu 2016-06-09 14:56:03 +02:00
commit 6ee69e3150
2 changed files with 21 additions and 7 deletions

View File

@ -109,6 +109,7 @@ module Vagrant
@provider_options = provider_options
@ui = Vagrant::UI::Prefixed.new(@env.ui, @name)
@ui_mutex = Mutex.new
@state_mutex = Mutex.new
# Read the ID, which is usually in local storage
@id = nil
@ -507,11 +508,17 @@ module Vagrant
# master index.
uuid = index_uuid
if uuid
entry = @env.machine_index.get(uuid)
if entry
entry.state = result.short_description
@env.machine_index.set(entry)
@env.machine_index.release(entry)
# active_machines provides access to query this info on each machine
# from a different thread, ensure multiple machines do not access
# the locked entry simultaneously as this triggers a locked machine
# exception.
@state_mutex.synchronize do
entry = @env.machine_index.get(uuid)
if entry
entry.state = result.short_description
@env.machine_index.set(entry)
@env.machine_index.release(entry)
end
end
end

View File

@ -139,8 +139,15 @@ module VagrantPlugins
inventory_file = Pathname.new(File.join(inventory_path, 'vagrant_ansible_inventory'))
@@lock.synchronize do
if !File.exists?(inventory_file) or inventory_content != File.read(inventory_file)
inventory_file.open('w') do |file|
file.write(inventory_content)
begin
# ansible dir inventory will ignore files starting with '.'
inventory_tmpfile = Tempfile.new('.vagrant_ansible_inventory', inventory_path)
inventory_tmpfile.write(inventory_content)
inventory_tmpfile.close
File.rename(inventory_tmpfile.path, inventory_file)
ensure
inventory_tmpfile.close
inventory_tmpfile.unlink
end
end
end