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

Collect more attributes for Ansible Tower job #14076

Merged
merged 1 commit into from
Feb 28, 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
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,17 @@ def refresh_ems
end

def update_with_provider_object(raw_job)
self.ems_ref = raw_job.id
self.status = raw_job.status
self.parameters =
raw_job.extra_vars_hash.collect do |para_key, para_val|
self.ems_ref ||= raw_job.id
self.status = raw_job.status
self.start_time ||= raw_job.started
self.finish_time ||= raw_job.finished
self.verbosity ||= raw_job.verbosity

if parameters.empty?
self.parameters = raw_job.extra_vars_hash.collect do |para_key, para_val|
OrchestrationStackParameter.new(:name => para_key, :value => para_val, :ems_ref => "#{raw_job.id}_#{para_key}")
end if parameters.empty?
end
end
save!
end
private :update_with_provider_object
Expand Down
8 changes: 8 additions & 0 deletions app/models/service_ansible_playbook.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ def execute(action)

_log.info("Launching Ansible Tower job with options: #{opts}")
new_job = ManageIQ::Providers::AnsibleTower::AutomationManager::Job.create_job(jt, 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)
Expand Down Expand Up @@ -119,4 +120,11 @@ def inventory_name(action)
def use_default_inventory?(hosts)
hosts.blank? || hosts == 'localhost'
end

# update job attributes only available to playbook provisioning
def update_job_for_playbook(action, job, hosts)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this need a hosts = nil or are you expecting someone to pass nil as the last arg?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a private method used internal only, but hosts might be nil if it is never set anywhere.

hosts = (hosts || 'localhost').split(',')
playbook_id = options.fetch_path(:config_info, action.downcase.to_sym, :playbook_id)
job.update_attributes(:configuration_script_base_id => playbook_id, :hosts => hosts)
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@
'id' => '1',
'name' => template.name,
'status' => 'Successful',
'extra_vars' => {'param1' => 'val1'}.to_json
'extra_vars' => {'param1' => 'val1'}.to_json,
'verbosity' => 3,
'started' => Time.current,
'finished' => Time.current,
).tap { |rjob| allow(rjob).to receive(:stdout).and_return('job stdout') }
end

Expand Down Expand Up @@ -52,6 +55,13 @@

it 'syncs the job with the provider' do
subject.refresh_ems
expect(subject).to have_attributes(
:ems_ref => the_raw_job.id,
:status => the_raw_job.status,
:start_time => the_raw_job.started,
:finish_time => the_raw_job.finished,
:verbosity => the_raw_job.verbosity
)
expect(subject.ems_ref).to eq(the_raw_job.id)
expect(subject.status).to eq(the_raw_job.status)
expect(subject.parameters.first).to have_attributes(:name => 'param1', :value => 'val1')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@
'id' => '1',
'name' => template.name,
'status' => 'Successful',
'extra_vars' => {'param1' => 'val1'}.to_json
'extra_vars' => {'param1' => 'val1'}.to_json,
'verbosity' => 3,
'started' => Time.current,
'finished' => Time.current,
).tap { |rjob| allow(rjob).to receive(:stdout).and_return('job stdout') }
end

Expand Down Expand Up @@ -52,6 +55,13 @@

it 'syncs the job with the provider' do
subject.refresh_ems
expect(subject).to have_attributes(
:ems_ref => the_raw_job.id,
:status => the_raw_job.status,
:start_time => the_raw_job.started,
:finish_time => the_raw_job.finished,
:verbosity => the_raw_job.verbosity
)
expect(subject.ems_ref).to eq(the_raw_job.id)
expect(subject.status).to eq(the_raw_job.status)
expect(subject.parameters.first).to have_attributes(:name => 'param1', :value => 'val1')
Expand Down
17 changes: 13 additions & 4 deletions spec/models/service_ansible_playbook_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
service_template = FactoryGirl.create(:service_template_ansible_playbook)
service_template.resource_actions.build(:action => action, :configuration_template => tower_job_temp)
service_template.save!
FactoryGirl.create(:service_ansible_playbook, :options => provision_options, :service_template => service_template)
FactoryGirl.create(:service_ansible_playbook,
:options => provision_options.merge(config_info_options),
:service_template => service_template)
end

let(:executed_service) do
Expand All @@ -35,8 +37,9 @@
{
:config_info => {
:provision => {
:hosts => "default_host1,default_host2",
:extra_vars => {
:hosts => "default_host1,default_host2",
:playbook_id => 10,
:extra_vars => {
"var1" => "default_val1",
"var2" => "default_val2",
"var3" => "default_val3"
Expand All @@ -59,6 +62,7 @@
:provision_job_options => {
:credential => 1,
:inventory => 2,
:hosts => "default_host1,default_host2",
:extra_vars => {'var1' => 'value1', 'var2' => 'value2'}
}
}
Expand Down Expand Up @@ -109,7 +113,12 @@
expect(ManageIQ::Providers::AnsibleTower::AutomationManager::Job)
.to receive(:create_job).with(tower_job_temp, provision_options[:provision_job_options]).and_return(tower_job)
loaded_service.execute(action)
expect(loaded_service.job(action)).to eq(tower_job)
expected_job_attributes = {
:id => tower_job.id,
:hosts => config_info_options.fetch_path(:config_info, :provision, :hosts).split(','),
:configuration_script_base_id => config_info_options.fetch_path(:config_info, :provision, :playbook_id)
}
expect(loaded_service.job(action)).to have_attributes(expected_job_attributes)
end
end

Expand Down
8 changes: 7 additions & 1 deletion spec/models/service_ansible_tower_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,13 @@
expect(template).to be_kind_of ConfigurationScript
expect(opts).to have_key(:limit)
expect(opts).to have_key(:extra_vars)
end.and_return(double(:raw_job, :id => 1, :status => "completed", :extra_vars_hash => {'var_name' => 'var_val'}))
end.and_return(double(:raw_job,
:id => 1,
:status => "completed",
:verbosity => 0,
:started => Time.current,
:finished => Time.current,
:extra_vars_hash => {'var_name' => 'var_val'}))

job_done = service_mix_dialog_setter.launch_job
expect(job_done).to have_attributes(:ems_ref => "1", :status => "completed")
Expand Down