Skip to content

Commit 2c8bf76

Browse files
jeremylenzchris1984
authored andcommitted
Generate host reports without shell commands or rake task (theforeman#1102)
(cherry picked from commit 596699a)
1 parent cf9d151 commit 2c8bf76

File tree

8 files changed

+126
-37
lines changed

8 files changed

+126
-37
lines changed

app/controllers/concerns/insights_cloud/package_profile_upload_extensions.rb

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,10 @@ def generate_host_report
1515
logger.debug("Generating host-specific report for host #{@host.name}")
1616

1717
ForemanTasks.async_task(
18-
ForemanInventoryUpload::Async::GenerateReportJob,
18+
ForemanInventoryUpload::Async::SingleHostReportJob,
1919
ForemanInventoryUpload.generated_reports_folder,
2020
@host.organization_id,
21-
false,
22-
"id=#{@host.id}"
21+
@host.id
2322
)
2423

2524
# in IoP case, the hosts are identified by the sub-man ID, and we can assume they already
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
module ForemanInventoryUpload
2+
module Async
3+
class CreateMissingInsightsFacets < ::Actions::EntryAction
4+
def plan(organization_id)
5+
plan_self(organization_id: organization_id)
6+
end
7+
8+
def run
9+
organization = ::Organization.find(input[:organization_id])
10+
hosts_without_facets = ::ForemanInventoryUpload::Generators::Queries.for_org(organization, hosts_query: 'null? insights_uuid')
11+
facet_count = hosts_without_facets.count
12+
hosts_without_facets.each do |batch|
13+
facets = batch.pluck(:id, 'katello_subscription_facets.uuid').map do |host_id, uuid|
14+
{
15+
host_id: host_id,
16+
uuid: uuid,
17+
}
18+
end
19+
# We don't need to validate the facets here as we create the necessary fields.
20+
# rubocop:disable Rails/SkipsModelValidations
21+
InsightsFacet.upsert_all(facets, unique_by: :host_id) unless facets.empty?
22+
# rubocop:enable Rails/SkipsModelValidations
23+
end
24+
output[:result] = facet_count.zero? ? _("There were no missing Insights facets") : format(_("Missing Insights facets created: %s"), facet_count)
25+
Rails.logger.info output[:result]
26+
end
27+
end
28+
end
29+
end
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
module ForemanInventoryUpload
2+
module Async
3+
class GenerateHostReport < ::Actions::EntryAction
4+
def plan(base_folder, organization_id, filter)
5+
plan_self(
6+
base_folder: base_folder,
7+
organization_id: organization_id,
8+
filter: filter
9+
)
10+
input[:target] = File.join(base_folder, ForemanInventoryUpload.facts_archive_name(input[:organization_id], input[:filter]))
11+
end
12+
13+
def run
14+
archived_report_generator = ForemanInventoryUpload::Generators::ArchivedReport.new(input[:target])
15+
archived_report_generator.render(organization: input[:organization_id], filter: input[:filter])
16+
output[:result] = "Generated #{input[:target]} for organization id #{input[:organization_id]}"
17+
end
18+
end
19+
end
20+
end

lib/foreman_inventory_upload/async/generate_report_job.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ def self.output_label(label)
55
"report_for_#{label}"
66
end
77

8-
def plan(base_folder, organization_id, disconnected, hosts_filter = nil)
8+
def plan(base_folder, organization_id, disconnected = false, hosts_filter = nil)
99
sequence do
1010
super(
1111
GenerateReportJob.output_label("#{organization_id}#{hosts_filter.empty? ? nil : "[#{hosts_filter.to_s.parameterize}]"}"),
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
module ForemanInventoryUpload
2+
module Async
3+
class HostInventoryReportJob < ::Actions::EntryAction
4+
def plan(base_folder, organization_id, hosts_filter = "", upload = true)
5+
sequence do
6+
plan_action(
7+
GenerateHostReport,
8+
base_folder,
9+
organization_id,
10+
hosts_filter
11+
)
12+
if upload
13+
plan_action(
14+
QueueForUploadJob,
15+
base_folder,
16+
ForemanInventoryUpload.facts_archive_name(organization_id, hosts_filter),
17+
organization_id
18+
)
19+
end
20+
21+
if ForemanRhCloud.with_iop_smart_proxy?
22+
plan_action(
23+
CreateMissingInsightsFacets,
24+
organization_id
25+
)
26+
end
27+
end
28+
end
29+
30+
def humanized_name
31+
_("Host inventory report job")
32+
end
33+
34+
def organization_id
35+
input[:organization_id]
36+
end
37+
end
38+
end
39+
end
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
module ForemanInventoryUpload
2+
module Async
3+
class SingleHostReportJob < HostInventoryReportJob
4+
def plan(base_folder, organization_id, host_id)
5+
input[:host_id] = host_id
6+
super(base_folder, organization_id, "id=#{input[:host_id]}")
7+
end
8+
9+
def hostname(host_id)
10+
host = ::Host.find_by(id: host_id)
11+
host&.name
12+
end
13+
14+
def humanized_name
15+
hostname_result = hostname(input[:host_id])
16+
hostname_result.present? ? format(_("Single-host report job for host %s"), hostname_result) : _("Single-host report job")
17+
end
18+
end
19+
end
20+
end

lib/foreman_inventory_upload/generators/slice.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ def report_yum_repos(host)
256256

257257
@stream.array_field('yum_repos') do
258258
host.content_facet.bound_repositories.each_with_index do |repo, index|
259-
report_yum_repo(host.content_source.load_balancer_pulp_content_url, repo)
259+
report_yum_repo(host.content_source&.load_balancer_pulp_content_url || ::SmartProxy.pulp_primary.pulp_content_url, repo)
260260
@stream.comma unless index == host.content_facet.bound_repositories.count - 1
261261
end
262262
end

lib/tasks/rh_cloud_inventory.rake

Lines changed: 14 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,20 @@ namespace :rh_cloud_inventory do
99
else
1010
organizations = [Organization.where(:id => ENV['organization_id']).first]
1111
end
12-
disconnected = ForemanRhCloud.with_iop_smart_proxy?
1312
User.as_anonymous_admin do
1413
organizations.each do |organization|
1514
ForemanTasks.async_task(
1615
ForemanInventoryUpload::Async::GenerateReportJob,
1716
ForemanInventoryUpload.generated_reports_folder,
18-
organization.id,
19-
disconnected
17+
organization.id
2018
)
2119
puts "Generated and uploaded inventory report for organization '#{organization.name}'"
2220
end
2321
end
2422
end
2523
desc 'Generate inventory report to be sent to Red Hat cloud'
26-
task generate: :environment do
27-
organizations = [ENV['organization_id']]
24+
task generate: [:environment, 'dynflow:client'] do
25+
organization_ids = [ENV['organization_id']]
2826
base_folder = ENV['target'] || Dir.pwd
2927
filter = ENV['hosts_filter']
3028

@@ -34,46 +32,30 @@ namespace :rh_cloud_inventory do
3432
puts "Using #{base_folder} for the output"
3533
end
3634

37-
if organizations.empty?
35+
if organization_ids.empty?
3836
puts "Must specify organization_id"
3937
return
4038
end
4139

4240
User.as_anonymous_admin do
43-
organizations.each do |organization|
44-
target = File.join(base_folder, ForemanInventoryUpload.facts_archive_name(organization, filter))
45-
archived_report_generator = ForemanInventoryUpload::Generators::ArchivedReport.new(target, Logger.new(STDOUT))
46-
archived_report_generator.render(organization: organization, filter: filter)
47-
puts "Successfully generated #{target} for organization id #{organization}"
48-
puts "Check the Uploading tab for report uploading status." if Setting[:subscription_connection_enabled]
49-
50-
next unless ForemanRhCloud.with_iop_smart_proxy?
51-
52-
puts 'Creating missing insights facets'
53-
hosts_without_facets = ForemanInventoryUpload::Generators::Queries.for_org(organization, hosts_query: 'null? insights_uuid')
54-
hosts_without_facets.each do |batch|
55-
facets = batch.pluck(:id, 'katello_subscription_facets.uuid').map do |host_id, uuid|
56-
{
57-
host_id: host_id,
58-
uuid: uuid,
59-
}
60-
end
61-
# We don't need to validate the facets here as we create the necessary fields.
62-
# rubocop:disable Rails/SkipsModelValidations
63-
InsightsFacet.upsert_all(facets, unique_by: :host_id) unless facets.empty?
64-
# rubocop:enable Rails/SkipsModelValidations
65-
end
66-
puts 'Missing Insights facets created'
41+
organization_ids.each do |organization_id|
42+
ForemanTasks.sync_task(
43+
ForemanInventoryUpload::Async::HostInventoryReportJob,
44+
base_folder,
45+
organization_id,
46+
filter,
47+
false # don't upload; the user ran report:generate and not report:generate_upload
48+
)
6749
end
50+
puts "Check the Uploading tab for report uploading status." if Setting[:subscription_connection_enabled]
6851
end
6952
end
7053
desc 'Upload generated inventory report to Red Hat cloud'
7154
task upload: [:environment, 'dynflow:client'] do
7255
base_folder = ENV['target'] || ForemanInventoryUpload.generated_reports_folder
7356
organization_id = ENV['organization_id']
7457
report_file = ForemanInventoryUpload.facts_archive_name(organization_id)
75-
disconnected = ForemanRhCloud.with_iop_smart_proxy?
76-
ForemanTasks.sync_task(ForemanInventoryUpload::Async::QueueForUploadJob, base_folder, report_file, organization_id, disconnected)
58+
ForemanTasks.sync_task(ForemanInventoryUpload::Async::QueueForUploadJob, base_folder, report_file, organization_id)
7759
puts "Uploaded #{report_file}"
7860
end
7961
end

0 commit comments

Comments
 (0)