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

53 lines
1.8 KiB
Ruby
Raw Normal View History

require "tempfile"
require "vagrant/util/template_renderer"
module VagrantPlugins
2013-08-06 01:07:45 +00:00
module GuestDarwin
module Cap
class ConfigureNetworks
include Vagrant::Util
def self.configure_networks(machine, networks)
# Slightly different than other plugins, using the template to build commands
# rather than templating the files.
machine.communicate.sudo("networksetup -detectnewhardware")
machine.communicate.sudo("networksetup -listnetworkserviceorder > /tmp/vagrant.interfaces")
tmpints = File.join(Dir.tmpdir, File.basename("#{machine.id}.interfaces"))
machine.communicate.download("/tmp/vagrant.interfaces",tmpints)
devlist = []
ints = ::IO.read(tmpints)
2013-08-06 01:07:45 +00:00
ints.split(/\n\n/m).each do |i|
if i.match(/Hardware/) and not i.match(/Ethernet/).nil?
devmap = {}
# Ethernet, should be 2 lines,
# (3) Thunderbolt Ethernet
# (Hardware Port: Thunderbolt Ethernet, Device: en1)
# multiline, should match "Thunderbolt Ethernet", "en1"
devicearry = i.match(/\([0-9]+\) (.+)\n.*Device: (.+)\)/m)
devmap[:interface] = devicearry[2]
devmap[:service] = devicearry[1]
devlist << devmap
end
end
File.delete(tmpints)
2013-08-06 01:07:45 +00:00
networks.each do |network|
2013-08-06 14:18:38 +00:00
service_name = devlist[network[:interface]][:service]
2013-08-06 01:07:45 +00:00
if network[:type].to_sym == :static
2013-08-06 14:18:38 +00:00
command = "networksetup -setmanual \"#{service_name}\" #{network[:ip]} #{network[:netmask]}"
2013-08-06 01:07:45 +00:00
elsif network[:type].to_sym == :dhcp
2013-08-06 14:18:38 +00:00
command = "networksetup -setdhcp \"#{service_name}\""
2013-08-06 01:07:45 +00:00
end
machine.communicate.sudo(command)
end
end
end
end
end
end