Module: ONeHelper

Defined in:
server/service/on_helper.rb

Overview

Useful methods for OpenNebula classes, functions and constants.

Constant Summary

VIM =

Alias for RbVmomi::VIM

RbVmomi::VIM
ON_INSTANCES =

#onblock supported instances list

{
    :vm  => VirtualMachine,
    :t   => Template,
    :h   => Host,
    :u   => User,
    :vn  => VirtualNetwork,
    :ds  => Datastore,
    :mpa => MarketPlaceApp,
    :ma  => MarketPlace,
    :vr  => VirtualRouter,
    :vdc => Vdc,
    :sg  => SecurityGroup,
    :z   => Zone,
    :d   => Document,
    :c   => Cluster,
    :acl => Acl,
    :g   => Group,
    :i   => Image,
    :p   => Pool
}

Instance Method Summary collapse

Instance Method Details

#ChooseDS(ds_type = nil) ⇒ Integer

Note:

Remember to configure DRIVE_TYPE(HDD|SSD) and DEPLOY(TRUE|FALSE) attributes at your Datastores

Returns random Datastore ID filtered by disk type

Parameters:

  • ds_type (String) (defaults to: nil)
    • Datastore type, may be HDD or SSD, returns any DS if not given

Returns:

  • (Integer)


163
164
165
166
167
168
169
# File 'server/service/on_helper.rb', line 163

def ChooseDS(ds_type = nil)
    dss = IONe.new($client, $db).DatastoresMonitoring('sys').sort! { | ds | 100 * ds['used'].to_f / ds['full_size'].to_f }
    dss.delete_if { |ds| ds['type'] != ds_type || ds['deploy'] != 'TRUE' } if ds_type != nil
    ds = dss[rand(dss.size)]
    LOG_DEBUG "Deploying to #{ds['name']}"
    ds['id']
end

#ClusterType(hostid) ⇒ String

Returns given cluster hypervisor type

Examples:

ClusterType(0) => 'vcenter'
ClusterType(1) => 'kvm'

Parameters:

  • hostid (Integer)

    ID of the host to check

Returns:



176
177
178
179
180
181
# File 'server/service/on_helper.rb', line 176

def ClusterType(hostid)
    onblock(:h, hostid) do | host |
        host.info!
        host.to_hash['HOST']['TEMPLATE']['HYPERVISOR']
    end
end

#get_ds_vdata(host, name) ⇒ String

Returns Datastore IP and Path

Parameters:

  • host (Integer)
    • host ID

  • name (String)
    • Datastore name

Returns:



73
74
75
76
77
78
79
80
81
82
83
# File 'server/service/on_helper.rb', line 73

def get_ds_vdata(host, name)
    get_vcenter_dc(onblock(:h, host)).datastoreFolder.children.each do | ds |
        next if ds.name != name
        begin
            return ds.info.nas.remoteHost, ds.info.nas.remotePath
        rescue => e
            return nil
        end
    end
    nil
end

#get_pool_element(type, id, client) ⇒ OpenNebula::PoolElement

Generates any 'Pool' element object

Examples:

Getting VirtualMachine object

vm = ONeHelper::get_pool_element(VirtualMachine, 777, Client.new('oneadmin:secret', 'http://localhost:2633/RPC2'))
p vm.class
    => #<OpenNebula::VirtualMachine:0x00000004c4ead8>

Parameters:

  • type (Class)
    • object class to create

  • id (Integer)
    • element id at its Pool

  • client (OpenNebula::Client)
    • auth provider object

Returns:

  • (OpenNebula::PoolElement)


126
127
128
# File 'server/service/on_helper.rb', line 126

def get_pool_element(type, id, client)
    type.new(type.build_xml(id), client)
end

#get_vcenter_dc(host) ⇒ Datacenter

Returns VIM::Datacenter for host

Parameters:

  • host (OpenNebula::Host)

Returns:

  • (Datacenter)


62
63
64
65
66
67
68
# File 'server/service/on_helper.rb', line 62

