Class: Berta::VirtualMachineHandler
- Inherits:
-
Object
- Object
- Berta::VirtualMachineHandler
- Defined in:
- lib/berta/virtual_machine_handler.rb
Overview
Class for Berta operations on virtual machines
Constant Summary collapse
- NOTIFIED_FLAG =
'BERTA_NOTIFIED'.freeze
Instance Attribute Summary collapse
-
#handle ⇒ Object
readonly
Returns the value of attribute handle.
Instance Method Summary collapse
- #==(other) ⇒ Object
-
#default_expiration ⇒ Berta::Entities::Expiration
Return default expiration, that means expiration with default expiration action that is in expiration offset interval and is closes to current date.
-
#expirations ⇒ Array<Berta::Entities::Expiration>
Returns array of expirations on vm.
-
#initialize(vm) ⇒ VirtualMachineHandler
constructor
Constructs Virtual machine handler from given vm.
-
#notified ⇒ Numeric
Return notified flag value from USER_TEMPLATE if it is set else nil.
-
#should_notify? ⇒ Boolean
Determines if VM meets criteria to be notified.
-
#update ⇒ Object
Updates vms expirations.
-
#update_notified ⇒ Object
Sets notified flag value in USER_TEMPLATE to default expiration time as integer.
Constructor Details
#initialize(vm) ⇒ VirtualMachineHandler
Constructs Virtual machine handler from given vm.
12 13 14 |
# File 'lib/berta/virtual_machine_handler.rb', line 12 def initialize(vm) @handle = vm end |
Instance Attribute Details
#handle ⇒ Object (readonly)
Returns the value of attribute handle.
6 7 8 |
# File 'lib/berta/virtual_machine_handler.rb', line 6 def handle @handle end |
Instance Method Details
#==(other) ⇒ Object
93 94 95 |
# File 'lib/berta/virtual_machine_handler.rb', line 93 def ==(other) handle.id == other.handle.id end |
#default_expiration ⇒ Berta::Entities::Expiration
Return default expiration, that means expiration with default expiration action that is in expiration offset interval and is closes to current date.
67 68 69 70 71 |
# File 'lib/berta/virtual_machine_handler.rb', line 67 def default_expiration expirations .find_all { |exp| exp.default_action? && exp.in_expiration_interval? } .min { |exp| exp.time.to_i } end |
#expirations ⇒ Array<Berta::Entities::Expiration>
Returns array of expirations on vm. Expirations are classes from USER_TEMPLATE/SCHED_ACTION.
77 78 79 80 81 82 |
# File 'lib/berta/virtual_machine_handler.rb', line 77 def expirations exps = [] handle.each('USER_TEMPLATE/SCHED_ACTION') \ { |saxml| exps.push(Berta::Entities::Expiration.from_xml(saxml)) } exps end |
#notified ⇒ Numeric
Return notified flag value from USER_TEMPLATE if it is set else nil.
88 89 90 91 |
# File 'lib/berta/virtual_machine_handler.rb', line 88 def notified time = handle["USER_TEMPLATE/#{NOTIFIED_FLAG}"] time.to_i if time end |
#should_notify? ⇒ Boolean
Determines if VM meets criteria to be notified. To be notified, VM musn’t have the same notification time as default expiration time and must be in notification interval.
55 56 57 58 59 60 |
# File 'lib/berta/virtual_machine_handler.rb', line 55 def should_notify? expiration = default_expiration return false unless expiration return false if notified == expiration.time.to_i expiration.in_notification_interval? end |
#update ⇒ Object
Updates vms expirations. That means it adds default expiration if vm has no default expiration. If VM has invalid expiration it will be deleted. Other expirations are kept.
20 21 22 23 24 25 26 27 |
# File 'lib/berta/virtual_machine_handler.rb', line 20 def update exps = remove_invalid(expirations) exps = add_default_expiration(exps) return if exps == expirations update_expirations(exps) rescue Berta::Errors::BackendError => e logger.error "#{e.} on vm with id #{handle['ID']}" end |
#update_notified ⇒ Object
This method modifies OpenNebula database
Sets notified flag value in USER_TEMPLATE to default expiration time as integer. If VM has no default expiration nothing will be updated. After updating notified value fetches data from opennebula database.
40 41 42 43 44 45 46 |
# File 'lib/berta/virtual_machine_handler.rb', line 40 def update_notified exp = default_expiration return unless exp notify_time = exp.time logger.info "Setting notified flag of VM with id #{handle['ID']} to #{notify_time}" send_update("#{NOTIFIED_FLAG} = #{notify_time.to_i}") end |