Class: Katello::Repository

Inherits:
Model
  • Object
show all
Includes:
ForemanTasks::Concerns::ActionSubject, Glue::Candlepin::Content, Glue::Pulp::Repo, Authorization::Repository, Ext::LabelFromName, Glue
Defined in:
app/models/katello/repository.rb

Overview

rubocop:disable Metrics/ClassLength

Constant Summary collapse

YUM_TYPE =
'yum'
FILE_TYPE =
'file'
PUPPET_TYPE =
'puppet'
DOCKER_TYPE =
'docker'
OSTREE_TYPE =
'ostree'
CHECKSUM_TYPES =
%w(sha1 sha256)

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Ext::LabelFromName

included, #label_not_changed, #setup_label_from_name

Methods included from Authorization::Repository

#deletable?, #readable?, #redhat_deletable?

Methods included from Glue

included, logger

Methods inherited from Model

#destroy!

Class Method Details

.clone_docker_repo_path(options) ⇒ Object



301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
# File 'app/models/katello/repository.rb', line 301

def self.clone_docker_repo_path(options)
  repo = options[:repository]
  org = repo.organization.label.downcase
  if options[:environment]
    cve = ContentViewEnvironment.where(:environment_id => options[:environment],
                                       :content_view_id => options[:content_view]).first
    view = repo.content_view.label
    product = repo.product.label
    env, _ = cve.label.split('/')
    "#{org}-#{env.downcase}-#{view}-#{product}-#{repo.label}"
  else
    content_path = repo.relative_path.gsub("#{org}-", '')
    "#{org}-#{options[:content_view].label}-#{options[:version].version}-#{content_path}"
  end
end

.clone_repo_path(options) ⇒ Object



288
289
290
291
292
293
294
295
296
297
298
299
# File 'app/models/katello/repository.rb', line 288

def self.clone_repo_path(options)
  repo = options[:repository]
  repo_lib = repo.library_instance ? repo.library_instance : repo
  org, _, content_path = repo_lib.relative_path.split("/", 3)
  if options[:environment]
    cve = ContentViewEnvironment.where(:environment_id => options[:environment],
                                       :content_view_id => options[:content_view]).first
    "#{org}/#{cve.label}/#{content_path}"
  else
    "#{org}/#{ContentView::CONTENT_DIR}/#{options[:content_view].label}/#{options[:version].version}/#{content_path}"
  end
end

.import_distributionsObject



510
511
512
513
514
# File 'app/models/katello/repository.rb', line 510

def self.import_distributions
  self.all.each do |repo|
    repo.import_distribution_data
  end
end

.in_content_views(views) ⇒ Object



156
157
158
159
# File 'app/models/katello/repository.rb', line 156

def self.in_content_views(views)
  joins(:content_view_version)
    .where("#{Katello::ContentViewVersion.table_name}.content_view_id" => views.map(&:id))
end

.in_environment(env_id) ⇒ Object



148
149
150
# File 'app/models/katello/repository.rb', line 148

def self.in_environment(env_id)
  where(environment_id: env_id)
end

.in_environments_products(env_ids, product_ids) ⇒ Object



169
170
171
# File 'app/models/katello/repository.rb', line 169

def self.in_environments_products(env_ids, product_ids)
  in_environment(env_ids).in_product(product_ids)
end

.in_organization(org) ⇒ Object



144
145
146
# File 'app/models/katello/repository.rb', line 144

def self.in_organization(org)
  where("#{Repository.table_name}.environment_id" => org.kt_environments.pluck("#{KTEnvironment.table_name}.id"))
end

.in_product(prod) ⇒ Object



152
153
154
# File 'app/models/katello/repository.rb', line 152

def self.in_product(prod)
  where(product_id: prod)
end

.repo_id(product_label, repo_label, env_label, organization_label, view_label, version, docker_repo_name = nil) ⇒ Object



317
318
319
320
321
322
323
324
325
326
327
328
329
# File 'app/models/katello/repository.rb', line 317

