Module: VagrantPlugins::ProviderLibvirt::Util::NetworkUtil

Includes:
Vagrant::Util::NetworkIP
Included in:
Action::CreateNetworkInterfaces, Action::CreateNetworks
Defined in:
lib/vagrant-libvirt/util/network_util.rb

Instance Method Summary collapse

Instance Method Details

#configured_networks(env, logger) ⇒ Object



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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/vagrant-libvirt/util/network_util.rb', line 10

def configured_networks(env, logger)
  management_network_name = env[:machine].provider_config.management_network_name
  management_network_address = env[:machine].provider_config.management_network_address
  management_network_mode = env[:machine].provider_config.management_network_mode
  management_network_mac = env[:machine].provider_config.management_network_mac
  management_network_guest_ipv6 = env[:machine].provider_config.management_network_guest_ipv6
  management_network_autostart = env[:machine].provider_config.management_network_autostart
  logger.info "Using #{management_network_name} at #{management_network_address} as the management network #{management_network_mode} is the mode"

  begin
    management_network_ip = IPAddr.new(management_network_address)
  rescue ArgumentError
    raise Errors::ManagementNetworkError,
          error_message: "#{management_network_address} is not a valid IP address"
  end

  # capture address into $1 and mask into $2
  management_network_ip.inspect =~ /IPv4:(.*)\/(.*)>/

  if Regexp.last_match(2) == '255.255.255.255'
    raise Errors::ManagementNetworkError,
          error_message: "#{management_network_address} does not include both an address and subnet mask"
  end

  management_network_options = {
    iface_type: :private_network,
    network_name: management_network_name,
    ip: Regexp.last_match(1),
    netmask: Regexp.last_match(2),
    dhcp_enabled: true,
    forward_mode: management_network_mode,
    guest_ipv6: management_network_guest_ipv6,
    autostart: management_network_autostart
  }

  unless management_network_mac.nil?
    management_network_options[:mac] = management_network_mac
  end

  if (env[:machine].config.vm.box &&
      !env[:machine].provider_config.mgmt_attach)
    raise Errors::ManagementNetworkRequired
  end

  # add management network to list of networks to check
  # unless mgmt_attach set to false
  networks = if env[:machine].provider_config.mgmt_attach
               [management_network_options]
             else
               []
             end

  env[:machine].config.vm.networks.each do |type, original_options|
    logger.debug "In config found network type #{type} options #{original_options}"
    # Options can be specified in Vagrantfile in short format (:ip => ...),
    # or provider format # (:libvirt__network_name => ...).
    # https://github.com/mitchellh/vagrant/blob/master/lib/vagrant/util/scoped_hash_override.rb
    options = scoped_hash_override(original_options, :libvirt)
    # store type in options
    # use default values if not already set
    options = {
      iface_type:  type,
      netmask:      '255.255.255.0',
      dhcp_enabled: true,
      forward_mode: 'nat'
    }.merge(options)

    if options[:type].to_s == 'dhcp' && options[:ip].nil?
      options[:network_name] = 'vagrant-private-dhcp'
    end

    # add to list of networks to check
    networks.push(options)
  end

  networks
end

#libvirt_networks(libvirt_client) ⇒ Object

Return a list of all (active and inactive) libvirt networks as a list of hashes with their name, network address and status (active or not)



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/vagrant-libvirt/util/network_util.rb', line 90

def libvirt_networks(libvirt_client)
  libvirt_networks = []

  active = libvirt_client.list_networks
  inactive = libvirt_client.list_defined_networks

  # Iterate over all (active and inactive) networks.
  active.concat(inactive).each do |network_name|
    libvirt_network = libvirt_client.lookup_network_by_name(
      network_name
    )

    # Parse ip address and netmask from the network xml description.
    xml = Nokogiri::XML(libvirt_network.xml_desc)
    ip = xml.xpath('/network/ip/@address').first
    ip = ip.value if ip
    netmask = xml.xpath('/network/ip/@netmask').first
    netmask = netmask.value if netmask

    dhcp_enabled = if xml.at_xpath('//network/ip/dhcp')
                     true
                   else
                     false
                   end

    # Calculate network address of network from ip address and
    # netmask.
    network_address = (network_address(ip, netmask) if ip && netmask)

    libvirt_networks << {
      name:             network_name,
      ip_address:       ip,
      netmask:          netmask,
      network_address:  network_address,
      dhcp_enabled:     dhcp_enabled,
      bridge_name:      libvirt_network.bridge_name,
      created:          true,
      active:           libvirt_network.active?,
      autostart:        libvirt_network.autostart?,
      libvirt_network:  libvirt_network
    }
  end

  libvirt_networks
end