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 retireable check to service ansible playbooks #18609

Conversation

d-m-u
Copy link
Contributor

@d-m-u d-m-u commented Mar 29, 2019

We are currently creating the service retire tasks before we send them to automate, which means that if the parent service is an ansible playbook service with config options specifying the resources to not be retired, we will have already created tasks for the retirement of said resources, and will not honor the playbook config options hash. This cuts off the task creation for APS's early if the config option to not retire resources is present.

Fixes https://bugzilla.redhat.com/show_bug.cgi?id=1692941

@d-m-u
Copy link
Contributor Author

d-m-u commented Mar 29, 2019

@miq-bot add_label bug
@miq-bot add_label hammer/yes

@d-m-u
Copy link
Contributor Author

d-m-u commented Mar 29, 2019

@miq-bot assign @gmcculloug

@d-m-u d-m-u changed the title Add retireable check to service ansible playbooooooooooks Add retireable check to service ansible playbooks Mar 29, 2019
@gmcculloug gmcculloug assigned tinaafitz and unassigned gmcculloug Mar 29, 2019
@gmcculloug gmcculloug requested review from lfu, tinaafitz and mkanoor March 29, 2019 14:37
@d-m-u
Copy link
Contributor Author

d-m-u commented Mar 29, 2019

@miq-bot add_reviewer @tinaafitz
@miq_bot add_reviewer @mkanoor

@d-m-u d-m-u force-pushed the adding_remove_resource_method_to_service_template_ansible_playbook branch from 1594bed to 0315267 Compare March 29, 2019 14:43
@@ -29,7 +29,7 @@ def after_request_task_create
update_attributes(:description => get_description)
parent_svc = Service.find_by(:id => options[:src_ids])
_log.info("- creating service subtasks for service task <#{self.class.name}:#{id}>, service <#{parent_svc.id}>")
create_retire_subtasks(parent_svc, self)
create_retire_subtasks(parent_svc, self) unless parent_svc.type == 'ServiceAnsiblePlaybook' && !parent_svc.retireable?
Copy link
Member

Choose a reason for hiding this comment

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

Two thoughts:

  1. Compare the class object instead of the class name string: parent_svc.kind_of?(ServiceAnsiblePlaybook)
  2. Instead of using a complex unless statement, which we try to avoid, move this into a separate private method retire_subtasks? which can be tested independently.

Will make this line easy to read:
create_retire_subtasks(parent_svc, self) if retire_subtasks?

@@ -32,6 +33,14 @@ def execute(action)
add_resource!(new_job, :name => action)
end

def retireable?
!retain_resources?
Copy link
Member

Choose a reason for hiding this comment

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

@tinaafitz Is this correct? The Service is considered non-retireable if we are retaining resources? I thought we would still run the playbook if it was configured and this feels like we would not do that.

Copy link
Member

Choose a reason for hiding this comment

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

@gmcculloug The service_resources for ansible services are considered non-retireable if we are retaining resources. The service_retire_task has already been created, so it's only preventing the service_resources from potentially creating retire tasks.

app/models/service_ansible_playbook.rb Outdated Show resolved Hide resolved
@d-m-u d-m-u force-pushed the adding_remove_resource_method_to_service_template_ansible_playbook branch 4 times, most recently from bc9e62c to 60eb777 Compare March 30, 2019 17:17
@miq-bot miq-bot requested a review from tinaafitz March 30, 2019 17:32
@d-m-u d-m-u force-pushed the adding_remove_resource_method_to_service_template_ansible_playbook branch from 97a7fdd to 4037d54 Compare April 1, 2019 13:39
@@ -32,6 +33,10 @@ def execute(action)
add_resource!(new_job, :name => action)
end

def retireable?
!retain_resources_on_retirement?
Copy link
Member

Choose a reason for hiding this comment

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

