Class: ForemanInventoryUpload::Async::UploadReportJob

Inherits:
ShellProcess
  • Object
show all
Defined in:
lib/foreman_inventory_upload/async/upload_report_job.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ShellProcess

#clear_task_output, #logger, #progress_output, #rescue_strategy_for_self

Methods included from ForemanRhCloud::Async::ExponentialBackoff

#attempts_before_next_interval, #done!, #done?, #invoke_external_task, #poll_external_task, #poll_intervals

Methods included from AsyncHelpers

#hash_to_s

Class Method Details

.output_label(label) ⇒ Object



6
7
8
# File 'lib/foreman_inventory_upload/async/upload_report_job.rb', line 6

def self.output_label(label)
  "upload_for_#{label}"
end

Instance Method Details

#certificateObject



62
63
64
# File 'lib/foreman_inventory_upload/async/upload_report_job.rb', line 62

def certificate
  ForemanRhCloud.with_iop_smart_proxy? ? foreman_certificate : manifest_certificate
end

#commandObject



44
45
46
# File 'lib/foreman_inventory_upload/async/upload_report_job.rb', line 44

def command
  ['/bin/bash', File.join(File.dirname(filename), ForemanInventoryUpload.upload_script_file)]
end

#content_disconnected?Boolean

Returns:

  • (Boolean)


91
92
93
# File 'lib/foreman_inventory_upload/async/upload_report_job.rb', line 91

def content_disconnected?
  input[:disconnected] || !Setting[:subscription_connection_enabled]
end

#envObject



48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/foreman_inventory_upload/async/upload_report_job.rb', line 48

def env
  env_vars = super.merge(
    'FILES' => filename,
    'CER_PATH' => @cer_path
  )

  http_proxy_string = ForemanRhCloud.http_proxy_string
  if http_proxy_string
    env_vars['http_proxy'] = http_proxy_string
    env_vars['https_proxy'] = http_proxy_string
  end
  env_vars
end

#filenameObject



83
84
85
# File 'lib/foreman_inventory_upload/async/upload_report_job.rb', line 83

def filename
  input[:filename]
end

#foreman_certificateObject



76
77
78
79
80
81
# File 'lib/foreman_inventory_upload/async/upload_report_job.rb', line 76

def foreman_certificate
  @foreman_certificate ||= {
    cert: File.read(Setting[:ssl_certificate]),
    key: File.read(Setting[:ssl_priv_key]),
  }
end

#manifest_certificateObject



66
67
68
69
70
71
72
73
74
# File 'lib/foreman_inventory_upload/async/upload_report_job.rb', line 66

def manifest_certificate
  @manifest_certificate ||= begin
    candlepin_id_certificate = organization.owner_details['upstreamConsumer']['idCert']
    {
      cert: candlepin_id_certificate['cert'],
      key: candlepin_id_certificate['key'],
    }
  end
end

#organizationObject



87
88
89
# File 'lib/foreman_inventory_upload/async/upload_report_job.rb', line 87

def organization
  @organization ||= Organization.find(input[:organization_id])
end

#plan(filename, organization_id, disconnected = false) ⇒ Object



10
11
12
13
# File 'lib/foreman_inventory_upload/async/upload_report_job.rb', line 10

def plan(filename, organization_id, disconnected = false)
  label = UploadReportJob.output_label(organization_id)
  super(label, filename: filename, organization_id: organization_id, disconnected: disconnected)
end

#try_executeObject



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
42
# File 'lib/foreman_inventory_upload/async/upload_report_job.rb', line 15

def try_execute
  if content_disconnected?
    progress_output do |progress_output|
      progress_output.write_line('Upload canceled because connection to Insights is not enabled or the --no-upload option was passed.')
      progress_output.status = "Task aborted, exit 1"
      done!
    end
    return
  end

  unless organization.owner_details&.fetch('upstreamConsumer')&.fetch('idCert')
    logger.info("Skipping organization '#{organization}', no candlepin certificate defined.")
    progress_output do |progress_output|
      progress_output.write_line("Skipping organization #{organization}, no candlepin certificate defined.")
      progress_output.status = "Task aborted, exit 1"
      done!
    end
    return
  end

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