def doCommand(self, base, basecmd, extcmds): opts = base.plugins.cmdline[0] pkgs = base.rpmdb product_dir = ProductDirectory() product = {} for p in product_dir.list(): for p in p.products: product[p.id] = p.name product_db = ProductDatabase() product_db.read() # convert IDs to names in the mapping product_repo_mapping = dict((product[k], v) for k, v in product_db.content.iteritems()) for product in extcmds: for ipkg in sorted(pkgs): if "from_repo" in ipkg.yumdb_info and ipkg.yumdb_info.from_repo in product_repo_mapping.get(product): # add the package to the erasure transaction base.remove(ipkg) if len(base.tsInfo) == 0: return 0, ["No packages found for selected products"] if base.doTransaction() == 0: return 0, ["Removed packages for selected products"] else: return 0, ["Error occured while removing packages. Please see yum.log for more details."]
def test_get_installed_products(self, MockExists): MockExists.return_value = True pd = ProductDirectory() top_product = StubProduct("top") provided_products = [StubProduct("provided")] pd.list = lambda: [StubProductCertificate(top_product, provided_products)] installed_products = pd.get_installed_products() self.assertTrue("top" in installed_products)
def __init__(self, product_dir=None): if not product_dir: product_dir = ProductDirectory() self.installed = {} for prod_cert in product_dir.list(): prod = prod_cert.getProduct() self.installed[prod.getHash()] = prod.getName()
def __init__(self, product_dir=None): if not product_dir: product_dir = ProductDirectory() self.installed = {} for prod_cert in product_dir.list(): prod = prod_cert.products[0] self.installed[prod.id] = prod.name
def test_default_products_matching_ids(self, MockExists): MockExists.return_value = True pd = ProductDirectory() top_product = StubProduct("top") default_product = StubProduct("top") pd.installed_prod_dir.list = lambda: [StubProductCertificate(top_product, [])] pd.default_prod_dir.list = lambda: [StubProductCertificate(default_product, [])] results = pd.list() self.assertEquals(1, len(results)) resulting_ids = [cert.products[0].id for cert in results] self.assertTrue("top" in resulting_ids)
class ProductManager: REPO = 'from_repo' PRODUCTID = 'productid' def __init__(self): self.pdir = ProductDirectory() self.db = ProductDatabase() self.db.read() self.meta_data_errors = [] def update(self, yb): if yb is None: yb = yum.YumBase() enabled = self.getEnabled(yb) active = self.getActive(yb) #only execute this on versions of yum that track #which repo a package came from if yum.__version_info__[2] >= 28: # check that we have any repo's enabled # and that we have some enabled repo's. Not just # that we have packages from repo's that are # not active. See #806457 if enabled and active: self.updateRemoved(active) self.updateInstalled(enabled, active) def _isWorkstation(self, product): if product.getName() == "Red Hat Enterprise Linux Workstation" and \ "rhel-5-client-workstation" in product.getProvidedTags() and \ product.getVersion()[0] == '5': return True return False def _isDesktop(self, product): if product.getName() == "Red Hat Enterprise Linux Desktop" and \ "rhel-5-client" in product.getProvidedTags() and \ product.getVersion()[0] == '5': return True return False def updateInstalled(self, enabled, active): for cert, repo in enabled: #nothing from this repo is installed if repo not in active: continue p = cert.getProduct() prod_hash = p.getHash() # Are we installing Workstation cert? if self._isWorkstation(p): # is the Desktop product cert installed? for pc in self.pdir.list(): if self._isDesktop(pc.getProduct()): # Desktop product cert is installed, # delete the Desktop product cert pc.delete() self.db.delete(prod_hash) self.db.write() # no point installing desktop only to delete it if self._isDesktop(p): for pc in self.pdir.list(): if self._isWorkstation(pc.getProduct()): # we are installing Desktop, but we already have workstation return if self.pdir.findByProduct(prod_hash): continue fn = '%s.pem' % prod_hash path = self.pdir.abspath(fn) cert.write(path) self.db.add(prod_hash, repo) self.db.write() # We should only delete productcerts if there are no # packages from that repo installed (not "active") # and we have the product cert installed. def updateRemoved(self, active): for cert in self.pdir.list(): p = cert.getProduct() prod_hash = p.getHash() repo = self.db.findRepo(prod_hash) # if we had errors with the repo or productid metadata # we could be very confused here, so do not # delete anything. see bz #736424 if repo in self.meta_data_errors: log.info("%s has meta-data errors. Not deleting product cert %s." % (repo, prod_hash)) continue # FIXME: not entirely sure why we do this # to avoid a none on cert.delete surely # but is there another reason? if repo is None: continue if repo in active: continue log.info("product cert %s for %s is being deleted" % (prod_hash, p.getName())) cert.delete() self.db.delete(prod_hash) self.db.write() # find the list of repo's that provide packages that # are actually installed. def getActive(self, yb): active = set() packages = yb.pkgSack.returnPackages() for p in packages: repo = p.repoid # if a pkg is in multiple repo's, this will consider # all the repo's with the pkg "active". db_pkg = yb.rpmdb.searchNevra(name=p.name, arch=p.arch) # that pkg is not actually installed if not db_pkg: continue # yum on 5.7 list everything as "installed" instead # of the repo it came from if repo in (None, "installed"): continue active.add(repo) return active def getEnabled(self, yb): lst = [] enabled = yb.repos.listEnabled() for repo in enabled: try: fn = repo.retrieveMD(self.PRODUCTID) cert = self.__getCert(fn) if cert is None: continue lst.append((cert, repo.id)) except Exception, e: log.warn("Error loading productid metadata for %s." % repo) log.exception(e) self.meta_data_errors.append(repo.id) return lst
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.")
class ProductManager: REPO = 'from_repo' PRODUCTID = 'productid' def __init__(self): self.pdir = ProductDirectory() self.db = ProductDatabase() self.db.read() self.meta_data_errors = [] def update(self, yb): if yb is None: yb = yum.YumBase() enabled = self.getEnabled(yb) active = self.getActive(yb) #only execute this on versions of yum that track #which repo a package came from if yum.__version_info__[2] >= 28: # check that we have any repo's enabled # and that we have some enabled repo's. Not just # that we have packages from repo's that are # not active. See #806457 if enabled and active: self.updateRemoved(active) self.updateInstalled(enabled, active) def _isWorkstation(self, product): if product.getName() == "Red Hat Enterprise Linux Workstation" and \ "rhel-5-client-workstation" in product.getProvidedTags() and \ product.getVersion()[0] == '5': return True return False def _isDesktop(self, product): if product.getName() == "Red Hat Enterprise Linux Desktop" and \ "rhel-5-client" in product.getProvidedTags() and \ product.getVersion()[0] == '5': return True return False def updateInstalled(self, enabled, active): for cert, repo in enabled: #nothing from this repo is installed if repo not in active: continue p = cert.getProduct() prod_hash = p.getHash() # Are we installing Workstation cert? if self._isWorkstation(p): # is the Desktop product cert installed? for pc in self.pdir.list(): if self._isDesktop(pc.getProduct()): # Desktop product cert is installed, # delete the Desktop product cert pc.delete() self.db.delete(prod_hash) self.db.write() # no point installing desktop only to delete it if self._isDesktop(p): for pc in self.pdir.list(): if self._isWorkstation(pc.getProduct()): # we are installing Desktop, but we already have workstation return if self.pdir.findByProduct(prod_hash): continue fn = '%s.pem' % prod_hash path = self.pdir.abspath(fn) cert.write(path) self.db.add(prod_hash, repo) self.db.write() # We should only delete productcerts if there are no # packages from that repo installed (not "active") # and we have the product cert installed. def updateRemoved(self, active): for cert in self.pdir.list(): p = cert.getProduct() prod_hash = p.getHash() repo = self.db.findRepo(prod_hash) # if we had errors with the repo or productid metadata # we could be very confused here, so do not # delete anything. see bz #736424 if repo in self.meta_data_errors: log.info( "%s has meta-data errors. Not deleting product cert %s." % (repo, prod_hash)) continue # FIXME: not entirely sure why we do this # to avoid a none on cert.delete surely # but is there another reason? if repo is None: continue if repo in active: continue log.info("product cert %s for %s is being deleted" % (prod_hash, p.getName())) cert.delete() self.db.delete(prod_hash) self.db.write() # find the list of repo's that provide packages that # are actually installed. def getActive(self, yb): active = set() packages = yb.pkgSack.returnPackages() for p in packages: repo = p.repoid # if a pkg is in multiple repo's, this will consider # all the repo's with the pkg "active". db_pkg = yb.rpmdb.searchNevra(name=p.name, arch=p.arch) # that pkg is not actually installed if not db_pkg: continue # yum on 5.7 list everything as "installed" instead # of the repo it came from if repo in (None, "installed"): continue active.add(repo) return active def getEnabled(self, yb): lst = [] enabled = yb.repos.listEnabled() for repo in enabled: try: fn = repo.retrieveMD(self.PRODUCTID) cert = self.__getCert(fn) if cert is None: continue lst.append((cert, repo.id)) except Exception, e: log.warn("Error loading productid metadata for %s." % repo) log.exception(e) self.meta_data_errors.append(repo.id) return lst