Class: Berta::VirtualMachineHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/berta/virtual_machine_handler.rb

Overview

Class for Berta operations on virtual machines

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(vm) ⇒ VirtualMachineHandler

Constructs Virtual machine handler from given vm.

Parameters:



10
11
12
# File 'lib/berta/virtual_machine_handler.rb', line 10

def initialize(vm)
  @handle = vm
end

Instance Attribute Details

#handle ⇒ Object (readonly)

Returns the value of attribute handle.



4
5
6
# File 'lib/berta/virtual_machine_handler.rb', line 4

def handle
  @handle
end

Instance Method Details

#add_expiration(time, action) ⇒ Object

Note:

This method modifies OpenNebula database

Adds schelude action to virtual machine. This command modifies USER_TEMPLATE of virtual machine. But does not delete old variables is USER_TEMPLATE.

Parameters:

  • time (Numeric) —

    Time when to notify user

  • action (String) —

    Action to perform on expiration



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

def add_expiration(time, action)
  logger.debug "Setting expiration date of #{handle['ID']} to #{action} on #{time} with id #{next_sched_action_id}"
  return if Berta::Settings['dry-run']
  new_expiration = \
    Berta::Entities::Expiration.new(next_sched_action_id,
                                    time,
                                    action)
  update_expirations(expirations << new_expiration)
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.

Returns:



106
107
108
109
110
# File 'lib/berta/virtual_machine_handler.rb', line 106

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:



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

def expirations
  exps = []
  handle.each('USER_TEMPLATE/SCHED_ACTION') \
    { |saxml| exps.push(Berta::Entities::Expiration.from_xml(saxml)) }
  exps
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.



38
39
40
41
# File 'lib/berta/virtual_machine_handler.rb', line 38

def notified
  time = handle['USER_TEMPLATE/NOTIFIED']
  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.



50
51
52
53
54
55
# File 'lib/berta/virtual_machine_handler.rb', line 50

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:



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

def update_expirations(exps)
  template = ''
  exps.each { |exp| template += exp.template }
  logger.debug "Setting multiple expirations:\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.



24
25
26
27
28
29
30
31
# File 'lib/berta/virtual_machine_handler.rb', line 24

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