Sha256: 46eec6c57d9f952bd01ef547c8cd79fbaf94ca6e00bdfe4ac85b44ae807663a2

Contents?: true

Size: 1.94 KB

Versions: 15

Compression:

Stored size: 1.94 KB

Contents

module ForemanInventoryUpload
  module Generators
    class ArchivedReport
      def initialize(target, logger = Rails.logger)
        @target = target
        @logger = logger
      end

      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 |metadata_out|
            metadata_generator = ForemanInventoryUpload::Generators::Metadata.new(metadata_out)
            metadata_generator.render do |inner_generator|
              first = true
              host_batches.each do |hosts_batch|
                slice_id = Foreman.uuid
                hosts_count = hosts_batch.count
                @logger.info "Adding slice #{slice_id} with #{hosts_count} hosts"
                generate_slice(tmpdir, slice_id, hosts_batch)
                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', '-zcvf', @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

      private

      def generate_slice(tmpdir, slice_id, hosts_batch)
        File.open(File.join(tmpdir, "#{slice_id}.json"), 'w') do |slice_out|
          slice_generator = ForemanInventoryUpload::Generators::Slice.new(hosts_batch, slice_out, slice_id)
          slice_generator.render
        end
      end
    end
  end
end

Version data entries

15 entries across 15 versions & 2 rubygems

Version Path
foreman_rh_cloud-0.9.4.1 lib/foreman_inventory_upload/generators/archived_report.rb
foreman_rh_cloud-1.0.4.1 lib/foreman_inventory_upload/generators/archived_report.rb
foreman_rh_cloud-1.0.4 lib/foreman_inventory_upload/generators/archived_report.rb
foreman_rh_cloud-2.0.4 lib/foreman_inventory_upload/generators/archived_report.rb
foreman_rh_cloud-0.9.4 lib/foreman_inventory_upload/generators/archived_report.rb
foreman_inventory_upload-2.0.4.pre.2 lib/foreman_inventory_upload/generators/archived_report.rb
foreman_inventory_upload-2.0.4.pre.1 lib/foreman_inventory_upload/generators/archived_report.rb
foreman_inventory_upload-2.0.3 lib/foreman_inventory_upload/generators/archived_report.rb
foreman_inventory_upload-1.0.3 lib/foreman_inventory_upload/generators/archived_report.rb
foreman_inventory_upload-0.9.2 lib/foreman_inventory_upload/generators/archived_report.rb
foreman_inventory_upload-1.0.2 lib/foreman_inventory_upload/generators/archived_report.rb
foreman_inventory_upload-0.9.1 lib/foreman_inventory_upload/generators/archived_report.rb
foreman_inventory_upload-1.0.1 lib/foreman_inventory_upload/generators/archived_report.rb
foreman_inventory_upload-0.9.0 lib/foreman_inventory_upload/generators/archived_report.rb
foreman_inventory_upload-1.0.0 lib/foreman_inventory_upload/generators/archived_report.rb