Class: VagrantPlugins::Openstack::Utils

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant-openstack-illuin-provider/utils.rb

Instance Method Summary collapse

Constructor Details

#initializeUtils

Returns a new instance of Utils.



4
5
6
# File 'lib/vagrant-openstack-illuin-provider/utils.rb', line 4

def initialize
  @logger = Log4r::Logger.new('vagrant_openstack::action::config_resolver')
end

Instance Method Details

#get_ip_address(env) ⇒ Object



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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/vagrant-openstack-illuin-provider/utils.rb', line 8

def get_ip_address(env)
  addresses = env[:openstack_client].nova.get_server_details(env, env[:machine].id)['addresses']
  # First, try to get a floating ip with the right version. If none of the floating ip is of the right version,
  # return the first floating ip anyway.
  fallback = nil
  addresses.each do |_, network|
    network.each do |network_detail|
      next unless network_detail['OS-EXT-IPS:type'] == 'floating'
      if env[:machine].provider_config.ip_version.nil?
        return network_detail['addr']
      elsif network_detail['version'] == env[:machine].provider_config.ip_version
        return network_detail['addr']
      end
      fallback ||= network_detail['addr']
    end
  end
  return fallback unless fallback.nil?

  fail Errors::UnableToResolveIP if addresses.size == 0

  # If only one network, we don't use networks config and return the first IP of the correct version
  # If no IP for this version, return the first IP anyway
  if addresses.size == 1
    net_addresses = addresses.first[1]
    fail Errors::UnableToResolveIP if net_addresses.size == 0
    if env[:machine].provider_config.ip_version.nil?
      return net_addresses[0]['addr']
    else
      right_version_ip = filter_by_version(net_addresses, env[:machine].provider_config.ip_version)
      if right_version_ip.nil?
        return net_addresses[0]['addr']
      else
        return right_version_ip
      end
    end
  end

  # If multiple networks exist, follow the order of the networks config and return the first IP of the correct
  # version, be it in the first network or not.
  fallback = nil
  env[:machine].provider_config.networks.each do |network|
    if network.is_a? String
      net_addresses = addresses[network]
    else
      net_addresses = addresses[network[:name]]
    end
    next if net_addresses.size == 0
    fallback ||= net_addresses[0]['addr']
    if env[:machine].provider_config.ip_version.nil?
      return net_addresses[0]['addr']
    else
      right_version_ip = filter_by_version(net_addresses, env[:machine].provider_config.ip_version)
      if right_version_ip.nil?
        next
      else
        return right_version_ip
      end
    end
  end
  fail Errors::UnableToResolveIP if fallback.nil?
  fallback
end