2016-05-31 03:17:02 +00:00
|
|
|
require "tempfile"
|
|
|
|
|
2016-05-29 03:17:40 +00:00
|
|
|
require_relative "../../../../lib/vagrant/util/template_renderer"
|
2013-04-04 18:56:42 +00:00
|
|
|
|
|
|
|
module VagrantPlugins
|
|
|
|
module GuestFreeBSD
|
|
|
|
module Cap
|
|
|
|
class ConfigureNetworks
|
2013-04-08 17:47:19 +00:00
|
|
|
include Vagrant::Util
|
2013-04-04 18:56:42 +00:00
|
|
|
|
|
|
|
def self.configure_networks(machine, networks)
|
2016-06-05 17:15:19 +00:00
|
|
|
options = { shell: "sh" }
|
|
|
|
comm = machine.communicate
|
|
|
|
|
|
|
|
commands = []
|
|
|
|
interfaces = []
|
|
|
|
|
2013-04-04 18:56:42 +00:00
|
|
|
# Remove any previous network additions to the configuration file.
|
2018-04-19 17:04:27 +00:00
|
|
|
commands << "sed -i '' -e '/^#VAGRANT-BEGIN/,/^#VAGRANT-END/ d' /etc/rc.conf"
|
2016-06-05 17:15:19 +00:00
|
|
|
|
2017-07-07 14:03:38 +00:00
|
|
|
comm.sudo("ifconfig -a | grep -o '^[0-9a-z]*' | grep -v '^lo'", options) do |_, stdout|
|
2016-06-05 17:15:19 +00:00
|
|
|
interfaces = stdout.split("\n")
|
|
|
|
end
|
2014-04-11 23:39:58 +00:00
|
|
|
|
2016-06-05 17:15:19 +00:00
|
|
|
networks.each.with_index do |network, i|
|
|
|
|
network[:device] = interfaces[network[:interface]]
|
|
|
|
|
|
|
|
entry = TemplateRenderer.render("guests/freebsd/network_#{network[:type]}",
|
|
|
|
options: network,
|
|
|
|
)
|
|
|
|
|
|
|
|
remote_path = "/tmp/vagrant-network-#{network[:device]}-#{Time.now.to_i}-#{i}"
|
2013-04-04 18:56:42 +00:00
|
|
|
|
2016-05-31 04:18:16 +00:00
|
|
|
Tempfile.open("vagrant-freebsd-configure-networks") do |f|
|
2016-05-31 03:17:02 +00:00
|
|
|
f.binmode
|
2016-05-29 03:17:40 +00:00
|
|
|
f.write(entry)
|
|
|
|
f.fsync
|
|
|
|
f.close
|
2016-06-05 17:15:19 +00:00
|
|
|
comm.upload(f.path, remote_path)
|
2016-05-29 03:17:40 +00:00
|
|
|
end
|
2013-04-04 18:56:42 +00:00
|
|
|
|
2016-06-05 17:15:19 +00:00
|
|
|
commands << <<-EOH.gsub(/^ {14}/, '')
|
|
|
|
cat '#{remote_path}' >> /etc/rc.conf
|
|
|
|
rm -f '#{remote_path}'
|
|
|
|
EOH
|
2013-04-04 18:56:42 +00:00
|
|
|
|
2016-06-05 17:15:19 +00:00
|
|
|
# If the network is DHCP, then we have to start the dhclient, unless
|
|
|
|
# it is already running. See GH-5852 for more information
|
|
|
|
if network[:type].to_sym == :dhcp
|
|
|
|
file = "/var/run/dhclient.#{network[:device]}.pid"
|
|
|
|
commands << <<-EOH.gsub(/^ {16}/, '')
|
|
|
|
if ! test -f '#{file}' || ! kill -0 $(cat '#{file}'); then
|
|
|
|
dhclient '#{network[:device]}'
|
|
|
|
fi
|
|
|
|
EOH
|
|
|
|
end
|
|
|
|
|
|
|
|
# For some reason, this returns status 1... every time
|
|
|
|
commands << "/etc/rc.d/netif restart '#{network[:device]}' || true"
|
2013-04-04 18:56:42 +00:00
|
|
|
end
|
2016-06-05 17:15:19 +00:00
|
|
|
|
|
|
|
comm.sudo(commands.join("\n"), options)
|
2013-04-04 18:56:42 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|