Class: VagrantPlugins::Openstack::ConfigResolver

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

Instance Method Summary collapse

Constructor Details

#initializeConfigResolver

Returns a new instance of ConfigResolver.



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

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

Instance Method Details

#resolve_flavor(env) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/vagrant-openstack-illuin-provider/config_resolver.rb', line 14

def resolve_flavor(env)
  @logger.info 'Resolving flavor'
  config = env[:machine].provider_config
  nova = env[:openstack_client].nova
  env[:ui].info(I18n.t('vagrant_openstack.finding_flavor'))
  flavors = nova.get_all_flavors(env)
  @logger.info "Finding flavor matching name '#{config.flavor}'"
  flavor = find_matching(flavors, config.flavor)
  fail Errors::NoMatchingFlavor unless flavor
  flavor
end

#resolve_floating_ip(env) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/vagrant-openstack-illuin-provider/config_resolver.rb', line 36

def resolve_floating_ip(env)
  config = env[:machine].provider_config
  nova = env[:openstack_client].nova
  return config.floating_ip if config.floating_ip

  fail Errors::UnableToResolveFloatingIP if config.floating_ip_pool.nil? || config.floating_ip_pool.empty?

  @logger.debug 'Searching for available ips'
  free_ip = search_free_ip(config, nova, env)
  config.floating_ip = free_ip
  return free_ip unless free_ip.nil?

  @logger.debug 'Allocate new ip anyway'
  allocated_ip = allocate_ip(config, nova, env)
  config.floating_ip = allocated_ip
  return allocated_ip unless allocated_ip.nil?
end

#resolve_image(env) ⇒ Object



26
27
28
29
# File 'lib/vagrant-openstack-illuin-provider/config_resolver.rb', line 26

def resolve_image(env)
  @logger.info 'Resolving image'
  resolve_image_internal(env, env[:machine].provider_config.image)
end

#resolve_keypair(env) ⇒ Object



54
55
56
57
58
59
60
61
62
# File 'lib/vagrant-openstack-illuin-provider/config_resolver.rb', line 54

def resolve_keypair(env)
  config = env[:machine].provider_config
  machine_config = env[:machine].config
  nova = env[:openstack_client].nova
  return nil unless machine_config.ssh.insert_key
  return config.keypair_name if config.keypair_name
  return nova.import_keypair_from_file(env, config.public_key_path) if config.public_key_path
  generate_keypair(env)
end

#resolve_networks(env) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/vagrant-openstack-illuin-provider/config_resolver.rb', line 64

def resolve_networks(env)
  @logger.info 'Resolving network(s)'
  config = env[:machine].provider_config
  return [] if config.networks.nil? || config.networks.empty?
  env[:ui].info(I18n.t('vagrant_openstack.finding_networks'))
  return resolve_networks_without_network_service(env) unless env[:openstack_client].session.endpoints.key? :network

  all_networks = env[:openstack_client].neutron.get_all_networks(env)
  all_network_ids = all_networks.map(&:id)

  networks = []
  config.networks.each do |network|
    networks << resolve_network(network, all_networks, all_network_ids)
  end
  @logger.debug("Resolved networks : #{networks.to_json}")
  networks
end

#resolve_security_groups(env) ⇒ Object



135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/vagrant-openstack-illuin-provider/config_resolver.rb', line 135

def resolve_security_groups(env)
  groups = []
  env[:machine].provider_config.security_groups.each do |group|
    case group
    when String
      groups << { name: group }
    when Hash
      groups << group
    end
  end unless env[:machine].provider_config.security_groups.nil?
  groups
end

#resolve_ssh_port(env) ⇒ Object



8
9
10
11
12
# File 'lib/vagrant-openstack-illuin-provider/config_resolver.rb', line 8

def resolve_ssh_port(env)
  machine_config = env[:machine].config
  return machine_config.ssh.port if machine_config.ssh.port
  22
end

#resolve_ssh_username(env) ⇒ Object



127
128
129
130
131
132
133
# File 'lib/vagrant-openstack-illuin-provider/config_resolver.rb', line 127

def resolve_ssh_username(env)
  config = env[:machine].provider_config
  machine_config = env[:machine].config
  return machine_config.ssh.username if machine_config.ssh.username
  return config.ssh_username if config.ssh_username
  fail Errors::NoMatchingSshUsername
end

#resolve_volume_boot(env) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/vagrant-openstack-illuin-provider/config_resolver.rb', line 82

def resolve_volume_boot(env)
  config = env[:machine].provider_config
  return nil if config.volume_boot.nil?
  return resolve_volume_without_volume_service(env, config.volume_boot, 'vda') unless env[:openstack_client].session.endpoints.key? :volume

  volume_list = env[:openstack_client].cinder.get_all_volumes(env)
  volume_ids = volume_list.map(&:id)

  @logger.debug(volume_list)

  volume = resolve_volume(config.volume_boot, volume_list, volume_ids)

  device = (volume[:device].nil?) ? 'vda' : volume[:device]
  size = (volume[:size].nil?) ? nil : volume[:size]
  delete_on_destroy = (volume[:delete_on_destroy].nil?) ? nil : volume[:delete_on_destroy]

  image = resolve_volume_boot_image(env) unless volume[:image].nil?
  image_id = (image.nil?) ? nil : image.id
  if image.nil?
    return { id: volume[:id], device: device }
  else
    { image: image_id, device: device, size: size, delete_on_destroy: delete_on_destroy }
  end
end

#resolve_volume_boot_image(env) ⇒ Object



31
32
33
34
# File 'lib/vagrant-openstack-illuin-provider/config_resolver.rb', line 31

def resolve_volume_boot_image(env)
  @logger.info 'Resolving image to create a volume from'
  resolve_image_internal(env, env[:machine].provider_config.volume_boot[:image])
end

#resolve_volumes(env) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/vagrant-openstack-illuin-provider/config_resolver.rb', line 107

def resolve_volumes(env)
  @logger.info 'Resolving volume(s)'
  config = env[:machine].provider_config
  return [] if config.volumes.nil? || config.volumes.empty?
  env[:ui].info(I18n.t('vagrant_openstack.finding_volumes'))
  return resolve_volumes_without_volume_service(env) unless env[:openstack_client].session.endpoints.key? :volume

  volume_list = env[:openstack_client].cinder.get_all_volumes(env)
  volume_ids = volume_list.map(&:id)

  @logger.debug(volume_list)

  volumes = []
  config.volumes.each do |volume|
    volumes << resolve_volume(volume, volume_list, volume_ids)
  end
  @logger.debug("Resolved volumes : #{volumes.to_json}")
  volumes
end