Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion berta.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Gem::Specification.new do |s|

s.add_runtime_dependency 'chronic_duration', '~> 0.10'
s.add_runtime_dependency 'mail', '~> 2.6'
s.add_runtime_dependency 'opennebula', '~> 5.8'
s.add_runtime_dependency 'opennebula', '>= 6.2', '<= 6.4'
s.add_runtime_dependency 'settingslogic', '~> 2.0'
s.add_runtime_dependency 'thor', '~> 0.19'
s.add_runtime_dependency 'tilt', '~> 2.0'
Expand Down
15 changes: 11 additions & 4 deletions lib/berta/entities/expiration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,14 @@ class Expiration
# @param xml [XMLElement] XML from which to create instance
# @return [Berta::Entities::Expiration] Expiration from given xml
# @raise [Berta::Errors::Entities::InvalidEntityXMLError] If XMLElement was not in correct format
def self.from_xml(xml)
def self.from_xml(xml, start_time=0)
check_xml!(xml)
Berta::Entities::Expiration.new(xml['ID'], xml['TIME'], xml['ACTION'])
time = if xml['TIME'].start_with?('+')
start_time + xml['TIME'].to_i
else
xml['TIME']
end
Berta::Entities::Expiration.new(xml['ID'], time, xml['ACTION'])
end

# Raises error if XML element is not in correct format.
Expand All @@ -37,15 +42,17 @@ def initialize(id, time, action)
end

# Generate schelude action template that can be used
# in VMS USER_TEMPLATE/SCHED_ACTION
# in VMS TEMPLATE/SCHED_ACTION
#
# @return [String] Schelude action template
def template
<<-VM_TEMPLATE
SCHED_ACTION = [
ID = "#{id}",
ACTION = "#{action}",
TIME = "#{time}"
TIME = "#{time}",
END_TYPE = "2",
END_VALUE = "#{time}"
]
VM_TEMPLATE
end
Expand Down
49 changes: 20 additions & 29 deletions lib/berta/virtual_machine_handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,8 @@ def initialize(virtual_machine)
# invalid expiration it will be deleted. Other expirations
# are kept.
def update
exps = remove_invalid(expirations)
exps = add_default_expiration(exps)
return if exps == expirations
update_expirations(exps)
remove_invalid
add_default_expiration
rescue Berta::Errors::BackendError => e
logger.error "#{e.message} on vm with id #{handle['ID']}"
end
Expand Down Expand Up @@ -71,13 +69,14 @@ def default_expiration
end

# Returns array of expirations on vm. Expirations are
# classes from USER_TEMPLATE/SCHED_ACTION.
# classes from SCHED_ACTION.
#
# @return [Array<Berta::Entities::Expiration>] All expirations on vm
def expirations
exps = []
handle.each('USER_TEMPLATE/SCHED_ACTION') \
{ |saxml| exps.push(Berta::Entities::Expiration.from_xml(saxml)) }
start_time = handle.retrieve_elements('STIME').first.to_i
handle.each('TEMPLATE/SCHED_ACTION') \
{ |saxml| exps.push(Berta::Entities::Expiration.from_xml(saxml, start_time)) }
exps
end

Expand All @@ -96,35 +95,27 @@ def ==(other)

private

def remove_invalid(exps)
valid = exps.select(&:in_expiration_interval?)
if valid.length != exps.length
logger.info "Removing #{exps.length - valid.length} invalid expirations on vm with" \
"id=#{handle['ID']} usr=#{handle['UNAME']} grp=#{handle['GNAME']}"
def remove_invalid
invalid = expirations.reject(&:in_expiration_interval?)
if invalid
invalid.each do |exp|
logger.info "Removing invalid expirations #{exp.id} on vm with" \
"id=#{handle['ID']} usr=#{handle['UNAME']} grp=#{handle['GNAME']}" \
", expires at #{Time.at(exp.time.to_i)}"
return if Berta::Settings['dry-run']
handle.sched_action_delete(exp.id)
end
end
valid
end

def add_default_expiration(exps)
def add_default_expiration
unless default_expiration
new_default = next_expiration
exps << new_default
logger.info "Adding default expiration on vm with id=#{handle['ID']} usr=#{handle['UNAME']} grp=#{handle['GNAME']}"\
", expires at #{Time.at(new_default.time)}"
return if Berta::Settings['dry-run']
handle.sched_action_add(new_default.template)
end
exps
end

# Sets array of expirations to vm, rewrites all old ones.
# Receiving empty array wont change anything.
#
# @note This method modifies OpenNebula database
# @param exps [Array<Berta::Entities::Expiration>] Expirations to use
def update_expirations(exps)
template = exps.inject('') { |temp, exp| temp + exp.template }
return if template == ''
logger.debug template.delete("\n ")
send_update(template)
end

# Sends data to opennebula service
Expand All @@ -140,7 +131,7 @@ def send_update(template)
#
# @return [Numeric] Next sched action id
def next_expiration_id
elems = handle.retrieve_elements('USER_TEMPLATE/SCHED_ACTION/ID')
elems = handle.retrieve_elements('TEMPLATE/SCHED_ACTION/ID')
return 0 unless elems
elems.to_a.max.to_i + 1
end
Expand Down