2014-06-09 03:48:52 +00:00
|
|
|
module Vagrant
|
|
|
|
module LXC
|
|
|
|
module Action
|
|
|
|
class PrivateNetworks
|
|
|
|
def initialize(app, env)
|
|
|
|
@app = app
|
|
|
|
end
|
|
|
|
|
|
|
|
def call(env)
|
|
|
|
@app.call(env)
|
|
|
|
|
|
|
|
if private_network_configured?(env[:machine].config)
|
2015-01-11 22:59:38 +00:00
|
|
|
env[:ui].output(I18n.t("vagrant_lxc.messages.setup_private_network"))
|
2014-06-09 03:48:52 +00:00
|
|
|
configure_private_networks(env)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def private_network_configured?(config)
|
|
|
|
config.vm.networks.find do |type, _|
|
|
|
|
type.to_sym == :private_network
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def configure_private_networks(env)
|
2016-01-16 17:49:22 +00:00
|
|
|
env[:machine].config.vm.networks.find_all.each do |type, config|
|
2014-06-09 03:48:52 +00:00
|
|
|
next if type.to_sym != :private_network
|
|
|
|
|
2016-01-16 17:49:22 +00:00
|
|
|
container_name = env[:machine].provider.driver.container_name
|
|
|
|
container_interface = config[:interface]
|
|
|
|
address_type = config[:type]
|
|
|
|
ip = config[:ip]
|
|
|
|
bridge_ip = config.fetch(:lxc__bridge_ip) { build_bridge_ip(ip) }
|
|
|
|
bridge = config.fetch(:lxc__bridge_name)
|
2015-01-08 05:31:01 +00:00
|
|
|
|
2016-01-16 17:49:22 +00:00
|
|
|
env[:machine].provider.driver.configure_private_network(bridge, bridge_ip, container_name, container_interface, address_type, ip)
|
2014-06-09 03:48:52 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-01-08 05:31:01 +00:00
|
|
|
def build_bridge_ip(ip)
|
2015-03-09 02:58:29 +00:00
|
|
|
if ip
|
|
|
|
ip.sub(/^(\d+\.\d+\.\d+)\.\d+/, '\1.254')
|
|
|
|
end
|
2015-01-08 05:31:01 +00:00
|
|
|
end
|
2014-06-09 03:48:52 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|