Class: SccAccount
- Inherits:
-
ApplicationRecord
- Object
- ApplicationRecord
- SccAccount
- Includes:
- Authorizable, Encryptable, ForemanTasks::Concerns::ActionSubject
- Defined in:
- app/models/scc_account.rb
Constant Summary collapse
- NEVER =
'never'.freeze
- DAILY =
'daily'.freeze
- WEEKLY =
'weekly'.freeze
- MONTHLY =
'monthly'.freeze
- TYPES =
[NEVER, DAILY, WEEKLY, MONTHLY].freeze
- SCC_MIRRORING_POLICIES =
MIRRORING_POLICY_COMPLETE doesn’t work for suse repository because deltarpm metadata is not supported by pulp3
::Katello::RootRepository::MIRRORING_POLICIES.dup
Instance Attribute Summary collapse
-
#scc_products_with_repos_count ⇒ Object
Returns the value of attribute scc_products_with_repos_count.
Class Method Summary collapse
Instance Method Summary collapse
- #add_recurring_logic(sync_date, interval) ⇒ Object
- #associate_recurring_logic ⇒ Object
- #cancel_recurring_logic ⇒ Object
- #enabled_toggle? ⇒ Boolean
- #get_scc_data(rel_url) ⇒ Object
- #init ⇒ Object
-
#invalidate_subscription_status(items_to_invalidate, save_record:) ⇒ Object
set all products/repos invalid params: items_to_invalidate: ActiveRecord_(*) save_record: store in database or not return: ActiveRecord elements with invalidated subscription status.
- #rec_logic_changed? ⇒ Boolean
-
#revalidate_subscription_status(elements, scc_id) ⇒ Object
validate the subscription status of a product/repo no saving to database params: elements: scc repos or products, Array or ActiveRecord_(*) scc_id: scc_id of the element that should be revalidated return: elements where for the element with scc_id subscription_valid is true.
- #save_with_logic! ⇒ Object
- #start_recurring_logic ⇒ Object
- #sync_date_is_valid_datetime ⇒ Object
- #sync_status ⇒ Object
- #test_connection ⇒ Object
- #to_s ⇒ Object
- #toggle_enabled ⇒ Object
- #update_attributes_with_logic!(params) ⇒ Object
- #update_scc_products(upstream_products) ⇒ Object
- #update_scc_repositories(upstream_repositories) ⇒ Object
- #use_recurring_logic? ⇒ Boolean
Instance Attribute Details
#scc_products_with_repos_count ⇒ Object
Returns the value of attribute scc_products_with_repos_count.
6 7 8 |
# File 'app/models/scc_account.rb', line 6 def scc_products_with_repos_count @scc_products_with_repos_count end |
Class Method Details
.download_policy_selection_values ⇒ Object
289 290 291 292 293 294 295 296 297 298 |
# File 'app/models/scc_account.rb', line 289 def self.download_policy_selection_values names = { ::Katello::RootRepository::DOWNLOAD_ON_DEMAND => _('On Demand'), ::Katello::RootRepository::DOWNLOAD_IMMEDIATE => _('Immediate'), } ::Katello::RootRepository::DOWNLOAD_POLICIES.map do |p| [names.fetch(p, p), p] end end |
.mirroring_policy_selection_values ⇒ Object
300 301 302 303 304 305 306 307 308 309 |
# File 'app/models/scc_account.rb', line 300 def self.mirroring_policy_selection_values names = { ::Katello::RootRepository::MIRRORING_POLICY_ADDITIVE => _('Additive'), ::Katello::RootRepository::MIRRORING_POLICY_CONTENT => _('Content Only'), } ::Katello::RootRepository::MIRRORING_POLICIES.map do |p| [names.fetch(p, p), p] end end |
Instance Method Details
#add_recurring_logic(sync_date, interval) ⇒ Object
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'app/models/scc_account.rb', line 108 def add_recurring_logic(sync_date, interval) sd = sync_date.presence || Time.now raise _('Interval cannot be nil') if interval.nil? cron = case interval.downcase when DAILY then "#{sd.min} #{sd.hour} * * *" when WEEKLY then "#{sd.min} #{sd.hour} * * #{sd.wday}" when MONTHLY then "#{sd.min} #{sd.hour} #{sd.day} * *" else raise _('Interval not set correctly') end recurring_logic = ForemanTasks::RecurringLogic.new_from_cronline(cron) raise _('Cron expression is not valid!') unless recurring_logic.valid_cronline? recurring_logic.save! recurring_logic end |
#associate_recurring_logic ⇒ Object
129 130 131 132 133 134 135 |
# File 'app/models/scc_account.rb', line 129 def associate_recurring_logic if self.use_recurring_logic? self.foreman_tasks_recurring_logic = add_recurring_logic(self.sync_date, self.interval) else self.foreman_tasks_recurring_logic = nil end end |
#cancel_recurring_logic ⇒ Object
155 156 157 |
# File 'app/models/scc_account.rb', line 155 def cancel_recurring_logic self.foreman_tasks_recurring_logic&.cancel end |
#enabled_toggle? ⇒ Boolean
163 164 165 |
# File 'app/models/scc_account.rb', line 163 def enabled_toggle? saved_change_to_attribute?(:enabled) end |
#get_scc_data(rel_url) ⇒ Object
167 168 169 |
# File 'app/models/scc_account.rb', line 167 def get_scc_data(rel_url) SccManager.get_scc_data(base_url, rel_url, login, password) end |
#init ⇒ Object
49 50 51 52 |
# File 'app/models/scc_account.rb', line 49 def init # set default values self.sync_date ||= Time.new if self.new_record? end |
#invalidate_subscription_status(items_to_invalidate, save_record:) ⇒ Object
set all products/repos invalid params: items_to_invalidate: ActiveRecord_(*)
save_record: store in database or not
return: ActiveRecord elements with invalidated subscription status
279 280 281 282 283 284 285 286 287 |
# File 'app/models/scc_account.rb', line 279 def invalidate_subscription_status(items_to_invalidate, save_record:) if items_to_invalidate.present? items_to_invalidate.each do |inv| inv.subscription_valid = false inv.save! if save_record end end items_to_invalidate end |
#rec_logic_changed? ⇒ Boolean
159 160 161 |
# File 'app/models/scc_account.rb', line 159 def rec_logic_changed? saved_change_to_attribute?(:sync_date) || saved_change_to_attribute?(:interval) end |
#revalidate_subscription_status(elements, scc_id) ⇒ Object
validate the subscription status of a product/repo no saving to database params: elements: scc repos or products, Array or ActiveRecord_(*)
scc_id: scc_id of the element that should be revalidated
return: elements where for the element with scc_id subscription_valid is true
266 267 268 269 270 271 272 273 |
# File 'app/models/scc_account.rb', line 266 def revalidate_subscription_status(elements, scc_id) return nil if elements.nil? revalidate = elements.find { |e| e.scc_id == scc_id } revalidate.subscription_valid = true unless revalidate.nil? # return modified list elements end |
#save_with_logic! ⇒ Object
80 81 82 83 84 85 86 87 88 89 |
# File 'app/models/scc_account.rb', line 80 def save_with_logic! self.task_group ||= SccAccountSyncPlanTaskGroup.create! associate_recurring_logic if self.valid? self.save! start_recurring_logic if self.use_recurring_logic? true end |
#start_recurring_logic ⇒ Object
141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'app/models/scc_account.rb', line 141 def start_recurring_logic # rubocop:disable Style/GuardClause if self.use_recurring_logic? User.as_anonymous_admin do if self.sync_date.to_time < Time.now self.foreman_tasks_recurring_logic.start(::Actions::SccManager::SyncPlanAccountRepositories, self) else self.foreman_tasks_recurring_logic.start_after(::Actions::SccManager::SyncPlanAccountRepositories, self.sync_date, self) end end end # rubocop:enable Style/GuardClause end |
#sync_date_is_valid_datetime ⇒ Object
54 55 56 |
# File 'app/models/scc_account.rb', line 54 def sync_date_is_valid_datetime errors.add(:sync_date, 'must be a valid datetime') if interval != NEVER && sync_date.blank? end |
#sync_status ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'app/models/scc_account.rb', line 62 def sync_status if sync_task.nil? _('never synced') elsif sync_task.state == 'stopped' if sync_task.result == 'success' synced else sync_task.result end else sync_task.state end end |
#test_connection ⇒ Object
171 172 173 174 175 176 177 |
# File 'app/models/scc_account.rb', line 171 def test_connection get_scc_data('/connect/organizations/subscriptions') true rescue StandardError => e ::Foreman::Logging.logger('foreman_scc_manager').warn "Error occurred while testing SCC-Connection to Account #{self}: #{e}" false end |
#to_s ⇒ Object
58 59 60 |
# File 'app/models/scc_account.rb', line 58 def to_s name end |
#toggle_enabled ⇒ Object
137 138 139 |
# File 'app/models/scc_account.rb', line 137 def toggle_enabled self.foreman_tasks_recurring_logic&.enabled = self.enabled end |
#update_attributes_with_logic!(params) ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'app/models/scc_account.rb', line 91 def update_attributes_with_logic!(params) transaction do self.update!(params) if rec_logic_changed? old_rec_logic = self.foreman_tasks_recurring_logic associate_recurring_logic self.save! old_rec_logic&.cancel # Can/Should we do that??? old_rec_logic&.destroy start_recurring_logic end toggle_enabled if enabled_toggle? end true end |
#update_scc_products(upstream_products) ⇒ Object
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 |
# File 'app/models/scc_account.rb', line 215 def update_scc_products(upstream_products) upstream_product_ids = [] # initially invalidate all products and validate them during update invalidated_products = invalidate_subscription_status(scc_products, save_record: false) # import products upstream_products.each do |up| cached_product = scc_products.find_or_initialize_by(scc_id: up['id']) cached_product.version = up['version'] cached_product.arch = up['arch'] cached_product.description = up['description'] cached_product.friendly_name = up['friendly_name'] cached_product.product_type = up['product_type'] cached_product.scc_repositories = scc_repositories.where(scc_id: up['repositories'].map { |repo| repo['id'] }) cached_product.name = cached_product.pretty_name cached_product.product_category = up['name'] cached_product.description = cached_product.pretty_description cached_product.subscription_valid = true cached_product.save! upstream_product_ids << up['id'] # set invalidated record to true, if exists invalidated_products = revalidate_subscription_status(invalidated_products, up['id']) end ::Foreman::Logging.logger('foreman_scc_manager').debug "Found #{upstream_product_ids.length} products" # all scc products that are kept but not available upstream need to be marked invalid # subscription_valid can be set to nil to_invalidate = invalidated_products.select { |ip| ip.product_id.present? && !ip.subscription_valid } ::Foreman::Logging.logger('foreman_scc_manager').debug "Invalidating #{to_invalidate.count} expired products" invalidate_subscription_status(to_invalidate, save_record: true) # delete products being removed upstream and that are not subscribed to to_delete = scc_products.where.not(scc_id: upstream_product_ids).where(product_id: nil) ::Foreman::Logging.logger('foreman_scc_manager').debug "Deleting #{to_delete.count} old products" to_delete.destroy_all # rewire product to product relationships upstream_products.each do |up| extensions = scc_products.where(scc_id: up['extensions'].map { |ext| ext['id'] }) begin scc_products.find_by!(scc_id: up['id']).update!(scc_extensions: extensions) rescue ActiveRecord::RecordNotFound ::Foreman::Logging.logger('foreman_scc_manager').info "Failed to find parent scc_product '#{up['name']}'." end end end |
#update_scc_repositories(upstream_repositories) ⇒ Object
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 |
# File 'app/models/scc_account.rb', line 179 def update_scc_repositories(upstream_repositories) upstream_repo_ids = [] # initially invalidate all repositories and validate them during update invalidated_repos = invalidate_subscription_status(scc_repositories, save_record: false) # import repositories upstream_repositories.each do |ur| cached_repository = scc_repositories.find_or_initialize_by(scc_id: ur['id']) cached_repository.distro_target = ur['distro_target'] cached_repository.description = ur['description'] cached_repository.url, cached_repository.token = ur['url'].split('?') cached_repository.enabled = ur['enabled'] cached_repository.autorefresh = ur['autorefresh'] cached_repository.installer_updates = ur['installer_updates'] # should be called after all attributes are set in case of dependencies (currently: description) cached_repository.name = cached_repository.pretty_name cached_repository.subscription_valid = true cached_repository.save! upstream_repo_ids << ur['id'] # set invalidated record to true, if exists invalidated_repos = revalidate_subscription_status(invalidated_repos, ur[id]) end ::Foreman::Logging.logger('foreman_scc_manager').debug "Found #{upstream_repo_ids.length} repositories" # all scc repos that are kept but not available upstream need to be marked invalid # subscription_valid can be set to nil to_invalidate = invalidated_repos.select { |ir| ir.katello_root_repositories.count > 0 && !ir.subscription_valid } ::Foreman::Logging.logger('foreman_scc_manager').debug "Invalidating #{to_invalidate.count} expired repositories" invalidate_subscription_status(to_invalidate, save_record: true) # delete repositories being removed upstream and that are not subscribed to to_delete = scc_repositories.where.not(scc_id: upstream_repo_ids).includes(:scc_katello_repositories).where(scc_katello_repositories: { id: nil }) ::Foreman::Logging.logger('foreman_scc_manager').debug "Deleting #{to_delete.count} old repositories" to_delete.destroy_all end |
#use_recurring_logic? ⇒ Boolean
76 77 78 |
# File 'app/models/scc_account.rb', line 76 def use_recurring_logic? self.interval != NEVER end |