Skip to content

Commit

Permalink
Merge pull request #16414 from bzwei/embedded_stdout
Browse files Browse the repository at this point in the history
Add log_output option for embedded ansible service
(cherry picked from commit 4e662a1)

https://bugzilla.redhat.com/show_bug.cgi?id=1511977
  • Loading branch information
gmcculloug authored and simaishi committed Nov 14, 2017
1 parent c3fdf23 commit 48e108b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
12 changes: 12 additions & 0 deletions app/models/service_ansible_playbook.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ def job(action)
def postprocess(action)
inventory_raw_id = options.fetch_path(job_option_key(action), :inventory)
delete_inventory(action, inventory_raw_id) if inventory_raw_id
log_stdout(action)
end

def on_error(action)
Expand Down Expand Up @@ -163,4 +164,15 @@ def decrypt_options(opts)
opts[:extra_vars].transform_values! { |val| val.kind_of?(String) ? MiqPassword.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.normalized_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
12 changes: 7 additions & 5 deletions spec/models/service_ansible_playbook_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@
expect(basic_service).to receive(:create_inventory_with_hosts).with(action, hosts).and_return(double(:id => 10))
basic_service.preprocess(action)
service.reload
expect(basic_service.options[:provision_job_options]).to have_attributes(:inventory => 10)
expect(basic_service.options[:provision_job_options]).to include(:inventory => 10)
end
end

Expand All @@ -92,7 +92,7 @@
expect(service).to receive(:create_inventory_with_hosts).with(action, hosts).and_return(double(:id => 20))
service.preprocess(action)
service.reload
expect(service.options[:provision_job_options]).to have_attributes(
expect(service.options[:provision_job_options]).to include(
:inventory => 20,
:credential => credential_1.manager_ref,
:extra_vars => {'var1' => 'value1', 'var2' => 'value2', 'var3' => 'default_val3', 'pswd' => encrypted_val}
Expand All @@ -113,11 +113,11 @@
expect(service).to receive(:create_inventory_with_hosts).with(action, hosts).and_return(double(:id => 20))
service.preprocess(action)
service.reload
expect(service.options[:retirement_job_options]).to have_attributes(
expect(service.options[:retirement_job_options]).to include(
:inventory => 20,
:credential => nil,
:extra_vars => {'var1' => 'default_val1', 'var2' => 'default_val2', 'var3' => 'default_val3'}
)
expect(service.options[:retirement_job_options]).not_to have_key(:credential)
end
end
end
Expand All @@ -128,7 +128,7 @@
expect(service).to receive(:create_inventory_with_hosts).with(action, hosts).and_return(double(:id => 30))
service.preprocess(action, override_options)
service.reload
expect(service.options[:provision_job_options]).to have_attributes(
expect(service.options[:provision_job_options]).to include(
:inventory => 30,
:credential => credential_2.manager_ref,
:extra_vars => {'var1' => 'new_val1', 'var2' => 'value2', 'var3' => 'default_val3', 'pswd' => encrypted_val2}
Expand Down Expand Up @@ -206,6 +206,7 @@
context 'with user selected hosts' do
it 'deletes temporary inventory' do
expect(executed_service).to receive(:delete_inventory)
expect(executed_service).to receive(:log_stdout)
executed_service.postprocess(action)
end
end
Expand All @@ -221,6 +222,7 @@
end

it 'needs not to delete the inventory' do
expect(executed_service).to receive(:log_stdout)
expect(executed_service).not_to receive(:delete_inventory)
executed_service.postprocess(action)
end
Expand Down

0 comments on commit 48e108b

Please sign in to comment.