def self.repo_id(product_label, repo_label, env_label, organization_label,
                 view_label, version, docker_repo_name = nil)
  actual_repo_id = [organization_label,
                    env_label,
                    view_label,
                    version,
                    product_label,
                    repo_label,
                    docker_repo_name].compact.join("-").gsub(/[^-\w]/, "_")
  # docker repo names need to be in lower case
  actual_repo_id = actual_repo_id.downcase if docker_repo_name
  actual_repo_id
end

.with_errata(errata) ⇒ Object



345
346
347
# File 'app/models/katello/repository.rb', line 345

def self.with_errata(errata)
  joins(:repository_errata).where("#{Katello::RepositoryErratum.table_name}.erratum_id" => errata)
end

Instance Method Details

#archive?Boolean



161
162
163
# File 'app/models/katello/repository.rb', line 161

def archive?
  self.environment.nil?
end

#as_json(*args) ⇒ Object



279
280
281
282
283
284
285
286
# File 'app/models/katello/repository.rb', line 279

def as_json(*args)
  ret = super
  ret["gpg_key_name"] = gpg_key ? gpg_key.name : ""
  ret["package_count"] = package_count rescue nil
  ret["last_sync"] = last_sync rescue nil
  ret["puppet_module_count"] = self.puppet_modules.count rescue nil
  ret
end

#assert_deletableObject



557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
# File 'app/models/katello/repository.rb', line 557

def assert_deletable
  if self.environment.try(:library?) && self.content_view.default?
    if self.environment.organization.being_deleted?
      return true
    elsif self.custom? && self.deletable?
      return true
    elsif !self.custom? && self.redhat_deletable?
      return true
    else
      errors.add(:base, _("Repository cannot be deleted since it has already been included in a published Content View. " \
                          "Please delete all Content View versions containing this repository before attempting to delete it."))

      return false
    end
  end
end

#build_clone(options) ⇒ Object

TODO: break up method rubocop:disable MethodLength



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
# File 'app/models/katello/repository.rb', line 360

def build_clone(options)
  to_env       = options[:environment]
  version      = options[:version]
  content_view = options[:content_view] || to_env.default_content_view
  to_version   = version || content_view.version(to_env)
  library      = self.library_instance ? self.library_instance : self

  if to_env && version
    fail "Cannot clone into both an environment and a content view version archive"
  end

  if to_version.nil?
    fail _("View %{view} has not been promoted to %{env}") %
              {:view => content_view.name, :env => to_env.name}
  end

  if content_view.default?
    fail _("Cannot clone repository from %{from_env} to %{to_env}. They are not sequential.") %
              {:from_env => self.environment.name, :to_env => to_env.name} if to_env.prior != self.environment
    fail _("Repository has already been promoted to %{to_env}") %
            {:to_env => to_env} if self.cloned_in?(to_env)
  else
    fail _("Repository has already been cloned to %{cv_name} in environment %{to_env}") %
              {:to_env => to_env, :cv_name => content_view.name} if to_env &&
        content_view.repos(to_env).where(:library_instance_id => library.id).count > 0
  end

  Repository.new(:environment => to_env,
                 :product => self.product,
                 :cp_label => self.cp_label,
                 :library_instance => library,
                 :label => self.label,
                 :name => self.name,
                 :arch => self.arch,
                 :major => self.major,
                 :minor => self.minor,
                 :content_id => self.content_id,
                 :content_view_version => to_version,
                 :content_type => self.content_type,
                 :download_policy => download_policy,
                 :unprotected => self.unprotected) do |clone|
    clone.checksum_type = self.checksum_type
    clone.pulp_id = clone.clone_id(to_env, content_view, version.try(:version))
    options = {
      :repository => self,
      :environment => to_env,
      :content_view => content_view,
      :version => version
    }

    clone.relative_path = if clone.docker?
                            Repository.clone_docker_repo_path(options)
                          else
                            Repository.clone_repo_path(options)
                          end
  end
end

#cancel_dynflow_syncObject



418
419
420
421
422
423
424
425
426
427
428
# File 'app/models/katello/repository.rb', line 418

