vaguerent/plugins/providers/docker/action/destroy_network.rb
Brian Cain 5ed5868067
Inspect networks before creating new ones
This commit updates the behavior of how the docker provider creates new
docker networks. It looks at each existing network to see if the
requested subnet has already been configured in the docker engine. If
so, Vagrant will use that network rather than creating a new one. This
includes networks not created by Vagrant. Vagrant will not clean up
these networks if created outside of Vagrant.
2019-03-12 10:36:57 -07:00

46 lines
1.4 KiB
Ruby

require 'log4r'
module VagrantPlugins
module DockerProvider
module Action
class DestroyNetwork
def initialize(app, env)
@app = app
@logger = Log4r::Logger.new('vagrant::plugins::docker::network')
end
def call(env)
# If we are using a host VM, then don't worry about it
machine = env[:machine]
if machine.provider.host_vm?
@logger.debug("Not setting up networks because docker host_vm is in use")
return @app.call(env)
end
machine.config.vm.networks.each do |type, options|
# We only handle private networks
next if type != :private_network
if options[:subnet]
network_name = "vagrant_network_#{options[:subnet]}"
else
network_name = "vagrant_network"
end
# Only cleans up networks defined by Vagrant
if machine.provider.driver.existing_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")
end
end
@app.call(env)
end
end
end
end
end