Class: OneDataAccessor
- Inherits:
-
Object
- Object
- OneDataAccessor
- Includes:
- Errors, InputValidator
- Defined in:
- lib/one_data_accessor.rb
Overview
Class for accessing OpenNebula via XML RPC and requesting accounting data
compatibility mode (omit some newer API functions)
Constant Summary collapse
- STATE_DONE =
'6'
- BENCHMARK_TYPE_XPATH =
'TEMPLATE/BENCHMARK_TYPE'
- BENCHMARK_VALUE_XPATH =
'TEMPLATE/BENCHMARK_VALUE'
Constants included from InputValidator
InputValidator::DECIMAL_RE, InputValidator::NON_ZERO_NUMBER_RE, InputValidator::NUMBER_RE, InputValidator::STRING_RE, InputValidator::URI_RE
Instance Attribute Summary collapse
-
#batch_size ⇒ Integer
readonly
number of vm records to request.
-
#client ⇒ OpenNebula::Client
readonly
client for communicaton with OpenNebula.
-
#compatibility ⇒ TrueClass, FalseClass
readonly
whether or not communicate in.
-
#log ⇒ Object
readonly
Returns the value of attribute log.
-
#logger ⇒ any logger
readonly
The current value of logger.
-
#start_vm_id ⇒ Object
Returns the value of attribute start_vm_id.
Instance Method Summary collapse
-
#benchmark_map ⇒ Hash
Check all hosts and gain benchmark name and value.
-
#benchmark_values(entity) ⇒ Hash
Returns benchmark type and values of specified entity.
-
#check_retval(rc, e_klass) ⇒ Object
Check OpenNebula return codes.
-
#cluster_for_host(host) ⇒ OpenNebula::Cluster
Returns object representing a cluster for specified host.
-
#initialize(compatibility, log = nil) ⇒ OneDataAccessor
constructor
A new instance of OneDataAccessor.
-
#initialize_client ⇒ Object
Initialize OpenNebula client for further connection.
-
#load_vm_pool ⇒ Object
Load part of virtual machine pool.
-
#mapping(pool_class, xpath) ⇒ Hash
Create mapping from element's ID to its xpath value.
-
#vm(vm_id) ⇒ OpenNebula::VirtualMachine
Retrieve virtual machine.
-
#vms(range, groups) ⇒ Array
Retriev IDs of specified virtual machines.
-
#want?(vm, range, groups) ⇒ TrueClass, FalseClass
Check whether obtained vm meets requierements.
Methods included from InputValidator
#decimal?, #is?, #non_zero_number?, #number?, #string?, #uri?
Constructor Details
#initialize(compatibility, log = nil) ⇒ OneDataAccessor
Returns a new instance of OneDataAccessor.
25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/one_data_accessor.rb', line 25 def initialize(compatibility, log = nil) @log = log ? log : Logger.new(STDOUT) @compatibility = compatibility @batch_size = Settings.output['num_of_vms_per_file'] ? Settings.output['num_of_vms_per_file'] : 500 fail ArgumentError, 'Wrong number of vms per file.' unless number?(@batch_size) @compatibility_vm_pool = nil @start_vm_id = 0 initialize_client end |
Instance Attribute Details
#batch_size ⇒ Integer (readonly)
number of vm records to request
14 15 16 |
# File 'lib/one_data_accessor.rb', line 14 def batch_size @batch_size end |
#client ⇒ OpenNebula::Client (readonly)
client for communicaton with OpenNebula
14 15 16 |
# File 'lib/one_data_accessor.rb', line 14 def client @client end |
#compatibility ⇒ TrueClass, FalseClass (readonly)
whether or not communicate in
14 15 16 |
# File 'lib/one_data_accessor.rb', line 14 def compatibility @compatibility end |
#log ⇒ Object (readonly)
Returns the value of attribute log.
22 23 24 |
# File 'lib/one_data_accessor.rb', line 22 def log @log end |
#logger ⇒ any logger (readonly)
Returns the current value of logger.
14 15 16 |
# File 'lib/one_data_accessor.rb', line 14 def logger @logger end |
#start_vm_id ⇒ Object
Returns the value of attribute start_vm_id.
23 24 25 |
# File 'lib/one_data_accessor.rb', line 23 def start_vm_id @start_vm_id end |
Instance Method Details
#benchmark_map ⇒ Hash
Check all hosts and gain benchmark name and value.
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 |
# File 'lib/one_data_accessor.rb', line 201 def benchmark_map map = Hash.new({}) host_pool = OpenNebula::HostPool.new(@client) rc = host_pool.info check_retval(rc, Errors::ResourceRetrievalError) host_pool.each do |host| benchmark = benchmark_values(host) unless benchmark[:benchmark_type] cluster = cluster_for_host(host) benchmark = benchmark_values(cluster) if cluster end map[host['ID']] = benchmark end map end |
#benchmark_values(entity) ⇒ Hash
Returns benchmark type and values of specified entity
224 225 226 |
# File 'lib/one_data_accessor.rb', line 224 def benchmark_values(entity) { benchmark_type: entity[BENCHMARK_TYPE_XPATH], benchmark_value: entity[BENCHMARK_VALUE_XPATH] } end |
#check_retval(rc, e_klass) ⇒ Object
Check OpenNebula return codes
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 |
# File 'lib/one_data_accessor.rb', line 182 def check_retval(rc, e_klass) return true unless OpenNebula.is_error?(rc) case rc.errno when OpenNebula::Error::EAUTHENTICATION fail Errors::AuthenticationError, rc. when OpenNebula::Error::EAUTHORIZATION fail Errors::UserNotAuthorizedError, rc. when OpenNebula::Error::ENO_EXISTS fail Errors::ResourceNotFoundError, rc. when OpenNebula::Error::EACTION fail Errors::ResourceStateError, rc. else fail e_klass, rc. end end |
#cluster_for_host(host) ⇒ OpenNebula::Cluster
Returns object representing a cluster for specified host
232 233 234 235 236 237 238 239 240 241 242 243 |
# File 'lib/one_data_accessor.rb', line 232 def cluster_for_host(host) cluster_id = host['CLUSTER_ID'].to_i # host without cluster return nil if cluster_id == -1 cluster = OpenNebula::Cluster.new(OpenNebula::Cluster.build_xml(cluster_id), @client) rc = cluster.info check_retval(rc, Errors::ResourceRetrievalError) cluster end |
#initialize_client ⇒ Object
Initialize OpenNebula client for further connection
39 40 41 42 43 44 45 |
# File 'lib/one_data_accessor.rb', line 39 def initialize_client secret = Settings['xml_rpc'] ? Settings.xml_rpc['secret'] : nil endpoint = Settings['xml_rpc'] ? Settings.xml_rpc['endpoint'] : nil fail ArgumentError, "#{endpoint} is not a valid URL." if endpoint && !uri?(endpoint) @client = OpenNebula::Client.new(secret, endpoint) end |
#load_vm_pool ⇒ Object
Load part of virtual machine pool
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 |
# File 'lib/one_data_accessor.rb', line 151 def load_vm_pool @log.debug("Loading vm pool from id: #{start_vm_id}.") from = @start_vm_id how_many = @batch_size to = from + how_many - 1 # if in compatibility mode, whole virtual machine pool has to be loaded for the first time if @compatibility unless @compatibility_vm_pool vm_pool = OpenNebula::VirtualMachinePool.new(@client) rc = vm_pool.info(OpenNebula::Pool::INFO_ALL, -1, -1, OpenNebula::VirtualMachinePool::INFO_ALL_VM) check_retval(rc, Errors::ResourceRetrievalError) @compatibility_vm_pool = vm_pool.to_a end pool = @compatibility_vm_pool[from..to] || [] @start_vm_id = pool.last.id + 1 unless pool.empty? return pool else vm_pool = OpenNebula::VirtualMachinePool.new(@client) rc = vm_pool.info(OpenNebula::Pool::INFO_ALL, from, -how_many, OpenNebula::VirtualMachinePool::INFO_ALL_VM) check_retval(rc, Errors::ResourceRetrievalError) @start_vm_id = vm_pool.entries.last.id + 1 unless vm_pool.count == 0 return vm_pool end end |
#mapping(pool_class, xpath) ⇒ Hash
Create mapping from element's ID to its xpath value
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/one_data_accessor.rb', line 53 def mapping(pool_class, xpath) @log.debug("Generating mapping for class: #{pool_class} and xpath: '#{xpath}'.") pool = pool_class.new(@client) # call info_all method instead of info on pools that support it if pool.respond_to? 'info_all' rc = pool.info_all check_retval(rc, Errors::ResourceRetrievalError) else rc = pool.info check_retval(rc, Errors::ResourceRetrievalError) end # generate mapping map = {} pool.each do |item| unless item['ID'] @log.error("Skipping a resource of the type #{pool_class} without an ID present.") next end map[item['ID']] = item[xpath] end map end |
#vm(vm_id) ⇒ OpenNebula::VirtualMachine
Retrieve virtual machine
83 84 85 86 87 88 89 90 |
# File 'lib/one_data_accessor.rb', line 83 def vm(vm_id) fail ArgumentError, "#{vm_id} is not a valid id." unless number?(vm_id) @log.debug("Retrieving virtual machine with id: #{vm_id}.") vm = OpenNebula::VirtualMachine.new(OpenNebula::VirtualMachine.build_xml(vm_id), @client) rc = vm.info check_retval(rc, Errors::ResourceRetrievalError) vm end |
#vms(range, groups) ⇒ Array
Retriev IDs of specified virtual machines
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/one_data_accessor.rb', line 98 def vms(range, groups) vms = [] # load specific batch vm_pool = load_vm_pool return nil if vm_pool.count == 0 @log.debug("Searching for vms based on range: #{range} and groups: #{groups}.") vm_pool.each do |vm| unless vm['ID'] @log.error('Skipping a record without an ID present.') next end # skip unsuitable virtual machines next unless want?(vm, range, groups) vms << vm['ID'].to_i end @log.debug("Selected vms: #{vms}.") vms end |
#want?(vm, range, groups) ⇒ TrueClass, FalseClass
Check whether obtained vm meets requierements
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/one_data_accessor.rb', line 128 def want?(vm, range, groups) if vm.nil? @log.warn('Obtained nil vm from vm pool.') return false end # range restriction unless range.nil? || range.empty? return false if range[:from] && vm['STATE'] == STATE_DONE && vm['ETIME'].to_i < range[:from].to_i return false if range[:to] && vm['STIME'].to_i > range[:to].to_i end # groups restriction unless groups.nil? || groups.empty? return false if groups[:include] && !groups[:include].include?(vm['GNAME']) return false if groups[:exclude] && groups[:exclude].include?(vm['GNAME']) end true end |