Class: Katello::Pulp3::Repository::Apt

Inherits:
Katello::Pulp3::Repository show all
Defined in:
app/services/katello/pulp3/repository/apt.rb

Constant Summary collapse

UNIT_LIMIT =
10_000
SIGNING_SERVICE_NAME =
'katello_deb_sign'.freeze

Constants inherited from Katello::Pulp3::Repository

COPY_UNIT_PAGE_SIZE

Instance Attribute Summary

Attributes inherited from Katello::Pulp3::Repository

#repo, #smart_proxy

Instance Method Summary collapse

Methods inherited from Katello::Pulp3::Repository

#add_content, #add_content_for_repo, api, #api, #append_proxy_cacert, build_prn, #common_remote_options, #compute_remote_options, #content_service, #copy_all, #copy_units_by_href, #copy_version, #core_api, #create, #create_distribution, #create_mirror_entities, #create_options, #create_publication, #create_remote, #create_version, #delete_distributions, #delete_distributions_by_path, #delete_publication, #delete_remote, #delete_repository, #delete_version, #distribution_needs_update?, #distribution_reference, #fail_missing_publication, #generate_backend_object_name, #get_distribution, #get_remote, #initialize, instance_for_type, #list, #lookup_distributions, #lookup_publication, #lookup_version, publication_href?, #published?, #read, #read_distribution, #refresh_distributions, #refresh_entities, #refresh_if_needed, #refresh_mirror_entities, #relative_path, #remote_partial_update, #remove_content, #repair, #repository_import_content, #repository_reference, #retain_package_versions_count, #save_distribution_references, #secure_distribution_options, #should_purge_empty_contents?, #skip_types, #specific_create_options, #ssl_remote_options, #sync, #unit_keys, #update, #update_distribution, #update_remote, version_href?, #version_zero?, #with_mirror_adapter

Methods included from ServiceCommon

#create_remote, #create_test_remote, #ignore_404_exception, #reformat_api_exception, #test_remote_name

Methods included from Util::HttpProxy

#proxy, #proxy_host, #proxy_port, #proxy_scheme, #proxy_uri

Constructor Details

This class inherits a constructor from Katello::Pulp3::Repository

Instance Method Details

#add_debs(source_repo_ids, filters, filter_list_map) ⇒ Object



242
243
244
245
246
247
248
249
250
# File 'app/services/katello/pulp3/repository/apt.rb', line 242

def add_debs(source_repo_ids, filters, filter_list_map)
  if (filter_list_map[:whitelist_ids].empty? && filters.select { |filter| filter.inclusion }.empty?)
    filter_list_map[:whitelist_ids] += source_repo_ids.collect do |source_repo_id|
      source_repo = ::Katello::Repository.find(source_repo_id)
      source_repo.debs.pluck(:pulp_id).sort
    end
  end
  filter_list_map
end

#add_filter_content(source_repo_ids, filters, filter_list_map) ⇒ Object



227
228
229
230
231
232
233
234
235
236
237
238
239
240
# File 'app/services/katello/pulp3/repository/apt.rb', line 227

def add_filter_content(source_repo_ids, filters, filter_list_map)
  filters.each do |filter|
    if filter.inclusion
      source_repo_ids.each do |repo_id|
        filter_list_map[:whitelist_ids] += filter.content_unit_pulp_ids(::Katello::Repository.find(repo_id))
      end
    else
      source_repo_ids.each do |repo_id|
        filter_list_map[:blacklist_ids] += filter.content_unit_pulp_ids(::Katello::Repository.find(repo_id))
      end
    end
  end
  filter_list_map
end

#copy_api_data_dup(data) ⇒ Object



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'app/services/katello/pulp3/repository/apt.rb', line 148

def copy_api_data_dup(data)
  data_dup = PulpDebClient::Copy.new
  data_dup.dependency_solving = data.dependency_solving
  data_dup.config = []
  data.config.each do |repo_config|
    config_hash = {
      source_repo_version: repo_config[:source_repo_version],
      dest_repo: repo_config[:dest_repo],
      content: [],
    }
    config_hash[:dest_base_version] = repo_config[:dest_base_version] if repo_config[:dest_base_version]
    data_dup.config << config_hash
  end
  data_dup
end