def cancel_dynflow_sync
  if latest_dynflow_sync
    plan = latest_dynflow_sync.execution_plan

    plan.steps.each_pair do |_number, step|
      if step.cancellable? && step.is_a?(Dynflow::ExecutionPlan::Steps::RunStep)
        ::ForemanTasks.dynflow.world.event(plan.id, step.id, Dynflow::Action::Cancellable::Cancel)
      end
    end
  end
end

#check_duplicate_branch_names(branch_names) ⇒ Object



530
531
532
533
534
535
536
537
538
539
540
541
542
543
# File 'app/models/katello/repository.rb', line 530

def check_duplicate_branch_names(branch_names)
  dupe_branch_checker = {}
  dupe_branch_checker.default = 0
  branch_names.each do |branch|
    dupe_branch_checker[branch] += 1
  end

  duplicate_branch_names = dupe_branch_checker.select { |_, value| value > 1 }.keys

  unless duplicate_branch_names.empty?
    fail ::Katello::Errors::ConflictException,
          _("Duplicate branches specified - %{branches}") % { branches: duplicate_branch_names.join(", ")}
  end
end

#clone_id(env, content_view, version = nil) ⇒ Object



331
332
333
334
335
# File 'app/models/katello/repository.rb', line 331

def clone_id(env, content_view, version = nil)
  Repository.repo_id(self.product.label, self.label, env.try(:label),
                     organization.label, content_view.label,
                     version)
end

#cloned_in?(env) ⇒ Boolean

is the repo cloned in the specified environment



236
237
238
# File 'app/models/katello/repository.rb', line 236

def cloned_in?(env)
  !get_clone(env).nil?
end

#clonesObject



225
226
227
228
# File 'app/models/katello/repository.rb', line 225

def clones
  lib_id = self.library_instance_id || self.id
  Repository.where(:library_instance_id => lib_id)
end

#container_repository_nameObject



354
355
356
# File 'app/models/katello/repository.rb', line 354

def container_repository_name
  pulp_id if docker?
end

#content_viewObject



140
141
142
# File 'app/models/katello/repository.rb', line 140

def content_view
  self.content_view_version.content_view
end

#create_clone(options) ⇒ Object



435
436
437
438
439
# File 'app/models/katello/repository.rb', line 435

def create_clone(options)
  clone = build_clone(options)
  clone.save!
  return clone
end

#custom?Boolean



195
196
197
# File 'app/models/katello/repository.rb', line 195

def custom?
  !(redhat?)
end

#dynflow_handled_last_sync?(pulp_task_id) ⇒ Boolean

Returns true if the pulp_task_id was triggered by the last synchronization action for the repository. Dynflow action handles the synchronization by it’s own so no need to synchronize it again in this callback. Since the callbacks are run just after synchronization is finished, it should be enough to check for the last synchronization task.



273
274
275
276
277
# File 'app/models/katello/repository.rb', line 273

def dynflow_handled_last_sync?(pulp_task_id)
  task = ForemanTasks::Task::DynflowTask.for_action(::Actions::Katello::Repository::Sync).
      for_resource(self).order(:started_at).last
  return task && task.main_action.pulp_task_id == pulp_task_id
end

#empty_errataObject



199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'app/models/katello/repository.rb', line 199

def empty_errata
  repository_rpm = Katello::RepositoryRpm.table_name
  repository_errata = Katello::RepositoryErratum.table_name
  rpm = Katello::Rpm.table_name
  errata = Katello::Erratum.table_name
  erratum_package = Katello::ErratumPackage.table_name

  errata_with_packages = Erratum.joins(
    "INNER JOIN #{erratum_package} on #{erratum_package}.erratum_id = #{errata}.id",
    "INNER JOIN #{repository_errata} on #{repository_errata}.erratum_id = #{errata}.id",
    "INNER JOIN #{rpm} on #{rpm}.filename = #{erratum_package}.filename",
    "INNER JOIN #{repository_rpm} on #{repository_rpm}.rpm_id = #{rpm}.id").
    where("#{repository_rpm}.repository_id" => self.id).
    where("#{repository_errata}.repository_id" => self.id)

  if errata_with_packages.any?
    self.errata.where("#{Katello::Erratum.table_name}.id NOT IN (?)", errata_with_packages.pluck("#{errata}.id"))
  else
    self.errata
  end
