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

53 lines
1.9 KiB
Ruby
Raw Normal View History

2013-04-04 18:56:42 +00:00
require "tempfile"
require "vagrant/util/template_renderer"
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)
# Remove any previous network additions to the configuration file.
machine.communicate.sudo("sed -i '' -e '/^#VAGRANT-BEGIN/,/^#VAGRANT-END/ d' /etc/rc.conf", {:shell => "sh"})
2013-04-04 18:56:42 +00:00
networks.each do |network|
2014-04-11 23:39:58 +00:00
# Determine the interface prefix...
command = "ifconfig -a | grep -o ^[0-9a-z]*"
result = ""
ifname = ""
machine.communicate.execute(command) do |type, data|
result << data if type == :stdout
if result.split(/\n/).any?{|i| i.match(/vtnet*/)}
ifname = "vtnet#{network[:interface]}"
2014-04-11 23:39:58 +00:00
else
ifname = "em#{network[:interface]}"
end
end
entry = TemplateRenderer.render("guests/freebsd/network_#{network[:type]}",
2013-04-04 18:56:42 +00:00
:options => network)
# Write the entry to a temporary location
temp = Tempfile.new("vagrant")
temp.binmode
temp.write(entry)
temp.close
machine.communicate.upload(temp.path, "/tmp/vagrant-network-entry")
machine.communicate.sudo("su -m root -c 'cat /tmp/vagrant-network-entry >> /etc/rc.conf'", {:shell => "sh"})
machine.communicate.sudo("rm /tmp/vagrant-network-entry", {:shell => "sh"})
2013-04-04 18:56:42 +00:00
if network[:type].to_sym == :static
2014-04-11 23:39:58 +00:00
machine.communicate.sudo("ifconfig #{ifname} inet #{network[:ip]} netmask #{network[:netmask]}", {:shell => "sh"})
2013-04-04 18:56:42 +00:00
elsif network[:type].to_sym == :dhcp
2014-04-11 23:39:58 +00:00
machine.communicate.sudo("dhclient #{ifname}", {:shell => "sh"})
2013-04-04 18:56:42 +00:00
end
end
end
end
end
end
end