Module: Glue::Candlepin::Consumer::InstanceMethods
- Defined in:
- app/models/katello/glue/candlepin/consumer.rb
Instance Method Summary collapse
- #arch ⇒ Object
- #arch=(arch) ⇒ Object
- #checkin(checkin_time) ⇒ Object
- #checkin_time ⇒ Object
-
#compliance_color ⇒ Object
As a convenience and common terminology.
- #compliant? ⇒ Boolean
- #compliant_until ⇒ Object
- #content_overrides ⇒ Object
- #convert_from_cp_fields(cp_json) ⇒ Object
- #convert_time(item) ⇒ Object
- #cp_environment_id ⇒ Object
- #created_time ⇒ Object
- #distribution ⇒ Object
- #distribution_name ⇒ Object
- #distribution_version ⇒ Object
- #entitlements_valid? ⇒ Boolean
- #export ⇒ Object
- #find_entitlement(pool_id) ⇒ Object
- #get_pool(id) ⇒ Object
- #hostname ⇒ Object
- #import_candlepin_tasks ⇒ Object
- #initialize(attrs = nil, options = {}) ⇒ Object
- #interfaces ⇒ Object
- #ip ⇒ Object
- #kernel ⇒ Object
- #load_from_cp(consumer_json) ⇒ Object
- #memory ⇒ Object
- #memory=(mem) ⇒ Object
- #memory_in_gigabytes(mem_str) ⇒ Object
- #name=(val) ⇒ Object
- #populate_from(candlepin_systems) ⇒ Object
- #product_compliance_color(product_id) ⇒ Object
- #products ⇒ Object
- #regenerate_identity_certificates ⇒ Object
- #reject_db_columns(cp_json) ⇒ Object
- #release ⇒ Object
-
#remove_hibernate_fields(elements) ⇒ Object
Candlepin sends back its internal hibernate fields in the json.
- #set_content_override(content_label, name, value = nil) ⇒ Object
-
#sockets ⇒ Object
Sockets are required to have a value in katello for searching as well as for checking subscription limits Force always to an integer value for consistency.
- #sockets=(sock) ⇒ Object
- #subscribe(pool, quantity = nil) ⇒ Object
- #to_json(options = {}) ⇒ Object
- #virtual_guest ⇒ Object
- #virtual_guest=(val) ⇒ Object
Instance Method Details
#arch ⇒ Object
168 169 170 |
# File 'app/models/katello/glue/candlepin/consumer.rb', line 168 def arch facts["uname.machine"] if @facts end |
#arch=(arch) ⇒ Object
172 173 174 |
# File 'app/models/katello/glue/candlepin/consumer.rb', line 172 def arch=(arch) facts["uname.machine"] = arch if @facts end |
#checkin(checkin_time) ⇒ Object
78 79 80 81 82 83 84 |
# File 'app/models/katello/glue/candlepin/consumer.rb', line 78 def checkin(checkin_time) Rails.logger.debug "Updating consumer check-in time: #{name}" Resources::Candlepin::Consumer.checkin(self.uuid, checkin_time) rescue => e Rails.logger.error "Failed to update consumer check-in time in candlepin for #{name}: #{e}, #{e.backtrace.join("\n")}" raise e end |
#checkin_time ⇒ Object
235 236 237 238 239 |
# File 'app/models/katello/glue/candlepin/consumer.rb', line 235 def checkin_time if lastCheckin convert_time(lastCheckin) end end |
#compliance_color ⇒ Object
As a convenience and common terminology
289 290 291 292 293 294 |
# File 'app/models/katello/glue/candlepin/consumer.rb', line 289 def compliance_color return 'green' if self.compliance['status'] == 'valid' return 'red' if self.compliance['status'] == 'invalid' return 'yellow' if self.compliance['status'] == 'partial' return 'red' end |
#compliant? ⇒ Boolean
284 285 286 |
# File 'app/models/katello/glue/candlepin/consumer.rb', line 284 def compliant? self.compliance['compliant'] end |
#compliant_until ⇒ Object
296 297 298 299 300 |
# File 'app/models/katello/glue/candlepin/consumer.rb', line 296 def compliant_until if self.compliance['compliantUntil'] Date.parse(self.compliance['compliantUntil']) end end |
#content_overrides ⇒ Object
280 281 282 |
# File 'app/models/katello/glue/candlepin/consumer.rb', line 280 def content_overrides Resources::Candlepin::Consumer.content_overrides(self.uuid) end |
#convert_from_cp_fields(cp_json) ⇒ Object
123 124 125 126 127 128 129 130 131 |
# File 'app/models/katello/glue/candlepin/consumer.rb', line 123 def convert_from_cp_fields(cp_json) cp_json.merge!(:cp_type => cp_json.delete(:type)[:label]) if cp_json.key?(:type) cp_json = reject_db_columns(cp_json) cp_json[:guestIds] = remove_hibernate_fields(cp_json[:guestIds]) if cp_json.key?(:guestIds) cp_json[:installedProducts] = remove_hibernate_fields(cp_json[:installedProducts]) if cp_json.key?(:installedProducts) cp_json end |
#convert_time(item) ⇒ Object
247 248 249 |
# File 'app/models/katello/glue/candlepin/consumer.rb', line 247 def convert_time(item) Time.parse(item) end |
#cp_environment_id ⇒ Object
144 145 146 147 148 149 150 |
# File 'app/models/katello/glue/candlepin/consumer.rb', line 144 def cp_environment_id if self.content_view self.content_view.cp_environment_id(self.environment) else self.environment_id end end |
#created_time ⇒ Object
241 242 243 244 245 |
# File 'app/models/katello/glue/candlepin/consumer.rb', line 241 def created_time if created convert_time(created) end end |
#distribution ⇒ Object
211 212 213 |
# File 'app/models/katello/glue/candlepin/consumer.rb', line 211 def distribution "#{distribution_name} #{distribution_version}" end |
#distribution_name ⇒ Object
203 204 205 |
# File 'app/models/katello/glue/candlepin/consumer.rb', line 203 def distribution_name facts["distribution.name"] end |
#distribution_version ⇒ Object
207 208 209 |
# File 'app/models/katello/glue/candlepin/consumer.rb', line 207 def distribution_version facts["distribution.version"] end |
#entitlements_valid? ⇒ Boolean
231 232 233 |
# File 'app/models/katello/glue/candlepin/consumer.rb', line 231 def entitlements_valid? "true" == facts["system.entitlements_valid"] end |
#export ⇒ Object
110 111 112 113 114 115 116 |
# File 'app/models/katello/glue/candlepin/consumer.rb', line 110 def export Rails.logger.debug "Exporting manifest" Resources::Candlepin::Consumer.export self.uuid rescue => e Rails.logger.debug e.backtrace.join("\n\t") raise e end |
#find_entitlement(pool_id) ⇒ Object
333 334 335 336 337 |
# File 'app/models/katello/glue/candlepin/consumer.rb', line 333 def find_entitlement(pool_id) entitlements = self.entitlements.collect { |ent| ent['id'] if ent["pool"]["id"] == pool_id } entitlements.compact! entitlements.first end |
#get_pool(id) ⇒ Object
94 95 96 97 98 99 |
# File 'app/models/katello/glue/candlepin/consumer.rb', line 94 def get_pool(id) Resources::Candlepin::Pool.find id rescue => e Rails.logger.debug e.backtrace.join("\n\t") raise e end |
#hostname ⇒ Object
152 153 154 |
# File 'app/models/katello/glue/candlepin/consumer.rb', line 152 def hostname facts["network.hostname"] end |
#import_candlepin_tasks ⇒ Object
308 309 310 311 312 313 314 315 316 317 318 319 320 |
# File 'app/models/katello/glue/candlepin/consumer.rb', line 308 def import_candlepin_tasks self.events.each do |event| event_status = {:task_id => event[:id], :state => event[:type], :start_time => event[:timestamp], :finish_time => event[:timestamp], :progress => "100", :result => event[:messageText]} unless self.task_statuses.where('katello_task_statuses.uuid' => event_status[:task_id]).exists? TaskStatus.make(self, event_status, :candlepin_event, :event => event) end end end |
#initialize(attrs = nil, options = {}) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'app/models/katello/glue/candlepin/consumer.rb', line 49 def initialize(attrs = nil, = {}) if attrs.nil? super else type_key = attrs.key?('type') ? 'type' : :type #rename "type" to "cp_type" (activerecord and candlepin variable name conflict) if attrs.key?(type_key) && !(attrs.key?(:cp_type) || attrs.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 if attrs_used_by_model["environment"].is_a? Hash attrs_used_by_model.delete("environment") end super(attrs_used_by_model, ) end end |
#interfaces ⇒ Object
156 157 158 |
# File 'app/models/katello/glue/candlepin/consumer.rb', line 156 def interfaces Katello::System.interfaces(facts) end |
#ip ⇒ Object
160 161 162 |
# File 'app/models/katello/glue/candlepin/consumer.rb', line 160 def ip facts['network.ipv4_address'] end |
#kernel ⇒ Object
164 165 166 |
# File 'app/models/katello/glue/candlepin/consumer.rb', line 164 def kernel facts["uname.release"] end |
#load_from_cp(consumer_json) ⇒ Object
70 71 72 73 74 75 76 |
# File 'app/models/katello/glue/candlepin/consumer.rb', line 70 def load_from_cp(consumer_json) self.uuid = consumer_json[:uuid] consumer_json[:facts] ||= {'sockets' => 0} convert_from_cp_fields(consumer_json).each do |k, v| instance_variable_set("@#{k}", v) if respond_to?("#{k}=") end end |
#memory ⇒ Object
215 216 217 218 219 220 221 222 223 224 |
# File 'app/models/katello/glue/candlepin/consumer.rb', line 215 def memory if facts mem = facts["memory.memtotal"] # dmi.memory.size is on older clients mem ||= facts["dmi.memory.size"] else mem = '0' end memory_in_gigabytes(mem.to_s) end |
#memory=(mem) ⇒ Object
226 227 228 229 |
# File 'app/models/katello/glue/candlepin/consumer.rb', line 226 def memory=(mem) mem = "#{mem.to_i} MB" facts["memory.memtotal"] = mem end |
#memory_in_gigabytes(mem_str) ⇒ Object
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 |
# File 'app/models/katello/glue/candlepin/consumer.rb', line 259 def memory_in_gigabytes(mem_str) # convert total memory into gigabytes return 0 if mem_str.nil? mem, unit = mem_str.split total_mem = mem.to_f case unit when 'B' then total_mem = 0 when 'kB' then total_mem = 0 when 'MB' then total_mem /= 1024 when 'GB' then total_mem *= 1 when 'TB' then total_mem *= 1024 # default memtotal is in kB else total_mem = (total_mem / (1024 * 1024)) end total_mem.round(2) end |
#name=(val) ⇒ Object
198 199 200 201 |
# File 'app/models/katello/glue/candlepin/consumer.rb', line 198 def name=(val) super(val) facts["network.hostname"] = val if @facts end |
#populate_from(candlepin_systems) ⇒ Object
322 323 324 325 326 |
# File 'app/models/katello/glue/candlepin/consumer.rb', line 322 def populate_from(candlepin_systems) found = candlepin_systems.find { |system| system['uuid'] == self.uuid } prepopulate(found.with_indifferent_access) if found !found.nil? end |
#product_compliance_color(product_id) ⇒ Object
302 303 304 305 306 |
# File 'app/models/katello/glue/candlepin/consumer.rb', line 302 def product_compliance_color(product_id) return 'green' if self.compliance['compliantProducts'].include? product_id return 'yellow' if self.compliance['partiallyCompliantProducts'].include? product_id return 'red' end |
#products ⇒ Object
328 329 330 331 |
# File 'app/models/katello/glue/candlepin/consumer.rb', line 328 def products pool_ids = self.entitlements.map { |entitlement| entitlement['pool']['id'] } Katello::Product.joins(:subscriptions => :pools).where("#{Katello::Pool.table_name}.cp_id" => pool_ids).enabled.uniq end |
#regenerate_identity_certificates ⇒ Object
86 87 88 89 90 91 92 |
# File 'app/models/katello/glue/candlepin/consumer.rb', line 86 def regenerate_identity_certificates Rails.logger.debug "Regenerating consumer identity certificates: #{name}" Resources::Candlepin::Consumer.regenerate_identity_certificates(self.uuid) rescue => e Rails.logger.debug e.backtrace.join("\n\t") raise e end |
#reject_db_columns(cp_json) ⇒ Object
140 141 142 |
# File 'app/models/katello/glue/candlepin/consumer.rb', line 140 def reject_db_columns(cp_json) cp_json.reject { |k, _v| self.class.column_defaults.keys.member?(k.to_s) } end |
#release ⇒ Object
251 252 253 254 255 256 257 |
# File 'app/models/katello/glue/candlepin/consumer.rb', line 251 def release if self.releaseVer.is_a? Hash self.releaseVer["releaseVer"] else self.releaseVer end end |
#remove_hibernate_fields(elements) ⇒ Object
Candlepin sends back its internal hibernate fields in the json. However it does not accept them in return when updating (PUT) objects.
135 136 137 138 |
# File 'app/models/katello/glue/candlepin/consumer.rb', line 135 def remove_hibernate_fields(elements) return nil unless elements elements.collect { |e| e.except(:id, :created, :updated) } end |
#set_content_override(content_label, name, value = nil) ⇒ Object
276 277 278 |
# File 'app/models/katello/glue/candlepin/consumer.rb', line 276 def set_content_override(content_label, name, value = nil) Resources::Candlepin::Consumer.update_content_override(self.uuid, content_label, name, value) end |
#sockets ⇒ Object
Sockets are required to have a value in katello for searching as well as for checking subscription limits Force always to an integer value for consistency
178 179 180 181 182 |
# File 'app/models/katello/glue/candlepin/consumer.rb', line 178 def sockets @facts ? Integer(facts["cpu.cpu_socket(s)"]) : 0 rescue 0 end |
#sockets=(sock) ⇒ Object
184 185 186 187 188 |
# File 'app/models/katello/glue/candlepin/consumer.rb', line 184 def sockets=(sock) s = Integer(sock) rescue 0 facts["cpu.cpu_socket(s)"] = s if @facts end |
#subscribe(pool, quantity = nil) ⇒ Object
101 102 103 104 105 106 107 108 |
# File 'app/models/katello/glue/candlepin/consumer.rb', line 101 def subscribe(pool, quantity = nil) quantity = quantity.to_i unless quantity.nil? Rails.logger.debug "Subscribing to pool '#{pool}' for : #{name}" Resources::Candlepin::Consumer.consume_entitlement self.uuid, pool, quantity rescue => e Rails.logger.debug e.backtrace.join("\n\t") raise e end |
#to_json(options = {}) ⇒ Object
118 119 120 121 |
# File 'app/models/katello/glue/candlepin/consumer.rb', line 118 def to_json( = {}) super(.merge(:methods => [:href, :facts, :idCert, :owner, :autoheal, :release, :releaseVer, :checkin_time, :installedProducts, :capabilities])) end |
#virtual_guest ⇒ Object
190 191 192 |
# File 'app/models/katello/glue/candlepin/consumer.rb', line 190 def virtual_guest ::Foreman::Cast.to_bool(facts["virt.is_guest"]) end |
#virtual_guest=(val) ⇒ Object
194 195 196 |
# File 'app/models/katello/glue/candlepin/consumer.rb', line 194 def virtual_guest=(val) facts["virt.is_guest"] = val if @facts end |