Class: OpenNebula::VirtualMachinePool

Inherits:
Pool show all
Defined in:
lib/opennebula/virtual_machine_pool.rb

Constant Summary collapse

VM_POOL_METHODS =

Constants and Class attribute accessors

{
    :info               => 'vmpool.info',
    :info_extended      => 'vmpool.infoextended',
    :info_set           => 'vmpool.infoset',
    :monitoring         => 'vmpool.monitoring',
    :accounting         => 'vmpool.accounting',
    :showback           => 'vmpool.showback',
    :calculate_showback => 'vmpool.calculateshowback'
}
INFO_NOT_DONE =

Constants for info queries (include/RequestManagerPoolInfoFilter.h)

-1
INFO_ALL_VM =
-2

Constants inherited from Pool

Pool::INFO_ALL, Pool::INFO_GROUP, Pool::INFO_MINE, Pool::INFO_PRIMARY_GROUP, Pool::PAGINATED_POOLS

Instance Attribute Summary

Attributes inherited from Pool

#element_name, #pool_name

Instance Method Summary collapse

Methods inherited from Pool

#each, #each_page, #each_page_delete, #each_with_xpath, #get_hash, #get_page, #info_paginated, #is_paginated?, #loop_page, #to_str

Methods inherited from XMLPool

#each_element

Methods inherited from XMLElement

#[], #add_element, #attr, build_xml, #delete_element, #each, #each_xpath, #element_xml, #has_elements?, #initialize_xml, #name, #retrieve_elements, #retrieve_xmlelements, #set_content, #template_like_str, #template_str, #template_xml, #text, #to_hash, #to_xml, #xml_nil?

Constructor Details

#initialize(client, user_id = 0) ⇒ VirtualMachinePool

client a Client object that represents a XML-RPC connection user_id is to refer to a Pool with VirtualMachines from that user



46
47
48
49
50
# File 'lib/opennebula/virtual_machine_pool.rb', line 46

def initialize(client, user_id=0)
    super('VM_POOL','VM',client)

    @user_id  = user_id
end

Instance Method Details

#accounting(filter_flag = INFO_ALL, options = {}) ⇒ Hash, OpenNebula::Error

Retrieves the accounting data for all the VMs in the pool

Examples:

