Class: ForemanAnsible::InsightsPlanRunner

Inherits:
Object
  • Object
show all
Includes:
RedhatAccess::Telemetry::LookUps
Defined in:
app/services/foreman_ansible/insights_plan_runner.rb

Overview

Fetch information about a plan from RH Insights and run it

Instance Method Summary collapse

Constructor Details

#initialize(organization, plan_id) ⇒ InsightsPlanRunner

Returns a new instance of InsightsPlanRunner.



9
10
11
12
# File 'app/services/foreman_ansible/insights_plan_runner.rb', line 9

def initialize(organization, plan_id)
  @organization = organization
  @plan_id = plan_id
end

Instance Method Details

#hostname_rules(rules) ⇒ Object



75
76
77
78
79
80
81
82
83
# File 'app/services/foreman_ansible/insights_plan_runner.rb', line 75

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

#parse_disclaimer(playbook = @raw_playbook) ⇒ Object

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…”



45
46
47
48
49
50
51
52
53
54
55
# File 'app/services/foreman_ansible/insights_plan_runner.rb', line 45

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

#playbookObject

Fetches the playbook from the Red Hat Insights API



31
32
33
34
35
36
37
38
39
40
# File 'app/services/foreman_ansible/insights_plan_runner.rb', line 31

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

#rules_to_hash(rules) ⇒ Object

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)


66
67
68
69
70
71
72
73
# File 'app/services/foreman_ansible/insights_plan_runner.rb', line 66

def rules_to_hash(rules)
  result = {}
  rules.map do |rule|
    rule['hosts'] = 'all'
    result[rule['name']] = rule
  end
  result
end

#run_playbookObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'app/services/foreman_ansible/insights_plan_runner.rb', line 14

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