class ForemanInventoryUpload::Async::UploadReportDirectJob

Public Class Methods

output_label(label) click to toggle source
# File lib/foreman_inventory_upload/async/upload_report_direct_job.rb, line 41
def self.output_label(label)
  "upload_for_#{label}"
end

Public Instance Methods

certificate() click to toggle source
# File lib/foreman_inventory_upload/async/upload_report_direct_job.rb, line 119
def certificate
  ForemanRhCloud.with_iop_smart_proxy? ? foreman_certificate : manifest_certificate
end
content_disconnected?() click to toggle source
# File lib/foreman_inventory_upload/async/upload_report_direct_job.rb, line 157
def content_disconnected?
  return false if ForemanRhCloud.with_iop_smart_proxy?
  !Setting[:subscription_connection_enabled]
end
filename() click to toggle source
# File lib/foreman_inventory_upload/async/upload_report_direct_job.rb, line 149
def filename
  input[:filename]
end
foreman_certificate() click to toggle source
# File lib/foreman_inventory_upload/async/upload_report_direct_job.rb, line 131
def foreman_certificate
  cert_path = Setting[:ssl_certificate]
  key_path = Setting[:ssl_priv_key]

  unless cert_path && File.readable?(cert_path)
    raise "SSL certificate file not found or not readable: #{cert_path}"
  end

  unless key_path && File.readable?(key_path)
    raise "SSL private key file not found or not readable: #{key_path}"
  end

  {
    cert: File.read(cert_path),
    key: File.read(key_path),
  }
end
logger() click to toggle source
# File lib/foreman_inventory_upload/async/upload_report_direct_job.rb, line 162
def logger
  Foreman::Logging.logger('background')
end
manifest_certificate() click to toggle source
# File lib/foreman_inventory_upload/async/upload_report_direct_job.rb, line 123
def manifest_certificate
  candlepin_id_certificate = organization.owner_details['upstreamConsumer']['idCert']
  {
    cert: candlepin_id_certificate['cert'],
    key: candlepin_id_certificate['key'],
  }
end
move_to_done_folder() click to toggle source
# File lib/foreman_inventory_upload/async/upload_report_direct_job.rb, line 109
def move_to_done_folder
  FileUtils.mkdir_p(ForemanInventoryUpload.done_folder)
  done_file = ForemanInventoryUpload.done_file_path(File.basename(filename))
  if File.exist?(done_file)
    logger.warn("Destination file #{done_file} already exists. Overwriting with new report.")
  end
  FileUtils.mv(filename, done_file, force: true)
  logger.debug("Moved #{filename} to #{done_file}")
end
organization() click to toggle source
# File lib/foreman_inventory_upload/async/upload_report_direct_job.rb, line 153
def organization
  Organization.find(input[:organization_id])
end
plan(filename, organization_id) click to toggle source
# File lib/foreman_inventory_upload/async/upload_report_direct_job.rb, line 49
def plan(filename, organization_id)
  organization = Organization.find(organization_id)
  action_subject(organization)

  plan_self(
    filename: filename,
    organization_id: organization_id
  )
end
rescue_strategy_for_self() click to toggle source
# File lib/foreman_inventory_upload/async/upload_report_direct_job.rb, line 166
def rescue_strategy_for_self
  Dynflow::Action::Rescue::Fail
end
resource_locks() click to toggle source
# File lib/foreman_inventory_upload/async/upload_report_direct_job.rb, line 45
def resource_locks
  :link
end
try_execute() click to toggle source
# File lib/foreman_inventory_upload/async/upload_report_direct_job.rb, line 59
def try_execute
  if content_disconnected?
    logger.info("Upload canceled: connection to Insights is not enabled. Report location: #{filename}")
    return
  end

  unless organization.owner_details&.dig('upstreamConsumer', 'idCert')
    logger.info("Skipping organization '#{organization}', no candlepin certificate defined.")
    return
  end

  Tempfile.create([organization.name, '.pem']) do |cer_file|
    cer_file.write(certificate[:cert])
    cer_file.write(certificate[:key])
    cer_file.flush
    upload_file(cer_file.path)
  end

  move_to_done_folder
  done!
end
upload_file(cer_path) click to toggle source
# File lib/foreman_inventory_upload/async/upload_report_direct_job.rb, line 81
def upload_file(cer_path)
  cert_content = File.read(cer_path)

  File.open(filename, 'rb') do |file|
    # Wrap file with FileUpload class for RestClient multipart handling
    # RestClient requires objects with :read, :path, and :content_type methods
    wrapped_file = FileUpload.new(file, content_type: 'application/vnd.redhat.qpc.tar+tgz')

    response = execute_cloud_request(
      method: :post,
      url: ForemanInventoryUpload.upload_url,
      payload: {
        multipart: true,
        file: wrapped_file,
      },
      headers: {
        'X-Org-Id' => organization.label,
      },
      ssl_client_cert: OpenSSL::X509::Certificate.new(cert_content),
      ssl_client_key: OpenSSL::PKey::RSA.new(cert_content),
      timeout: 600,
      open_timeout: 60
    )

    logger.debug("Upload response code: #{response.code}")
  end
end