#copy_content_chunked(data) ⇒ Object



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
# File 'app/services/katello/pulp3/repository/apt.rb', line 164

def copy_content_chunked(data)
  tasks = []
  # Don't chunk if there aren't enough content units
  if data.config.sum { |repo_config| repo_config[:content].size } <= UNIT_LIMIT
    return api.copy_api.copy_content(data)
  end

  unit_copy_counter = 0
  i = 0
  leftover_units = data.config.first[:content].deep_dup

  # Copy data and clear its content fields
  data_dup = copy_api_data_dup(data)

  while i < data_dup.config.size
    # Copy all units within repo or only some?
    if leftover_units.length < UNIT_LIMIT - unit_copy_counter
      copy_amount = leftover_units.length
    else
      copy_amount = UNIT_LIMIT - unit_copy_counter
    end

    data_dup.config[i][:content] = leftover_units.pop(copy_amount)
    unit_copy_counter += copy_amount
    if unit_copy_counter != 0
      tasks << api.copy_api.copy_content(data_dup)
      unit_copy_counter = 0
    end

    if leftover_units.empty?
      # Nothing more to copy -- clear current config's content
      data_dup.config[i][:content] = []
      i += 1
      # Fetch unit list for next data config
      leftover_units = data.config[i][:content].deep_dup unless i == data_dup.config.size
    end
  end

  tasks
end

#copy_content_for_source(source_repository, options = {}) ⇒ Object



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
# File 'app/services/katello/pulp3/repository/apt.rb', line 272

def copy_content_for_source(source_repository, options = {})
  # copy_units_by_href(source_repository.debs.pluck(:pulp_id))
  filters = ContentViewDebFilter.where(:id => options[:filter_ids])

  whitelist_ids = []
  blacklist_ids = []
  filters.each do |filter|
    if filter.inclusion
      whitelist_ids += filter.content_unit_pulp_ids(source_repository)
    else
      blacklist_ids += filter.content_unit_pulp_ids(source_repository)
    end
  end

  whitelist_ids = source_repository.debs.pluck(:pulp_id).sort if (whitelist_ids.empty? && filters.select { |filter| filter.inclusion }.empty?)

  content_unit_hrefs = whitelist_ids - blacklist_ids

  pulp_deb_copy_serializer = PulpDebClient::Copy.new
  pulp_deb_copy_serializer.dependency_solving = false
  pulp_deb_copy_serializer.config = [{
    source_repo_version: source_repository.version_href,
    dest_repo: repository_reference.repository_href,
    content: content_unit_hrefs,
  }]

  remove_all = options[:remove_all]
  remove_all = true if remove_all.nil?

  if remove_all
    remove_all_content_from_repo(repository_reference.repository_href)
  end

  copy_content_chunked(pulp_deb_copy_serializer)
end

#copy_content_from_mapping(repo_id_map, _options = {}) ⇒ Object



252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
# File 'app/services/katello/pulp3/repository/apt.rb', line 252

def copy_content_from_mapping(repo_id_map, _options = {})
  repo_id_map.each do |source_repo_ids, dest_repo_map|
    filters = ContentViewDebFilter.where(:id => dest_repo_map[:filter_ids])

    filter_list_map = { whitelist_ids: [], blacklist_ids: [] }
    filter_list_map = add_filter_content(source_repo_ids, filters, filter_list_map)
    filter_list_map = add_debs(source_repo_ids, filters, filter_list_map)

    whitelist_ids = filter_list_map[:whitelist_ids].flatten&.uniq
    blacklist_ids = filter_list_map[:blacklist_ids].flatten&.uniq
    content_unit_hrefs = whitelist_ids - blacklist_ids

    dest_repo_map[:content_unit_hrefs] = content_unit_hrefs.uniq.sort
  end

  dependency_solving = false

  multi_copy_units(repo_id_map, dependency_solving)
end

#distribution_options(path) ⇒ Object



103
104
105
106
107
108
109
# File 'app/services/katello/pulp3/repository/apt.rb', line 103

def distribution_options(path)
  {
    base_path: path,
    publication: repo.publication_href,
    name: "#{generate_backend_object_name}",
  }
end

#initialize_emptyObject



19
20
21
22
23
24
25
# File 'app/services/katello/pulp3/repository/apt.rb', line 19