So discussed more with @tinaafitz and this does not feel correct. If you ask the service instance if it can be retired the answer should not be solely used to drive if child resources are retired, it should be answering the question for it self. Another way of looking at it would be if the UI was using this method to determine if the service itself could be retired. I think we want the response to be true regardless of the child-items.

Seems like the retain_resources_on_retirement is doing more what we want and this method is not needed.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I was under the impression that at the end of the conversation I and you and Madhu had in the kitchen that the proposed solution then was to use this retireable? check from the CiFeatureMixin to determine whether the service was retireable. If we're not using this approach I think we should have the discussion that Jason kinda started on last week about how checking to see if something is retireable requires both a general check like those used by the SupportsFeatureMixin and something more specific because since this issue is related it'd be easier to have that discussion sooner.

@@ -68,12 +73,16 @@ def on_error(action)

private

def retain_resources_on_retirement?
%w[no_without_playbook no_with_playbook].include?(options.fetch_path(:config_info, :retirement, :remove_resources))
Copy link
Member

Choose a reason for hiding this comment

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

Realized I already commented on this line, but looking at the options it seems like it is just:
options.fetch_path(:config_info, :retirement, :remove_resources).to_s.starts_with("no_")

Not a required change, but seems simpler and maybe more future proof.

app/models/service_ansible_playbook.rb Show resolved Hide resolved
@@ -68,6 +70,10 @@ def create_task(svc_rsc, parent_service, nh, parent_task)

private

def retire_subtasks?(parent_svc)
parent_svc.kind_of?(ServiceAnsiblePlaybook) && !parent_svc.retireable? ? false : true
Copy link
Member

Choose a reason for hiding this comment

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

I think this method should call retain_resources_on_retirement? based on comments about retireable?. It will need to handle if the parent_service exposes this method.

@d-m-u d-m-u force-pushed the adding_remove_resource_method_to_service_template_ansible_playbook branch 6 times, most recently from b5599fa to 2a39ffd Compare April 1, 2019 17:49
@@ -68,6 +70,10 @@ def create_task(svc_rsc, parent_service, nh, parent_task)

private

def retain_ansible_playbook_subtasks?(parent_svc)
parent_svc.kind_of?(ServiceAnsiblePlaybook) && parent_svc.retain_resources_on_retirement?
Copy link
Member

Choose a reason for hiding this comment

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

I suggest trying to make these method names more generic. Also the currently naming is misleading, you are not retaining subtasks, you are retaining resources of the service by skipping the subtasks that would retire them.

def create_subtasks?(parent_svc)
  !parent_svc.try(:retain_resources_on_retirement?)
end

This makes the caller above easier to read as it would change to:
if create_subtasks?(parent_svc)

@@ -28,8 +28,10 @@ def task_active
def after_request_task_create
update_attributes(:description => get_description)
parent_svc = Service.find_by(:id => options[:src_ids])
_log.info("- creating service subtasks for service task <#{self.class.name}:#{id}>, service <#{parent_svc.id}>")
create_retire_subtasks(parent_svc, self)
unless retain_ansible_playbook_subtasks?(parent_svc)
Copy link
Member

Choose a reason for hiding this comment

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

With the suggested change to the "create_subtasks?" method this would be easier to read as:
if create_subtasks?(parent_svc)

@d-m-u d-m-u force-pushed the adding_remove_resource_method_to_service_template_ansible_playbook branch from 2a39ffd to a309f31 Compare April 1, 2019 20:42
Copy link
Member

@gmcculloug gmcculloug left a comment

Choose a reason for hiding this comment

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

@tinaafitz @lfu Please review

describe '#retain_resources_on_retirement?' do
context "no_with_playbook is false" do
let(:remove_resources) { 'no_with_playbook' }
let(:can_children_be_retired?) { true }
Copy link
Member

@lfu lfu Apr 1, 2019

Choose a reason for hiding this comment

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

The logic here seems not right.

