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,
  vnt: VNTemplate,
  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 =

Used by api for translation of /one.X.pool.method to pool instance

{
  vm:  VirtualMachinePool,
  t:   TemplatePool,
  h:   HostPool,
  u:   UserPool,
  vn:  VirtualNetworkPool,
  vnt: VNTemplatePool,
  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)


174
175
176
177
178
179
180
181
182
183
184
# File 'service/on_helper.rb', line 174

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 do |ds|
    ds['type'] != ds_type || ds['deploy'] != 'TRUE' || (!hypervisor.nil? && ds['hypervisor'] != hypervisor.upcase)
  end 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:



192
193
194
195
196
197
# File 'service/on_helper.rb', line 192

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:



76
77
78
79
80
81
82
83
84
85
86
# File 'service/on_helper.rb', line 76

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)


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

def get_vcenter_dc(host)
  host.info! true
  VIM.connect(
    :host => host['//VCENTER_HOST'], :insecure => true,
    :user => host['//VCENTER_USER'], :password => host['//VCENTER_PASSWORD']
  ).serviceInstance.find_datacenter
end

#onblock(object, id, client = $client) {|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: $client)
    • auth provider object, if 'none' uses global variable '$client'

Yields:

  • (object)

    If block is given, onblock yields given object

Returns:

  • (OpenNebula::PoolElement)


158
159
160
161
162
163
164
165
166
167
168
# File 'service/on_helper.rb', line 158

def onblock(object, id, client = $client)
  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:



91
92
93
94
95
96
# File 'service/on_helper.rb', line 91

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


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

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