Class: NCC::Connection::OpenStack

Inherits:
NCC::Connection show all
Defined in:
lib/ncc/connection/openstack.rb

Overview

/* Copyright 2013 Proofpoint, Inc. All rights reserved.

Copyright 2014 Evernote Corporation. All rights reserved.

Licensed under the Apache License, Version 2.0 (the “License”); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */

Instance Attribute Summary

Attributes inherited from NCC::Connection

#fog

Instance Method Summary collapse

Methods inherited from NCC::Connection

#collection, #communication_error, connect, #create_instance, #current?, #debug, #delete, #do_connect, #do_fog, #error, #fatal, #fields, #generic_provider_request, #generic_translate, #get_provider_object, #get_provider_objects, #host_fqdn, #id_field, #id_map, #id_map_hash, #id_of, #ids, #images, #info, #initialize, #instance_for, #instance_name, #instance_not_found, #instances, #inventory_request, #keyintern, #keyname_for, #map_from_raw_provider_id, #map_to_id, #map_to_provider_id, #maybe_invalidate, #me, #merge_configured, #modify_instance_request, #notice, #object_of, #provider_id_map, #provider_request, #sizes, #to_s, #translate, #translated_id_of, #use_only_mapped, #warn

Constructor Details

This class inherits a constructor from NCC::Connection

Instance Method Details

#connection_paramsObject



57
58
59
# File 'lib/ncc/connection/openstack.rb', line 57

def connection_params
    ['openstack_auth_url', 'openstack_username', 'openstack_api_key', 'openstack_tenant']
end

#console(instance_id, console_type = 'novnc') ⇒ Object



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/ncc/connection/openstack.rb', line 127

def console(instance_id, console_type='novnc')
    server = do_fog { |fog| fog.servers.get(instance_id) }
    if server.nil?
        instance_not_found instance_id
    else
        begin
            response = do_fog { |fog| fog.get_vnc_console(server.id, console_type) }
            body = response.body
        rescue Exception => e
            communication_error e.message
        end

        unless body.respond_to?(:[]) and
                body['console'] and
                body['console'].respond_to?(:[]) and
                body['console']['url']
            raise communication_error "Expected Fog::Compute::Openstack#get_vnc_console(#{instance_id.inspect}, " +
                "#{console_type.inspect})# to return object with key 'console' => 'url'; " +
                "instead got: #{body.inspect}"
        end

        body['console']
    end
end

#console_log(instance_id) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/ncc/connection/openstack.rb', line 114

def console_log(instance_id)
    server = do_fog { |fog| fog.servers.get(instance_id) }
    if server.nil?
        instance_not_found instance_id
    else
        begin
            server.console.body
        rescue Exception => e
            communication_error e.message
        end
    end
end

#image_id_fieldObject



24
25
26
# File 'lib/ncc/connection/openstack.rb', line 24

def image_id_field
    :name
end

#instance_host(server) ⇒ Object



110
111
112
# File 'lib/ncc/connection/openstack.rb', line 110

def instance_host(server)
    server.os_ext_srv_attr_host
end

#instance_image(server) ⇒ Object



88
89
90
# File 'lib/ncc/connection/openstack.rb', line 88

def instance_image(server)
    map_from_raw_provider_id(:image, server.image['id'])
end

#instance_ip_address(server) ⇒ Object



100
101
102
103
104
105
106
107
108
# File 'lib/ncc/connection/openstack.rb', line 100

def instance_ip_address(server)
    begin
        server.private_ip_address.to_s
    rescue StandardError => err
        # Fog collapses badly when you just ask it for this
        # field which isn't there (yet?)
        nil
    end
end

#instance_size(server) ⇒ Object



92
93
94
# File 'lib/ncc/connection/openstack.rb', line 92

def instance_size(server)
    map_from_raw_provider_id(:size, server.flavor['id'])
end

#instance_status(server) ⇒ Object



96
97
98
# File 'lib/ncc/connection/openstack.rb', line 96

def instance_status(server)
    map_to_status(server.state)
end

#map_to_provider_status(abstract_status) ⇒ Object



169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/ncc/connection/openstack.rb', line 169

def map_to_provider_status(abstract_status)
    case abstract_status
    when 'active', 'provider-operation', 'shutting-down', 'suspending'
        'ACTIVE'
    when 'build'
        'BUILD'
    when 'terminated'
        'DELETED'
    when 'error'
        'ERROR'
    when 'hard-reboot'
        'HARD_REBOOT'
    when 'reboot'
        'REBOOT'
    when 'suspend'
        'SUSPENDED'
    when 'unknown'
        'UNKNOWN'
    when 'needs-verify'
        'VERIFY_RESIZE'
    else
        'UNKNOWN'
    end
end

#map_to_status(provider_status) ⇒ Object



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/ncc/connection/openstack.rb', line 61

def map_to_status(provider_status)
    case provider_status
    when 'ACTIVE', 'PASSWORD', 'SHUTOFF'
        'active'
    when 'BUILD'
        'build'
    when 'DELETED'
        'terminated'
    when 'ERROR'
        'error'
    when 'HARD_REBOOT'
        'hard-reboot'
    when 'REBOOT'
        'reboot'
    when 'REBUILD', 'RESCUE', 'RESIZE', 'REVERT_RESIZE'
        'provider-operation'
    when 'SUSPENDED'
        'suspend'
    when 'UNKNOWN'
        'unknown'
    when 'VERIFY_RESIZE'
        'needs-verify'
    else
        'unknown'
    end
end

#providerObject



48
49
50
# File 'lib/ncc/connection/openstack.rb', line 48

def provider
    'openstack'
end

#provider_request_of(instance) ⇒ Object



152
153
154
155
156
157
158
# File 'lib/ncc/connection/openstack.rb', line 152

def provider_request_of(instance)
    {
        :name => instance.name,
        :flavor_ref => sizes(instance.size)['provider_id'],
        :image_ref => images(instance.image)['provider_id'],
    }.merge(instance.extra provider)
end

#reboot(instance_id) ⇒ Object



160
161
162
163
164
165
166
167
# File 'lib/ncc/connection/openstack.rb', line 160

def reboot(instance_id)
    server = do_fog { |fog| fog.servers.get(instance_id) }
    if server.nil?
        instance_not_found instance_id
    else
        server.reboot 'HARD'
    end
end

#size_desc(f) ⇒ Object



52
53
54
55
# File 'lib/ncc/connection/openstack.rb', line 52

def size_desc(f)
    (f.vcpus > 1 ? "#{f.vcpus}CPU " : "") +
        "#{(f.ram / 1024).round}GB RAM #{f.disk + f.ephemeral}GB disk"
end

#size_id_fieldObject



20
21
22
# File 'lib/ncc/connection/openstack.rb', line 20

def size_id_field
    :name
end

#translate_image(pimage) ⇒ Object



38
39
40
41
42
43
44
45
46
# File 'lib/ncc/connection/openstack.rb', line 38

def translate_image(pimage)
    image = generic_translate(:image, pimage)
    pimage..each do |metadatum|
        if %(ramdisk_id kernel_id).include? metadatum.key
            image[metadatum.key] = metadatum.value
        end
    end
    image
end

#translate_size(flavor) ⇒ Object



28
29
30
31
32
33
34
35
36
# File 'lib/ncc/connection/openstack.rb', line 28

def translate_size(flavor)
    generic_translate(:size, flavor).
        merge({
                  'ram' => flavor.ram,
                  'cores' => flavor.vcpus,
                  'description' => size_desc(flavor),
                  'disk' => flavor.disk + flavor.ephemeral
              })
end