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, #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_log(instance_id) ⇒ Object



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

def console_log(instance_id)
    server = @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



112
113
114
# File 'lib/ncc/connection/openstack.rb', line 112

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
109
110
# File 'lib/ncc/connection/openstack.rb', line 100

def instance_ip_address(server)
    instance_network_names = ['instance_net', 'instnace_net']
    if !server.addresses.nil?
        instance_network_names.each do |network_name|
            if server.addresses.has_key? network_name
                return server.addresses[network_name].first['addr']
            end
        end
    end
    return nil
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



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/ncc/connection/openstack.rb', line 146

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



129
130
131
132
133
134
135
# File 'lib/ncc/connection/openstack.rb', line 129

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



137
138
139
140
141
142
143
144
# File 'lib/ncc/connection/openstack.rb', line 137

def reboot(instance_id)
    server = @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