describe '#retain_resources_on_retirement?' do
    context "no_with_playbook" do
      let(:remove_resources) { 'no_with_playbook' }
      let(:can_children_be_retired?) { false }
      it_behaves_like "#retain_resources_on_retirement"

service_options[:config_info][:retirement] = service_options[:config_info][:provision]
service_options[:config_info][:retirement][:remove_resources] = remove_resources
service.update_attributes(:options => service_options)
expect(service.retain_resources_on_retirement?).to eq(can_children_be_retired?)
Copy link
Member

Choose a reason for hiding this comment

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

When resource is to be retained, then children can not be retired.

expect(service.retain_resources_on_retirement?).to eq(!can_children_be_retired?)

expect(ServiceRetireTask.count).to eq(1)
expect(VmRetireTask.count).to eq(0)
end
end
Copy link
Member

Choose a reason for hiding this comment

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

Shared example can be used for above two cases.

expect(VmRetireTask.count).to eq(1)
end
end
end
Copy link
Member

Choose a reason for hiding this comment

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

Same here.


context "yes_without_playbook is true" do
let(:remove_resources) { 'yes_without_playbook' }
let(:can_children_be_retired?) { false }
Copy link
Member

Choose a reason for hiding this comment

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

Same here.


context "yes_with_playbook is true" do
let(:remove_resources) { 'yes_with_playbook' }
let(:can_children_be_retired?) { false }
Copy link
Member

Choose a reason for hiding this comment

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

Same here.

context "no_without_playbook is false" do
let(:remove_resources) { 'no_without_playbook' }
let(:can_children_be_retired?) { true }
it_behaves_like "#retain_resources_on_retirement"
Copy link
Member

Choose a reason for hiding this comment

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

Same here.

@d-m-u d-m-u force-pushed the adding_remove_resource_method_to_service_template_ansible_playbook branch from a309f31 to 0cf67b4 Compare April 1, 2019 23:12
@miq-bot
Copy link
Member

miq-bot commented Apr 1, 2019

Checked commit d-m-u@0cf67b4 with ruby 2.3.3, rubocop 0.52.1, haml-lint 0.20.0, and yamllint 1.10.0
4 files checked, 0 offenses detected
Everything looks fine. 👍

@d-m-u
Copy link
Contributor Author

d-m-u commented Apr 2, 2019

@lfu please re-review

@gmcculloug gmcculloug merged commit ff41ba2 into ManageIQ:master Apr 2, 2019
@gmcculloug gmcculloug added this to the Sprint 108 Ending Apr 1, 2019 milestone Apr 2, 2019
@d-m-u d-m-u deleted the adding_remove_resource_method_to_service_template_ansible_playbook branch April 2, 2019 13:30
@simaishi
Copy link
Contributor

simaishi commented Apr 3, 2019

@d-m-u There is a conflict backporting this to hammer branch due to refactoring that happened in #18057. Can you please create a separate PR for hammer?

File: app/models/service_ansible_playbook.rb

 +  def manageiq_connection_env
      {
++<<<<<<< HEAD
 +      'url'         => api_url,
 +      'token'       => api_token,
 +      'X_MIQ_Group' => evm_owner.current_group.description
 +    }
 +  end
 +
 +  def api_token
 +    @api_token ||= Api::UserTokenService.new.generate_token(evm_owner.userid, 'api')
 +  end
 +
 +  def api_url
 +    @api_url ||= MiqRegion.my_region.remote_ws_url
++=======
+       'service' => href_slug,
+       'action'  => action
+     }.merge(manageiq_env(evm_owner, miq_group, miq_request_task))
+       .merge(request_options_extra_vars)
++>>>>>>> ff41ba27ab... Merge pull request #18609 from d-m-u/adding_remove_resource_method_to_service_template_ansible_playbook

@d-m-u
Copy link
Contributor Author

d-m-u commented Apr 3, 2019

sure @simaishi right away ma'am

@simaishi
Copy link
Contributor

simaishi commented Apr 4, 2019

Backported to Hammer via #18620

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants