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:



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

#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.

Returns:



93
94
95
96
97
# File 'lib/berta/virtual_machine_handler.rb', line 93

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.

Returns:



81
82
83
84
85
86
# File 'lib/berta/virtual_machine_handler.rb', line 81

def expirations
  exps = []
  handle.each('USER_TEMPLATE/SCHED_ACTION') \
    { |saxml| exps.push(Berta::Entities::Expiration.from_xml(saxml)) }
  exps
end

#next_expiration_id ⇒ Object

TODO



100
101
102
103
104
# File 'lib/berta/virtual_machine_handler.rb', line 100

def next_expiration_id
  elems = handle.retrieve_elements('USER_TEMPLATE/SCHED_ACTION/ID')
  return 0 unless elems
  elems.to_a.max.to_i + 1
end

#notified ⇒ Numeric

Return NOTIFIED value from USER_TEMPLATE if it is set else nil.

Returns:

  • (Numeric) —

    Time when notified was set else nil. Time is in UNIX epoch time format.



41
42
43
44
# File 'lib/berta/virtual_machine_handler.rb', line 41

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 be notified and must have expiration with valid expiration action in notification interval.

Returns:

  • (Boolean) —

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



53
54
55
56
57
58
# File 'lib/berta/virtual_machine_handler.rb', line 53

def should_notify?
  return false if notified
  expiration = default_expiration
  return false unless expiration
  expiration.in_notification_interval?
end

#update_expirations(exps) ⇒ Object

Note:

This method modifies OpenNebula database

Sets array of expirations to vm, rewrites all old ones. Receiving empty array wont change anything.

Parameters:



65
66
67
68
69
70
71
72
73
74
75
# File 'lib/berta/virtual_machine_handler.rb', line 65

def update_expirations(exps)
  template = ''
  exps.each { |exp| template += exp.template }
  return if template == ''
  logger.debug "Setting multiple expirations on vm with id=#{handle['ID']} :\n#{template}"
  return if Berta::Settings['dry-run']
  Berta::Utils::OpenNebula::Helper.handle_error do
    handle.update(template, true)
    handle.info
  end
end

#update_notified ⇒ Object

Note:

This method modifies OpenNebula database

Sets NOTIFIED value in USER_TEMPLATE to current time as integer. After updating notified value fetches data from opennebula database.



26
27
28
29
30
31
32
33
34
# File 'lib/berta/virtual_machine_handler.rb', line 26

def update_notified
  notify_time = Time.now
  logger.debug "Setting notified flag of VM with id #{handle['ID']} to #{notify_time}"
  return if Berta::Settings['dry-run']
  Berta::Utils::OpenNebula::Helper.handle_error do
    handle.update("#{NOTIFIED_FLAG} = #{notify_time.to_i}", true)
    handle.info
  end
end