Skip to content

Commit

Permalink
Merge pull request ManageIQ#17250 from carbonin/ansible_job_retention
Browse files Browse the repository at this point in the history
Add a setting for ansible job data retention
  • Loading branch information
bdunne authored Apr 5, 2018
2 parents 54589f6 + 85435c9 commit ce34861
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ group :amazon, :manageiq_default do
end

group :ansible, :manageiq_default do
gem "ansible_tower_client", "~>0.12.2", :require => false
gem "ansible_tower_client", "~>0.13.0", :require => false
end

group :azure, :manageiq_default do
Expand Down
8 changes: 8 additions & 0 deletions app/models/embedded_ansible_worker/runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ def heartbeat
def do_work
embedded_ansible.start if !embedded_ansible.alive? && !embedded_ansible.running?
provider.authentication_check if embedded_ansible.alive? && !provider.authentication_status_ok?
update_job_data_retention
end

def before_exit(*_)
Expand Down Expand Up @@ -65,6 +66,13 @@ def message_sync_config(*_args); end

private

def update_job_data_retention
return if @job_data_retention == ::Settings.embedded_ansible.job_data_retention_days

embedded_ansible.set_job_data_retention
@job_data_retention = ::Settings.embedded_ansible.job_data_retention_days
end

def provider
@provider ||= ManageIQ::Providers::EmbeddedAnsible::Provider.first_or_initialize
end
Expand Down
1 change: 1 addition & 0 deletions config/settings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@
:keep_drift_states: 6.months
:purge_window_size: 10000
:embedded_ansible:
:job_data_retention_days: 120
:docker:
:task_image_name: ansible/awx_task
:task_image_tag: latest
Expand Down
14 changes: 14 additions & 0 deletions lib/embedded_ansible.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,22 @@ def alive?
true
end

def set_job_data_retention
sched = job_cleanup_template.schedules.first
sched.extra_data.days = ::Settings.embedded_ansible.job_data_retention_days
sched.save!
end

def run_job_data_retention(days = ::Settings.embedded_ansible.job_data_retention_days)
job_cleanup_template.launch("days" => days)
end

private

def job_cleanup_template
api_connection.api.system_job_templates.all.find { |t| t.job_type == "cleanup_jobs" }
end

def api_connection_raw(host, port)
admin_auth = miq_database.ansible_admin_authentication
AnsibleTowerClient::Connection.new(
Expand Down
48 changes: 44 additions & 4 deletions spec/lib/embedded_ansible_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,16 @@
EvmSpecHelper.create_guid_miq_server_zone
end

shared_context "api connection" do
let(:api_conn) { double("AnsibleAPIConnection") }
let(:api) { double("AnsibleAPIResource") }

before do
expect(subject).to receive(:api_connection).and_return(api_conn)
expect(api_conn).to receive(:api).and_return(api)
end
end

describe "#alive?" do
it "returns false if the service is not configured" do
expect(subject).to receive(:configured?).and_return false
Expand All @@ -118,14 +128,11 @@
end

context "when a connection is attempted" do
let(:api_conn) { double("AnsibleAPIConnection") }
let(:api) { double("AnsibleAPIResource") }
include_context "api connection"

before do
expect(subject).to receive(:configured?).and_return true
expect(subject).to receive(:running?).and_return true
expect(subject).to receive(:api_connection).and_return(api_conn)
expect(api_conn).to receive(:api).and_return(api)

miq_database.set_ansible_admin_authentication(:password => "adminpassword")
end
Expand Down Expand Up @@ -164,6 +171,39 @@
end
end

context "with a job cleanup template" do
include_context "api connection"
let(:data) { double("ExtraData") }
let(:schedule) { double("Schedule", :save! => true, :extra_data => data) }
let(:cleanup_jobs) { double("SystemJobTemplate", :job_type => "cleanup_jobs", :schedules => [schedule]) }
let(:cleanup_facts) { double("SystemJobTemplate", :job_type => "cleanup_facts") }

before do
expect(api).to receive(:system_job_templates).and_return(double("Enumerator", :all => [cleanup_jobs, cleanup_facts]))
end

describe "#set_job_data_retention" do
it "sets the retention value in the schedule extra data" do
stub_settings(:embedded_ansible => {:job_data_retention_days => 123})
expect(data).to receive(:days=).with(123)
subject.set_job_data_retention
end
end

describe "#run_job_data_retention" do
it "runs the cleanup job on demand with the passed value" do
expect(cleanup_jobs).to receive(:launch).with("days" => 14)
subject.run_job_data_retention(14)
end

it "defaults the days to the setting" do
stub_settings(:embedded_ansible => {:job_data_retention_days => 1})
expect(cleanup_jobs).to receive(:launch).with("days" => 1)
subject.run_job_data_retention
end
end
end

describe "#find_or_create_database_authentication (private)" do
let(:password) { "secretpassword" }
let(:quoted_password) { ActiveRecord::Base.connection.quote(password) }
Expand Down
13 changes: 13 additions & 0 deletions spec/models/embedded_ansible_worker/runner_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,10 @@
end

context "#do_work" do
before do
runner.instance_variable_set(:@job_data_retention, ::Settings.embedded_ansible.job_data_retention_days)
end

it "starts embedded ansible if it is not alive and not running" do
allow(embedded_ansible_instance).to receive(:alive?).and_return(false)
allow(embedded_ansible_instance).to receive(:running?).and_return(false)
Expand Down Expand Up @@ -155,6 +159,15 @@

runner.do_work
end

it "sets the embedded ansible job data retention value when the setting changes" do
allow(embedded_ansible_instance).to receive(:alive?).and_return(true)
allow(runner).to receive(:provider).and_return(provider)
stub_settings(:embedded_ansible => {:job_data_retention_days => 30})

expect(embedded_ansible_instance).to receive(:set_job_data_retention)
runner.do_work
end
end
end
end
Expand Down

0 comments on commit ce34861

Please sign in to comment.