def initialize_empty
  # For every empty APT library instance repository we must add at least a release component to
  # ensure we have a publishable repo with consumable metadata. Otherwise smart proxy syncs will
  # fail, and consuming hosts will choke on empty repos.
  opts = {:repository => repository_reference.repository_href, :component => "empty", :distribution => "katello"}
  api.content_release_components_api.create(opts)
end

#mirror_remote_optionsObject



69
70
71
# File 'app/services/katello/pulp3/repository/apt.rb', line 69

def mirror_remote_options
  super.merge({distributions: pulp_distributions.join(' ')})
end

#multi_copy_units(repo_id_map, _dependency_solving) ⇒ Object



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
# File 'app/services/katello/pulp3/repository/apt.rb', line 115

def multi_copy_units(repo_id_map, _dependency_solving)
  tasks = []

  if repo_id_map.values.pluck(:content_unit_hrefs).flatten.any?
    data = PulpDebClient::Copy.new
    data.dependency_solving = false
    data.config = []
    repo_id_map.each do |source_repo_ids, dest_repo_id_map|
      dest_repo = ::Katello::Repository.find(dest_repo_id_map[:dest_repo])
      dest_repo_href = ::Katello::Pulp3::Repository::Apt.new(dest_repo, SmartProxy.pulp_primary).repository_reference.repository_href
      content_unit_hrefs = dest_repo_id_map[:content_unit_hrefs]
      # Not needed during incremental update due to dest_base_version
      # -> Unless incrementally updating a CV repo that is a soft copy of its library instance.
      # -> I.e. no filters and not an incremental version.
      # Don't perform extra content actions if the repo is a soft copy of its library instance.
      # Taken care of by the IncrementalUpdate action.
      if !dest_repo_id_map[:base_version] && !dest_repo.soft_copy_of_library?
        tasks << remove_all_content_from_repo(dest_repo_href)
      end
      source_repo_ids.each do |source_repo_id|
        source_repo_version = ::Katello::Repository.find(source_repo_id).version_href
        config = { source_repo_version: source_repo_version, dest_repo: dest_repo_href, content: content_unit_hrefs }
        config[:dest_base_version] = dest_repo_id_map[:base_version] if dest_repo_id_map[:base_version]
        data.config << config
      end
    end
    tasks << copy_content_chunked(data)
  else
    tasks << remove_all_content_from_mapping(repo_id_map)
  end
  tasks.flatten
end

#partial_repo_pathObject



111
112
113
# File 'app/services/katello/pulp3/repository/apt.rb', line 111

def partial_repo_path
  "/pulp/deb/#{repo.relative_path}/".sub('//', '/')
end

#publication_options(repository) ⇒ Object



92
93
94
95
96
97
98
99
100
101
# File 'app/services/katello/pulp3/repository/apt.rb', line 92

def publication_options(repository)
  ss = api.signing_services_api.list(name: SIGNING_SERVICE_NAME).results
  popts = super(repository)
  if version_missing_structure_content?
    popts.merge!({ structured: false })
    popts.merge!({ simple: true })
  end
  popts[:signing_service] = ss[0].pulp_href if ss && ss.length == 1
  popts
end

#pulp_componentsObject



27
28
29
30
31
# File 'app/services/katello/pulp3/repository/apt.rb', line 27

def pulp_components
  return [] if repo.version_href.blank?
  return ["all"] if version_missing_structure_content?
  pulp_primary_api.content_release_components_api.list({:repository_version => repo.version_href}).results.map { |x| x.plain_component }.uniq.sort
end

#pulp_distributionsObject



40
41
42
43
44
# File 'app/services/katello/pulp3/repository/apt.rb', line 40

def pulp_distributions
  return [] if repo.version_href.blank?
  return ["default"] if version_missing_structure_content?
  pulp_primary_api.content_release_components_api.list({:repository_version => repo.version_href}).results.map { |x| sanitize_pulp_distribution(x.distribution) }.uniq.sort
end

#pulp_primary_apiObject



10
11
12
13
14
15
16
17
# File 'app/services/katello/pulp3/repository/apt.rb', line 10

