diff --git a/app/services/foreman_ansible/insights_notification_builder.rb b/app/services/foreman_ansible/insights_notification_builder.rb deleted file mode 100644 index 2d29a04d5..000000000 --- a/app/services/foreman_ansible/insights_notification_builder.rb +++ /dev/null @@ -1,64 +0,0 @@ -# frozen_string_literal: true - -module ForemanAnsible - # A class that builds custom notificaton for REX job if it's insights - # remediation feature - class InsightsNotificationBuilder < ::UINotifications::RemoteExecutionJobs::BaseJobFinish - def deliver! - ::Notification.create!( - :audience => Notification::AUDIENCE_USER, - :notification_blueprint => blueprint, - :initiator => initiator, - :message => message, - :subject => subject, - :actions => { - :links => links - } - ) - end - - def blueprint - name = 'insights_remediation_successful' - @blueprint ||= NotificationBlueprint.unscoped.find_by(:name => name) - end - - def hosts_count - @hosts_count ||= subject.template_invocations_hosts.size - end - - def message - UINotifications::StringParser.new(blueprint.message, - :hosts_count => hosts_count) - end - - def links - job_links + insights_links - end - - def insights_links - pattern_template = subject.pattern_template_invocations.first - plan_id = pattern_template.input_values. - joins(:template_input). - where('template_inputs.name' => 'plan_id'). - first.try(:value) - return [] if plan_id.nil? - - [ - { - :href => "/redhat_access/insights/planner/#{plan_id}", - :title => _('Remediation Plan') - } - ] - end - - def job_links - UINotifications::URLResolver.new( - subject, - :links => [{ - :path_method => :job_invocation_path, - :title => _('Job Details') - }] - ).actions[:links] - end - end -end diff --git a/app/services/foreman_ansible/insights_plan_runner.rb b/app/services/foreman_ansible/insights_plan_runner.rb deleted file mode 100644 index cf3254f0f..000000000 --- a/app/services/foreman_ansible/insights_plan_runner.rb +++ /dev/null @@ -1,86 +0,0 @@ -# frozen_string_literal: true - -if defined?(RedhatAccess) - module ForemanAnsible - # Fetch information about a plan from RH Insights and run it - class InsightsPlanRunner - include RedhatAccess::Telemetry::LookUps - - def initialize(organization, plan_id) - @organization = organization - @plan_id = plan_id - end - - def run_playbook - rules = playbook - hostname_rules_relation = hostname_rules(rules) - hosts = hostname_rules_relation.keys.map do |hostname| - Host::Managed.find_by(:name => hostname) - end - - composer = JobInvocationComposer.for_feature( - :ansible_run_insights_plan, - hosts, - :organization_id => @organization.id, :plan_id => @plan_id - ) - composer.save - composer.trigger - end - - # Fetches the playbook from the Red Hat Insights API - def playbook - resource = RestClient::Resource.new( - "#{insights_api_host}/r/insights/"\ - "v3/maintenance/#{@plan_id}/playbook", - get_ssl_options_for_org(@organization, nil).\ - merge(:proxy => get_portal_http_proxy) - ) - @raw_playbook = resource.get.body - YAML.safe_load(@raw_playbook) - end - - # To parse the disclaimer we iterate over the first lines of the - # playbook (all comments) until we get to a line that looks like - # "Generated by Red Hat Insights on..." - def parse_disclaimer(playbook = @raw_playbook) - return '' if playbook.blank? - disclaimer = [] - playbook.split("\n").each do |line| - next if line == '---' - break unless line[0] == '#' - disclaimer << line - break if /Generated by Red Hat Insights on/ =~ line - end - disclaimer.join("\n") - end - - # This method creates a hash like this: - # { - # hostname1 => [rule1,rule2,rule3], - # hostname2 => [rule1,rule3], - # } - # - # Rules are distinguished by name and saved without the 'hosts' field - # as it's irrelevant in the Foreman REX context ('hosts: all' is used - # so that all=job invocation targets) - def rules_to_hash(rules) - result = {} - rules.map do |rule| - rule['hosts'] = 'all' - result[rule['name']] = rule - end - result - end - - def hostname_rules(rules) - result = Hash.new { |h, k| h[k] = [] } - rules.each do |rule| - rule['hosts'].split(',').each do |host| - result[host] << rule['name'] - end - end - result - end - end - end -end diff --git a/app/services/foreman_ansible/renderer_methods.rb b/app/services/foreman_ansible/renderer_methods.rb deleted file mode 100644 index ddc743604..000000000 --- a/app/services/foreman_ansible/renderer_methods.rb +++ /dev/null @@ -1,46 +0,0 @@ -# frozen_string_literal: true - -module ForemanAnsible - # Macro to fetch RH Insights plan playbook - module RendererMethods - extend ActiveSupport::Concern - extend ApipieDSL::Module - - apipie :class, 'Macros related to Ansible playbooks' do - name 'Ansible' - sections only: %w[all jobs] - end - - apipie :method, 'Returns Insights maintenance plan for host' do - required :plan_id, String, desc: 'The playbook for the rule coming from insights' - optional :organization_id, Integer, desc: 'The Foreman organization associated with the Insights account', default: 'Current organization ID' - returns String, desc: 'Insights maintenance plan for host' - end - def insights_remediation(plan_id, organization_id = Organization.current.id) - return "$INSIGHTS_REMEDIATION[#{plan_id}, #{organization_id}]" if preview? - - cached("insights_#{plan_id}_#{organization_id}") do - Rails.logger.debug 'cache miss for insights plan fetching' - insights_plan = ForemanAnsible::InsightsPlanRunner.new( - Organization.find(organization_id), - plan_id - ) - rules = insights_plan.playbook - disclaimer = insights_plan.parse_disclaimer - hostname_rules_relation = insights_plan.hostname_rules(rules) - global_rules = insights_plan.rules_to_hash(rules) - host_playbooks = individual_host_playbooks(hostname_rules_relation, - global_rules) - "#{disclaimer}\n#{host_playbooks.to_yaml}" - end - end - - private - - def individual_host_playbooks(hostname_rules_relation, global_rules) - hostname_rules_relation[@host.name].reduce([]) do |acc, cur| - acc << global_rules[cur] - end - end - end -end diff --git a/app/views/foreman_ansible/job_templates/maintenance_plan.erb b/app/views/foreman_ansible/job_templates/maintenance_plan.erb deleted file mode 100644 index 494514ded..000000000 --- a/app/views/foreman_ansible/job_templates/maintenance_plan.erb +++ /dev/null @@ -1,19 +0,0 @@ -<%# -kind: job_template -name: Ansible - Run insights maintenance plan -job_category: Ansible Playbook -description_format: 'Insights maintenance plan for host' -feature: ansible_run_insights_plan -template_inputs: -- name: plan_id - description: The playbook for the rule coming from insights. - input_type: user - required: true -- name: organization_id - description: The Foreman organization associated with the Insights account - input_type: user - required: true -provider_type: Ansible -%> - -<%= insights_remediation(input(:plan_id), input(:organization_id)) %> diff --git a/db/seeds.d/90_notification_blueprints.rb b/db/seeds.d/90_notification_blueprints.rb index d7ef66c2c..58c68054b 100644 --- a/db/seeds.d/90_notification_blueprints.rb +++ b/db/seeds.d/90_notification_blueprints.rb @@ -1,19 +1,6 @@ # frozen_string_literal: true blueprints = [ - { - :group => N_('Jobs'), - :name => 'insights_remediation_successful', - :message => N_('Insights remediation on %{hosts_count}' \ - ' host(s) has finished successfully'), - :level => 'success', - :actions => { - :links => [ - :path_method => :job_invocation_path, - :title => N_('Job Details') - ] - } - }, { :group => N_('Roles'), :name => 'Sync_roles_and_variables_successfully', diff --git a/lib/foreman_ansible/register.rb b/lib/foreman_ansible/register.rb index 26029c0d7..97e973527 100644 --- a/lib/foreman_ansible/register.rb +++ b/lib/foreman_ansible/register.rb @@ -178,8 +178,6 @@ 'Permissions required for the user which is used by Ansible Tower Dynamic Inventory Item' add_all_permissions_to_default_roles - extend_template_helpers ForemanAnsible::RendererMethods - allowed_template_helpers :insights_remediation base_role_assignment_params = { :ansible_role_ids => [], :ansible_roles => [] } diff --git a/lib/foreman_ansible/remote_execution.rb b/lib/foreman_ansible/remote_execution.rb index cb302e89c..1474731dc 100644 --- a/lib/foreman_ansible/remote_execution.rb +++ b/lib/foreman_ansible/remote_execution.rb @@ -22,14 +22,6 @@ def self.register_rex_feature ' the roles defined for a host'), :host_action_button => true ) - RemoteExecutionFeature.register( - :ansible_run_insights_plan, - N_('Ansible: Run Insights maintenance plan'), - :description => N_('Runs a given maintenance plan from Red Hat '\ - 'Access Insights given an ID.'), - :provided_inputs => %w[organization_id plan_id], - :notification_builder => ForemanAnsible::InsightsNotificationBuilder - ) RemoteExecutionFeature.register( :ansible_run_playbook, N_('Run playbook'), diff --git a/test/unit/services/insights_plan_runner_test.rb b/test/unit/services/insights_plan_runner_test.rb deleted file mode 100644 index d43b988bc..000000000 --- a/test/unit/services/insights_plan_runner_test.rb +++ /dev/null @@ -1,38 +0,0 @@ -# frozen_string_literal: true - -require 'test_plugin_helper' - -if defined? RedhatAccess - module ForemanAnsible - # Tests for the RH Insights plan runner. Mostly about checking whether - # the playbook can be parsed properly - class InsightsPlanRunnerTest < ActiveSupport::TestCase - test 'disclaimer is saved as raw_playbook' do - disclaimer = <<-DISCLAIMER.strip_heredoc.strip - # Red Hat Insights has recommended one or more actions for you, a system administrator, to review and if you - # deem appropriate, deploy on your systems running Red Hat software. Based on the analysis, we have automatically - # generated an Ansible Playbook for you. Please review and test the recommended actions and the Playbook as - # they may contain configuration changes, updates, reboots and/or other changes to your systems. Red Hat is not - # responsible for any adverse outcomes related to these recommendations or Playbooks. - # - # Addresses maintenance plan 38439429 (demo) - # https://access.redhat.com/insights/planner/438294928 - # Generated by Red Hat Insights on Wed, 18 Apr 2018 07:54:18 GMT - DISCLAIMER - - playbook = File.read(ansible_fixture_file('insights_playbook.yaml')) - planner = ::ForemanAnsible::InsightsPlanRunner.new( - FactoryBot.build(:organization), rand - ) - assert_empty planner.parse_disclaimer - planner.expects(:insights_api_host).returns('') - planner.expects(:get_ssl_options_for_org).returns(nil) - RestClient::Resource.any_instance.expects(:get).returns( - OpenStruct.new(:body => playbook) - ) - planner.playbook - assert_equal disclaimer, planner.parse_disclaimer - end - end - end -end