forked from Katello/katello
-
Notifications
You must be signed in to change notification settings - Fork 0
/
repo.rb
854 lines (744 loc) · 31.8 KB
/
repo.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
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
module Katello
module Glue::Pulp::Repo
# TODO: move into submodules
# rubocop:disable MethodLength
# rubocop:disable ModuleLength
def self.included(base)
base.send :include, LazyAccessor
base.send :include, InstanceMethods
base.class_eval do
lazy_accessor :pulp_repo_facts,
:initializer => (lambda do |_s|
if pulp_id
begin
Katello.pulp_server.extensions.repository.retrieve_with_details(pulp_id)
rescue RestClient::ResourceNotFound
nil # not found = it was not orchestrated yet
end
end
end)
lazy_accessor :importers,
:initializer => lambda { |_s| pulp_repo_facts["importers"] if pulp_id }
lazy_accessor :distributors,
:initializer => lambda { |_s| pulp_repo_facts["distributors"] if pulp_id }
def self.ensure_sync_notification
resource = Katello.pulp_server.resources.event_notifier
url = SETTINGS[:katello][:post_sync_url]
type = Runcible::Resources::EventNotifier::EventTypes::REPO_SYNC_COMPLETE
notifs = resource.list
#delete any similar tasks with the wrong url (in case it changed)
notifs.select { |n| n['event_types'] == [type] && n['notifier_config']['url'] != url }.each do |e|
resource.delete(e['id'])
end
#only create a notifier if one doesn't exist with the correct url
exists = notifs.select { |n| n['event_types'] == [type] && n['notifier_config']['url'] == url }
resource.create(Runcible::Resources::EventNotifier::NotifierTypes::REST_API, {:url => url}, [type]) if exists.empty?
end
def self.delete_orphaned_content
Katello.pulp_server.resources.content.remove_orphans
end
def self.distribution_bootable?(distribution)
# Not every distribution from Pulp represents a bootable
# repo. Determine based on the files in the repo.
distribution["files"].any? do |file|
if file.is_a? Hash
filename = file[:relativepath]
else
filename = file
end
filename.include?("vmlinuz") || filename.include?("pxeboot")
end
end
end
end
# rubocop:disable Metrics/ModuleLength
module InstanceMethods
# TODO: This module is too long. See https://projects.theforeman.org/issues/12584.
def last_sync
last = self.latest_dynflow_sync
last.nil? ? nil : last.to_s
end
def initialize(attrs = nil, options = {})
if attrs.nil?
super
else
#rename "type" to "cp_type" (activerecord and candlepin variable name conflict)
#if attrs.has_key?(type_key) && !(attrs.has_key?(:cp_type) || attrs.has_key?('cp_type'))
# attrs[:cp_type] = attrs[type_key]
#end
attrs_used_by_model = attrs.reject do |k, _v|
!self.class.column_defaults.keys.member?(k.to_s) && (!respond_to?(:"#{k.to_s}=") rescue true)
end
super(attrs_used_by_model, options)
end
end
def uri
uri = URI.parse(SETTINGS[:katello][:pulp][:url])
"https://#{uri.host}/pulp/repos/#{relative_path}"
end
def to_hash
pulp_repo_facts.merge(as_json).merge(:sync_state => sync_state)
end
def pulp_checksum_type
find_distributor['config']['checksum_type'] if self.try(:yum?) && find_distributor
end
def create_pulp_repo
#if we are in library, no need for an distributor, but need to sync
if self.environment.try(:library?)
importer = generate_importer
else
#if not in library, no need for sync info, but we need a distributor
case self.content_type
when Repository::YUM_TYPE
importer = Runcible::Models::YumImporter.new
when Repository::PUPPET_TYPE
importer = Runcible::Models::PuppetImporter.new
end
end
distributors = generate_distributors
Katello.pulp_server.extensions.repository.create_with_importer_and_distributors(self.pulp_id,
importer,
distributors,
:display_name => self.name)
rescue RestClient::ServiceUnavailable => e
message = _("Pulp service unavailable during creating repository '%s', please try again later.") % self.name
raise PulpErrors::ServiceUnavailable.new(message, e)
end
def generate_importer(capsule = false)
case self.content_type
when Repository::YUM_TYPE
Runcible::Models::YumImporter.new(yum_importer_values(capsule))
when Repository::FILE_TYPE
Runcible::Models::IsoImporter.new(importer_ssl_options(capsule).merge(:feed => importer_feed_url(capsule)))
when Repository::PUPPET_TYPE
options = {}
options[:feed] = importer_feed_url(capsule)
Runcible::Models::PuppetImporter.new(options)
when Repository::DOCKER_TYPE
options = {}
options[:upstream_name] = capsule ? self.pulp_id : self.docker_upstream_name
options[:feed] = docker_feed_url(capsule)
options[:enable_v1] = false if self.respond_to?(:enable_v1)
Runcible::Models::DockerImporter.new(options)
when Repository::OSTREE_TYPE
options = importer_ssl_options(capsule)
options[:feed] = self.url if self.respond_to?(:url)
Runcible::Models::OstreeImporter.new(options)
else
fail _("Unexpected repo type %s") % self.content_type
end
end
def docker_feed_url(capsule = false)
pulp_uri = URI.parse(SETTINGS[:katello][:pulp][:url])
if capsule
"https://#{pulp_uri.host.downcase}:5000"
else
self.url if self.respond_to?(:url)
end
end
def importer_feed_url(capsule = false)
if capsule
self.full_path
else
self.url
end
end
def yum_importer_values(capsule)
if capsule && self.library_instance
new_download_policy = self.library_instance.download_policy
else
new_download_policy = self.download_policy
end
config = {
:feed => self.importer_feed_url(capsule),
:download_policy => new_download_policy,
:remove_missing => capsule ? true : self.mirror_on_sync?
}
config.merge(importer_ssl_options(capsule))
end
def importer_ssl_options(capsule = nil)
if capsule
ueber_cert = ::Cert::Certs.ueber_cert(organization)
{
:ssl_client_cert => ueber_cert[:cert],
:ssl_client_key => ueber_cert[:key],
:ssl_ca_cert => ::Cert::Certs.ca_cert
}
elsif redhat? && self.content_view.default?
{
:ssl_client_cert => self.product.certificate,
:ssl_client_key => self.product.key,
:ssl_ca_cert => Resources::CDN::CdnResource.ca_file_contents
}
else
{
:ssl_client_cert => nil,
:ssl_client_key => nil,
:ssl_ca_cert => nil
}
end
end
def generate_distributors
case self.content_type
when Repository::YUM_TYPE
yum_dist_id = self.pulp_id
yum_dist_options = {:protected => true, :id => yum_dist_id, :auto_publish => true}
#check the instance variable, as we do not want to go to pulp
yum_dist_options['checksum_type'] = self.checksum_type
yum_dist = Runcible::Models::YumDistributor.new(self.relative_path, (self.unprotected), true,
yum_dist_options)
clone_dist = Runcible::Models::YumCloneDistributor.new(:id => "#{self.pulp_id}_clone",
:destination_distributor_id => yum_dist_id)
export_dist = Runcible::Models::ExportDistributor.new(false, false, self.relative_path)
distributors = [yum_dist, clone_dist, export_dist]
when Repository::FILE_TYPE
dist = Runcible::Models::IsoDistributor.new(true, true)
dist.auto_publish = true
distributors = [dist]
when Repository::PUPPET_TYPE
dist_options = { :id => self.pulp_id, :auto_publish => true }
repo_path = File.join(SETTINGS[:katello][:puppet_repo_root],
Environment.construct_name(self.organization,
self.environment,
self.content_view),
'modules')
puppet_install_dist = Runcible::Models::PuppetInstallDistributor.new(repo_path, dist_options)
dist_options[:id] = "#{self.pulp_id}_puppet"
puppet_dist = Runcible::Models::PuppetDistributor.new(nil, (self.unprotected || false),
true, dist_options)
distributors = [puppet_dist, puppet_install_dist]
when Repository::DOCKER_TYPE
options = { :protected => !self.unprotected, :id => self.pulp_id, :auto_publish => true }
docker_dist = Runcible::Models::DockerDistributor.new(options)
distributors = [docker_dist]
when Repository::OSTREE_TYPE
options = { :id => self.pulp_id,
:auto_publish => true,
:relative_path => relative_path }
dist = Runcible::Models::OstreeDistributor.new(options)
distributors = [dist]
else
fail _("Unexpected repo type %s") % self.content_type
end
distributors
end
def importer_type
case self.content_type
when Repository::YUM_TYPE
Runcible::Models::YumImporter::ID
when Repository::FILE_TYPE
Runcible::Models::IsoImporter::ID
when Repository::PUPPET_TYPE
Runcible::Models::PuppetImporter::ID
when Repository::DOCKER_TYPE
Runcible::Models::DockerImporter::ID
when Repository::OSTREE_TYPE
Runcible::Models::OstreeImporter::ID
else
fail _("Unexpected repo type %s") % self.content_type
end
end
def refresh_pulp_repo
Katello.pulp_server.extensions.repository.update_importer(self.pulp_id, self.importers.first['id'], generate_importer.config)
existing_distributors = self.distributors
generate_distributors.each do |distributor|
found = existing_distributors.find { |i| i['distributor_type_id'] == distributor.type_id }
if found
Katello.pulp_server.extensions.repository.update_distributor(self.pulp_id, found['id'], distributor.config)
else
Katello.pulp_server.extensions.repository.associate_distributor(self.pulp_id, distributor.type_id, distributor.config,
:distributor_id => distributor.id)
end
end
end
def populate_from(repos_map)
found = repos_map[self.pulp_id]
prepopulate(found) if found
!found.nil?
end
def generate_applicability
task = Katello.pulp_server.extensions.repository.regenerate_applicability_by_ids([self.pulp_id])
PulpTaskStatus.using_pulp_task(task)
end
def other_repos_with_same_product_and_content
Repository.where(:content_id => self.content_id).in_product(self.product).pluck(:pulp_id) - [self.pulp_id]
end
def other_repos_with_same_content
Repository.where(:content_id => self.content_id).pluck(:pulp_id) - [self.pulp_id]
end
def pulp_rpm_ids
Katello.pulp_server.extensions.repository.rpm_ids(self.pulp_id)
end
def pulp_errata_ids
Katello.pulp_server.extensions.repository.errata_ids(self.pulp_id)
end
def package_group_count
content_unit_counts = 0
if self.pulp_repo_facts
content_unit_counts = self.pulp_repo_facts[:content_unit_counts][:package_group]
end
content_unit_counts
end
# remove errata and groups from this repo
# that have no packages
def purge_empty_groups_errata
package_lists = package_lists_for_publish
rpm_names = package_lists[:names]
filenames = package_lists[:filenames]
# Remove all errata with no packages
errata_to_delete = errata.collect do |erratum|
erratum.errata_id if filenames.intersection(erratum.package_filenames).empty?
end
errata_to_delete.compact!
#do the errata remove call
unless errata_to_delete.empty?
unassociate_by_filter(ContentViewErratumFilter::CONTENT_TYPE,
"id" => { "$in" => errata_to_delete })
end
# Remove all package groups with no packages
package_groups_to_delete = package_groups.collect do |group|
group.package_group_id if rpm_names.intersection(group.package_names).empty?
end
package_groups_to_delete.compact!
unless package_groups_to_delete.empty?
unassociate_by_filter(ContentViewPackageGroupFilter::CONTENT_TYPE,
"id" => { "$in" => package_groups_to_delete })
end
end
def index_db_errata(force = false)
if self.content_view.default? || force
errata_json.each do |erratum_json|
begin
erratum = Erratum.where(:uuid => erratum_json['_id']).first_or_create
rescue ActiveRecord::RecordNotUnique
retry
end
erratum.update_from_json(erratum_json)
end
end
Katello::Erratum.sync_repository_associations(self, pulp_errata_ids)
end
def index_db_rpms(force = false)
if self.content_view.default? || force
rpms_json.each do |rpm_json|
begin
rpm = Rpm.where(:uuid => rpm_json['_id']).first_or_create
rescue ActiveRecord::RecordNotUnique
retry
end
rpm.update_from_json(rpm_json)
end
end
Katello::Rpm.sync_repository_associations(self, pulp_rpm_ids)
end
def index_db_puppet_modules(force = false)
if self.content_view.default? || force
puppet_modules_json.each do |puppet_module_json|
begin
puppet_module = Katello::PuppetModule.where(:uuid => puppet_module_json['_id']).first_or_create
rescue ActiveRecord::RecordNotUnique
retry
end
puppet_module.update_from_json(puppet_module_json)
end
end
Katello::PuppetModule.sync_repository_associations(self, pulp_puppet_module_ids)
end
def puppet_modules_json
tmp_puppet_modules = []
#we fetch ids and then fetch errata by id, because repo errata
# do not contain all the info we need (bz 854260)
self.pulp_puppet_module_ids.each_slice(SETTINGS[:katello][:pulp][:bulk_load_size]) do |sub_list|
tmp_puppet_modules.concat(Katello.pulp_server.extensions.puppet_module.find_all_by_unit_ids(sub_list))
end
tmp_puppet_modules
end
def errata_json
tmp_errata = []
#we fetch ids and then fetch errata by id, because repo errata
# do not contain all the info we need (bz 854260)
self.pulp_errata_ids.each_slice(SETTINGS[:katello][:pulp][:bulk_load_size]) do |sub_list|
tmp_errata.concat(Katello.pulp_server.extensions.errata.find_all_by_unit_ids(sub_list))
end
tmp_errata
end
def index_db_package_groups
package_group_json.each do |pg_json|
begin
package_group = Katello::PackageGroup.where(:uuid => pg_json['_id']).first_or_create
rescue ActiveRecord::RecordNotUnique
retry
end
package_group.update_from_json(pg_json)
end
pg_ids = package_group_json.map { |pg| pg['_id'] }
Katello::PackageGroup.sync_repository_associations(self, pg_ids)
end
def package_group_json
Katello.pulp_server.extensions.repository.package_groups(self.pulp_id)
end
def rpms_json
tmp_packages = []
self.pulp_rpm_ids.each_slice(SETTINGS[:katello][:pulp][:bulk_load_size]) do |sub_list|
tmp_packages.concat(Katello.pulp_server.extensions.rpm.find_all_by_unit_ids(
sub_list, ::Katello::Pulp::Rpm::PULP_INDEXED_FIELDS))
end
tmp_packages
end
def index_db_docker_manifests
docker_manifests_json.each do |manifest_json|
manifest = DockerManifest.where(:uuid => manifest_json[:_id]).first_or_create
manifest.update_from_json(manifest_json)
create_docker_tag(manifest, manifest_json[:tag])
end
DockerManifest.sync_repository_associations(self, pulp_docker_manifest_ids)
end
def docker_manifests_json
docker_manifests = []
pulp_docker_manifest_ids.each_slice(SETTINGS[:katello][:pulp][:bulk_load_size]) do |sub_list|
docker_manifests.concat(Katello.pulp_server.extensions.docker_manifest.find_all_by_unit_ids(sub_list))
end
docker_manifests
end
def create_docker_tag(manifest, tag_name)
tag = unit_search(:type_ids => [Runcible::Extensions::DockerTag.content_type],
:filters => { :unit => { :manifest_digest => manifest.digest,
:name => tag_name } }).first
DockerTag.where(:repository_id => id, :docker_manifest_id => manifest.id,
:name => tag[:metadata][:name], :uuid => tag[:metadata][:_id]).first_or_create
end
def package_group_categories(search_args = {})
categories = Katello.pulp_server.extensions.repository.package_categories(self.pulp_id)
unless search_args.empty?
categories.delete_if do |category_attrs|
search_args.any? { |attr, value| category_attrs[attr] != value }
end
end
categories
end
def pulp_puppet_module_ids
Katello.pulp_server.extensions.repository.puppet_module_ids(self.pulp_id)
end
def pulp_docker_manifest_ids
Katello.pulp_server.extensions.repository.docker_manifest_ids(self.pulp_id)
end
def pulp_ostree_branch_ids
Katello.pulp_server.extensions.repository.ostree_branch_ids(self.pulp_id)
end
def index_db_ostree_branches
ostree_branches_json.each do |ostree_branch_json|
branch = OstreeBranch.where(:uuid => ostree_branch_json[:_id]).first_or_create
branch.update_from_json(ostree_branch_json)
end
OstreeBranch.sync_repository_associations(self, pulp_ostree_branch_ids)
end
def ostree_branches_json
ostree_branches = []
pulp_ostree_branch_ids.each_slice(SETTINGS[:katello][:pulp][:bulk_load_size]) do |sub_list|
ostree_branches.concat(Katello.pulp_server.extensions.ostree_branch.find_all_by_unit_ids(sub_list))
end
ostree_branches
end
def sync_schedule(date_and_time)
if date_and_time
Katello.pulp_server.extensions.repository.create_or_update_schedule(self.pulp_id, importer_type, date_and_time)
else
Katello.pulp_server.extensions.repository.remove_schedules(self.pulp_id, importer_type)
end
end
def find_packages_by_name(name)
Katello.pulp_server.extensions.repository.rpms_by_nvre self.pulp_id, name
end
def find_packages_by_nvre(name, version, release, epoch)
Katello.pulp_server.extensions.repository.rpms_by_nvre self.pulp_id, name, version, release, epoch
end
def pulp_update_needed?
changeable_attributes = %w(url unprotected checksum_type docker_upstream_name download_policy mirror_on_sync)
changeable_attributes << "name" if docker?
changeable_attributes.any? { |key| previous_changes.key?(key) }
end
def sync(options = {})
sync_options = {}
sync_options[:max_speed] ||= SETTINGS[:katello][:pulp][:sync_KBlimit] if SETTINGS[:katello][:pulp][:sync_KBlimit] # set bandwidth limit
sync_options[:num_threads] ||= SETTINGS[:katello][:pulp][:sync_threads] if SETTINGS[:katello][:pulp][:sync_threads] # set threads per sync
pulp_tasks = Katello.pulp_server.extensions.repository.sync(self.pulp_id, :override_config => sync_options)
task = PulpSyncStatus.using_pulp_task(pulp_tasks) do |t|
t.organization = organization
t.parameters ||= {}
t.parameters[:options] = options
end
task.save!
return [task]
end
def clone_contents_by_filter(to_repo, content_type, filter_clauses, override_config = {})
content_classes = {
Katello::Rpm::CONTENT_TYPE => :rpm,
Katello::PackageGroup::CONTENT_TYPE => :package_group,
Katello::Erratum::CONTENT_TYPE => :errata,
Katello::PuppetModule::CONTENT_TYPE => :puppet_module
}
fail "Invalid content type #{content_type} sent. It needs to be one of #{content_classes.keys}"\
unless content_classes[content_type]
criteria = {}
if content_type == Runcible::Extensions::Rpm.content_type
criteria[:fields] = Pulp::Rpm::PULP_SELECT_FIELDS
end
if filter_clauses && !filter_clauses.empty?
if content_type == Runcible::Extensions::PuppetModule.content_type
criteria[:filters] = {:association => filter_clauses}
else
criteria[:filters] = {:unit => filter_clauses}
end
end
criteria[:override_config] = override_config unless override_config.empty?
Katello.pulp_server.extensions.send(content_classes[content_type]).copy(self.pulp_id, to_repo.pulp_id, criteria)
end
def clone_contents(to_repo)
events = []
if self.content_type == Repository::PUPPET_TYPE
events << Katello.pulp_server.extensions.puppet_module.copy(self.pulp_id, to_repo.pulp_id)
else
# In order to reduce the memory usage of pulp during the copy process,
# include the fields that will uniquely identify the rpm. If no fields
# are listed, pulp will retrieve every field it knows about for the rpm
# (e.g. changelog, filelist...etc).
events << Katello.pulp_server.extensions.rpm.copy(self.pulp_id, to_repo.pulp_id,
:fields => Pulp::Rpm::PULP_SELECT_FIELDS)
# Since the rpms will be copied above, during the copy of errata and package groups,
# include the copy_children flag to request that pulp skip copying them again.
events << Katello.pulp_server.extensions.errata.copy(self.pulp_id, to_repo.pulp_id, :copy_children => false)
events << Katello.pulp_server.extensions.package_group.copy(self.pulp_id, to_repo.pulp_id, :copy_children => false)
events << clone_file_metadata(to_repo)
end
events
end
def clone_file_metadata(to_repo)
Katello.pulp_server.extensions.yum_repo_metadata_file.copy(self.pulp_id, to_repo.pulp_id)
end
def unassociate_by_filter(content_type, filter_clauses)
criteria = {:type_ids => [content_type], :filters => {:unit => filter_clauses}}
if content_type == Katello.pulp_server.extensions.rpm.content_type
criteria[:fields] = { :unit => Pulp::Rpm::PULP_SELECT_FIELDS}
end
Katello.pulp_server.extensions.repository.unassociate_units(self.pulp_id, criteria)
end
def clear_contents
tasks = content_types.flat_map { |type| type.unassociate_from_repo(self.pulp_id, {}) }
tasks << Katello.pulp_server.extensions.repository.unassociate_units(self.pulp_id,
:type_ids => ['rpm'], :filters => {}, :fields => { :unit => Pulp::Rpm::PULP_SELECT_FIELDS})
tasks
end
def content_types
[Katello.pulp_server.extensions.errata,
Katello.pulp_server.extensions.package_group,
Katello.pulp_server.extensions.puppet_module
]
end
def sync_start
status = self.sync_status
retval = nil
if status.nil? || status['progress']['start_time'].nil?
retval = nil
else
retval = status['progress']['start_time']
# retval = date.strftime("%H:%M:%S %Y-%m-%d")
end
retval
end
def sync_finish
status = self.sync_status
retval = nil
if status.nil? || status['progress']['finish_time'].nil?
retval = nil
else
retval = status['progress']['finish_time']
end
retval
end
def sync_status
self._get_most_recent_sync_status if @sync_status.nil?
end
def sync_state
status = sync_status
return PulpSyncStatus::Status::NOT_SYNCED if status.nil?
status.state
end
def synced?
sync_history = self.sync_status
!sync_history.nil? && successful_sync?(sync_history)
end
def successful_sync?(sync_history_item)
sync_history_item['state'] == PulpTaskStatus::Status::FINISHED.to_s
end
def find_distributor(use_clone_distributor = false)
dist_type_id = if use_clone_distributor
case self.content_type
when Repository::YUM_TYPE
Runcible::Models::YumCloneDistributor.type_id
when Repository::PUPPET_TYPE
Runcible::Models::PuppetInstallDistributor.type_id
end
else
case self.content_type
when Repository::YUM_TYPE
Runcible::Models::YumDistributor.type_id
when Repository::PUPPET_TYPE
Runcible::Models::PuppetInstallDistributor.type_id
end
end
distributors.detect { |dist| dist["distributor_type_id"] == dist_type_id }
end
def find_node_distributor
self.distributors.detect { |i| i["distributor_type_id"] == Runcible::Models::NodesHttpDistributor.type_id }
end
def sort_sync_status(statuses)
statuses.sort! do |a, b|
if a['finish_time'].nil? && b['finish_time'].nil?
if a['start_time'].nil?
1
elsif b['start_time'].nil?
-1
else
a['start_time'] <=> b['start_time']
end
elsif a['finish_time'].nil?
if a['start_time'].nil?
1
else
-1
end
elsif b['finish_time'].nil?
if b['start_time'].nil?
-1
else
1
end
else
b['finish_time'] <=> a['finish_time']
end
end
return statuses
end
def unit_type_id
case content_type
when Repository::YUM_TYPE
"rpm"
when Repository::PUPPET_TYPE
"puppet_module"
when Repository::DOCKER_TYPE
"docker_manifest"
when Repository::OSTREE_TYPE
"ostree"
when Repository::FILE_TYPE
"iso"
end
end
def unit_search(options = {})
Katello.pulp_server.extensions.repository.unit_search(self.pulp_id, options)
end
# A helper method used by purge_empty_groups_errata
# to obtain a list of package filenames and names
# so that it could mix/match empty package groups
# and errata and purge them.
def package_lists_for_publish
names = []
filenames = []
rpm_list = []
self.pulp_rpm_ids.each_slice(SETTINGS[:katello][:pulp][:bulk_load_size]) do |sub_list|
rpm_list.concat(Katello.pulp_server.extensions.rpm.find_all_by_unit_ids(
sub_list, %w(filename name), :include_repos => false))
end
rpm_list.each do |rpm|
filenames << rpm["filename"]
names << rpm["name"]
end
{:names => names.to_set,
:filenames => filenames.to_set}
end
def docker?
self.content_type == Repository::DOCKER_TYPE
end
def puppet?
self.content_type == Repository::PUPPET_TYPE
end
def file?
self.content_type == Repository::FILE_TYPE
end
def yum?
self.content_type == Repository::YUM_TYPE
end
def ostree?
self.content_type == Repository::OSTREE_TYPE
end
def capsule_download_policy
if self.yum?
repo = self.library_instance || self
repo.download_policy
end
end
def distributors_match?(capsule_distributors)
generated_distributors = self.generate_distributors.map(&:as_json)
capsule_distributors.each do |dist|
dist.merge!(dist["config"])
dist.delete("config")
end
generated_distributors.any? do |gen_dist|
capsule_distributors.any? do |cap_dist|
(gen_dist.to_a - cap_dist.to_a).empty?
end
end
end
def importer_matches?(capsule_importer)
generated_importer = self.generate_importer(true).as_json
(generated_importer.to_a - capsule_importer.to_a).empty?
end
protected
def object_to_hash(object)
hash = {}
object.instance_variables.each { |var| hash[var.to_s.delete("@")] = object.instance_variable_get(var) }
hash
end
def _get_most_recent_sync_status
begin
history = Katello.pulp_server.extensions.repository.sync_status(pulp_id)
if history.nil? || history.empty?
history = PulpSyncStatus.convert_history(Katello.pulp_server.extensions.repository.sync_history(pulp_id))
end
rescue
history = PulpSyncStatus.convert_history(Katello.pulp_server.extensions.repository.sync_history(pulp_id))
end
if history.nil? || history.empty?
return PulpSyncStatus.new(:state => PulpSyncStatus::Status::NOT_SYNCED)
else
history = sort_sync_status(history)
return PulpSyncStatus.pulp_task(history.first.with_indifferent_access)
end
end
end
def full_path(smart_proxy = nil)
pulp_uri = URI.parse(smart_proxy ? smart_proxy.url : SETTINGS[:katello][:pulp][:url])
scheme = (self.unprotected ? 'http' : 'https')
if docker?
"#{pulp_uri.host.downcase}:5000/#{pulp_id}"
elsif file?
"#{scheme}://#{pulp_uri.host.downcase}/pulp/isos/#{pulp_id}"
elsif puppet?
"#{scheme}://#{pulp_uri.host.downcase}/pulp/puppet/#{pulp_id}"
elsif ostree?
"#{scheme}://#{pulp_uri.host.downcase}/pulp/ostree/web/#{pulp_id}"
else
"#{scheme}://#{pulp_uri.host.downcase}/pulp/repos/#{relative_path}"
end
end
def index_content
self.index_db_rpms
self.index_db_errata
self.index_db_docker_manifests
self.index_db_puppet_modules
self.index_db_package_groups
self.index_db_ostree_branches if Katello::RepositoryTypeManager.find(Repository::OSTREE_TYPE).present?
self.import_distribution_data
true
end
end
end