{"HISTORY_RECORDS"=>
    {"HISTORY"=> [
      {"OID"=>"0",
       "SEQ"=>"0",
       "HOSTNAME"=>"dummy",
       ...
      },
      {"OID"=>"0",
       "SEQ"=>"0",
       "HOSTNAME"=>"dummy",

:order_by_1 => VM/UID

{"0"=>
    {"HISTORY_RECORDS"=>
       {"HISTORY"=> [
         {"OID"=>"0",
          "SEQ"=>"0",
          "HOSTNAME"=>"dummy",
          ...
         },
         {"OID"=>"0",
          "SEQ"=>"0",
          "HOSTNAME"=>"dummy",

:order_by_1 => VM/UID, :order_by_2 => VM/ID

{"0"=>
    {"0"=>
        {"HISTORY_RECORDS"=>
            {"HISTORY"=> [
              {"OID"=>"0",
               "SEQ"=>"0",
               "HOSTNAME"=>"dummy",
               ...
              },
              {"OID"=>"0",
               "SEQ"=>"0",
               "HOSTNAME"=>"dummy",

Parameters:

  • filter_flag (Integer) (defaults to: INFO_ALL)

    Optional filter flag to retrieve all or part of the Pool. Possible values: INFO_ALL, INFO_GROUP, INFO_MINE or user_id

  • options (Hash) (defaults to: {})
  • params (Hash)

    a customizable set of options

Returns:

  • (Hash, OpenNebula::Error)

    The first level hash uses the :order_by_1 values as keys, and as value a Hash with the :order_by_2 values and the HISTORY_RECORDS



304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
# File 'lib/opennebula/virtual_machine_pool.rb', line 304

def accounting(filter_flag=INFO_ALL, options={})
    acct_hash = Hash.new

    rc = build_accounting(filter_flag, options) do |history|
        hash = acct_hash

        if options[:order_by_1]
            id_1 = history[options[:order_by_1]]
            acct_hash[id_1] ||= Hash.new

            if options[:order_by_2]
                id_2 = history[options[:order_by_2]]
                acct_hash[id_1][id_2] ||= Hash.new

                hash = acct_hash[id_1][id_2]
            else
                hash = acct_hash[id_1]
            end
        end

        hash["HISTORY_RECORDS"] ||= Hash.new
        hash["HISTORY_RECORDS"]["HISTORY"] ||= Array.new
        hash["HISTORY_RECORDS"]["HISTORY"] << history.to_hash['HISTORY']
    end

    return rc if OpenNebula.is_error?(rc)

    acct_hash
end

#accounting_xml(filter_flag = INFO_ALL, options = {}) ⇒ String

Retrieves the accounting data for all the VMs in the pool in xml

Parameters:

  • filter_flag (Integer) (defaults to: INFO_ALL)

    Optional filter flag to retrieve all or part of the Pool. Possible values: INFO_ALL, INFO_GROUP, INFO_MINE or user_id

  • options (Hash) (defaults to: {})
  • params (Hash)

    a customizable set of options

Returns:

  • (String)

    the xml representing the accounting data



350
351
352
353
354
355
356
357
358
359
360
361
362
# File 'lib/opennebula/virtual_machine_pool.rb', line 350

def accounting_xml(filter_flag=INFO_ALL, options={})
    acct_hash = Hash.new
    xml_str = "<HISTORY_RECORDS>\n"

    rc = build_accounting(filter_flag, options) do |history|
        xml_str << history.to_xml
    end

    return rc if OpenNebula.is_error?(rc)

    xml_str << "\n</HISTORY_RECORDS>"
    xml_str
end

#calculate_showback(start_month, start_year, end_month, end_year) ⇒ Object

Processes all the history records, and stores the monthly cost for each VM

@param [Integer] start_month First month (+year) to process. January is 1.
Use -1 to unset
@param [Integer] start_year First year (+month) to process. e.g. 2014.
Use -1 to unset
@param [Integer] end_month Last month (+year) to process. January is 1.
Use -1 to unset
@param [Integer] end_year Last year (+month) to process. e.g. 2014.
Use -1 to unset


233
234
235
236
237
238
239
240
241
# File 'lib/opennebula/virtual_machine_pool.rb', line 233

def calculate_showback(start_month, start_year, end_month, end_year)
    start_month ||= -1
    start_year  ||= -1
    end_month   ||= -1
    end_year    ||= -1

    return @client.call(VM_POOL_METHODS[:calculate_showback],
                        start_month, start_year, end_month, end_year)
end

#factory(element_xml) ⇒ Object

Default Factory Method for the Pools



60
61
62
# File 'lib/opennebula/virtual_machine_pool.rb', line 60

def factory(element_xml)
    OpenNebula::VirtualMachine.new(element_xml,@client)
end

#get_hash_extendedObject

Get info extended VM



53
54
55
56
57
# File 'lib/opennebula/virtual_machine_pool.rb', line 53

def get_hash_extended
    rc = info_search(:extended => true)
    return rc if OpenNebula.is_error?(rc)
    to_hash
end

#info(*args) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/opennebula/virtual_machine_pool.rb', line 74

def info(*args)
    case args.size
    when 0
        info_filter(VM_POOL_METHODS[:info],
                    @user_id,
                    -1,
                    -1,
                    INFO_NOT_DONE)
    when 1
        info_filter(VM_POOL_METHODS[:info],
                    args[0],
                    -1,
                    -1,
                    INFO_NOT_DONE)
    when 3
        info_filter(VM_POOL_METHODS[:info],
                    args[0],
                    args[1],
                    args[2],
                    INFO_NOT_DONE)
    when 4
        info_filter(VM_POOL_METHODS[:info],
                    args[0],
                    args[1],
                    args[2],
                    args[3])
    end
end

#info!Object

Retrieves all or part of the VirtualMachines in the pool. No arguments, returns the not-in-done VMs for the user

user_id, start_id, end_id
user_id, start_id, end_id, state


72
# File 'lib/opennebula/virtual_machine_pool.rb', line 72

alias_method :info!, :info

#info_search(args = {}) ⇒ Object



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/opennebula/virtual_machine_pool.rb', line 138

def info_search(args = {})
    default_args = {
        :who      => INFO_ALL,
        :start_id => -1,
        :end_id   => -1,
        :state    => INFO_NOT_DONE,
        :query    => "",
        :extended => false
    }.merge!(args)

    if args[:extended]
        method = VM_POOL_METHODS[:info_extended]
    else
        method = VM_POOL_METHODS[:info]
    end

    info_filter(method,
                default_args[:who],
                default_args[:start_id],
                default_args[:end_id],
                default_args[:state],
                default_args[:query])
end

#info_set(vm_ids, extended) ⇒ Object

Retrieves the set of VMs especified in vm_ids

Parameters:

  • comma (String)

    separated list of vm ids.

  • if (Boolean)

    true extended body is retrieved.



167
168
169
# File 'lib/opennebula/virtual_machine_pool.rb', line 167

def info_set(vm_ids, extended)
    xmlrpc_info(VM_POOL_METHODS[:info_set], vm_ids, extended)
end

#monitoring(xpaths, filter_flag = INFO_ALL) ⇒ Hash<String, <Hash<String, Array<Array<int>>>>>, OpenNebula::Error

Retrieves the monitoring data for all the VMs in the pool

Examples:

vm_pool.monitoring( ['MONITORING/CPU', 'MONITORING/NETTX'] )

{"3"=>
  {
  "MONITORING/CPU"=>[["1435085098", "47"], ["1435085253", "5"],
    ["1435085410", "48"], ["1435085566", "3"], ["1435088136", "2"]],
  "MONITORING/NETTX"=>[["1435085098", "0"], ["1435085253", "50"],
  ["1435085410", "50"], ["1435085566", "50"], ["1435085723", "50"]]
  },
 "43" =>
  {
  "MONITORING/CPU"=>[["1435085098", "47"], ["1435085253", "5"],
    ["1435085410", "48"], ["1435085566", "3"], ["1435088136", "2"]],
  "MONITORING/NETTX"=>[["1435085098", "0"], ["1435085253", "50"],
  ["1435085410", "50"], ["1435085566", "50"], ["1435085723", "50"]]
  }
}

Parameters:

  • xpath_expressions (Array<String>)

    Elements to retrieve.

  • filter_flag (Integer) (defaults to: INFO_ALL)

    Optional filter flag to retrieve all or part of the Pool. Possible values: INFO_ALL, INFO_GROUP, INFO_MINE.

Returns:

  • (Hash<String, <Hash<String, Array<Array<int>>>>>, OpenNebula::Error)

    The first level hash uses the VM ID as keys, and as value a Hash with the requested xpath expressions, and an Array of ‘timestamp, value’.



201
202
203
# File 'lib/opennebula/virtual_machine_pool.rb', line 201

def monitoring(xpaths, filter_flag=INFO_ALL)
    return super(VM_POOL_METHODS[:monitoring], xpaths, filter_flag)
end

#monitoring_last(filter_flag = INFO_ALL) ⇒ Object



205
206
207
# File 'lib/opennebula/virtual_machine_pool.rb', line 205

def monitoring_last(filter_flag=INFO_ALL)
    return super(VM_POOL_METHODS[:monitoring], filter_flag, 0)
end

#monitoring_xml(filter_flag = INFO_ALL, num = nil) ⇒ String

Retrieves the monitoring data for all the VMs in the pool, in XML

Parameters:

  • filter_flag (Integer) (defaults to: INFO_ALL)

    Optional filter flag to retrieve all or part of the Pool. Possible values: INFO_ALL, INFO_GROUP, INFO_MINE.

  • num (Integer) (defaults to: nil)

    Optional Retrieve monitor records in the last num seconds. 0 just the last record, -1 or nil all records

Returns:

  • (String)

    VM monitoring data, in XML



216
217
218
219
220
# File 'lib/opennebula/virtual_machine_pool.rb', line 216

def monitoring_xml(filter_flag=INFO_ALL, num=nil)
    return @client.call(VM_POOL_METHODS[:monitoring], filter_flag) if num.nil?

    @client.call(VM_POOL_METHODS[:monitoring], filter_flag, num.to_i)
end

#showback(filter_flag = INFO_ALL, options = {}) ⇒ Hash, OpenNebula::Error

Retrieves the showback data for all the VMs in the pool

Parameters:

  • filter_flag (Integer) (defaults to: INFO_ALL)

    Optional filter flag to retrieve all or part of the Pool. Possible values: INFO_ALL, INFO_GROUP, INFO_MINE or user_id

  • options (Hash) (defaults to: {})
  • params (Hash)

    a customizable set of options

Returns:

  • (Hash, OpenNebula::Error)

    The first level hash uses the :order_by_1 values as keys, and as value a Hash with the :order_by_2 values and the SHOWBACK_RECORDS



388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
# File 'lib/opennebula/virtual_machine_pool.rb', line 388

def showback(filter_flag=INFO_ALL, options={})
    data_hash = Hash.new

    rc = build_showback(filter_flag, options) do |record|
        hash = data_hash

        if options[:order_by_1]
            id_1 = record[options[:order_by_1]]
            data_hash[id_1] ||= Hash.new

            if options[:order_by_2]
                id_2 = record[options[:order_by_2]]
                data_hash[id_1][id_2] ||= Hash.new

                hash = data_hash[id_1][id_2]
            else
                hash = data_hash[id_1]
            end
        end

        hash["SHOWBACK_RECORDS"] ||= Hash.new
        hash["SHOWBACK_RECORDS"]["SHOWBACK"] ||= Array.new
        hash["SHOWBACK_RECORDS"]["SHOWBACK"] << record.to_hash['SHOWBACK']
    end

    return rc if OpenNebula.is_error?(rc)

    data_hash
end

#showback_xml(filter_flag = INFO_ALL, options = {}) ⇒ String

Retrieves the showback data for all the VMs in the pool, in xml

Parameters:

  • filter_flag (Integer) (defaults to: INFO_ALL)

    Optional filter flag to retrieve all or part of the Pool. Possible values: INFO_ALL, INFO_GROUP, INFO_MINE or user_id

  • options (Hash) (defaults to: {})
  • params (Hash)

    a customizable set of options

Returns:

  • (String)

    the xml representing the showback data



437
438
439
440
441
442
443
444
445
446
447
448
# File 'lib/opennebula/virtual_machine_pool.rb', line 437

def showback_xml(filter_flag=INFO_ALL, options={})
    xml_str = "<SHOWBACK_RECORDS>\n"

    rc = build_showback(filter_flag, options) do |showback|
        xml_str << showback.to_xml
    end

    return rc if OpenNebula.is_error?(rc)

    xml_str << "\n</SHOWBACK_RECORDS>"
    xml_str
end