def get_vcenter_dc(host)
    host = host.to_hash!['HOST']['TEMPLATE']
    VIM.connect(
        :host => host['VCENTER_HOST'], :insecure => true,
        :user => host['VCENTER_USER'], :password => host['VCENTER_PASSWORD_ACTUAL']
    ).serviceInstance.find_datacenter
end

#onblock(object, id, client = 'none') {|object| ... } ⇒ OpenNebula::PoolElement

Generates any 'Pool' element object or yields it

Examples:

Getting VirtualMachine object

$client = Client.new('oneadmin:secret', 'http://localhost:2633/RPC2')
    * * *
vm = onblock :vm, 777
p vm.class
    => #<OpenNebula::VirtualMachine:0x00000004c64720>

Using VirtualMachine object inside block

onblock :vm, 777 do | vm |
    vm.info!
    puts JSON.pretty_generate(vm.to_hash)
end

Parameters:

  • object (Class | Symbol)
    • object class to create or symbol linked to target class

  • id (Integer)
    • element id at its Pool

  • client (OpenNebula::Client) (defaults to: 'none')
    • auth provider object, if 'none' uses global variable '$client'

Yields:

  • (object)

    If block is given, onblock yields given object

Returns:

  • (OpenNebula::PoolElement)


147
148
149
150
151
152
153
154
155
156
157
158
# File 'server/service/on_helper.rb', line 147

def onblock(object, id, client = 'none')
    client = $client if client == 'none'
    if object.class != Class then
        object = ON_INSTANCES[object]
        return 'Error: Unknown instance name given' if object.nil?
    end
    if block_given?
        yield get_pool_element(object, id.to_i, client)
    else
        get_pool_element(object, id.to_i, client)
    end
end

#putc(*args) ⇒ NilClass

Prints given objects classes

Parameters:

Returns:



88
89
90
91
92
93
# File 'server/service/on_helper.rb', line 88

def putc(*args)
    args.each do | el |
        puts el.class
    end
    nil
end

#recursive_find_ds(folder, name, exact = false) ⇒ Array<RbVmomi::VIM::Datastore>

Searches Instances at vCenter by name at given folder

Parameters:

  • folder (RbVmomi::VIM::Folder)
    • folder where search

  • name (String)
    • DS name at vCenter

  • exact (Boolean) (defaults to: false)

Returns:

  • (Array<RbVmomi::VIM::Datastore>)


39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'server/service/on_helper.rb', line 39

def recursive_find_ds(folder, name, exact = false)
    # @!visibility private
    # Comparator for object names
    def matches(child, name, exact = false)
        is_ds = child.class == RbVmomi::VIM::Datastore
        name_matches = (name == "*") || (exact ? (child.name == name) : (child.name.include? name))
        is_ds && name_matches
    end
    found = []
    folder.children.each do |child|
      if matches(child, name, exact)
        found << child
      elsif child.class == RbVmomi::VIM::Folder
        found << recursive_find_vm(child, name, exact)
      end
    end
  
    found.flatten
end

#recursive_find_vm(folder, name, exact = false) ⇒ Array<RbVmomi::VIM::VirtualMachine>

Note:

Tested and used for VMs, but can search at any datacenter folder

Searches Instances at vCenter by name at given folder

Parameters:

  • folder (RbVmomi::VIM::Folder)
    • folder where search

  • name (String)
    • VM name at vCenter

  • exact (Boolean) (defaults to: false)

Returns:

  • (Array<RbVmomi::VIM::VirtualMachine>)


15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'server/service/on_helper.rb', line 15

def recursive_find_vm(folder, name, exact = false)
    # @!visibility private
    # Comparator for object names
    def matches(child, name, exact = false)
        is_vm = child.class == RbVmomi::VIM::VirtualMachine
        name_matches = (name == "*") || (exact ? (child.name == name) : (child.name.include? name))
        is_vm && name_matches
    end
    found = []
    folder.children.each do |child|
      if matches(child, name, exact)
        found << child
      elsif child.class == RbVmomi::VIM::Folder
        found << recursive_find_vm(child, name, exact)
      end
    end
  
    found.flatten
end