end

#environmental_instances(view) ⇒ Object

returns other instances of this repo with the same library equivalent of repo



443
444
445
446
447
# File 'app/models/katello/repository.rb', line 443

def environmental_instances(view)
  repo = self.library_instance || self
  search = Repository.non_archived.where("library_instance_id=%s or #{Katello::Repository.table_name}.id=%s" % [repo.id, repo.id])
  search.in_content_views([view])
end

#errata_filenamesObject



349
350
351
352
# File 'app/models/katello/repository.rb', line 349

def errata_filenames
  Katello::ErratumPackage.joins(:erratum => :repository_errata).
      where("#{RepositoryErratum.table_name}.repository_id" => self.id).pluck("#{ Katello::ErratumPackage.table_name}.filename")
end

#exist_for_environment?(environment, content_view, attribute = nil) ⇒ Boolean



473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
# File 'app/models/katello/repository.rb', line 473

def exist_for_environment?(environment, content_view, attribute = nil)
  if environment.present?
    repos = content_view.version(environment).repos(environment)

    repos.any? do |repo|
      not_self = (repo.id != self.id)
      same_product = (repo.product.id == self.product.id)

      repo_exists = same_product && not_self

      if repo_exists && attribute
        same_attribute = repo.send(attribute) == self.send(attribute)
        repo_exists = same_attribute
      end

      repo_exists
    end
  else
    false
  end
end

#get_clone(env) ⇒ Object



248
249
250
251
252
253
254
255
256
257
258
# File 'app/models/katello/repository.rb', line 248

def get_clone(env)
  if self.content_view.default
    # this repo is part of a default content view
    lib_id = self.library_instance_id || self.id
    Repository.in_environment(env).where(:library_instance_id => lib_id).
        joins(:content_view_version => :content_view).where("#{Katello::ContentView.table_name}.default" => true).first
  else
    # this repo is part of a content view that was published from a user created view
    self.content_view.get_repo_clone(env, self).first
  end
end

#gpg_key_name=(name) ⇒ Object



260
261
262
263
264
265
266
# File 'app/models/katello/repository.rb', line 260

def gpg_key_name=(name)
  if name.blank?
    self.gpg_key = nil
  else
    self.gpg_key = GpgKey.readable.find_by!(:name => name)
  end
end

#groupObject



230
231
232
233
# File 'app/models/katello/repository.rb', line 230

def group
  library_repo = library_instance? ? self : library_instance
  clones << library_repo
end

#hosts_with_applicabilityObject



584
585
586
# File 'app/models/katello/repository.rb', line 584

def hosts_with_applicability
  ::Host.joins(:content_facet => :bound_repositories).where("#{Katello::Repository.table_name}.id" => (self.clones.pluck(:id) + [self.id]))
end

#import_distribution_dataObject



516
517
518
519
520
521
522
523
524
525
526
527
528
# File 'app/models/katello/repository.rb', line 516

def import_distribution_data
  distribution = Katello.pulp_server.extensions.repository.distributions(self.pulp_id).first
  if distribution
    self.update_attributes!(
      :distribution_version => distribution["version"],
      :distribution_arch => distribution["arch"],
      :distribution_family => distribution["family"],
      :distribution_variant => distribution["variant"],
      :distribution_uuid => distribution["_id"],
      :distribution_bootable => ::Katello::Repository.distribution_bootable?(distribution)
    )
  end
end

#import_host_applicabilityObject



574
575
576
577
578
579
580
581
582
# File 'app/models/katello/repository.rb', line 574

def import_host_applicability
  self.hosts_with_applicability.find_each do |host|
    begin
      host.content_facet.import_applicability if host.content_facet.try(:uuid)
    rescue => e
      Rails.logger.error("Could not import applicability for #{host.name}: #{e}")
    end
  end
end

#in_default_view?Boolean



165
166
167
# File 'app/models/katello/repository.rb', line 165

def in_default_view?
  content_view_version && content_view_version.default_content_view?
end

#latest_dynflow_syncObject



