class VCenterDriver::VIHelper

Class VIHelper

Constants

ETC_LOCATION
VCENTER_DRIVER_DEFAULT
VM_PREFIX_DEFAULT

Public Class Methods

add_ref_hash(attr, one_object) click to toggle source
# File lib/vi_helper.rb, line 206
def self.add_ref_hash(attr, one_object)
    raise 'cache is empty!' unless @ref_hash

    refkey = get_ref_key(one_object, attr)

    return unless @ref_hash[attr]

    @ref_hash[attr][refkey] = one_object
end
check_error(rc, message, exit_condition = false) click to toggle source
# File lib/vi_helper.rb, line 66
def self.check_error(rc, message, exit_condition = false)
    return unless OpenNebula.is_error?(rc)

    OpenNebula.error_message("\n    Error #{message}: #{rc.message}\n")
    exit 1 if exit_condition

    raise rc.message
end
check_opts(opts, att_list) click to toggle source
# File lib/vi_helper.rb, line 92
def self.check_opts(opts, att_list)
    att_list.each do |att|
        raise "#{att} option is mandatory" if opts[att].nil?
    end
end
clean_ref_hash() click to toggle source
# File lib/vi_helper.rb, line 202
def self.clean_ref_hash
    @ref_hash = {}
end
client() click to toggle source

rubocop:disable Style/GlobalVars rubocop:disable Style/ClassVars

# File lib/vi_helper.rb, line 33
def self.client
    if $conf.nil?
        @@client ||= OpenNebula::Client.new # rubocop:disable Style/ClassVars
    else
        @@client ||= OpenNebula::Client.new(
            nil,
            $conf[:one_xmlrpc]
        )
    end
end
create_ref_hash(attribute, pool, vcenter_uuid = nil) click to toggle source
# File lib/vi_helper.rb, line 191
def self.create_ref_hash(attribute, pool, vcenter_uuid = nil)
    hash = {}

    pool.each_element(proc do |e|
        refkey = get_ref_key(e, attribute, vcenter_uuid)
        hash[refkey] = e
    end)

    hash
end
find_by_name(the_class, name, pool = nil, raise_if_fail = true) click to toggle source
# File lib/vi_helper.rb, line 136
def self.find_by_name(the_class, name, pool = nil, raise_if_fail = true)
    pool = one_pool(the_class, raise_if_fail) if pool.nil?
    element = pool.find {|e| e['NAME'] == name.to_s }
    if element.nil? && raise_if_fail
        raise "Could not find element '#{name}' in pool '#{the_class}'"
    end

    element
end
find_by_ref( the_class, attribute, ref, vcenter_uuid, pool = nil ) click to toggle source
# File lib/vi_helper.rb, line 226
def self.find_by_ref(
    the_class,
    attribute,
    ref,
    vcenter_uuid,
    pool = nil
)
    pool = one_pool(the_class, false) if pool.nil?
    @ref_hash ||= {}

    if @ref_hash[attribute].nil? || @ref_hash[attribute] == {}
        @ref_hash[attribute] = create_ref_hash(attribute,
                                               pool,
                                               vcenter_uuid)
    end

    refkey = ''
    refkey = ref if ref
    refkey += vcenter_uuid if vcenter_uuid

    @ref_hash[attribute][refkey]
end
find_image_by(att, the_class, path, ds_id, pool = nil) click to toggle source
# File lib/vi_helper.rb, line 250
def self.find_image_by(att, the_class, path, ds_id, pool = nil)
    pool = one_pool(the_class, false) if pool.nil?
    pool.find do |e|
        e[att] == Addressable::URI.escape(path) &&
            e['DATASTORE_ID'] == ds_id
    end
end
find_persistent_image_by_source(source, pool) click to toggle source
# File lib/vi_helper.rb, line 258
def self.find_persistent_image_by_source(source, pool)
    pool.find do |e|
        e['SOURCE'] == source &&
            e['PERSISTENT'] == '1'
    end
end
find_vcenter_vm_by_name(one_vm, host, vi_client) click to toggle source
# File lib/vi_helper.rb, line 265
def self.find_vcenter_vm_by_name(one_vm, host, vi_client)
    # Let's try to find the VM object only by its name
    # Let's build the VM name
    vm_prefix = host['TEMPLATE/VM_PREFIX']
    vm_prefix = VM_PREFIX_DEFAULT if vm_prefix.nil?
    vm_prefix.gsub!('$i', one_vm['ID'])
    vm_name = vm_prefix + one_vm['NAME']

    # We have no DEPLOY_ID, the VM has never been deployed
    # let's use a view to try to find the VM from the root folder
    view = vi_client
           .vim
           .serviceContent
           .viewManager
           .CreateContainerView(
               {
                   :container => vi_client.vim.rootFolder,
               :type => ['VirtualMachine'],
               :recursive => true
               }
           )

    if !view.view.nil? && !view.view.empty?
        vcenter_vm = view
                     .view
                     .find {|v| v.name == vm_name }
    end

    view.DestroyView # Destroy the view

    vcenter_vm
