Class: Berta::VirtualMachineHandler

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(vm) ⇒ VirtualMachineHandler

Constructs Virtual machine handler from given vm.

Parameters:

  • vm (OpenNebula::VirtualMachine)

    VM that will this handler use.



12
13
14
# File 'lib/berta/virtual_machine_handler.rb', line 12

def initialize(vm)
  @handle = vm
end

Instance Attribute Details

#handleObject (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_expirationBerta::Entities::Expiration

Return default expiration, that means expiration with default expiration action that is in expiration offset interval and is closes to current date.

Returns:



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

#expirationsArray<Berta::Entities::Expiration>

Returns array of expirations on vm. Expirations are classes from USER_TEMPLATE/SCHED_ACTION.

Returns:



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

#notifiedNumeric

Return notified flag value from USER_TEMPLATE if it is set else nil.

Returns:

  • (Numeric)

    Time of expiration that VM was notified about



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.

Returns:

  • (Boolean)

    If this vm should be notified. True if vm should be notified else false.



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

#updateObject

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.message} on vm with id #{handle['ID']}"
end

#update_notifiedObject

Note:

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