Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add log_output option for embedded ansible service #16414

Merged
merged 1 commit into from
Nov 8, 2017
Merged
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
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