end
generate_name(opts, nbytes) click to toggle source
# File lib/vi_helper.rb, line 146
def self.generate_name(opts, nbytes)
    return opts[:name] if nbytes <= 0

    @sha256 ||= Digest::SHA256.new
    chain = opts[:key]
    hash  = @sha256.hexdigest(chain)[0..nbytes-1]

    "#{opts[:name]}-#{hash}"
end
get_cluster_id(clusters) click to toggle source
# File lib/vi_helper.rb, line 75
def self.get_cluster_id(clusters)
    clusters.each do |id|
        return id unless id == -1
    end
    -1
end
get_default(xpath) click to toggle source
# File lib/vi_helper.rb, line 298
def self.get_default(xpath)
    begin
        xml = OpenNebula::XMLElement.new
        xml.initialize_xml(File.read(VCENTER_DRIVER_DEFAULT), 'VCENTER')
        xml[xpath]
    rescue StandardError
        nil
    end
end
get_deploy_id(deploy_id) click to toggle source

Since github.com/OpenNebula/one/issues/5689 there two deploy_ids allowed:

* moref, eg: vm-567
* moref +"_" + vcenter uuid, eg:
       2499952a-6c85-480e-b7df-4cbd2137eb69_vm-456

This function will always return the moref

# File lib/vi_helper.rb, line 132
def self.get_deploy_id(deploy_id)
    deploy_id.split('_')[0]
end
get_location(item) click to toggle source
# File lib/vi_helper.rb, line 308
def self.get_location(item)
    folders = []
    until item.instance_of? RbVmomi::VIM::Datacenter
        item = item.parent
        if !item.instance_of?(RbVmomi::VIM::Datacenter) &&
           item.name != 'host'
            folders << item.name
        end
        raise 'Could not find the location' if item.nil?
    end
    location   = folders.reverse.join('/')
    location = '/' if location.empty?

    location
end
get_ref_key(element, attribute, vcenter_uuid = nil) click to toggle source
# File lib/vi_helper.rb, line 172
def self.get_ref_key(element, attribute, vcenter_uuid = nil)
    key = element[attribute]

    return if key.nil?

    tvid = element['TEMPLATE/VCENTER_INSTANCE_ID']
    uvid = element['USER_TEMPLATE/VCENTER_INSTANCE_ID']

    if tvid
        key += tvid
    elsif uvid
        key += uvid
    elsif vcenter_uuid
        key += vcenter_uuid
    end

    key
end
new_one_item(the_class) click to toggle source
# File lib/vi_helper.rb, line 109
def self.new_one_item(the_class)
    the_class.new(the_class.build_xml, client)
end
one_item(the_class, id, exit_if_fail = true) click to toggle source
# File lib/vi_helper.rb, line 98
def self.one_item(the_class, id, exit_if_fail = true)
    item = the_class.new_with_id(id, client)
    rc=nil
    if the_class == OpenNebula::VirtualMachine
        rc = item.info(true)
    else
        rc = item.info
    end
    return_if_error(rc, item, exit_if_fail)
end
one_managed?(object) click to toggle source
# File lib/vi_helper.rb, line 82
def self.one_managed?(object)
    if object.class.ancestors.include?(OpenNebula::XMLElement)
        managed =
            object['TEMPLATE/OPENNEBULA_MANAGED'] ||
            object['USER_TEMPLATE/OPENNEBULA_MANAGED']
        return managed != 'NO'
    end
    false
end
one_name(the_class, name, key, pool = nil, bytes = 0) click to toggle source
# File lib/vi_helper.rb, line 156
def self.one_name(the_class, name, key, pool = nil, bytes = 0)
    # Remove \u007F character that comes from vcenter
    name = name.tr("\u007F", '')
    pool = one_pool(the_class) if pool.nil?

    import_name = generate_name({ :name => name, :key => key }, bytes)

    begin
        find_by_name(the_class, import_name, pool)
    rescue StandardError
        return import_name
    end

    one_name(the_class, name, key, pool, bytes+2)
end
one_pool(the_class, exit_if_fail = true) click to toggle source
# File lib/vi_helper.rb, line 113
def self.one_pool(the_class, exit_if_fail = true)
    item = the_class.new(client)

    rc = nil
    begin
        rc = item.info_all
    rescue StandardError
        rc = item.info
    end

    return_if_error(rc, item, exit_if_fail)
end
remove_ref_hash(attr, one_object) click to toggle source
# File lib/vi_helper.rb, line 216
def self.remove_ref_hash(attr, one_object)
    raise 'cache is empty!' unless @ref_hash

    refkey = get_ref_key(one_object, attr)

    return unless @ref_hash[attr]

    @ref_hash[attr].delete(refkey)
end
return_if_error(rc, item, exit_if_fail) click to toggle source

rubocop:enable Style/ClassVars

# File lib/vi_helper.rb, line 54
def self.return_if_error(rc, item, exit_if_fail)
    if OpenNebula.is_error?(rc)
        raise rc.message unless exit_if_fail

        STDERR.puts rc.message
        exit 1
    else
        item
    end
end
set_client(options, client = nil) click to toggle source

rubocop:enable Style/GlobalVars

# File lib/vi_helper.rb, line 45
def self.set_client(options, client = nil)
    if client.nil?
        @@client=OpenNebulaHelper::OneHelper.get_client(options, true)
    else
        @@client = client
    end
end