class InsightsCloud::Async::InsightsFullSync

Public Instance Methods

logger() click to toggle source
# File lib/insights_cloud/async/insights_full_sync.rb, line 58
def logger
  action_logger
end
perform_hits_sync(organization) click to toggle source
# File lib/insights_cloud/async/insights_full_sync.rb, line 49
def perform_hits_sync(organization)
  hits = query_insights_hits(organization)

  uuids = hits.map { |hit| hit['uuid'] }
  setup_host_ids(uuids, organization)

  replace_hits_data(hits, organization)
end
plan(organizations) click to toggle source
# File lib/insights_cloud/async/insights_full_sync.rb, line 10
def plan(organizations)
  organizations = organizations.select do |organization|
    checker = ::Katello::UpstreamConnectionChecker.new(organization)
    if cert_auth_available?(organization) && !organization.manifest_expired? && checker.can_connect?
      true
    else
      logger.info("A manifest is not available for org: #{organization.name}, or it has expired, or been deleted. skipping insights sync")
      false
    end
  end

  sequence do
    # This can be turned off when we enable automatic status syncs
    # This step will query cloud inventory to retrieve inventory uuids for each host
    valid_organizations = organizations.reject(&:manifest_expired?)
    if valid_organizations.any?
      plan_hosts_sync(organizations)
      plan_self(organization_ids: organizations.map(&:id))
      concurrence do
        plan_rules_sync(organizations)
        plan_notifications
      end
    end
  end
end
try_execute() click to toggle source
# File lib/insights_cloud/async/insights_full_sync.rb, line 36
def try_execute
  organizations.each do |organization|
    checker = ::Katello::UpstreamConnectionChecker.new(organization)
    if !cert_auth_available?(organization) && organization.manifest_expired && !checker.can_connect?
      logger.info("A manifest is not available for org: #{organization.name}, or it has expired, or been deleted. skipping insights sync")
      next
    end

    perform_hits_sync(organization)
  end
  done!
end

Private Instance Methods

host_id(uuid) click to toggle source
# File lib/insights_cloud/async/insights_full_sync.rb, line 101
def host_id(uuid)
  @host_ids[uuid]
end
organizations() click to toggle source
# File lib/insights_cloud/async/insights_full_sync.rb, line 146
def organizations
  @organizations ||= Organization.where(id: input[:organization_ids])
end
plan_hosts_sync(organizations) click to toggle source
# File lib/insights_cloud/async/insights_full_sync.rb, line 64
def plan_hosts_sync(organizations)
  plan_action(InventorySync::Async::InventoryHostsSync, organizations)
end
plan_notifications() click to toggle source
# File lib/insights_cloud/async/insights_full_sync.rb, line 72
def plan_notifications
  plan_action InsightsGenerateNotifications
end
plan_rules_sync(organizations) click to toggle source
# File lib/insights_cloud/async/insights_full_sync.rb, line 68
def plan_rules_sync(organizations)
  plan_action(InsightsRulesSync, organizations)
end
query_insights_hits(organization) click to toggle source
# File lib/insights_cloud/async/insights_full_sync.rb, line 76
def query_insights_hits(organization)
  hits_response = execute_cloud_request(
    organization: organization,
    method: :get,
    url: InsightsCloud.hits_export_url
  )

  JSON.parse(hits_response)
end
query_insights_rules() click to toggle source
# File lib/insights_cloud/async/insights_full_sync.rb, line 86
def query_insights_rules
  rules_response = execute_cloud_request(
    method: :get,
    url: InsightsCloud.rules_url
  )

  JSON.parse(rules_response)
end
replace_hits_data(hits, organization) click to toggle source
# File lib/insights_cloud/async/insights_full_sync.rb, line 105
def replace_hits_data(hits, organization)
  InsightsHit.transaction do
    # Reset hit counters to 0, they will be recreated later
    InsightsFacet.for_organizations(organization.id).update_all(hits_count: 0)
    InsightsHit.for_organizations(organization.id).delete_all
    InsightsHit.create(hits.map { |hits_hash| to_model_hash(hits_hash) }.compact)
  end
end
rescue_strategy_for_self() click to toggle source
# File lib/insights_cloud/async/insights_full_sync.rb, line 142
def rescue_strategy_for_self
  Dynflow::Action::Rescue::Fail
end
safe_results_match(results_url) click to toggle source
# File lib/insights_cloud/async/insights_full_sync.rb, line 136
def safe_results_match(results_url)
  match = results_url.match(/\/(?<id>[^\/]*)\/[^\/]*\/\z/)

  match || { id: nil }
end
setup_host_ids(uuids, organization) click to toggle source
# File lib/insights_cloud/async/insights_full_sync.rb, line 95
def setup_host_ids(uuids, organization)
  @host_ids = Hash[
    InsightsFacet.for_organizations(organization.id).where(uuid: uuids).pluck(:uuid, :host_id)
  ]
end
to_model_hash(hit_hash) click to toggle source
# File lib/insights_cloud/async/insights_full_sync.rb, line 114
def to_model_hash(hit_hash)
  hit_host_id = host_id(hit_hash['uuid'])

  return unless hit_host_id

  {
    host_id: hit_host_id,
    last_seen: DateTime.parse(hit_hash['last_seen']),
    publish_date: DateTime.parse(hit_hash['publish_date']),
    title: hit_hash['title'],
    solution_url: hit_hash['solution_url'],
    total_risk: hit_hash['total_risk'].to_i,
    likelihood: hit_hash['likelihood'].to_i,
    results_url: hit_hash['results_url'],
    rule_id: to_rule_id(hit_hash['results_url']),
  }
end
to_rule_id(results_url) click to toggle source
# File lib/insights_cloud/async/insights_full_sync.rb, line 132
def to_rule_id(results_url)
  CGI.unescape(safe_results_match(results_url)[:id] || '')
end