foreman/app/models/orchestration.rb @ 614d9c7f
2a0cffd3 | Ohad Levy | require_dependency "proxy_api"
|
|
6454273f | Paul Kelly | require 'orchestration/queue'
|
|
90b83222 | Ohad Levy | ||
module Orchestration
|
|||
def self.included(base)
|
|||
base.send :include, InstanceMethods
|
|||
base.class_eval do
|
|||
a6f4f5f7 | Ohad Levy | attr_reader :queue, :old, :record_conflicts
|
|
90b83222 | Ohad Levy | # stores actions to be performed on our proxies based on priority
|
|
before_validation :set_queue
|
|||
before_validation :setup_clone
|
|||
# extend our Host model to know how to handle subsystems
|
|||
117ba92a | Paul Kelly | include Orchestration::DHCP
|
|
ac36e7ce | Ohad Levy | include Orchestration::DNS
|
|
90b83222 | Ohad Levy | include Orchestration::TFTP
|
|
36f93e4d | Ohad Levy | include Orchestration::Puppetca
|
|
96be8845 | Ohad Levy | include Orchestration::Libvirt
|
|
90b83222 | Ohad Levy | ||
# save handles both creation and update of hosts
|
|||
before_save :on_save
|
|||
822eef5a | Paul Kelly | after_destroy :on_destroy
|
|
90b83222 | Ohad Levy | end
|
|
end
|
|||
module InstanceMethods
|
|||
protected
|
|||
def on_save
|
|||
process queue
|
|||
end
|
|||
822eef5a | Paul Kelly | def on_destroy
|
|
errors.empty? ? process(queue) : false
|
|||
end
|
|||
90b83222 | Ohad Levy | def rollback
|
|
raise ActiveRecord::Rollback
|
|||
end
|
|||
# log and add to errors
|
|||
ac36e7ce | Ohad Levy | def failure msg, backtrace=nil
|
|
e85584bf | Paul Kelly | logger.warn(backtrace ? msg + backtrace.join("\n") : msg)
|
|
ac36e7ce | Ohad Levy | errors.add :base, msg
|
|
90b83222 | Ohad Levy | false
|
|
end
|
|||
public
|
|||
# we override this method in order to include checking the
|
|||
# after validation callbacks status, as rails by default does
|
|||
# not care about their return status.
|
|||
017e1049 | Ohad Levy | def valid?(context = nil)
|
|
90b83222 | Ohad Levy | super
|
|
ac36e7ce | Ohad Levy | errors.empty?
|
|
90b83222 | Ohad Levy | end
|
|
# we override the destroy method, in order to ensure our queue exists before other callbacks
|
|||
# and to process the queue only if we found no errors
|
|||
def destroy
|
|||
set_queue
|
|||
super
|
|||
end
|
|||
private
|
|||
e85584bf | Paul Kelly | def proxy_error e
|
|
cd9079df | Paul Kelly | (e.respond_to?(:response) and !e.response.nil?) ? e.response : e
|
|
e85584bf | Paul Kelly | end
|
|
90b83222 | Ohad Levy | # Handles the actual queue
|
|
# takes care for running the tasks in order
|
|||
# if any of them fail, it rollbacks all completed tasks
|
|||
# in order not to keep any left overs in our proxies.
|
|||
def process q
|
|||
return true if Rails.env == "test"
|
|||
# queue is empty - nothing to do.
|
|||
return if q.empty?
|
|||
# process all pending tasks
|
|||
q.pending.each do |task|
|
|||
# if we have failures, we don't want to process any more tasks
|
|||
c7c5d840 | Paul Kelly | break unless q.failed.empty?
|
|
90b83222 | Ohad Levy | task.status = "running"
|
|
begin
|
|||
task.status = execute({:action => task.action}) ? "completed" : "failed"
|
|||
a6f4f5f7 | Ohad Levy | rescue Net::Conflict => e
|
|
task.status = "conflict"
|
|||
@record_conflicts << e
|
|||
ac36e7ce | Ohad Levy | failure e.message
|
|
c7c5d840 | Paul Kelly | rescue RestClient::Exception => e
|
|
task.status = "failed"
|
|||
failure "#{task.name} task failed with the following error: #{e.response}"
|
|||
90b83222 | Ohad Levy | rescue => e
|
|
task.status = "failed"
|
|||
f28a6895 | Ohad Levy | failure "#{task.name} task failed with the following error: #{e}"
|
|
90b83222 | Ohad Levy | end
|
|
end
|
|||
# if we have no failures - we are done
|
|||
ac36e7ce | Ohad Levy | return true if q.failed.empty? and q.pending.empty? and q.conflict.empty? and errors.empty?
|
|
90b83222 | Ohad Levy | ||
f28a6895 | Ohad Levy | logger.warn "Rolling back due to a problem: #{q.failed + q.conflict}"
|
|
90b83222 | Ohad Levy | # handle errors
|
|
# we try to undo all completed operations and trigger a DB rollback
|
|||
102a8035 | Paul Kelly | (q.completed + q.running).sort.reverse_each do |task|
|
|
90b83222 | Ohad Levy | begin
|
|
task.status = "rollbacked"
|
|||
execute({:action => task.action, :rollback => true})
|
|||
rescue => e
|
|||
# if the operation failed, we can just report upon it
|
|||
a6f4f5f7 | Ohad Levy | failure "Failed to perform rollback on #{task.name} - #{e}"
|
|
90b83222 | Ohad Levy | end
|
|
end
|
|||
rollback
|
|||
end
|
|||
def execute opts = {}
|
|||
obj, met = opts[:action]
|
|||
rollback = opts[:rollback] || false
|
|||
# at the moment, rollback are expected to replace set with del in the method name
|
|||
if rollback
|
|||
met = met.to_s
|
|||
case met
|
|||
when /set/
|
|||
met.gsub!("set","del")
|
|||
when /del/
|
|||
met.gsub!("del","set")
|
|||
else
|
|||
raise "Dont know how to rollback #{met}"
|
|||
end
|
|||
met = met.to_sym
|
|||
end
|
|||
if obj.respond_to?(met)
|
|||
return obj.send(met)
|
|||
else
|
|||
a6f4f5f7 | Ohad Levy | failure "invalid method #{met}"
|
|
90b83222 | Ohad Levy | raise "invalid method #{met}"
|
|
end
|
|||
end
|
|||
def set_queue
|
|||
6454273f | Paul Kelly | @queue = Orchestration::Queue.new
|
|
a6f4f5f7 | Ohad Levy | @record_conflicts = []
|
|
90b83222 | Ohad Levy | end
|
|
# we keep the before update host object in order to compare changes
|
|||
def setup_clone
|
|||
return if new_record?
|
|||
@old = clone
|
|||
for key in (changed_attributes.keys - ["updated_at"])
|
|||
@old.send "#{key}=", changed_attributes[key]
|
|||
672f931d | Paul Kelly | # At this point the old cached bindings may still be present so we force an AR association reload
|
|
# This logic may not work or be required if we switch to Rails 3
|
|||
if (match = key.match(/^(.*)_id$/))
|
|||
name = match[1].to_sym
|
|||
next if name == :owner # This does not work for the owner association even from the console
|
|||
self.send(name, true) if (send(name) and send(name).id != @attributes[key])
|
|||
old.send(name, true) if (old.send(name) and old.send(name).id != old.attributes[key])
|
|||
end
|
|||
90b83222 | Ohad Levy | end
|
|
end
|
|||
614d9c7f | Paul Kelly | # Ensure that the bootserver is in IP format and not a hostname
|
|
# We can reuse the DNS subsystem, if we are managing it
|
|||
def bootserver_ip name_or_ip
|
|||
return name_or_ip if name_or_ip =~ Net::Validations::IP_REGEXP
|
|||
return dns_ptr_record.dns_lookup(name_or_ip).ip if dns_ptr_record
|
|||
# Looks like we are managing DHCP but not DNS
|
|||
domain.resolver.getaddress(name_or_ip).to_s
|
|||
end
|
|||
90b83222 | Ohad Levy | end
|
|
end
|