-
Notifications
You must be signed in to change notification settings - Fork 898
/
service_ansible_playbook.rb
176 lines (142 loc) · 5.42 KB
/
service_ansible_playbook.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
class ServiceAnsiblePlaybook < ServiceGeneric
include AnsibleExtraVarsMixin
delegate :job_template, :to => :service_template, :allow_nil => true
# A chance for taking options from automate script to override options from a service dialog
def preprocess(action, add_options = {})
if add_options.present?
_log.info("Override with new options:")
$log.log_hashes(add_options)
end
save_job_options(action, add_options)
end
def execute(action)
jt = job_template(action)
opts = get_job_options(action).deep_merge(
:extra_vars => {
'manageiq' => service_manageiq_env(action),
'manageiq_connection' => manageiq_connection_env(evm_owner)
}
)
opts[:hosts] = hosts_array(opts.delete(:hosts))
_log.info("Launching Ansible Tower job with options:")
$log.log_hashes(opts)
new_job = ManageIQ::Providers::EmbeddedAnsible::AutomationManager::Job.create_job(jt, decrypt_options(opts))
update_job_for_playbook(action, new_job, opts[:hosts])
_log.info("Ansible Tower job with ref #{new_job.ems_ref} was created.")
add_resource!(new_job, :name => action)
end
def check_completed(action)
status, reason = job(action).raw_status.normalized_status
done = status != 'transient'
message = status == 'create_complete' ? nil : reason
[done, message]
rescue MiqException::MiqOrchestrationStackNotExistError, MiqException::MiqOrchestrationStatusError => err
[true, err.message] # consider done with an error when exception is caught
end
def refresh(action)
job(action).refresh_ems
end
def check_refreshed(_action)
[true, nil]
end
def job(action)
service_resources.find_by(:name => action, :resource_type => 'OrchestrationStack').try(:resource)
end
def on_error(action)
_log.info("on_error called for service action: #{action}")
update_attributes(:retirement_state => 'error') if action == "Retirement"
job(action).try(:refresh_ems)
log_stdout(action)
end
def retain_resources_on_retirement?
options.fetch_path(:config_info, :retirement, :remove_resources).to_s.start_with?("no_")
end
private
def service_manageiq_env(action)
{
'service' => href_slug,
'action' => action
}.merge(manageiq_env(evm_owner, miq_group, miq_request_task))
.merge(request_options_extra_vars)
end
def request_options_extra_vars
miq_request_task.options.fetch_path(:request_options, :manageiq_extra_vars) || {}
end
def get_job_options(action)
options[job_option_key(action)].deep_dup
end
CONFIG_OPTIONS_WHITELIST = %i[
hosts
extra_vars
credential_id
vault_credential_id
network_credential_id
cloud_credential_id
].freeze
def config_options(action)
options.fetch_path(:config_info, action.downcase.to_sym).slice(*CONFIG_OPTIONS_WHITELIST).with_indifferent_access
end
def translate_credentials!(job_options)
%i[credential vault_credential network_credential cloud_credential].each do |cred|
cred_sym = "#{cred}_id".to_sym
credential_id = job_options.delete(cred_sym)
job_options[cred] = Authentication.find(credential_id).native_ref if credential_id.present?
end
end
def save_job_options(action, overrides)
job_options = config_options(action)
job_options[:extra_vars].try(:transform_values!) do |val|
val.kind_of?(String) ? val : val[:default] # TODO: support Hash only
end
job_options.deep_merge!(parse_dialog_options) unless action == ResourceAction::RETIREMENT
job_options.deep_merge!(overrides)
translate_credentials!(job_options)
options[job_option_key(action)] = job_options
save!
end
def job_option_key(action)
"#{action.downcase}_job_options".to_sym
end
def parse_dialog_options
dialog_options = options[:dialog] || {}
{
:credential_id => dialog_options['dialog_credential'],
:hosts => dialog_options['dialog_hosts'].to_s.strip.presence
}.compact.merge(extra_vars_from_dialog)
end
def extra_vars_from_dialog
params =
(options[:dialog] || {}).each_with_object({}) do |(attr, val), obj|
var_key = attr.sub(/^(password::)?dialog_param_/, '')
obj[var_key] = val unless var_key == attr
end
params.blank? ? {} : {:extra_vars => params}
end
def use_default_inventory?(hosts)
hosts.blank? || hosts == 'localhost'
end
def hosts_array(hosts_string)
return ["localhost"] if use_default_inventory?(hosts_string)
hosts_string.split(',').map(&:strip).delete_blanks
end
# update job attributes only available to playbook provisioning
def update_job_for_playbook(action, job, hosts)
playbook_id = options.fetch_path(:config_info, action.downcase.to_sym, :playbook_id)
job.update!(:configuration_script_base_id => playbook_id, :hosts => hosts)
end
def decrypt_options(opts)
opts.tap do
opts[:extra_vars].transform_values! { |val| val.kind_of?(String) ? ManageIQ::Password.try_decrypt(val) : val }
end
end
def log_stdout(action)
log_option = options.fetch_path(:config_info, action.downcase.to_sym, :log_output) || 'on_error'
return unless %(on_error always).include?(log_option)
job = job(action)
return if log_option == 'on_error' && job.raw_status.succeeded?
_log.info("Stdout from ansible job #{job.name}: #{job.raw_stdout('txt_download')}")
rescue => err
_log.error("Failed to get stdout from ansible job #{job.name}")
_log.log_backtrace(err)
end
end