Add basic ability to configure some networks for containers

This commit is contained in:
Brian Cain 2019-02-26 15:00:04 -08:00
parent e860c7709d
commit 4dc5f7c330
No known key found for this signature in database
GPG Key ID: 9FC4639B2E4510A0
3 changed files with 54 additions and 6 deletions

View File

@ -23,6 +23,14 @@ module VagrantPlugins
# If network is defined in machines config as isolated single container network, then delete
# If it's custom and or default, check if other containers are using it, and if not, delete
network_name = "#{env[:root_path].basename.to_s}_network_#{machine.name}"
if machine.provider.driver.existing_network?(network_name) &&
!machine.provider.driver.network_used?(network_name)
env[:ui].info("Removing network #{network_name}")
machine.provider.driver.rm_network(network_name)
else
@logger.debug("Network #{network_name} not found")
end
end
@app.call(env)

View File

@ -17,6 +17,8 @@ module VagrantPlugins
return @app.call(env)
end
env[:ui].info("Configuring and enabling network interfaces...")
machine.config.vm.networks.each do |type, options|
# We only handle private and public networks
next if type != :private_network && type != :public_network
@ -26,12 +28,31 @@ module VagrantPlugins
# that all containers with this default option are attached to
# - If provider config option is defined, create the requested network
# and attach that container to it
connect_cli_opts = []
create_cli_opts = []
# make this a function that generates the proper flags
if options[:type] != "dhcp"
if options[:subnet]
create_cli_opts.concat(["--subnet", options[:subnet]])
end
if options[:ip]
connect_cli_opts.concat(["--ip", options[:ip]])
end
end
network_name = "#{env[:root_path].basename.to_s}_network_#{machine.name}"
container_id = machine.id
# TODO: Need to check if network already exists and not error
machine.provider.driver.create_network(network_name)
options = ["--ip", options[:ip]]
machine.provider.driver.connect_network(network_name, container_id, options)
if !machine.provider.driver.existing_network?(network_name)
@logger.debug("Creating network #{network_name}")
machine.provider.driver.create_network(network_name, create_cli_opts)
else
@logger.debug("Network #{network_name} already created")
end
@logger.debug("Connecting network #{network_name} to container guest #{machine.name}")
machine.provider.driver.connect_network(network_name, container_id, connect_cli_opts)
end
@app.call(env)

View File

@ -205,8 +205,8 @@ module VagrantPlugins
# @param[Array] networks - list of networks to inspect
# @param[Array] opts - An array of flags used for listing networks
def inspect_network(networks, *opts)
command = ['docker', 'network', 'inspect', networks].concat(*opts)
def inspect_network(network, *opts)
command = ['docker', 'network', 'inspect', network].concat(*opts)
output = execute(*command)
output
end
@ -227,12 +227,31 @@ module VagrantPlugins
# @param[String] network - name of network to remove
# @param[Array] opts - An array of flags used for listing networks
def rm_network(networks, *opts)
def rm_network(network, *opts)
command = ['docker', 'network', 'rm', network].concat(*opts)
output = execute(*command)
output
end
# Docker network helpers
# @param[String] network - name of network to look for
def existing_network?(network)
result = list_network(["--format='{{json .Name}}'"])
result.include?(network)
end
# @param[String] network - name of network to look for
def network_used?(network)
result = inspect_network(network)
begin
result = JSON.parse(result)
return result.first["Containers"].size > 0
rescue JSON::ParserError => e
# Could not parse result of network inspection
end
end
# @param[Array] opts - An array of flags used for listing networks
def execute(*cmd, **opts, &block)
@executor.execute(*cmd, **opts, &block)