Class: VagrantPlugins::GuestNetBSD::Cap::ConfigureNetworks

Inherits:
Object
  • Object
show all
Includes:
Vagrant::Util
Defined in:
lib/vagrant-guest-netbsd/cap/configure_networks.rb

Class Method Summary collapse

Class Method Details

.configure_networks(machine, networks) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/vagrant-guest-netbsd/cap/configure_networks.rb', line 11

def self.configure_networks(machine, networks)
  networks.each do |network|
    ifname = "wm#{network[:interface]}"
    newrcconf = "/tmp/rc.conf.vagrant_configurenetworks_#{network[:interface]}"

    machine.communicate.sudo("sed -e '/^#VAGRANT-BEGIN/,/^#VAGRANT-END/ d' /etc/rc.conf > #{newrcconf}")

    # using templates would be cleaner but I can't find a way
    # to use templates in a non-core vagrant plugin -stoned
    machine.communicate.sudo("echo '#VAGRANT-BEGIN' >> #{newrcconf}")

    if network[:type].to_sym == :static
      machine.communicate.sudo(%Q{echo 'ifconfig_#{ifname}="media autoselect up;inet #{network[:ip]} netmask #{network[:netmask]}"' >> #{newrcconf}})
    elsif network[:type].to_sym == :dhcp
      machine.communicate.sudo("echo 'ifconfig_#{ifname}=dhcp' >> #{newrcconf}")
    end

    machine.communicate.sudo("echo '#VAGRANT-END' >> #{newrcconf}")

    machine.communicate.sudo("mv #{newrcconf} /etc/rc.conf")


    # remove old configurations
    machine.communicate.sudo("/sbin/dhcpcd -x #{ifname}", { :error_check => false })
    machine.communicate.sudo("/sbin/ifconfig #{ifname} inet delete", { :error_check => false })

    if network[:type].to_sym == :static
      machine.communicate.sudo("/sbin/ifconfig #{ifname} media autoselect up")
      machine.communicate.sudo("/sbin/ifconfig #{ifname} inet #{network[:ip]} netmask #{network[:netmask]}")
    elsif network[:type].to_sym == :dhcp
      machine.communicate.sudo("/sbin/dhcpcd -n -q #{ifname}")
    end
  end
end