def pulp_primary_api
  # Sometimes we need to make sure we are talking to the pulp primary and not a smart proxy!
  if smart_proxy.pulp_primary?
    api
  else
    self.class.instance_for_type(repo, ::SmartProxy.pulp_primary).api
  end
end

#regenerate_applicabilityObject



308
309
310
311
# File 'app/services/katello/pulp3/repository/apt.rb', line 308

def regenerate_applicability
  # TODO
  fail NotImplementedError
end

#remote_optionsObject



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'app/services/katello/pulp3/repository/apt.rb', line 46

def remote_options
  deb_remote_options = {
    policy: root.download_policy,
    distributions: root.deb_releases,
  }
  deb_remote_options[:components] = root.deb_components.present? ? root.deb_components : nil
  deb_remote_options[:architectures] = root.deb_architectures.present? ? root.deb_architectures : nil

  if root.url.blank?
    deb_remote_options[:url] = nil
  end

  deb_remote_options[:gpgkey] = root.gpg_key.present? ? root.gpg_key.content : nil

  common_remote_options.merge(deb_remote_options)
end

#remove_all_contentObject



221
222
223
224
225
# File 'app/services/katello/pulp3/repository/apt.rb', line 221

def remove_all_content
  data = PulpDebClient::RepositoryAddRemoveContent.new(
    remove_content_units: ['*'])
  api.repositories_api.modify(repository_reference.repository_href, data)
end

#remove_all_content_from_mapping(repo_id_map) ⇒ Object



205
206
207
208
209
210
211
212
213
# File 'app/services/katello/pulp3/repository/apt.rb', line 205

def remove_all_content_from_mapping(repo_id_map)
  tasks = []
  repo_id_map.each do |_source_repo_ids, dest_repo_id_map|
    dest_repo = ::Katello::Repository.find(dest_repo_id_map[:dest_repo])
    dest_repo_href = ::Katello::Pulp3::Repository::Apt.new(dest_repo, SmartProxy.pulp_primary).repository_reference.repository_href
    tasks << remove_all_content_from_repo(dest_repo_href)
  end
  tasks
end

#remove_all_content_from_repo(repo_href) ⇒ Object



215
216
217
218
219
# File 'app/services/katello/pulp3/repository/apt.rb', line 215

def remove_all_content_from_repo(repo_href)
  data = PulpDebClient::RepositoryAddRemoveContent.new(
    remove_content_units: ['*'])
  api.repositories_api.modify(repo_href, data)
end

#sanitize_pulp_distribution(distribution) ⇒ Object



33
34
35
36
37
38
# File 'app/services/katello/pulp3/repository/apt.rb', line 33

def sanitize_pulp_distribution(distribution)
  return "flat-repo" if distribution == "/"
  return distribution.chomp("/") if distribution&.end_with?("/")
  # Only needed for repository versions created with pulp_deb <= 3.6
  distribution
end

#sync_url_params(sync_options) ⇒ Object



63
64
65
66
67
# File 'app/services/katello/pulp3/repository/apt.rb', line 63

def sync_url_params(sync_options)
  params = super
  params[:optimize] = sync_options[:optimize] if sync_options.key?(:optimize)
  params
end

#version_missing_structure_content?Boolean

Returns:

  • (Boolean)


73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'app/services/katello/pulp3/repository/apt.rb', line 73

def version_missing_structure_content?
  # There may be old pulp_deb repo versions that have no structure content for some or all packages.
  # This could be because packages were uploaded with Katello < 4.12
  # It may also affect filtered CV versions created with very old Katello versions.
  # This method can identify such cases, so that we may fall back to simple publishing.
  #
  # Note: For performance reasons, we identify affected repos using a heuristic.
  # Namely, repo versions that have more packages than PRC (package_release_components) are affected.
  # In theory, there could be repo versions where some packages have multiple PRC, while others have zero.
  # If this results in an overall PRC count > than the number of packages, then the repo version is not
  # identified as affected. We have not seen such cases in the real world, and checking each package
  # individually would be prohibitively inefficent.
  return false if repo.version_href.blank?
  version_content = pulp_primary_api.repository_versions_api.read(repo.version_href).content_summary.present
  packages = version_content.fetch('deb.package', {:count => 0})
  prc = version_content.fetch('deb.package_release_component', {:count => 0})
  return packages.fetch(:count) > prc.fetch(:count)
end