Class: ForemanInventoryUpload::Async::UploadReportDirectJob

Inherits:
Actions::EntryAction
  • Object
show all
Includes:
AsyncHelpers, ForemanRhCloud::Async::ExponentialBackoff, ForemanRhCloud::CloudRequest
Defined in:
lib/foreman_inventory_upload/async/upload_report_direct_job.rb

Defined Under Namespace

Classes: FileUpload

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ForemanRhCloud::CloudRequest

#execute_cloud_request

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



41
42
43
# File 'lib/foreman_inventory_upload/async/upload_report_direct_job.rb', line 41

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

Instance Method Details

#certificateObject



119
120
121
# 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?Boolean

Returns:

  • (Boolean)


157
158
159
160
# 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

#filenameObject



149
150
151
# File 'lib/foreman_inventory_upload/async/upload_report_direct_job.rb', line 149

def filename
  input[:filename]
end

#foreman_certificateObject



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# 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

#loggerObject



162
163
164
# File 'lib/foreman_inventory_upload/async/upload_report_direct_job.rb', line 162

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

#manifest_certificateObject



123
124
125
126
127
128
129
# 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_folderObject



109
110
111
112
113
114
115
116
117
# 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

#organizationObject



153
154
155
# 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) ⇒ Object



49
50
51
52
53
54
55
56
57
# 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_selfObject



166
167
168
# File 'lib/foreman_inventory_upload/async/upload_report_direct_job.rb', line 166

def rescue_strategy_for_self
  Dynflow::Action::Rescue::Fail
end

#resource_locksObject



45
46
47
# File 'lib/foreman_inventory_upload/async/upload_report_direct_job.rb', line 45

def resource_locks
  :link
end

#try_executeObject



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# 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) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# 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