vagrant/plugins/guests/freebsd/cap/configure_networks.rb

68 lines
2.1 KiB
Ruby
Raw Normal View History

require "tempfile"
require_relative "../../../../lib/vagrant/util/template_renderer"
2013-04-04 18:56:42 +00:00
module VagrantPlugins
module GuestFreeBSD
module Cap
class ConfigureNetworks
include Vagrant::Util
2013-04-04 18:56:42 +00:00
def self.configure_networks(machine, networks)
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.
commands << "sed -i'' -e '/^#VAGRANT-BEGIN/,/^#VAGRANT-END/ d' /etc/rc.conf"
comm.sudo("ifconfig -a | grep -o '^[0-9a-z]*' | grep -v '^lo'", options) do |_, stdout|
interfaces = stdout.split("\n")
end
2014-04-11 23:39:58 +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|
f.binmode
f.write(entry)
f.fsync
f.close
comm.upload(f.path, remote_path)
end
2013-04-04 18:56:42 +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
# 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
comm.sudo(commands.join("\n"), options)
2013-04-04 18:56:42 +00:00
end
end
end
end
end