-
Notifications
You must be signed in to change notification settings - Fork 284
/
core.rb
233 lines (181 loc) · 6.37 KB
/
core.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
require "pulpcore_client"
module Katello
module Pulp3
module Api
class Core
attr_accessor :smart_proxy, :repository_type
def initialize(smart_proxy, repository_type = Katello::RepositoryTypeManager.find_by(:pulp3_api_class, self.class))
@smart_proxy = smart_proxy
@repository_type = repository_type
end
def client_module
repository_type.client_module_class
end
delegate :remote_class, to: :repository_type
def self.remote_uln_class
fail NotImplementedError
end
delegate :distribution_class, to: :repository_type
delegate :publication_class, to: :repository_type
def repository_sync_url_class
repository_type.repo_sync_url_class
end
def api_client
config = smart_proxy.pulp3_configuration(repository_type.configuration_class)
config.params_encoder = Faraday::FlatParamsEncoder
api_client_class(repository_type.api_class.new(config))
end
def api_exception_class
client_module::ApiError
end
def remotes_api
repository_type.remotes_api_class.new(api_client)
end
def remotes_uln_api
fail NotImplementedError
end
def publications_api
repository_type.publications_api_class.new(api_client) #Optional
end
def distributions_api
repository_type.distributions_api_class.new(api_client)
end
def repositories_api
repository_type.repositories_api_class.new(api_client)
end
def repository_versions_api
repository_type.repository_versions_api_class.new(api_client)
end
def self.ignore_409_exception(*)
yield
rescue => e
raise e unless e&.code == 409
nil
end
def repository_version_class
client_module::RepositoryVersion
end
def cancel_task(task_href)
data = PulpcoreClient::TaskResponse.new(state: 'canceled')
self.class.ignore_409_exception do
tasks_api.tasks_cancel(task_href, data)
end
end
def exporter_api
PulpcoreClient::ExportersPulpApi.new(core_api_client)
end
def importer_api
PulpcoreClient::ImportersPulpApi.new(core_api_client)
end
def importer_check_api
PulpcoreClient::ImportersPulpImportCheckApi.new(core_api_client)
end
def export_api
PulpcoreClient::ExportersPulpExportsApi.new(core_api_client)
end
def import_api
PulpcoreClient::ImportersPulpImportsApi.new(core_api_client)
end
def orphans_api
PulpcoreClient::OrphansCleanupApi.new(core_api_client)
end
def artifacts_api
PulpcoreClient::ArtifactsApi.new(core_api_client)
end
def core_api_client
client = PulpcoreClient::ApiClient.new(smart_proxy.pulp3_configuration(PulpcoreClient::Configuration))
api_client_class(client)
end
def api_client_class(client)
request_id = ::Logging.mdc['request']
client.default_headers['Correlation-ID'] = request_id if request_id
client
end
def uploads_api
PulpcoreClient::UploadsApi.new(core_api_client)
end
def upload_commit_class
PulpcoreClient::UploadCommit
end
def signing_services_api
PulpcoreClient::SigningServicesApi.new(core_api_client)
end
def tasks_api
PulpcoreClient::TasksApi.new(core_api_client)
end
def task_groups_api
PulpcoreClient::TaskGroupsApi.new(core_api_client)
end
def upload_class
PulpcoreClient::Upload
end
def ignore_404_exception(*)
yield
rescue self.api_exception_class => e
raise e unless e.code == 404
nil
end
def delete_orphans
[orphans_api.cleanup(PulpcoreClient::OrphansCleanup.new(orphan_protection_time: Setting[:orphan_protection_time]))]
end
def delete_remote(remote_href)
ignore_404_exception { remotes_api.delete(remote_href) }
end
def repository_version_hrefs(options = {})
repository_versions(options).map(&:pulp_href).uniq
end
def repository_versions(options = {})
current_pulp_repositories = self.list_all(options)
repo_hrefs = current_pulp_repositories.collect { |repo| repo.pulp_href }.uniq
version_hrefs = repo_hrefs.collect do |href|
versions_list_for_repository(href, options)
end
version_hrefs.flatten
end
def versions_list_for_repository(repository_href, options)
self.class.fetch_from_list { |page_opts| repository_versions_api.list(repository_href, page_opts.merge(options)) }
end
def distributions_list_all(args = {})
self.class.fetch_from_list do |page_opts|
distributions_api.list(page_opts.merge(args))
end
end
def get_distribution(href)
ignore_404_exception { distributions_api.read(href) }
end
def delete_distribution(href)
ignore_404_exception { distributions_api.delete(href) }
end
def list_all(options = {})
self.class.fetch_from_list do |page_opts|
repositories_api.list(page_opts.merge(options))
end
end
def remotes_list(args = {})
remotes_api.list(args).results
end
def remotes_list_all(_smart_proxy, options)
self.class.fetch_from_list do |page_opts|
remotes_api.list(page_opts.merge(options))
end
end
def self.fetch_from_list
page_size = Setting[:bulk_load_size]
page_opts = { "offset" => 0, limit: page_size }
response = {}
results = []
loop do
page_opts = page_opts.with_indifferent_access
break unless (
(response.count && (page_opts['offset'] < response.count)) ||
page_opts["offset"] == 0)
response = yield page_opts
results = results.concat(response.results)
page_opts[:offset] += page_size
end
results
end
end
end
end
end