Class: ForemanInventoryUpload::Generators::ArchivedReport

Inherits:
Object
  • Object
show all
Defined in:
lib/foreman_inventory_upload/generators/archived_report.rb

Instance Method Summary collapse

Constructor Details

#initialize(target, logger = Rails.logger) ⇒ ArchivedReport

Returns a new instance of ArchivedReport.



4
5
6
7
# File 'lib/foreman_inventory_upload/generators/archived_report.rb', line 4

def initialize(target, logger = Rails.logger)
  @target = target
  @logger = logger
end

Instance Method Details

#render(organization:) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/foreman_inventory_upload/generators/archived_report.rb', line 9

def render(organization:)
  Dir.mktmpdir do |tmpdir|
    @logger.info "Started generating hosts report in #{tmpdir}"
    host_batches = ForemanInventoryUpload::Generators::Queries.for_org(organization)
    File.open(File.join(tmpdir, 'metadata.json'), 'w') do ||
       = ForemanInventoryUpload::Generators::Metadata.new()
      .render do |inner_generator|
        first = true
        host_batches.each do |hosts_batch|
          slice_id = Foreman.uuid
          @logger.info "Adding slice #{slice_id}"
          hosts_count = generate_slice(tmpdir, slice_id, hosts_batch)
          @logger.info "slice #{slice_id} was created with #{hosts_count} hosts"
          inner_generator.add_slice(slice_id, hosts_count, first)
          first = false
        end
      end
    end
    @logger.info 'Report generation finished'

    @logger.info 'Archiving generated report'
    # success = system('tar', '-zcvf', @target, '-C', tmpdir, '.')
    Open3.popen2e('tar', '-Jcvf', @target, '-C', tmpdir, '.') do |_in, out, wait_thr|
      @logger.info("tar: #{out.read}")

      if wait_thr.value.success?
        @logger.info 'Report archived successfully'
      else
        @logger.info "Tar command failed: #{$CHILD_STATUS}"
      end
    end
  end
end