class NCC::Connection::OpenStack

/* 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. */

Public Instance Methods

connection_params() click to toggle source
# 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') click to toggle source
# 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) click to toggle source
# 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_field() click to toggle source
# File lib/ncc/connection/openstack.rb, line 24
def image_id_field
    :name
end
instance_host(server) click to toggle source
# File lib/ncc/connection/openstack.rb, line 110
def instance_host(server)
    server.os_ext_srv_attr_host
end
instance_image(server) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/ncc/connection/openstack.rb, line 96
def instance_status(server)
    map_to_status(server.state)
end
map_to_provider_status(abstract_status) click to toggle source
# 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) click to toggle source
# 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
provider() click to toggle source
# File lib/ncc/connection/openstack.rb, line 48
def provider
    'openstack'
end
provider_request_of(instance) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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_field() click to toggle source
# File lib/ncc/connection/openstack.rb, line 20
def size_id_field
    :name
end
translate_image(pimage) click to toggle source
# File lib/ncc/connection/openstack.rb, line 38
def translate_image(pimage)
    image = generic_translate(:image, pimage)
    pimage.metadata.each do |metadatum|
        if %(ramdisk_id kernel_id).include? metadatum.key
            image[metadatum.key] = metadatum.value
        end
    end
    image
end
translate_size(flavor) click to toggle source
# 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