Sha256: a0cf89228ec2c69ca57c7e877f499f6ac1facaeb37b55d249c25354697dcd1d7

Contents?: true

Size: 1.5 KB

Versions: 3

Compression:

Stored size: 1.5 KB

Contents

module ForemanInventoryUpload
  module Async
    class QueueForUploadJob < ::ApplicationJob
      def perform(base_folder, report_file, organization_id)
        @base_folder = base_folder
        @report_file = report_file
        logger.debug('Ensuring objects')
        ensure_ouput_folder
        ensure_output_script
        logger.debug("Copying #{report_file} to #{uploads_folder}")
        enqueued_file_name = File.join(uploads_folder, report_file)
        FileUtils.mv(File.join(base_folder, report_file), enqueued_file_name)
        logger.debug("Done copying #{report_file} to #{enqueued_file_name}")

        UploadReportJob.perform_later(enqueued_file_name, organization_id)
      end

      def uploads_folder
        @uploads_folder ||= ForemanInventoryUpload.uploads_folder
      end

      def script_file
        @script_file ||= File.join(uploads_folder, ForemanInventoryUpload.upload_script_file)
      end

      def ensure_ouput_folder
        FileUtils.mkdir_p(uploads_folder)
      end

      def ensure_output_script
        return if File.exist?(script_file)

        script_source_file = File.join(ForemanInventoryUpload::Engine.root, 'app/views/scripts/uploader.sh.erb')

        script_source = File.read(script_source_file)
        script_source.gsub!('<%= @upload_url %>', ForemanInventoryUpload.upload_url)

        File.write(script_file, script_source)
        FileUtils.chmod('+x', script_file)
      end

      def logger
        Foreman::Logging.logger('background')
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
foreman_inventory_upload-0.9.2 lib/foreman_inventory_upload/async/queue_for_upload_job.rb
foreman_inventory_upload-0.9.1 lib/foreman_inventory_upload/async/queue_for_upload_job.rb
foreman_inventory_upload-0.9.0 lib/foreman_inventory_upload/async/queue_for_upload_job.rb