Class: Katello::ContentViewVersion
- Includes:
- ForemanTasks::Concerns::ActionSubject, Authorization::ContentViewVersion, Katello::Concerns::SearchByRepositoryName
- Defined in:
- app/models/katello/content_view_version.rb
Overview
rubocop:disable Metrics/ClassLength
Defined Under Namespace
Classes: Jail
Class Method Summary collapse
- .component_of(versions) ⇒ Object
- .contains_file(file_unit_id) ⇒ Object
- .find_by_version(_key, operator, value) ⇒ Object
- .for_version(version) ⇒ Object
- .in_environment(env) ⇒ Object
- .with_library_repo(repo) ⇒ Object
Instance Method Summary collapse
- #active_history ⇒ Object
- #add_applied_filters! ⇒ Object
- #after_promote_hooks ⇒ Object
- #ansible_collections ⇒ Object
- #archived_repos ⇒ Object
- #auto_publish_composites! ⇒ Object
- #available_debs ⇒ Object
- #available_errata ⇒ Object
- #available_packages ⇒ Object
- #available_releases ⇒ Object
- #before_promote_hooks ⇒ Object
- #check_ready_to_promote!(to_env) ⇒ Object
- #components_needing_errata(errata) ⇒ Object
- #content_counts_map ⇒ Object
- #debs ⇒ Object
- #default_content_view? ⇒ Boolean
- #deletable?(from_env) ⇒ Boolean
- #description ⇒ Object
- #docker_manifest_lists ⇒ Object
- #docker_manifests ⇒ Object
- #docker_tags ⇒ Object
- #env_promote_date(env) ⇒ Object
- #errata(errata_type = nil) ⇒ Object
- #file_units ⇒ Object
- #filters_applied? ⇒ Boolean
- #generic_content_units(content_type) ⇒ Object
- #get_repo_clone(env, repo) ⇒ Object
- #importable_repositories ⇒ Object
- #in_composite? ⇒ Boolean
- #in_environment? ⇒ Boolean
- #incrementally_updated? ⇒ Boolean
- #last_event ⇒ Object
- #latest? ⇒ Boolean
- #library_debs ⇒ Object
- #library_errata ⇒ Object
- #library_packages ⇒ Object
- #library_repos ⇒ Object
- #module_streams ⇒ Object
- #name ⇒ Object
- #next_incremental_version ⇒ Object
- #non_archive_repos ⇒ Object
- #package_groups ⇒ Object
- #packages ⇒ Object
- #products(env = nil) ⇒ Object
- #promotable?(target_envs) ⇒ Boolean
- #published_in_composite? ⇒ Boolean
- #rabl_path ⇒ Object
- #removable? ⇒ Boolean
- #repos(env) ⇒ Object
- #repos_ordered_by_product(env) ⇒ Object
- #repository_type_counts_map ⇒ Object
- #sorted_organization_readable_environments ⇒ Object
- #srpms ⇒ Object
- #to_s ⇒ Object
- #update_content_counts! ⇒ Object
- #validate_destroyable!(skip_environment_check: false) ⇒ Object
- #version ⇒ Object
Methods included from Authorization::ContentViewVersion
Methods inherited from Model
Class Method Details
.component_of(versions) ⇒ Object
94 95 96 |
# File 'app/models/katello/content_view_version.rb', line 94 def self.component_of(versions) joins(:content_view_version_composites).where("#{Katello::ContentViewVersionComponent.table_name}.composite_version_id" => versions) end |
.contains_file(file_unit_id) ⇒ Object
118 119 120 |
# File 'app/models/katello/content_view_version.rb', line 118 def self.contains_file(file_unit_id) where(id: Katello::Repository.where(id: Katello::RepositoryFileUnit.where(file_unit_id: file_unit_id).select(:repository_id)).select(:content_view_version_id)) end |
.find_by_version(_key, operator, value) ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'app/models/katello/content_view_version.rb', line 76 def self.find_by_version(_key, operator, value) conditions = "" if ['>', '<', '=', '<=', '>=', "<>", "!=", 'IN', 'NOT IN'].include?(operator) && value.to_f >= 0 major, minor = value.split(".") case when /[<>]/ =~ operator minor ||= 0 query = where("major #{operator} :major OR (major = :major AND minor #{operator} :minor)", :major => major, :minor => minor) when minor.nil? query = where("major #{operator} (:major)", :major => major) else query = where("major #{operator} (:major) and minor #{operator} (:minor)", :major => major, :minor => minor) end _, conditions = query.to_sql.split("WHERE") end { :conditions => conditions } end |
.for_version(version) ⇒ Object
102 103 104 105 106 |
# File 'app/models/katello/content_view_version.rb', line 102 def self.for_version(version) major, minor = version.to_s.split('.') minor ||= 0 where(:major => major, :minor => minor) end |
.in_environment(env) ⇒ Object
222 223 224 |
# File 'app/models/katello/content_view_version.rb', line 222 def self.in_environment(env) joins(:content_view_environments).where("#{Katello::ContentViewEnvironment.table_name}.environment_id" => env) end |
.with_library_repo(repo) ⇒ Object
98 99 100 |
# File 'app/models/katello/content_view_version.rb', line 98 def self.with_library_repo(repo) joins(:repositories).where("#{Katello::Repository.table_name}.library_instance_id" => repo) end |
Instance Method Details
#active_history ⇒ Object
128 129 130 |
# File 'app/models/katello/content_view_version.rb', line 128 def active_history self.history.select { |history| history.task.try(:pending) } end |
#add_applied_filters! ⇒ Object
444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 |
# File 'app/models/katello/content_view_version.rb', line 444 def add_applied_filters! applied_filters_and_rules = content_view.filters.map do |f| { filter: f, rules: f.rules, affected_repos: f.applicable_repos.map do |repo| repo.slice(:id, :name, :label, :arch, :major, :minor, :content_type, :os_versions, :url, :content_id).merge(redhat: repo.redhat?, root: repo.root.id, product: repo.product.slice(:id, :label)) end, } end self.applied_filters = { applied_filters: applied_filters_and_rules, dependency_solving: content_view.solve_dependencies, } save! end |
#after_promote_hooks ⇒ Object
437 438 439 440 441 442 |
# File 'app/models/katello/content_view_version.rb', line 437 def after_promote_hooks run_callbacks :sync do logger.debug "custom hook after_promote on #{name} will be executed if defined." true end end |
#ansible_collections ⇒ Object
122 123 124 |
# File 'app/models/katello/content_view_version.rb', line 122 def ansible_collections AnsibleCollection.in_repositories(archived_repos) end |
#archived_repos ⇒ Object
188 189 190 |
# File 'app/models/katello/content_view_version.rb', line 188 def archived_repos (self.default? || self.rolling?) ? self.repositories : self.repos(nil) end |
#auto_publish_composites! ⇒ Object
364 365 366 367 368 369 370 371 372 373 374 |
# File 'app/models/katello/content_view_version.rb', line 364 def auto_publish_composites! = { description: _("Auto Publish - Triggered by '%s'") % self.name, triggered_by: self.id, } self.content_view.auto_publish_components.pluck(:composite_content_view_id).each do |composite_id| ::Katello::EventQueue.push_event(::Katello::Events::AutoPublishCompositeView::EVENT_TYPE, composite_id) do |attrs| attrs[:metadata] = end end end |
#available_debs ⇒ Object
303 304 305 |
# File 'app/models/katello/content_view_version.rb', line 303 def available_debs library_packages.where.not(:id => debs) end |
#available_errata ⇒ Object
317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 |
# File 'app/models/katello/content_view_version.rb', line 317 def available_errata # The simple/obvious solution is: # library_errata.where.not(:id => errata) # However, when the list of exclusions is large, the SQL "NOT IN" clause # is extremely inefficient, and it is much better to use a # "LEFT OUTER JOIN" with a subquery. # ActiveRecord .joins() only supports subqueries by supplying raw SQL. We # use .to_sql to avoid hard-coding raw SQL for self.errata, although # .to_sql may also be somewhat brittle. For example, see: # https://github.com/rails/rails/issues/18379 library_errata.joins( "LEFT OUTER JOIN (#{errata.select('id').to_sql}) AS exclude_errata ON " \ 'katello_errata.id = exclude_errata.id' ).where('exclude_errata.id IS NULL') end |
#available_packages ⇒ Object
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 |
# File 'app/models/katello/content_view_version.rb', line 266 def available_packages # The simple/obvious solution is: # library_packages.where.not(:id => packages) # However, when the list of exclusions is large, the SQL "NOT IN" clause # is extremely inefficient, and it is much better to use a # "LEFT OUTER JOIN" with a subquery. # ActiveRecord .joins() only supports subqueries by supplying raw SQL. We # use .to_sql to avoid hard-coding raw SQL for self.packages, although # .to_sql may also be somewhat brittle. For example, see: # https://github.com/rails/rails/issues/18379 library_packages.joins( "LEFT OUTER JOIN (#{packages.select('id').to_sql}) AS exclude_rpms ON " \ 'katello_rpms.id = exclude_rpms.id' ).where('exclude_rpms.id IS NULL') end |
#available_releases ⇒ Object
168 169 170 |
# File 'app/models/katello/content_view_version.rb', line 168 def available_releases Katello::RootRepository.where(:id => self.repositories.select(:root_id)).pluck(:minor).compact.uniq.sort end |
#before_promote_hooks ⇒ Object
430 431 432 433 434 435 |
# File 'app/models/katello/content_view_version.rb', line 430 def before_promote_hooks run_callbacks :sync do logger.debug "custom hook before_promote on #{name} will be executed if defined." true end end |
#check_ready_to_promote!(to_env) ⇒ Object
392 393 394 395 396 397 |
# File 'app/models/katello/content_view_version.rb', line 392 def check_ready_to_promote!(to_env) fail _("Default and Rolling content view versions cannot be promoted") if default? || rolling? content_view.check_composite_action_allowed!(to_env) content_view.check_docker_repository_names!(to_env) content_view.check_orphaned_content_facets!(environments: [to_env]) end |
#components_needing_errata(errata) ⇒ Object
247 248 249 250 251 252 |
# File 'app/models/katello/content_view_version.rb', line 247 def components_needing_errata(errata) component_repos = Repository.where(:content_view_version_id => self.components) library_repos = Repository.where(:id => component_repos.pluck(:library_instance_id)).with_errata(errata) component_repos -= component_repos.with_errata(errata) #find component repos without the errata component_repos.select { |repo| library_repos.include?(repo.library_instance) }.map(&:content_view_version).uniq end |
#content_counts_map ⇒ Object
384 385 386 387 388 389 390 |
# File 'app/models/katello/content_view_version.rb', line 384 def content_counts_map # if its empty, calculate it on demand update_content_counts! if content_counts.blank? counts = Hash[content_counts.map { |key, value| ["#{key}_count", value] }] counts.merge("module_stream_count" => counts["modulemd_count"], "package_count" => counts["rpm_count"]) end |
#debs ⇒ Object
295 296 297 |
# File 'app/models/katello/content_view_version.rb', line 295 def debs Katello::Deb.in_repositories(self.repositories.archived) end |
#default_content_view? ⇒ Boolean
148 149 150 |
# File 'app/models/katello/content_view_version.rb', line 148 def default_content_view? default? end |
#deletable?(from_env) ⇒ Boolean
234 235 236 237 |
# File 'app/models/katello/content_view_version.rb', line 234 def deletable?(from_env) ::Host.in_content_view_environment(self.content_view, from_env).empty? || self.content_view.versions.in_environment(from_env).count > 1 end |
#description ⇒ Object
144 145 146 |
# File 'app/models/katello/content_view_version.rb', line 144 def description history.publish.first.try(:notes) end |
#docker_manifest_lists ⇒ Object
341 342 343 |
# File 'app/models/katello/content_view_version.rb', line 341 def docker_manifest_lists DockerManifestList.in_repositories(archived_repos) end |
#docker_manifests ⇒ Object
337 338 339 |
# File 'app/models/katello/content_view_version.rb', line 337 def docker_manifests DockerManifest.in_repositories(archived_repos) end |
#docker_tags ⇒ Object
290 291 292 293 |
# File 'app/models/katello/content_view_version.rb', line 290 def # Don't count tags from non-archived repos; this causes count errors ::Katello::DockerMetaTag.where(:id => RepositoryDockerMetaTag.where(:repository_id => repositories.archived.docker_type).select(:docker_meta_tag_id)) end |
#env_promote_date(env) ⇒ Object
136 137 138 |
# File 'app/models/katello/content_view_version.rb', line 136 def env_promote_date(env) Katello::ContentViewEnvironment.where(:environment_id => env.id, :content_view_version_id => self.id).pluck(:updated_at).try(:first) end |
#errata(errata_type = nil) ⇒ Object
307 308 309 310 311 |
# File 'app/models/katello/content_view_version.rb', line 307 def errata(errata_type = nil) errata = Erratum.in_repositories(archived_repos) errata = errata.of_type(errata_type) if errata_type errata end |
#file_units ⇒ Object
333 334 335 |
# File 'app/models/katello/content_view_version.rb', line 333 def file_units FileUnit.in_repositories(archived_repos) end |
#filters_applied? ⇒ Boolean
463 464 465 466 467 468 |
# File 'app/models/katello/content_view_version.rb', line 463 def filters_applied? # For older content view versions, we do not know if filters were applied. # For these, return nil. return nil if applied_filters.nil? applied_filters["applied_filters"].present? end |
#generic_content_units(content_type) ⇒ Object
258 259 260 |
# File 'app/models/katello/content_view_version.rb', line 258 def generic_content_units(content_type) GenericContentUnit.in_repositories(archived_repos).where(content_type: content_type) end |
#get_repo_clone(env, repo) ⇒ Object
217 218 219 220 |
# File 'app/models/katello/content_view_version.rb', line 217 def get_repo_clone(env, repo) lib_id = repo.library_instance_id || repo.id self.repos(env).where("#{Katello::Repository.table_name}.library_instance_id" => lib_id) end |
#importable_repositories ⇒ Object
422 423 424 425 426 427 428 |
# File 'app/models/katello/content_view_version.rb', line 422 def importable_repositories if default? repositories.exportable else archived_repos.exportable end end |
#in_composite? ⇒ Boolean
156 157 158 |
# File 'app/models/katello/content_view_version.rb', line 156 def in_composite? composite_content_views.any? end |
#in_environment? ⇒ Boolean
164 165 166 |
# File 'app/models/katello/content_view_version.rb', line 164 def in_environment? environments.any? end |
#incrementally_updated? ⇒ Boolean
180 181 182 |
# File 'app/models/katello/content_view_version.rb', line 180 def incrementally_updated? minor != 0 end |
#last_event ⇒ Object
132 133 134 |
# File 'app/models/katello/content_view_version.rb', line 132 def last_event self.history.order(:created_at).last end |
#latest? ⇒ Boolean
152 153 154 |
# File 'app/models/katello/content_view_version.rb', line 152 def latest? content_view.latest_version_id == self.id end |
#library_debs ⇒ Object
299 300 301 |
# File 'app/models/katello/content_view_version.rb', line 299 def library_debs Katello::Deb.in_repositories(library_repos) end |
#library_errata ⇒ Object
313 314 315 |
# File 'app/models/katello/content_view_version.rb', line 313 def library_errata Erratum.in_repositories(library_repos) end |
#library_packages ⇒ Object
262 263 264 |
# File 'app/models/katello/content_view_version.rb', line 262 def library_packages Rpm.in_repositories(library_repos) end |
#library_repos ⇒ Object
196 197 198 |
# File 'app/models/katello/content_view_version.rb', line 196 def library_repos archived_repos.includes(:library_instance).map(&:library_instance) end |
#module_streams ⇒ Object
286 287 288 |
# File 'app/models/katello/content_view_version.rb', line 286 def module_streams ModuleStream.in_repositories(archived_repos) end |
#name ⇒ Object
140 141 142 |
# File 'app/models/katello/content_view_version.rb', line 140 def name "#{content_view} #{version}" end |
#next_incremental_version ⇒ Object
172 173 174 |
# File 'app/models/katello/content_view_version.rb', line 172 def next_incremental_version "#{major}.#{minor + 1}" end |
#non_archive_repos ⇒ Object
192 193 194 |
# File 'app/models/katello/content_view_version.rb', line 192 def non_archive_repos self.repositories.non_archived end |
#package_groups ⇒ Object
345 346 347 |
# File 'app/models/katello/content_view_version.rb', line 345 def package_groups PackageGroup.in_repositories(archived_repos) end |
#packages ⇒ Object
254 255 256 |
# File 'app/models/katello/content_view_version.rb', line 254 def packages Rpm.in_repositories(archived_repos) end |
#products(env = nil) ⇒ Object
200 201 202 203 204 205 206 |
# File 'app/models/katello/content_view_version.rb', line 200 def products(env = nil) if env repos(env).map(&:product).uniq(&:id) else self.repositories.map(&:product).uniq(&:id) end end |
#promotable?(target_envs) ⇒ Boolean
239 240 241 242 243 244 245 |
# File 'app/models/katello/content_view_version.rb', line 239 def promotable?(target_envs) target_envs = Array.wrap(target_envs) all_environments = target_envs + environments target_envs.all? do |environment| all_environments.include?(environment.prior) || environments.empty? && environment == organization.library end end |
#published_in_composite? ⇒ Boolean
160 161 162 |
# File 'app/models/katello/content_view_version.rb', line 160 def published_in_composite? content_view_version_composites.any? end |
#rabl_path ⇒ Object
470 471 472 |
# File 'app/models/katello/content_view_version.rb', line 470 def rabl_path "katello/api/v2/#{self.class.to_s.demodulize.tableize}/show" end |
#removable? ⇒ Boolean
226 227 228 229 230 231 232 |
# File 'app/models/katello/content_view_version.rb', line 226 def removable? if environments.blank? content_view.promotable_or_removable? else content_view.promotable_or_removable? && KTEnvironment.where(:id => environments).any_promotable? end end |
#repos(env) ⇒ Object
184 185 186 |
# File 'app/models/katello/content_view_version.rb', line 184 def repos(env) self.repositories.in_environment(env) end |
#repos_ordered_by_product(env) ⇒ Object
208 209 210 211 212 213 214 215 |
# File 'app/models/katello/content_view_version.rb', line 208 def repos_ordered_by_product(env) # The repository model has a default scope that orders repositories by name; # however, for content views, it is desirable to order the repositories # based on the name of the product the repository is part of. Repository.send(:with_exclusive_scope) do self.repositories.joins(:product).in_environment(env).order("#{Katello::Product.table_name}.name asc") end end |
#repository_type_counts_map ⇒ Object
376 377 378 379 380 381 382 |
# File 'app/models/katello/content_view_version.rb', line 376 def repository_type_counts_map counts = {} Katello::RepositoryTypeManager.enabled_repository_types.keys.each do |repo_type| counts["#{repo_type}_repository_count"] = archived_repos.with_type(repo_type).count end counts end |
#sorted_organization_readable_environments ⇒ Object
112 113 114 115 116 |
# File 'app/models/katello/content_view_version.rb', line 112 def sorted_organization_readable_environments organization_readable_environments = organization.readable_promotion_paths.flatten organization_readable_environments.insert(0, organization.library) organization_readable_environments.intersection(environments) end |
#srpms ⇒ Object
282 283 284 |
# File 'app/models/katello/content_view_version.rb', line 282 def srpms Katello::Srpm.in_repositories(self.repositories) end |
#to_s ⇒ Object
108 109 110 |
# File 'app/models/katello/content_view_version.rb', line 108 def to_s name end |
#update_content_counts! ⇒ Object
349 350 351 352 353 354 355 356 357 358 359 360 361 362 |
# File 'app/models/katello/content_view_version.rb', line 349 def update_content_counts! self.content_counts = {} RepositoryTypeManager.indexable_content_types.each do |content_type| case content_type&.model_class::CONTENT_TYPE when DockerTag::CONTENT_TYPE content_counts[DockerTag::CONTENT_TYPE] = .count when GenericContentUnit::CONTENT_TYPE content_counts[content_type.content_type] = content_type&.model_class&.in_repositories(self.repositories.archived)&.where(:content_type => content_type.content_type)&.count else content_counts[content_type&.model_class::CONTENT_TYPE] = content_type&.model_class&.in_repositories(self.repositories.archived)&.count end end save! end |
#validate_destroyable!(skip_environment_check: false) ⇒ Object
399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 |
# File 'app/models/katello/content_view_version.rb', line 399 def validate_destroyable!(skip_environment_check: false) unless organization.being_deleted? if !skip_environment_check && in_environment? fail _("Cannot delete version while it is in environments: %s") % environments.map(&:name).join(",") end if in_composite? fail _("Cannot delete version while it is in use by composite content views: %s") % composite_content_views.map(&:name).join(",") end if published_in_composite? list = composites.map do |version| "#{version.content_view.name} Version #{version.version}" end fail _("Cannot delete version while it is in use by composite content views: %s") % list.join(",") end end true end |
#version ⇒ Object
176 177 178 |
# File 'app/models/katello/content_view_version.rb', line 176 def version "#{major}.#{minor}" end |