Sha256: 197a3a2620b463b38b68b52b1b61f3404e692f71479c0b8875a7125b76e77e91

Contents?: true

Size: 1.89 KB

Versions: 37

Compression:

Stored size: 1.89 KB

Contents

require "ipaddr"
require "socket"
require "tempfile"

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

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

        def self.configure_networks(machine, networks)
          comm = machine.communicate
          commands = []

          interfaces = machine.guest.capability(:network_interfaces)
          networks.each.with_index do |network, i|
            network[:device] = interfaces[network[:interface]]

            # Arch expects netmasks to be in the "24" or "64", but users may
            # specify IPV4 netmasks like "255.255.255.0". This magic converts
            # the netmask to the proper value.
            if network[:netmask] && network[:netmask].to_s.include?(".")
              network[:netmask] = (32-Math.log2((IPAddr.new(network[:netmask], Socket::AF_INET).to_i^0xffffffff)+1)).to_i
            end

            entry = TemplateRenderer.render("guests/arch/network_#{network[:type]}",
              options: network,
            )

            remote_path = "/tmp/vagrant-network-#{network[:device]}-#{Time.now.to_i}-#{i}"

            Tempfile.open("vagrant-arch-configure-networks") do |f|
              f.binmode
              f.write(entry)
              f.fsync
              f.close
              comm.upload(f.path, remote_path)
            end

            commands << <<-EOH.gsub(/^ {14}/, '').rstrip
              # Configure #{network[:device]}
              mv '#{remote_path}' '/etc/netctl/#{network[:device]}' &&
              ip link set '#{network[:device]}' down &&
              netctl restart '#{network[:device]}' &&
              netctl enable '#{network[:device]}'
            EOH
          end

          # Run all the network modification commands in one communicator call.
          comm.sudo(commands.join(" && \n"))
        end
      end
    end
  end
end

Version data entries

37 entries across 33 versions & 4 rubygems

Version Path
vagrant-unbundled-2.0.4.0 plugins/guests/arch/cap/configure_networks.rb
vagrant-unbundled-2.0.3.0 plugins/guests/arch/cap/configure_networks.rb
vagrant-aws-detiber-0.7.2.pre.4 vendor/bundle/ruby/2.3.0/bundler/gems/vagrant-419afb4dcffe/plugins/guests/arch/cap/configure_networks.rb
vagrant-aws-detiber-0.7.2.pre.3 vendor/bundle/ruby/2.3.0/bundler/gems/vagrant-419afb4dcffe/plugins/guests/arch/cap/configure_networks.rb
vagrant-aws-detiber-0.7.2.pre.2 vendor/bundle/ruby/2.3.0/bundler/gems/vagrant-419afb4dcffe/plugins/guests/arch/cap/configure_networks.rb
vagrant-unbundled-2.0.2.0 plugins/guests/arch/cap/configure_networks.rb
vagrant-unbundled-2.0.1.0 plugins/guests/arch/cap/configure_networks.rb
vagrant-aws-mkubenka-0.7.2.pre.22 vendor/bundle/ruby/2.3.0/bundler/gems/vagrant-f3fdbf414272/plugins/guests/arch/cap/configure_networks.rb
vagrant-aws-mkubenka-0.7.2.pre.16 vendor/bundle/ruby/2.3.0/bundler/gems/vagrant-1ee58c40e3f5/plugins/guests/arch/cap/configure_networks.rb
vagrant-unbundled-2.0.0.1 plugins/guests/arch/cap/configure_networks.rb
vagrant-unbundled-1.9.8.1 plugins/guests/arch/cap/configure_networks.rb
vagrant-unbundled-1.9.7.1 plugins/guests/arch/cap/configure_networks.rb
vagrant-aws-mkubenka-0.7.2.pre.14 vendor/bundle/ruby/2.3.0/bundler/gems/vagrant-5333e60e2d38/plugins/guests/arch/cap/configure_networks.rb
vagrant-aws-mkubenka-0.7.2.pre.11 vendor/bundle/ruby/2.3.0/bundler/gems/vagrant-5333e60e2d38/plugins/guests/arch/cap/configure_networks.rb
vagrant-aws-mkubenka-0.7.2.pre.10 vendor/bundle/ruby/2.3.0/bundler/gems/vagrant-5333e60e2d38/plugins/guests/arch/cap/configure_networks.rb
vagrant-aws-mkubenka-0.7.2.pre.9 vendor/bundle/ruby/2.3.0/bundler/gems/vagrant-5333e60e2d38/plugins/guests/arch/cap/configure_networks.rb
vagrant-unbundled-1.9.5.1 plugins/guests/arch/cap/configure_networks.rb