forked from Katello/katello
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pulp.rb
640 lines (531 loc) · 21 KB
/
pulp.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
#
# Copyright 2011 Red Hat, Inc.
#
# This software is licensed to you under the GNU General Public
# License as published by the Free Software Foundation; either version
# 2 of the License (GPLv2) or (at your option) any later version.
# There is NO WARRANTY for this software, express or implied,
# including the implied warranties of MERCHANTABILITY,
# NON-INFRINGEMENT, or FITNESS FOR A PARTICULAR PURPOSE. You should
# have received a copy of GPLv2 along with this software; if not, see
# http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.
require 'rubygems'
require 'rest_client'
require 'http_resource'
module Pulp
class Proxy
def self.post path, body
Rails.logger.debug "Sending POST request to Pulp: #{path}"
client = PulpResource.rest_client(Net::HTTP::Post, :post, path_with_pulp_prefix(path))
client.post body, PulpResource.default_headers
end
def self.delete path
Rails.logger.debug "Sending DELETE request to Pulp: #{path}"
client = PulpResource.rest_client(Net::HTTP::Delete, :delete, path_with_pulp_prefix(path))
client.delete(PulpResource.default_headers)
end
def self.get path
Rails.logger.debug "Sending GET request to Pulp: #{path}"
client = PulpResource.rest_client(Net::HTTP::Get, :get, path_with_pulp_prefix(path))
client.get(PulpResource.default_headers)
end
def self.path_with_pulp_prefix path
PulpResource.prefix + path
end
end
class PulpResource < HttpResource
cfg = AppConfig.pulp
url = cfg.url
self.prefix = URI.parse(url).path
self.site = url.gsub(self.prefix, "")
self.consumer_secret = cfg.oauth_secret
self.consumer_key = cfg.oauth_key
self.ca_cert_file = cfg.ca_cert_file
def self.default_headers
{'accept' => 'application/json',
'accept-language' => I18n.locale,
'content-type' => 'application/json'}.merge(::User.pulp_oauth_header)
end
# some old Pulp API need text/plain content type
def self.default_headers_text
h = self.default_headers
h['content-type'] = 'text/plain'
h
end
# the path is expected to have trailing slash
def self.path_with_prefix path
PulpResource.prefix + path
end
end
class PulpPing < PulpResource
class << self
def ping
# For now we have to query repositories because there is no
# URL that is available in Pulp that returns something small
# but requires authentication. Please do not change this to
# /pulp/api/services/status/ because that path does *not* require
# auth and will not accurately report if Katello can talk
# to Pulp using OAuth.
response = get('/pulp/api/users/', self.default_headers).body
JSON.parse(response)
end
end
end
class Package < PulpResource
class << self
# Get all the Repositories known by Pulp
def all
response = get(package_path, self.default_headers).body
JSON.parse(response)
end
def find id
response = get(package_path + id + "/", self.default_headers).body
JSON.parse(response)
rescue JSON::ParserError => e
nil
end
def search name, regex=false
path = '/pulp/api/services/search/packages/'
response = post(path, {:name=>name, :regex=>regex}.to_json, self.default_headers)
JSON.parse(response)
end
def name_search name
pkgs = search("^" + name, true)
pkgs.collect{|pkg| pkg["name"]}
end
def package_path
"/pulp/api/packages/"
end
def dep_solve pkgnames, repoids
path = "/pulp/api/services/dependencies/"
response = post(path, JSON.generate({:pkgnames=>pkgnames, :repoids=>repoids}), self.default_headers)
JSON.parse(response)
end
end
end
class Errata < PulpResource
class << self
def find(errata_id)
response = get(errata_path + errata_id + "/", self.default_headers)
JSON.parse(response.body).with_indifferent_access
rescue JSON::ParserError => e
nil
end
def errata_path
"/pulp/api/errata/"
end
def filter(filter)
path = "#{errata_path}?#{filter.to_param}"
response = get(path, self.default_headers)
JSON.parse(response.body).map(&:with_indifferent_access)
end
end
end
class Distribution < PulpResource
class << self
def find dist_id
response = get(dist_path + dist_id + "/", self.default_headers)
JSON.parse(response.body).with_indifferent_access
end
def dist_path
"/pulp/api/distributions/"
end
end
end
class Repository < PulpResource
class << self
def clone_repo from_repo, to_repo, feed = "parent", filters = [] #clone is a built in method, hence redundant name
data = { :clone_id => to_repo.pulp_id,
:feed =>feed,
:clone_name => to_repo.name,
:groupid=>to_repo.groupid,
:relative_path => to_repo.relative_path,
:filters => filters }
path = Repository.repository_path + from_repo.pulp_id + "/clone/"
response = post(path, JSON.generate(data), self.default_headers)
JSON.parse(response.body).with_indifferent_access
end
def find repo_id, yell_on_404 = false
response = get(repository_path + repo_id + "/", self.default_headers)
body = response.body
JSON.parse(body).with_indifferent_access
rescue RestClient::ResourceNotFound => e
return nil if !yell_on_404
raise e
end
def find_all repo_ids, yell_on_404 = false
ids = "id=#{repo_ids.join('&id=')}"
response = get(repository_path + "/?#{ids}", self.default_headers)
body = response.body
JSON.parse(body).with_indifferent_access
rescue RestClient::ResourceNotFound => e
return nil if !yell_on_404
raise e
end
# Get all the Repositories known by Pulp
# currently filtering against only one groupid is supported in PULP
def all groupids=nil, search_params = {}
search_query = get_repo_search_query(groupids, search_params)
response = get(self.repository_path + search_query , self.default_headers)
JSON.parse(response.body)
rescue RestClient::ResourceNotFound => e
return nil if !yell_on_404
raise e
end
def start_discovery url, type
response = post("/pulp/api/services/discovery/repo/", JSON.generate(:url => url, :type => type), self.default_headers)
return JSON.parse(response.body).with_indifferent_access if response.code == 202
Rails.logger.error("Failed to start repository discovery. HTTP status: #{response.code}. #{response.body}")
raise RuntimeError, "#{response.code}, failed to start repository discovery: #{response.body}"
end
def repository_path
"/pulp/api/repositories/"
end
# :id, :name, :arch, :groupid, :feed
def create attrs
body = put(Repository.repository_path, JSON.generate(attrs), self.default_headers).body
JSON.parse(body).with_indifferent_access
end
# :id, :name, :arch, :groupid, :feed
def update repo_id, attrs
body = put(Repository.repository_path + repo_id +"/", JSON.generate(attrs), self.default_headers).body
find repo_id
end
# specific call to just update the sync schedule for a repo
def update_schedule(repo_id, schedule)
body = put(Repository.repository_path + repo_id +"/schedules/sync/", JSON.generate(:schedule => schedule), self.default_headers).body
end
def delete_schedule(repo_id)
body = self.delete(Repository.repository_path + repo_id +"/schedules/sync/", self.default_headers).body
end
def add_packages repo_id, pkg_id_list
body = post(Repository.repository_path + repo_id +"/add_package/", {:packageid=>pkg_id_list}.to_json, self.default_headers).body
end
def add_errata repo_id, errata_id_list
body = post(Repository.repository_path + repo_id +"/add_errata/", {:errataid=>errata_id_list}.to_json, self.default_headers).body
end
def add_distribution repo_id, distribution_id
body = post(Repository.repository_path + repo_id +"/add_distribution/", {:distributionid=>distribution_id}.to_json, self.default_headers).body
end
def destroy repo_id
raise ArgumentError, "repo id has to be specified" unless repo_id
self.delete(repository_path + repo_id + "/", self.default_headers).code.to_i
end
def sync (repo_id, data = {})
data[:limit] ||= AppConfig.pulp.sync_KBlimit if AppConfig.pulp.sync_KBlimit # set bandwidth limit
data[:threads] ||= AppConfig.pulp.sync_threads if AppConfig.pulp.sync_threads # set threads per sync
path = Repository.repository_path + repo_id + "/sync/"
response = post(path, JSON.generate(data), self.default_headers)
JSON.parse(response.body).with_indifferent_access
end
def sync_history repo_id
begin
response = get(Repository.repository_path + repo_id + "/history/sync/", self.default_headers)
json_history = JSON.parse(response.body)
json_history.collect {|jh| jh.with_indifferent_access }
rescue RestClient::ResourceNotFound => error
# Return nothing if there is a 404 which indicates there
# is no sync status for this repo. Not an error.
return
end
end
def sync_status(repo_id)
path = Repository.repository_path + repo_id + "/sync/"
response = get(path, self.default_headers)
parsed = JSON.parse(response.body)
parsed.reject!{ |task| task['start_time'].nil? }
return parsed if parsed.empty?
parsed.sort!{|a,b|
if a['finish_time'].nil? && b['finish_time'].nil?
a['start_time'] <=> b['start_time']
elsif a['finish_time'].nil?
-1
elsif b['finish_time'].nil?
1
else
b['finish_time'] <=> a['finish_time']
end
}
return [parsed.first.with_indifferent_access]
end
def destroy repo_id
raise ArgumentError, "repository id has to be specified" unless repo_id
path = Repository.repository_path + repo_id +"/"
self.delete(path, self.default_headers).code.to_i
end
def packages repo_id
response = get(repository_path + repo_id + "/packages/", self.default_headers)
body = response.body
JSON.parse(body)
end
def packages_by_name repo_id, name
response = get(repository_path + repo_id + "/packages/?name=^" + name + "$", self.default_headers)
body = response.body
JSON.parse(body)
end
def packages_by_nvre repo_id, name, version, release, epoch
#TODO: switch to https://fedorahosted.org/pulp/wiki/UGREST-Repositories#GetPackageByNVREA
path = repository_path + repo_id + "/packages/?name=^" + name +"$"
path += "&release=" + release if not release.nil?
path += "&version=" + version if not version.nil?
path += "&epoch=" + epoch if not epoch.nil?
response = get(path, self.default_headers)
body = response.body
JSON.parse(body)
end
def errata(repo_id, filter = {})
path = "#{repository_path}#{repo_id}/errata/?#{filter.to_param}"
response = get(path, self.default_headers)
body = response.body
JSON.parse(body)
end
def distributions(repo_id)
response = get(repository_path + repo_id + "/distribution/", self.default_headers)
body = response.body
JSON.parse(body)
end
def add_filters repo_id, filter_ids
response = post(repository_path + repo_id + "/add_filters/", {:filters => filter_ids}.to_json, self.default_headers)
response.body
end
def remove_filters repo_id, filter_ids
response = post(repository_path + repo_id + "/remove_filters/", {:filters => filter_ids}.to_json, self.default_headers)
response.body
end
def generate_metadata repo_id
response = post(repository_path + repo_id + "/generate_metadata/", {}, self.default_headers)
JSON.parse(response.body)
end
private
def get_repo_search_query groupids=nil, search_params = {}
search_query = ""
if not groupids.nil?
search_query = "?_intersect=groupid&" + groupids.collect do |gid|
"groupid="+gid
end.join("&")
end
if not search_params.empty?
if search_query.length == 0
search_query = "?" + search_params.to_query
else
search_query += "&" + search_params.to_query
end
end
search_query
end
end
end
class Consumer < PulpResource
class << self
def create key, uuid, description = "", key_value_pairs = {}
url = consumer_path() + "?owner=#{key}"
attrs = {:id => uuid, :description => description, :key_value_pairs => key_value_pairs}
response = self.post(url, attrs.to_json, self.default_headers)
JSON.parse(response.body).with_indifferent_access
end
def upload_package_profile uuid, package_profile
attrs = {:id => uuid, :package_profile => package_profile}
response = put(consumer_path(uuid) + "package_profile/", attrs.to_json, self.default_headers)
raise RuntimeError, "update failed" unless response
return response
end
def update key, uuid, description = ""
url = consumer_path(uuid) + "?owner=#{key}"
attrs = {:id => uuid, :description => description}
response = self.put(url, attrs.to_json, self.default_headers)
raise RuntimeError, "update failed" unless response
return response
end
def find consumer_id
response = get(consumer_path(consumer_id), self.default_headers)
JSON.parse(response.body).with_indifferent_access
end
def installed_packages consumer_id
response = get(consumer_path(consumer_id) + "package_profile/", self.default_headers)
JSON.parse(response.body)
end
def install_packages consumer_id, package_names
url = consumer_path(consumer_id) + "installpackages/"
attrs = {:packagenames => package_names}
response = self.post(url, attrs.to_json, self.default_headers)
JSON.parse(response.body).with_indifferent_access
end
def uninstall_packages consumer_id, package_names
url = consumer_path(consumer_id) + "uninstallpackages/"
attrs = {:packagenames => package_names}
response = self.post(url, attrs.to_json, self.default_headers)
JSON.parse(response.body).with_indifferent_access
end
def update_packages consumer_id, package_names
url = consumer_path(consumer_id) + "updatepackages/"
attrs = {:packagenames => package_names}
response = self.post(url, attrs.to_json, self.default_headers)
JSON.parse(response.body).with_indifferent_access
end
def install_package_groups consumer_id, package_groups
url = consumer_path(consumer_id) + "installpackagegroups/"
attrs = {:groupids => package_groups}
response = self.post(url, attrs.to_json, self.default_headers)
JSON.parse(response.body).with_indifferent_access
end
def uninstall_package_groups consumer_id, package_groups
url = consumer_path(consumer_id) + "uninstallpackagegroups/"
attrs = {:groupids => package_groups}
response = self.post(url, attrs.to_json, self.default_headers)
JSON.parse(response.body).with_indifferent_access
end
def destroy consumer_id
raise ArgumentError, "consumer_id id has to be specified" unless consumer_id
self.delete(consumer_path(consumer_id), self.default_headers).code.to_i
end
def errata consumer_id
response = get(consumer_path(consumer_id) + "errata/", self.default_headers)
JSON.parse(response.body)
end
def errata_by_consumer repos
repoids_param = nil
repos.each do |repo|
if repoids_param.nil?
repoids_param = "?repoids=" + repo.pulp_id
else
repoids_param += "&repoids=" + repo.pulp_id
end
end
url = consumer_path() + "applicable_errata_in_repos/" + repoids_param
response = get(url, self.default_headers)
JSON.parse(response.body)
end
def install_errata consumer_id, errata_ids
url = consumer_path(consumer_id) + "installerrata/"
attrs = { :errataids => errata_ids }
response = self.post(url, attrs.to_json, self.default_headers)
JSON.parse(response.body).with_indifferent_access
end
def repoids consumer_id
response = get(consumer_path(consumer_id) + "repoids/", self.default_headers)
JSON.parse(response.body)
end
def bind uuid, repoid
url = consumer_path(uuid) + "bind/"
# this is old-style Pulp API call
response = self.post(url, '"' + repoid + '"', self.default_headers_text)
response.body
end
def unbind uuid, repoid
url = consumer_path(uuid) + "unbind/"
# this is old-style Pulp API call
response = self.post(url, '"' + repoid + '"', self.default_headers_text)
response.body
end
def consumer_path id = nil
id.nil? ? "/pulp/api/consumers/" : "/pulp/api/consumers/#{id}/"
end
end
end
class Task < PulpResource
class << self
def find uuids
ids = "id=#{uuids.join('&id=')}"
query_url = path + "?state=archived&state=current&#{ids}"
response = self.get(query_url, self.default_headers)
body = response.body
JSON.parse(body).collect{|k| k.with_indifferent_access}
end
def cancel uuid
response = self.post(path(uuid) +"cancel/" , {}, self.default_headers)
JSON.parse(response.body).with_indifferent_access
end
def destroy uuid
response = self.delete(path(uuid), self.default_headers)
JSON.parse(response.body).with_indifferent_access
end
def path uuid=nil
uuid.nil? ? "/pulp/api/tasks/" : "/pulp/api/tasks/#{uuid}/"
end
end
end
class PackageGroup < PulpResource
class << self
def all repo_id
response = get path(repo_id), self.default_headers
JSON.parse(response.body).with_indifferent_access
end
def path repo_id
self.path_with_prefix("/repositories/#{repo_id}/packagegroups/")
end
end
end
class PackageGroupCategory < PulpResource
class << self
def all repo_id
response = get path(repo_id), self.default_headers
JSON.parse(response.body).with_indifferent_access
end
def path repo_id
self.path_with_prefix("/repositories/#{repo_id}/packagegroupcategories/")
end
end
end
class User < PulpResource
class << self
def create attrs
response = self.post path, attrs.to_json, self.default_headers
JSON.parse(response.body).with_indifferent_access
end
def destroy login
self.delete(path(login), self.default_headers).code.to_i
end
def find login
response = self.get path(login), self.default_headers
JSON.parse(response.body).with_indifferent_access
end
def path(login=nil)
users = self.path_with_prefix("/users/")
login.nil? ? users : users + "#{login}/"
end
end
end
class Roles < PulpResource
class << self
def add role_name, username
added = self.post(path(role_name) + "/add/", {:username => username}.to_json, self.default_headers)
added.body == "true"
end
def remove role_name, username
removed = self.post(path(role_name) + "/remove/", {:username => username}.to_json, self.default_headers)
removed.body == "true"
end
def path(role_name=nil)
roles = self.path_with_prefix("/roles/")
role_name.nil? ? roles : roles + "#{role_name}/"
end
end
end
class Filter < PulpResource
class << self
def create attrs
response = self.post path, attrs.to_json, self.default_headers
JSON.parse(response.body).with_indifferent_access
end
def destroy id
self.delete(path(id), self.default_headers).code.to_i
end
def find id
response = self.get path(id), self.default_headers
JSON.parse(response.body).with_indifferent_access
end
def add_packages id, packages
response = self.post path(id) + "add_packages/", {:packages => packages}.to_json, self.default_headers
return response.body
end
def remove_packages id, packages
response = self.post path(id) + "remove_packages/", {:packages => packages}.to_json, self.default_headers
return response.body
end
def path(id=nil)
filters = self.path_with_prefix("/filters/")
id.nil? ? filters : filters + "#{id}/"
end
end
end
end