Class: OpenNebula::VMRole
Overview
Service Role class
Constant Summary
Constants inherited from Role
Role::FAILURE_STATES, Role::IMMUTABLE_ATTRS, Role::LOG_COMP, Role::RECOVER_DEPLOY_STATES, Role::RECOVER_SCALE_STATES, Role::RECOVER_UNDEPLOY_STATES, Role::SCALE_WAYS, Role::SCHEDULE_ACTIONS, Role::STATE, Role::STATE_STR, Role::VM_INFO
Instance Attribute Summary collapse
-
#service ⇒ Object
readonly
Returns the value of attribute service.
Instance Method Summary collapse
-
#batch_action(action, period, vms_per_period, args) ⇒ Object
Schedule the given action on all the VMs that belong to the Role.
-
#chown(uid, gid) ⇒ Array<true, nil>, Array<false, String>
Changes the owner/group of all the nodes in this role.
- #clean_scale_way ⇒ Object
- #cooldown ⇒ Object
-
#delete_sched_action(sched_id) ⇒ Array<true, String>, Array<false, String>
Delete the given scheduled action from the VM in this role that owns it.
-
#deploy ⇒ Array<true, nil>, Array<false, String>
Deploys all the nodes in this role.
- #elasticity_policies ⇒ Object
-
#initialize(body, service) ⇒ VMRole
constructor
A new instance of VMRole.
-
#max_cardinality ⇒ Integer?
Returns the role max cardinality.
-
#min_cardinality ⇒ Integer?
Returns the role min cardinality.
-
#recover_deploy(report) ⇒ Object
Recover.
- #recover_scale(report) ⇒ Object
- #recover_undeploy ⇒ Object
-
#scale?(vm_pool) ⇒ Array<Integer>
Returns a positive, 0, or negative number of nodes to adjust, according to the elasticity and scheduled policies.
- #scale_way(way) ⇒ Object
-
#state=(state) ⇒ Object
Sets a new state.
-
#update(template) ⇒ nil, OpenNebula::Error
Updates the role.
- #update_cooldown(new_cooldown) ⇒ Object
- #update_elasticity_policies(new_policies) ⇒ Object
Methods inherited from Role
#any_parent_on_hold?, #can_recover_deploy?, #can_recover_scale?, #can_recover_undeploy?, #can_release?, #cardinality, #cardinality=, #check_new_template, for, #info_nodes, init_default_cooldown, init_default_shutdown, init_default_vm_name_template, init_default_vr_name_template, init_force_deletion, #name, #nodes, #nodes_ids, #on_hold=, #on_hold?, #parents, #release, #scheduled_policies, #service_on_hold?, #shutdown, #state, #state_str, #update_scheduled_policies, vm_failure?
Constructor Details
#initialize(body, service) ⇒ VMRole
Returns a new instance of VMRole.
24 25 26 27 28 |
# File 'lib/models/vmrole.rb', line 24 def initialize(body, service) super(body, service) @body['cooldown'] = @@default_cooldown if @body['cooldown'].nil? end |
Instance Attribute Details
#service ⇒ Object (readonly)
Returns the value of attribute service.
22 23 24 |
# File 'lib/models/vmrole.rb', line 22 def service @service end |
Instance Method Details
#batch_action(action, period, vms_per_period, args) ⇒ Object
Schedule the given action on all the VMs that belong to the Role
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 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 180 181 182 183 |
# File 'lib/models/vmrole.rb', line 128 def batch_action(action, period, vms_per_period, args) vms_id = [] error_msgs = [] nodes = @body['nodes'] now = Time.now.to_i time_offset = 0 # if role is done, return error if state == 5 return OpenNebula::Error.new("Role #{name} is in DONE state") end do_offset = !period.nil? && period.to_i > 0 && !vms_per_period.nil? && vms_per_period.to_i > 0 nodes.each_with_index do |node, index| vm_id = node['deploy_id'] vm = OpenNebula::VirtualMachine.new_with_id(vm_id, @service.client) if do_offset offset = (index / vms_per_period.to_i).floor time_offset = offset * period.to_i end tmp_str = 'SCHED_ACTION = [' tmp_str << "ACTION = #{action}," tmp_str << "ARGS = \"#{args}\"," if args tmp_str << "TIME = #{now + time_offset}]" rc = vm.sched_action_add(tmp_str) if OpenNebula.is_error?(rc) msg = "Role #{name} : VM #{vm_id} error scheduling "\ "action; #{rc.}" error_msgs << msg Log.error LOG_COMP, msg, @service.id @service.log_error(msg) else vms_id << vm.id end end log_msg = "Action:#{action} scheduled on Role:#{name}"\ "VMs:#{vms_id.join(',')}" Log.info LOG_COMP, log_msg, @service.id return [true, log_msg] if error_msgs.empty? error_msgs << log_msg [false, error_msgs.join('\n')] end |
#chown(uid, gid) ⇒ Array<true, nil>, Array<false, String>
Changes the owner/group of all the nodes in this role
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/models/vmrole.rb', line 57 def chown(uid, gid) nodes.each do |node| vm_id = node['deploy_id'] Log.debug LOG_COMP, "Role #{name} : Chown for VM #{vm_id}", @service.id vm = OpenNebula::VirtualMachine.new_with_id(vm_id, @service.client) rc = vm.chown(uid, gid) if OpenNebula.is_error?(rc) msg = "Role #{name} : Chown failed for VM #{vm_id}; " \ "#{rc.}" Log.error LOG_COMP, msg, @service.id @service.log_error(msg) return [false, rc.] else Log.debug LOG_COMP, "Role #{name} : Chown success for VM #{vm_id}", @service.id end end [true, nil] end |
#clean_scale_way ⇒ Object
330 331 332 |
# File 'lib/models/vmrole.rb', line 330 def clean_scale_way @body.delete('scale_way') end |
#cooldown ⇒ Object
318 319 320 |
# File 'lib/models/vmrole.rb', line 318 def cooldown @body['cooldown'] end |
#delete_sched_action(sched_id) ⇒ Array<true, String>, Array<false, String>
Delete the given scheduled action from the VM in this role that owns it.
Missing scheduled actions are ignored to keep the operation idempotent. Missing VM records are also ignored because there is no scheduled action left to delete from them.
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 |
# File 'lib/models/vmrole.rb', line 196 def delete_sched_action(sched_id) sched_id = sched_id.to_i nodes.each do |node| vm_id = node['deploy_id'] if vm_id.nil? Log.debug LOG_COMP, "Role #{name} : skipping node without VM", @service.id next end vm = OpenNebula::VirtualMachine.new_with_id(vm_id, @service.client) rc = vm.info if OpenNebula.is_error?(rc) Log.warn LOG_COMP, "Role #{name} : VM #{vm_id} error getting " \ "information; #{rc.}", @service.id next end has_sched_action = vm.has_elements?( "TEMPLATE/SCHED_ACTION[ID=#{sched_id}]" ) next unless has_sched_action rc = vm.sched_action_delete(sched_id) if OpenNebula.is_error?(rc) msg = "Role #{name} : VM #{vm_id} error deleting sched " \ "action #{sched_id}; #{rc.}" Log.error LOG_COMP, msg, @service.id @service.log_error(msg) return [false, msg] else msg = "Role #{name} : sched action #{sched_id} " \ "deleted from VM #{vm_id}" Log.debug LOG_COMP, msg, @service.id return [true, msg] end end [true, "Sched action:#{sched_id} not found on Role:#{name}"] end |
#deploy ⇒ Array<true, nil>, Array<false, String>
Deploys all the nodes in this role
343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 |
# File 'lib/models/vmrole.rb', line 343 def deploy deployed_nodes = [] n_nodes = cardinality - nodes.size return [deployed_nodes, nil] if n_nodes == 0 template_id, template, extra_template = init_template_attributes n_nodes.times do vm_name = @@vm_name_template .gsub('$SERVICE_ID', @service.id.to_s) .gsub('$SERVICE_NAME', @service.name.to_s) .gsub('$ROLE_NAME', name.to_s) .gsub('$VM_NUMBER', @body['last_vmname'].to_s) @body['last_vmname'] += 1 Log.debug( LOG_COMP, "Role #{name} : Instantiate template #{template_id}, name #{vm_name}", @service.id ) # Instantiate VM vm_id = template.instantiate(vm_name, on_hold?, extra_template) if OpenNebula.is_error?(vm_id) msg = "Role #{name} : Instantiate failed for template " \ "#{template_id}; #{vm_id.}" Log.error(LOG_COMP, msg, @service.id) @service.log_error(msg) return [false, "Error instantiating VM Template #{template_id} in Role " \ "#{name}: #{vm_id.}"] end Log.debug( LOG_COMP, "Role #{name} : Instantiate success, VM ID #{vm_id}", @service.id ) # Once deployed, save VM info in role node body deployed_nodes << vm_id fill_node_info(vm_id) end [deployed_nodes, nil] end |
#elasticity_policies ⇒ Object
310 311 312 |
# File 'lib/models/vmrole.rb', line 310 def elasticity_policies @body['elasticity_policies'] end |
#max_cardinality ⇒ Integer?
Returns the role max cardinality
255 256 257 258 259 260 261 |
# File 'lib/models/vmrole.rb', line 255 def max_cardinality max = @body['max_vms'] return if max.nil? max.to_i end |
#min_cardinality ⇒ Integer?
Returns the role min cardinality
265 266 267 268 269 270 271 |
# File 'lib/models/vmrole.rb', line 265 def min_cardinality min = @body['min_vms'] return if min.nil? min.to_i end |
#recover_deploy(report) ⇒ Object
Recover
399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 |
# File 'lib/models/vmrole.rb', line 399 def recover_deploy(report) nodes = @body['nodes'] deployed_nodes = [] nodes.each do |node| vm_id = node['deploy_id'] vm = OpenNebula::VirtualMachine.new_with_id(vm_id, @service.client) rc = vm.info if OpenNebula.is_error?(rc) msg = "Role #{name} : Retry failed for VM "\ "#{vm_id}; #{rc.}" Log.error LOG_COMP, msg, @service.id next true end vm_state = vm.state lcm_state = vm.lcm_state # ACTIVE/RUNNING next false if vm_state == 3 && lcm_state == 3 && !report next true if vm_state == '6' # Delete DONE nodes if Role.vm_failure?(vm_state, lcm_state) rc = vm.recover(2) if OpenNebula.is_error?(rc) msg = "Role #{name} : Retry failed for VM "\ "#{vm_id}; #{rc.}" Log.error LOG_COMP, msg, @service.id @service.log_error(msg) else deployed_nodes << vm_id end else vm.resume deployed_nodes << vm_id end end rc = deploy unless rc[0] return [false, "Error deploying nodes for role `#{name}`"] end deployed_nodes.concat(rc[0]) deployed_nodes end |
#recover_scale(report) ⇒ Object
469 470 471 472 473 474 475 476 477 478 479 |
# File 'lib/models/vmrole.rb', line 469 def recover_scale(report) rc = nil if @body['scale_way'] == SCALE_WAYS['UP'] rc = [recover_deploy(report), true] elsif @body['scale_way'] == SCALE_WAYS['DOWN'] rc = [recover_undeploy, false] end rc end |
#recover_undeploy ⇒ Object
457 458 459 460 461 462 463 464 465 466 467 |
# File 'lib/models/vmrole.rb', line 457 def recover_undeploy undeployed_nodes = [] rc = shutdown(true) return false unless rc[0] undeployed_nodes.concat(rc[0]) if rc[1].nil? undeployed_nodes end |
#scale?(vm_pool) ⇒ Array<Integer>
Returns a positive, 0, or negative number of nodes to adjust, according to the elasticity and scheduled policies
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 |
# File 'lib/models/vmrole.rb', line 277 def scale?(vm_pool) elasticity_pol = @body['elasticity_policies'] scheduled_pol = @body['scheduled_policies'] elasticity_pol ||= [] scheduled_pol ||= [] scheduled_pol.each do |policy| diff, cooldown_duration = scale_time?(policy) return [diff, cooldown_duration] if diff != 0 end elasticity_pol.each do |policy| diff, cooldown_duration = scale_attributes?(policy, vm_pool) next if diff == 0 cooldown_duration = @body['cooldown'] if cooldown_duration.nil? cooldown_duration = @@default_cooldown if cooldown_duration.nil? return [diff, cooldown_duration] end # Implicit rule that scales up to maintain the min_cardinality, with # no cooldown period if cardinality < min_cardinality.to_i return [min_cardinality.to_i - cardinality, 0] end [0, 0] end |
#scale_way(way) ⇒ Object
326 327 328 |
# File 'lib/models/vmrole.rb', line 326 def scale_way(way) @body['scale_way'] = SCALE_WAYS[way] end |
#state=(state) ⇒ Object
Sets a new state
32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/models/vmrole.rb', line 32 def state=(state) super(state) return unless state == STATE['SCALING'] elasticity_pol = @body['elasticity_policies'] return if elasticity_pol.nil? elasticity_pol.each do |policy| policy.delete('true_evals') end end |
#update(template) ⇒ nil, OpenNebula::Error
Updates the role
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/models/vmrole.rb', line 91 def update(template) force = template['force'] == true new_cardinality = template['cardinality'] return if new_cardinality.nil? new_cardinality = new_cardinality.to_i if !force if new_cardinality < min_cardinality.to_i return OpenNebula::Error.new( "Minimum cardinality is #{min_cardinality}" ) elsif !max_cardinality.nil? && new_cardinality > max_cardinality.to_i return OpenNebula::Error.new( "Maximum cardinality is #{max_cardinality}" ) end end self.cardinality = new_cardinality nil end |
#update_cooldown(new_cooldown) ⇒ Object
322 323 324 |
# File 'lib/models/vmrole.rb', line 322 def update_cooldown(new_cooldown) @body['cooldown'] = new_cooldown unless new_cooldown.nil? end |
#update_elasticity_policies(new_policies) ⇒ Object
314 315 316 |
# File 'lib/models/vmrole.rb', line 314 def update_elasticity_policies(new_policies) @body['elasticity_policies'] = new_policies end |