forked from ManageIQ/manageiq
-
Notifications
You must be signed in to change notification settings - Fork 1
/
miq_retire_task.rb
84 lines (70 loc) · 2.8 KB
/
miq_retire_task.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
class MiqRetireTask < MiqRequestTask
validate :validate_request_type, :validate_state
AUTOMATE_DRIVES = true
def self.get_description(req_obj)
name = if req_obj.source.nil?
m = model_being_retired.find_by(:id => req_obj.options[:src_ids].first)
m.nil? ? "" : m.name
else
req_obj.source.name
end
_("%{request_description} for: %{request_source}") % {:request_description => _(request_class::TASK_DESCRIPTION), :request_source => name}
end
def deliver_to_automate(req_type = request_type, zone = nil)
args = {
:object_type => self.class.name,
:object_id => id,
:attrs => {"request" => req_type},
:instance_name => "AUTOMATION",
:user_id => miq_request.requester.id,
:miq_group_id => miq_request.requester.current_group_id,
:tenant_id => miq_request.requester.current_group.tenant_id,
}
MiqAeEngine.set_automation_attributes_from_objects(source, args[:attrs])
zone ||= source.respond_to?(:my_zone) ? source.my_zone : MiqServer.my_zone
MiqQueue.put(
:class_name => 'MiqAeEngine',
:method_name => 'deliver',
:args => [args],
:role => 'automate',
:zone => options.fetch(:miq_zone, zone),
:tracking_label => tracking_label_id
)
update_and_notify_parent(:state => "pending", :status => "Ok", :message => "Automation Starting")
end
def after_request_task_create
update(:description => get_description)
end
def after_ae_delivery(ae_result)
_log.info("ae_result=#{ae_result.inspect}")
reload
return if ae_result == 'retry'
return if miq_request.state == 'finished'
if ae_result == 'ok'
update_and_notify_parent(:state => "finished", :status => "Ok", :message => display_message("#{request_class::TASK_DESCRIPTION} completed"))
else
mark_pending_items_as_finished
update_and_notify_parent(:state => "finished", :status => "Error", :message => display_message("#{request_class::TASK_DESCRIPTION} failed"))
end
end
def before_ae_starts(_options)
reload
if state.to_s.downcase.in?(%w[pending queued])
_log.info("Executing #{request_class::TASK_DESCRIPTION} request: [#{description}]")
update_and_notify_parent(:state => "active", :status => "Ok", :message => "In Process")
end
end
def mark_pending_items_as_finished
miq_request.miq_request_tasks.each do |s|
if s.state == 'pending' && !(id == s.id)
s.update_and_notify_parent(:state => "finished", :status => "Warn", :message => "Error in Request: #{miq_request.id}. Setting pending Task: #{id} to finished.")
end
end
end
def completed_state
"retired"
end
def self.display_name(number = 1)
n_('Retire Task', 'Retire Tasks', number)
end
end