430
431
432
433
# File 'app/models/katello/repository.rb', line 430

def latest_dynflow_sync
  @latest_dynflow_sync ||= ForemanTasks::Task::DynflowTask.for_action(::Actions::Katello::Repository::Sync).
                            for_resource(self).order(:started_at).last
end

#library_instance?Boolean



221
222
223
# File 'app/models/katello/repository.rb', line 221

def library_instance?
  library_instance.nil?
end

#name_conflictsObject



453
454
455
456
457
458
459
460
461
462
463
# File 'app/models/katello/repository.rb', line 453

def name_conflicts
  if puppet?
    modules = PuppetModule.search("*", :repoids => self.pulp_id,
                                       :fields => [:name],
                                       :page_size => self.puppet_modules.count)

    modules.map(&:name).group_by(&:to_s).select { |_, v| v.size > 1 }.keys
  else
    []
  end
end

#node_syncable?Boolean



469
470
471
# File 'app/models/katello/repository.rb', line 469

def node_syncable?
  environment
end

#organizationObject



132
133
134
135
136
137
138
# File 'app/models/katello/repository.rb', line 132

def organization
  if self.environment
    self.environment.organization
  else
    self.content_view.organization
  end
end

#ostree_branch_namesObject



495
496
497
# File 'app/models/katello/repository.rb', line 495

def ostree_branch_names
  self.ostree_branches.map(&:name)
end

#other_repos_with_same_contentObject



178
179
180
# File 'app/models/katello/repository.rb', line 178

def other_repos_with_same_content
  Repository.where(:content_id => self.content_id).where("#{self.class.table_name}.id != #{self.id}")
end

#other_repos_with_same_product_and_contentObject



173
174
175
176
# File 'app/models/katello/repository.rb', line 173

def other_repos_with_same_product_and_content
  Repository.in_product(Product.find(self.product.id)).where(:content_id => self.content_id)
      .where("#{self.class.table_name}.id != #{self.id}")
end

#packages_without_errataObject



337
338
339
340
341
342
343
# File 'app/models/katello/repository.rb', line 337

def packages_without_errata
  if errata_filenames.any?
    self.rpms.where("#{Rpm.table_name}.filename NOT in (?)", errata_filenames)
  else
    self.rpms
  end
end

#product_typeObject



189
190
191
# File 'app/models/katello/repository.rb', line 189

def product_type
  redhat? ? "redhat" : "custom"
end

#promoted?Boolean



240
241
242
243
244
245
246
# File 'app/models/katello/repository.rb', line 240

def promoted?
  if environment && environment.library? && Repository.where(:library_instance_id => self.id).any?
    true
  else
    false
  end
end


465
466
467
# File 'app/models/katello/repository.rb', line 465

def related_resources
  self.product
end

#remove_content(units) ⇒ Object



545
546
547
548
549
550
551
552
553
554
555
# File 'app/models/katello/repository.rb', line 545

def remove_content(units)
  if yum?
    self.rpms -= units
  elsif puppet?
    self.puppet_modules -= units
  elsif ostree?
    self.ostree_branches -= units
  elsif docker?
    remove_docker_content(units)
  end
end

#units_for_removal(ids) ⇒ Object



499
500
501
502
503
504
505
506
507
508
# File 'app/models/katello/repository.rb', line 499

def units_for_removal(ids)
  table_name = removable_unit_association.table_name
  is_integer = Integer(ids.first) rescue false #assume all ids are either integers or not

  if is_integer
    self.removable_unit_association.where("#{table_name}.id in (?)", ids)
  else
    self.removable_unit_association.where("#{table_name}.uuid in (?)", ids)
  end
end

#url?Boolean



449
450
451
# File 'app/models/katello/repository.rb', line 449

def url?
  url.present?
end

#yum_gpg_key_urlObject



182
183
184
185
186
187
# File 'app/models/katello/repository.rb', line 182

def yum_gpg_key_url
  # if the repo has a gpg key return a url to access it
  if (self.gpg_key && self.gpg_key.content.present?)
    "../..#{gpg_key_content_api_repository_url(self, :only_path => true)}"
  end
end