def _do_update(self): uuid = ConsumerIdentity.read().getConsumerId() consumer = self.uep.getConsumer(uuid) if 'autoheal' in consumer and consumer['autoheal']: try: log.info("Checking if system requires healing.") today = datetime.now(GMT()) tomorrow = today + timedelta(days=1) # Check if we're not valid today and heal if so. If not # we'll do the same check for tomorrow to hopefully always keep # us valid: ent_dir = EntitlementDirectory() cs = cert_sorter.CertSorter(self._product_dir, ent_dir, self.facts_dict, on_date=today) cert_updater = CertLib(uep=self.uep) if not cs.is_valid(): log.warn("Found invalid entitlements for today: %s" % today) self.uep.bind(uuid, today) cert_updater.update() else: log.info("Entitlements are valid for today: %s" % today) cs = cert_sorter.CertSorter(self._product_dir, ent_dir, self.facts_dict, on_date=tomorrow) if not cs.is_valid(): log.warn( "Found invalid entitlements for tomorrow: %s" % tomorrow) self.uep.bind(uuid, tomorrow) cert_updater.update() else: log.info("Entitlements are valid for tomorrow: %s" % tomorrow) except Exception, e: log.error("Error attempting to auto-heal:") log.exception(e) return 0 else: log.info("Auto-heal check complete.") return 1
def polling_worker(server): """ Thread worker using periodical polling of directories with certificates. This is used only in fallback mode, when it is not possible to use pyinotify or using of inotify was disabled in rhsm.conf. :param server: Reference to instance of Server :return: None """ cs = cert_sorter.CertSorter() while server.terminate_loop is False: cs.force_cert_check() time.sleep(2.0)
def _get_validity_facts(self, facts_dict): validity_facts = {'system.entitlements_valid': 'valid'} if not ClassicCheck().is_registered_with_classic(): sorter = cert_sorter.CertSorter(self.product_dir, self.entitlement_dir, facts_dict) if (len(sorter.partially_valid_products) > 0) or \ (len(sorter.partial_stacks) > 0): validity_facts['system.entitlements_valid'] = 'partial' if ((len(sorter.expired_products) + len(sorter.unentitled_products)) > 0): validity_facts['system.entitlements_valid'] = 'invalid' return validity_facts
def __init__(self, backend, consumer, facts, tab_icon, parent, ent_dir=None, prod_dir=None): widgets = [ 'product_text', 'product_id_text', 'validity_text', 'subscription_text', 'subscription_status_label', 'update_certificates_button', 'register_button' ] super(InstalledProductsTab, self).__init__('installed.glade', widgets) self.tab_icon = tab_icon self.product_dir = prod_dir or ProductDirectory() self.entitlement_dir = ent_dir or EntitlementDirectory() self.facts = facts self.cs = cert_sorter.CertSorter(prod_dir, ent_dir, self.facts.get_facts()) # Product column text_renderer = gtk.CellRendererText() image_renderer = gtk.CellRendererPixbuf() column = gtk.TreeViewColumn(_('Product'), image_renderer, pixbuf=self.store['image']) column.set_expand(True) column.pack_end(text_renderer, True) column.add_attribute(text_renderer, 'text', self.store['product']) column.add_attribute(text_renderer, 'cell-background', self.store['background']) self.top_view.append_column(column) cols = [] cols.append((column, 'text', 'product')) column = self.add_text_column(_('Version'), 'version') cols.append((column, 'text', 'version')) column = self.add_text_column(_('Arch'), 'arch') column.set_alignment(0.5) cols.append((column, 'text', 'arch')) column = self.add_text_column(_('Status'), 'status') cols.append((column, 'text', 'status')) column = self.add_date_column(_('Start Date'), 'start_date') cols.append((column, 'date', 'start_date')) column = self.add_date_column(_('End Date'), 'expiration_date') cols.append((column, 'date', 'expiration_date')) self.set_sorts(cols) self.glade.signal_autoconnect({ "on_update_certificates_button_clicked": parent._update_certificates_button_clicked, }) self.glade.signal_autoconnect({ "on_register_button_clicked": parent._register_item_clicked, }) self.update_products() # Monitor entitlements/products for additions/deletions def on_cert_change(filemonitor): self.update_products() self._set_validity_status() backend.monitor_certs(on_cert_change)
def update_products(self): self.store.clear() self.cs = cert_sorter.CertSorter(self.product_dir, self.entitlement_dir, self.facts.get_facts()) for product_cert in self.product_dir.list(): for product in product_cert.getProducts(): product_id = product.getHash() status = self.cs.get_status(product_id) entry = {} entry['product'] = product.getName() entry['version'] = product.getVersion() entry['arch'] = product.getArch() entry['product_id'] = product_id # Common properties entry['align'] = 0.5 # TODO: Pull this date logic out into a separate lib! # This is also used in mysubstab... if status != NOT_SUBSCRIBED: range_calculator = ValidProductDateRangeCalculator(self.cs) compliant_range = range_calculator.calculate( product.getHash()) start = '' end = '' if compliant_range: start = compliant_range.begin() end = compliant_range.end() contract_ids, sub_names = self._calc_subs_providing( product_id, compliant_range) name = ", ".join(sub_names) contract = ", ".join(contract_ids) entry['subscription'] = name entry['start_date'] = start entry['expiration_date'] = end if status == FUTURE_SUBSCRIBED: entry['image'] = self._render_icon('red') entry['status'] = _('Future Subscription') entry['validity_note'] = _("Future Subscribed") elif status == EXPIRED: entry['image'] = self._render_icon('red') entry['status'] = _('Expired') sub_numbers = set([]) for ent_cert in self.cs.get_entitlements_for_product( product_id): order = ent_cert.getOrder() # FIXME: getSubscription() seems to always be None...? if order.getSubscription(): sub_numbers.add(order.getSubscription()) subs_str = ', '.join(sub_numbers) entry['validity_note'] = \ _('Subscription %s is expired') % subs_str elif status == PARTIALLY_SUBSCRIBED: entry['image'] = self._render_icon('yellow') entry['status'] = _('Partially Subscribed') entry['validity_note'] = _("Partially Subscribed") else: entry['image'] = self._render_icon('green') entry['status'] = _('Subscribed') entry['validity_note'] = \ _('Covered by contract(s) %s through %s') % \ (contract, managerlib.formatDate(entry['expiration_date'])) else: entry['image'] = self._render_icon('red') entry['status'] = _('Not Subscribed') entry['validity_note'] = _("Not Subscribed") self.store.add_map(entry) # 811340: Select the first product in My Installed Software # table by default. selection = self.top_view.get_selection() selection.select_path(0)
def find_first_invalid_date(ent_dir=None, product_dir=None, facts_dict=None): """ Find the first date when the system is invalid at midnight GMT. WARNING: This method does *not* return the exact first datetime when we're invalid. Due to it's uses in the GUI it needs to return a datetime into the first day of being completely invalid, so the subscription assistant can search for that time and find expired products. If there are no products installed, return None, as there technically is no first invalid date. """ if not ent_dir: ent_dir = EntitlementDirectory() if not product_dir: product_dir = ProductDirectory() if facts_dict is None: facts_dict = Facts().get_facts() current_date = datetime.now(GMT()) if not product_dir.list(): # If there are no products installed, return None, there is no # invalid date: log.debug("Unable to determine first invalid date, no products " "installed.") return None # change _scan_entitlement_certs to take product lists, # run it for the future to figure this out # First check if we have anything installed but not entitled *today*: cs = cert_sorter.CertSorter(product_dir, ent_dir, facts_dict, on_date=current_date) if not cs.is_valid(): log.debug("Found unentitled products or partial stacks.") return current_date # Sort all the ent certs by end date. (ascending) all_ent_certs = ent_dir.list() def get_date(ent_cert): return ent_cert.validRange().end() all_ent_certs.sort(key=get_date) # Loop through all current and future entitlement certs, check validity # status on their end date, and return the first date where we're not # valid. for ent_cert in all_ent_certs: # Adding a timedelta of one day here so we can be sure we get a date # the subscription assitant (which does not use time) can use to search # for. end_date = ent_cert.validRange().end() + timedelta(days=1) if end_date < current_date: # This cert is expired, ignore it: continue log.debug("Checking cert: %s, end date: %s" % (ent_cert.serialNumber(), end_date)) # new cert_sort stuff, use _scan_for_entitled_products, since # we just need to know if stuff is expired cs = cert_sorter.CertSorter(product_dir, ent_dir, facts_dict, on_date=end_date) if not cs.is_valid(): log.debug("Found non-valid status on %s" % end_date) return end_date else: log.debug("Valid status on %s" % end_date) # Should never hit this: raise Exception("Unable to determine first invalid date.")