Module: ONeHelper

Defined in:
service/on_helper.rb

Overview

Useful methods for OpenNebula classes, functions and constants.

Constant Summary collapse

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,
    hk:  Hook
}
ON_INSTANCE_POOLS =
{
    vm:  VirtualMachinePool,
    t:   TemplatePool,
    h:   HostPool,
    u:   UserPool,
    vn:  VirtualNetworkPool,
    ds:  DatastorePool,
    vr:  VirtualRouterPool,
    vdc: VdcPool,
    sg:  SecurityGroupPool,
    z:   ZonePool,
    d:   DocumentPool,
    c:   ClusterPool,
    acl: AclPool,
    g:   GroupPool,
    i:   ImagePool,
    hk:  HookPool
}

Instance Method Summary collapse

Instance Method Details

#ChooseDS(ds_type = nil, hypervisor = 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)


169
170
171
172
173
174
175
176
# File 'service/on_helper.rb', line 169

def ChooseDS(ds_type = nil, hypervisor = 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' || (!hypervisor.nil? && ds['hypervisor'] != hypervisor.upcase) } unless ds_type.nil?
    raise "No suitable DataStores were found" if dss.size == 0
    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:

  • (String)


183
184
185
186
187
188
# File 'service/on_helper.rb', line 183

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:

  • (String, String)

    ip, path



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

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
            nil
        end
    end
end

#get_vcenter_dc(host) ⇒ Datacenter

Returns VIM::Datacenter for host

Parameters:

Returns:

  • (Datacenter)


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

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)


153
154
155
156
157
158
159
160
161
162
163
164
# File 'service/on_helper.rb', line 153

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 object.new_with_id id.to_i, client
    else
        object.new_with_id id.to_i, client
    end
end

#putc(*args) ⇒ NilClass

Prints given objects classes

Parameters:

Returns:



88
89
90
91
92
93
# File '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>)


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

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>)


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

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