Class: Bosh::Registry::InstanceManager::Openstack

Inherits:
Bosh::Registry::InstanceManager show all
Defined in:
lib/bosh/registry/instance_manager/openstack.rb

Instance Method Summary collapse

Methods inherited from Bosh::Registry::InstanceManager

#delete_settings, #read_settings, #update_settings

Constructor Details

#initialize(cloud_config) ⇒ Openstack

Returns a new instance of Openstack.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/bosh/registry/instance_manager/openstack.rb', line 9

def initialize(cloud_config)
  validate_options(cloud_config)

  @logger = Bosh::Registry.logger

  @openstack_properties = cloud_config['openstack']

  unless @openstack_properties['auth_url'].match(/\/tokens$/)
    @openstack_properties['auth_url'] = @openstack_properties['auth_url'] + '/tokens'
  end

  @openstack_options = {
    :provider => 'OpenStack',
    :openstack_auth_url => @openstack_properties['auth_url'],
    :openstack_username => @openstack_properties['username'],
    :openstack_api_key => @openstack_properties['api_key'],
    :openstack_tenant => @openstack_properties['tenant'],
    :openstack_region => @openstack_properties['region'],
    :openstack_endpoint_type => @openstack_properties['endpoint_type'],
    :connection_options => @openstack_properties['connection_options']
  }
end

Instance Method Details

#instance_ips(instance_id) ⇒ Object

Get the list of IPs belonging to this instance

Raises:



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/bosh/registry/instance_manager/openstack.rb', line 48

def instance_ips(instance_id)
  # If we get an Unauthorized error, it could mean that the OpenStack auth token has expired, so we are
  # going renew the fog connection one time to make sure that we get a new non-expired token.
  retried = false
  begin
    instance  = openstack.servers.find { |s| s.name == instance_id }
  rescue Excon::Errors::Unauthorized => e
    unless retried
      retried = true
      @openstack = nil
      retry
    end
    raise ConnectionError, "Unable to connect to OpenStack API: #{e.message}"
  end
  raise InstanceNotFound, "Instance `#{instance_id}' not found" unless instance
  return (instance.private_ip_addresses + instance.floating_ip_addresses).compact
end

#openstackObject



32
33
34
# File 'lib/bosh/registry/instance_manager/openstack.rb', line 32

def openstack
  @openstack ||= Fog::Compute.new(@openstack_options)
end

#validate_options(cloud_config) ⇒ Object



36
37
38
39
40
41
42
43
44
45
# File 'lib/bosh/registry/instance_manager/openstack.rb', line 36

def validate_options(cloud_config)
  unless cloud_config.has_key?('openstack') &&
      cloud_config['openstack'].is_a?(Hash) &&
      cloud_config['openstack']['auth_url'] &&
      cloud_config['openstack']['username'] &&
      cloud_config['openstack']['api_key'] &&
      cloud_config['openstack']['tenant']
    raise ConfigError, 'Invalid OpenStack configuration parameters'
  end
end