Include synchronization as the environment lock is per process only

This commit is contained in:
Chris Roberts 2019-03-19 13:46:14 -07:00
parent a645ce3c25
commit a1c7eec441
2 changed files with 49 additions and 40 deletions

View File

@ -4,6 +4,9 @@ module VagrantPlugins
module DockerProvider
module Action
class DestroyNetwork
@@lock = Mutex.new
def initialize(app, env)
@app = app
@logger = Log4r::Logger.new('vagrant::plugins::docker::network')
@ -17,21 +20,23 @@ module VagrantPlugins
return @app.call(env)
end
machine.config.vm.networks.each do |type, options|
next if type != :private_network && type != :public_network
@@lock.synchronize do
machine.env.lock("docker-network-destroy", retry: true) do
vagrant_networks = machine.provider.driver.list_network_names.find_all do |n|
n.start_with?("vagrant_network")
end
machine.config.vm.networks.each do |type, options|
next if type != :private_network && type != :public_network
vagrant_networks.each do |network_name|
if machine.provider.driver.existing_named_network?(network_name) &&
!machine.provider.driver.network_used?(network_name)
env[:ui].info(I18n.t("docker_provider.network_destroy", network_name: network_name))
machine.provider.driver.rm_network(network_name)
else
@logger.debug("Network #{network_name} not found or in use")
vagrant_networks = machine.provider.driver.list_network_names.find_all do |n|
n.start_with?("vagrant_network")
end
vagrant_networks.each do |network_name|
if machine.provider.driver.existing_named_network?(network_name) &&
!machine.provider.driver.network_used?(network_name)
env[:ui].info(I18n.t("docker_provider.network_destroy", network_name: network_name))
machine.provider.driver.rm_network(network_name)
else
@logger.debug("Network #{network_name} not found or in use")
end
end
end
end

View File

@ -8,6 +8,8 @@ module VagrantPlugins
include Vagrant::Util::ScopedHashOverride
@@lock = Mutex.new
def initialize(app, env)
@app = app
@logger = Log4r::Logger.new('vagrant::plugins::docker::preparenetworks')
@ -292,37 +294,39 @@ module VagrantPlugins
end
connections = {}
machine.env.lock("docker-network-create", retry: true) do
env[:ui].info(I18n.t("docker_provider.network_create"))
machine.config.vm.networks.each_with_index do |net_info, net_idx|
type, options = net_info
network_options = scoped_hash_override(options, :docker_network)
network_options.delete_if{|k,_| options.key?(k)}
@@lock.synchronize do
machine.env.lock("docker-network-create", retry: true) do
env[:ui].info(I18n.t("docker_provider.network_create"))
machine.config.vm.networks.each_with_index do |net_info, net_idx|
type, options = net_info
network_options = scoped_hash_override(options, :docker_network)
network_options.delete_if{|k,_| options.key?(k)}
case type
when :public_network
network_name, network_options = process_public_network(
options, network_options, env)
when :private_network
network_name, network_options = process_private_network(
options, network_options, env)
else
next # unsupported type so ignore
end
case type
when :public_network
network_name, network_options = process_public_network(
options, network_options, env)
when :private_network
network_name, network_options = process_private_network(
options, network_options, env)
else
next # unsupported type so ignore
end
if !network_name
raise Errors::NetworkInvalidOption, container: machine.name
end
if !network_name
raise Errors::NetworkInvalidOption, container: machine.name
end
if !machine.provider.driver.existing_named_network?(network_name)
@logger.debug("Creating network #{network_name}")
cli_opts = generate_create_cli_arguments(network_options)
machine.provider.driver.create_network(network_name, cli_opts)
else
@logger.debug("Network #{network_name} already created")
validate_network_configuration!(network_name, options, network_options, machine.provider.driver)
if !machine.provider.driver.existing_named_network?(network_name)
@logger.debug("Creating network #{network_name}")
cli_opts = generate_create_cli_arguments(network_options)
machine.provider.driver.create_network(network_name, cli_opts)
else
@logger.debug("Network #{network_name} already created")
validate_network_configuration!(network_name, options, network_options, machine.provider.driver)
end
connections[net_idx] = network_name
end
connections[net_idx] = network_name
end
end