Class: ForemanInventoryUpload::Async::UploadReportDirectJob
Defined Under Namespace
Classes: FileUpload
Class Method Summary
collapse
Instance Method Summary
collapse
#execute_cloud_request
#attempts_before_next_interval, #done!, #done?, #invoke_external_task, #poll_external_task, #poll_intervals
#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
#certificate ⇒ Object
145
146
147
|
# File 'lib/foreman_inventory_upload/async/upload_report_direct_job.rb', line 145
def certificate
ForemanRhCloud.with_iop_smart_proxy? ? foreman_certificate : manifest_certificate
end
|
#clear_task_output(label) ⇒ Object
195
196
197
198
|
# File 'lib/foreman_inventory_upload/async/upload_report_direct_job.rb', line 195
def clear_task_output(label)
TaskOutputLine.where(label: label).delete_all
TaskOutputStatus.where(label: label).delete_all
end
|
#content_disconnected? ⇒ Boolean
172
173
174
|
# File 'lib/foreman_inventory_upload/async/upload_report_direct_job.rb', line 172
def content_disconnected?
!Setting[:subscription_connection_enabled]
end
|
#filename ⇒ Object
164
165
166
|
# File 'lib/foreman_inventory_upload/async/upload_report_direct_job.rb', line 164
def filename
input[:filename]
end
|
#foreman_certificate ⇒ Object
157
158
159
160
161
162
|
# File 'lib/foreman_inventory_upload/async/upload_report_direct_job.rb', line 157
def foreman_certificate
{
cert: File.read(Setting[:ssl_certificate]),
key: File.read(Setting[:ssl_priv_key]),
}
end
|
#instance_label ⇒ Object
183
184
185
|
# File 'lib/foreman_inventory_upload/async/upload_report_direct_job.rb', line 183
def instance_label
input[:instance_label]
end
|
#logger ⇒ Object
187
188
189
|
# File 'lib/foreman_inventory_upload/async/upload_report_direct_job.rb', line 187
def logger
Foreman::Logging.logger('background')
end
|
#manifest_certificate ⇒ Object
149
150
151
152
153
154
155
|
# File 'lib/foreman_inventory_upload/async/upload_report_direct_job.rb', line 149
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 ⇒ Object
137
138
139
140
141
142
143
|
# File 'lib/foreman_inventory_upload/async/upload_report_direct_job.rb', line 137
def move_to_done_folder
FileUtils.mkdir_p(ForemanInventoryUpload.done_folder)
done_file = ForemanInventoryUpload.done_file_path(File.basename(filename))
FileUtils.mv(filename, done_file)
logger.debug("Moved #{filename} to #{done_file}")
done_file
end
|
#organization ⇒ Object
168
169
170
|
# File 'lib/foreman_inventory_upload/async/upload_report_direct_job.rb', line 168
def organization
Organization.find(input[:organization_id])
end
|
#plan(filename, organization_id) ⇒ Object
45
46
47
48
49
50
51
52
53
54
55
56
57
58
|
# File 'lib/foreman_inventory_upload/async/upload_report_direct_job.rb', line 45
def plan(filename, organization_id)
label = UploadReportDirectJob.output_label(organization_id)
clear_task_output(label)
plan_self(
instance_label: label,
filename: filename,
organization_id: organization_id
)
end
|
#progress_output ⇒ Object
176
177
178
179
180
181
|
# File 'lib/foreman_inventory_upload/async/upload_report_direct_job.rb', line 176
def progress_output
progress_output = ProgressOutput.register(instance_label)
yield(progress_output)
ensure
progress_output.close
end
|
#rescue_strategy_for_self ⇒ Object
191
192
193
|
# File 'lib/foreman_inventory_upload/async/upload_report_direct_job.rb', line 191
def rescue_strategy_for_self
Dynflow::Action::Rescue::Fail
end
|
#try_execute ⇒ Object
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
|
# File 'lib/foreman_inventory_upload/async/upload_report_direct_job.rb', line 60
def try_execute
if content_disconnected?
progress_output do |progress_output|
progress_output.write_line("Report was not moved and upload was canceled because connection to Insights is not enabled. Report location: #{filename}.")
progress_output.status = "Task aborted, exit 1"
done!
end
return
end
unless organization.owner_details&.dig('upstreamConsumer', '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
upload_report(cer_file.path)
end
done!
end
|
#upload_file(cer_path) ⇒ Object
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
|
# File 'lib/foreman_inventory_upload/async/upload_report_direct_job.rb', line 109
def upload_file(cer_path)
cert_content = File.read(cer_path)
File.open(filename, 'rb') do |file|
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
|
#upload_report(cer_path) ⇒ Object
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 90
def upload_report(cer_path)
progress_output do |progress_output|
progress_output.write_line("Uploading report for organization #{organization.label}...")
progress_output.status = "Running upload"
begin
upload_file(cer_path)
progress_output.write_line("Upload completed successfully")
done_path = move_to_done_folder
progress_output.write_line("Uploaded report moved to #{done_path}")
progress_output.status = "pid #{Process.pid} exit 0"
rescue StandardError => e
progress_output.write_line("Upload failed: #{e.message}")
progress_output.status = "pid #{Process.pid} exit 1"
raise
end
end
end
|