File: configure_networks.rb

package info (click to toggle)
vagrant 2.3.7%2Bgit20230731.5fc64cde%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 17,616 kB
  • sloc: ruby: 111,820; sh: 462; makefile: 123; ansic: 34; lisp: 1
file content (53 lines) | stat: -rw-r--r-- 1,989 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
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
45
46
47
48
49
50
51
52
53
require "tempfile"

require_relative "../../../../lib/vagrant/util/template_renderer"

module VagrantPlugins
  module GuestNetBSD
    module Cap
      class ConfigureNetworks
        include Vagrant::Util

        def self.configure_networks(machine, networks)

          # setup a new rc.conf file
          newrcconf = "/tmp/rc.conf.vagrant_configurenetworks"
          machine.communicate.sudo("sed -e '/^#VAGRANT-BEGIN/,/^#VAGRANT-END/ d' /etc/rc.conf > #{newrcconf}")

          networks.each do |network|

            # create an interface configuration file fragment
            entry = TemplateRenderer.render("guests/netbsd/network_#{network[:type]}",
                                            options: network)

            Tempfile.open("vagrant-netbsd-configure-networks") do |f|
              f.binmode
              f.write(entry)
              f.fsync
              f.close
              machine.communicate.upload(f.path, "/tmp/vagrant-network-entry")
            end

            machine.communicate.sudo("cat /tmp/vagrant-network-entry >> #{newrcconf}")
            machine.communicate.sudo("rm -f /tmp/vagrant-network-entry")

            ifname = "wm#{network[:interface]}"
            # remove old configuration
            machine.communicate.sudo("/sbin/dhcpcd -x #{ifname}", { error_check: false })
            machine.communicate.sudo("/sbin/ifconfig #{ifname} inet delete", { error_check: false })

            # live new configuration
            if network[:type].to_sym == :static
              machine.communicate.sudo("/sbin/ifconfig #{ifname} media autoselect up;/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

          # install new rc.conf
          machine.communicate.sudo("install -c -o 0 -g 0 -m 644 #{newrcconf} /etc/rc.conf")
        end
      end
    end
  end
end