def _entitle(self, entitlement): system_entitlements = server_lib.check_entitlement(self.server["id"]) system_entitlements = list(system_entitlements.keys()) if entitlement not in system_entitlements: entitle_server = rhnSQL.Procedure("rhn_entitlements.entitle_server") entitle_server(self.server['id'], entitlement)
def _entitle(self, entitlement): system_entitlements = server_lib.check_entitlement(self.server["id"]) system_entitlements = system_entitlements.keys() if entitlement not in system_entitlements: entitle_server = rhnSQL.Procedure("rhn_entitlements.entitle_server") entitle_server(self.server['id'], entitlement)
def entitle(self, server_id, history, virt_type=None): """ Entitle a server according to the entitlements we have configured. """ log_debug(3, self.entitlements) # check for bootstrap_entitled and unentitle first cur = check_entitlement(server_id, True) if cur and 'bootstrap_entitled' in cur: remove_ent = rhnSQL.Procedure( "rhn_entitlements.remove_server_entitlement") remove_ent(server_id, "bootstrap_entitled") entitle_server = rhnSQL.Procedure("rhn_entitlements.entitle_server") # TODO: entitle_server calls can_entitle_server, so we're doing this # twice for each successful call. Is it necessary for external error # handling or can we ditch it? can_entitle_server = rhnSQL.Function( "rhn_entitlements.can_entitle_server", rhnSQL.types.NUMBER()) can_ent = None history["entitlement"] = "" for entitlement in self.entitlements: if virt_type is not None and entitlement[0] == VIRT_ENT_LABEL: continue try: can_ent = can_entitle_server(server_id, entitlement[0]) except rhnSQL.SQLSchemaError: e = sys.exc_info()[1] can_ent = 0 try: # bugzilla #160077, skip attempting to entitle if we cant if can_ent: entitle_server(server_id, entitlement[0]) except rhnSQL.SQLSchemaError: e = sys.exc_info()[1] log_error("Token failed to entitle server", server_id, self.get_names(), entitlement[0], e.errmsg) #No idea what error may be here... raise_with_tb(rhnFault(90, e.errmsg), sys.exc_info()[2]) except rhnSQL.SQLError: e = sys.exc_info()[1] log_error("Token failed to entitle server", server_id, self.get_names(), entitlement[0], e.args) raise_with_tb(rhnFault(90, str(e)), sys.exc_info()[2]) else: history[ "entitlement"] = "Entitled as a %s member" % entitlement[1]
def entitle(self, server_id, history, virt_type=None): """ Entitle a server according to the entitlements we have configured. """ log_debug(3, self.entitlements) # check for bootstrap_entitled and unentitle first cur = check_entitlement(server_id, True) if cur and 'bootstrap_entitled' in cur: remove_ent = rhnSQL.Procedure("rhn_entitlements.remove_server_entitlement") remove_ent(server_id, "bootstrap_entitled") entitle_server = rhnSQL.Procedure("rhn_entitlements.entitle_server") # TODO: entitle_server calls can_entitle_server, so we're doing this # twice for each successful call. Is it necessary for external error # handling or can we ditch it? can_entitle_server = rhnSQL.Function( "rhn_entitlements.can_entitle_server", rhnSQL.types.NUMBER()) can_ent = None history["entitlement"] = "" for entitlement in self.entitlements: if virt_type is not None and entitlement[0] == VIRT_ENT_LABEL: continue try: can_ent = can_entitle_server(server_id, entitlement[0]) except rhnSQL.SQLSchemaError: e = sys.exc_info()[1] can_ent = 0 try: # bugzilla #160077, skip attempting to entitle if we cant if can_ent: entitle_server(server_id, entitlement[0]) except rhnSQL.SQLSchemaError: e = sys.exc_info()[1] log_error("Token failed to entitle server", server_id, self.get_names(), entitlement[0], e.errmsg) #No idea what error may be here... raise_with_tb(rhnFault(90, e.errmsg), sys.exc_info()[2]) except rhnSQL.SQLError: e = sys.exc_info()[1] log_error("Token failed to entitle server", server_id, self.get_names(), entitlement[0], e.args) raise_with_tb(rhnFault(90, str(e)), sys.exc_info()[2]) else: history["entitlement"] = "Entitled as a %s member" % entitlement[1]
def check_entitlement(self): if not self.server.has_key("id"): return None log_debug(3, self.server["id"]) return server_lib.check_entitlement(self.server['id'])
def save_packages_byid(self, sysid, schedule=1): """ save the package list """ log_debug(3, sysid, "Errata cache to run:", schedule, "Changed:", self.__changed, "%d total packages" % len(self.__p)) if not self.__changed: return 0 commits = 0 # get rid of the deleted packages dlist = [a for a in list(self.__p.values()) if a.real and a.status in (DELETED, UPDATED)] if dlist: log_debug(4, sysid, len(dlist), "deleted packages") h = rhnSQL.prepare(""" delete from rhnServerPackage where server_id = :sysid and name_id = :name_id and evr_id = :evr_id and ((:package_arch_id is null and package_arch_id is null) or package_arch_id = :package_arch_id) """) h.execute_bulk({ 'sysid': [sysid] * len(dlist), 'name_id': [a.name_id for a in dlist], 'evr_id': [a.evr_id for a in dlist], 'package_arch_id': [a.package_arch_id for a in dlist], }) commits = commits + len(dlist) del dlist # And now add packages alist = [a for a in list(self.__p.values()) if a.status in (ADDED, UPDATED)] if alist: log_debug(4, sysid, len(alist), "added packages") h = rhnSQL.prepare(""" insert into rhnServerPackage (server_id, name_id, evr_id, package_arch_id, installtime) values (:sysid, LOOKUP_PACKAGE_NAME(:n), LOOKUP_EVR(:e, :v, :r), LOOKUP_PACKAGE_ARCH(:a), TO_TIMESTAMP(:instime, 'YYYY-MM-DD HH24:MI:SS') ) """) # some fields are not allowed to contain empty string (varchar) def lambdaae(a): if a.e == '': return None else: return a.e package_data = { 'sysid': [sysid] * len(alist), 'n': [a.n for a in alist], 'v': [a.v for a in alist], 'r': [a.r for a in alist], 'e': list(map(lambdaae, alist)), 'a': [a.a for a in alist], 'instime': [self.__expand_installtime(a.installtime) for a in alist], } try: h.execute_bulk(package_data) rhnSQL.commit() except rhnSQL.SQLSchemaError: e = sys.exc_info()[1] # LOOKUP_PACKAGE_ARCH failed if e.errno == 20243: log_debug(2, "Unknown package arch found", e) raise_with_tb(rhnFault(45, "Unknown package arch found"), sys.exc_info()[2]) commits = commits + len(alist) del alist if schedule: # queue this server for an errata update update_errata_cache(sysid) # if provisioning box, and there was an actual delta, snapshot ents = check_entitlement(sysid) if commits and "enterprise_entitled" in ents: snapshot_server(sysid, "Package profile changed") # Our new state does not reflect what's on the database anymore self.__loaded = 0 self.__changed = 0 return 0
def save_packages_byid(self, sysid, schedule=1): """ save the package list """ log_debug(3, sysid, "Errata cache to run:", schedule, "Changed:", self.__changed, "%d total packages" % len(self.__p)) if not self.__changed: return 0 commits = 0 # get rid of the deleted packages dlist = [ a for a in list(self.__p.values()) if a.real and a.status in (DELETED, UPDATED) ] if dlist: log_debug(4, sysid, len(dlist), "deleted packages") h = rhnSQL.prepare(""" delete from rhnServerPackage where server_id = :sysid and name_id = :name_id and evr_id = :evr_id and ((:package_arch_id is null and package_arch_id is null) or package_arch_id = :package_arch_id) """) h.execute_bulk({ 'sysid': [sysid] * len(dlist), 'name_id': [a.name_id for a in dlist], 'evr_id': [a.evr_id for a in dlist], 'package_arch_id': [a.package_arch_id for a in dlist], }) commits = commits + len(dlist) del dlist # And now add packages alist = [ a for a in list(self.__p.values()) if a.status in (ADDED, UPDATED) ] if alist: log_debug(4, sysid, len(alist), "added packages") h = rhnSQL.prepare(""" insert into rhnServerPackage (server_id, name_id, evr_id, package_arch_id, installtime) values (:sysid, LOOKUP_PACKAGE_NAME(:n), LOOKUP_EVR(:e, :v, :r), LOOKUP_PACKAGE_ARCH(:a), TO_TIMESTAMP(:instime, 'YYYY-MM-DD HH24:MI:SS') ) """) # some fields are not allowed to contain empty string (varchar) def lambdaae(a): if a.e == '': return None else: return a.e package_data = { 'sysid': [sysid] * len(alist), 'n': [a.n for a in alist], 'v': [a.v for a in alist], 'r': [a.r for a in alist], 'e': list(map(lambdaae, alist)), 'a': [a.a for a in alist], 'instime': [self.__expand_installtime(a.installtime) for a in alist], } try: h.execute_bulk(package_data) rhnSQL.commit() except rhnSQL.SQLSchemaError: e = sys.exc_info()[1] # LOOKUP_PACKAGE_ARCH failed if e.errno == 20243: log_debug(2, "Unknown package arch found", e) raise_with_tb(rhnFault(45, "Unknown package arch found"), sys.exc_info()[2]) commits = commits + len(alist) del alist if schedule: # queue this server for an errata update update_errata_cache(sysid) # if provisioning box, and there was an actual delta, snapshot ents = check_entitlement(sysid) if commits and "enterprise_entitled" in ents: snapshot_server(sysid, "Package profile changed") # Our new state does not reflect what's on the database anymore self.__loaded = 0 self.__changed = 0 return 0
class Packages: def __init__(self): self.__p = {} # Have we loaded the packages or not? self.__loaded = 0 self.__changed = 0 def add_package(self, sysid, entry): log_debug(4, sysid, entry) p = dbPackage(entry) if p is None: # Not a valid package spec return -1 if not self.__loaded: self.reload_packages_byid(sysid) if self.__p.has_key(p.nvrea): if self.__p[p.nvrea].installtime != p.installtime: self.__p[p.nvrea].installtime = p.installtime self.__p[p.nvrea].status = UPDATED else: self.__p[p.nvrea].add() self.__changed = 1 return 0 self.__p[p.nvrea] = p self.__changed = 1 return 0 def delete_package(self, sysid, entry): """ delete a package from the list """ log_debug(4, sysid, entry) p = dbPackage(entry) if p is None: # Not a valid package spec return -1 if not self.__loaded: self.reload_packages_byid(sysid) if self.__p.has_key(p.nvrea): log_debug(4, " Package deleted") self.__p[p.nvrea].delete() self.__changed = 1 # deletion is always successfull return 0 def dispose_packages(self, sysid): """ delete all packages and get an empty package list """ log_debug(4, sysid) if not self.__loaded: self.reload_packages_byid(sysid) for k in self.__p.keys(): self.__p[k].delete() self.__changed = 1 return 0 def get_packages(self): """ produce a list of packages """ return map(lambda a: a.nvrea, filter(lambda a: a.status != DELETED, self.__p.values())) def __expand_installtime(self, installtime): """ Simulating the ternary operator, one liner is ugly """ if installtime: return time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(installtime)) else: return None def save_packages_byid(self, sysid, schedule=1): """ save the package list """ log_debug(3, sysid, "Errata cache to run:", schedule, "Changed:", self.__changed, "%d total packages" % len(self.__p)) if not self.__changed: return 0 commits = 0 # get rid of the deleted packages dlist = filter(lambda a: a.real and a.status in (DELETED, UPDATED), self.__p.values()) if dlist: log_debug(4, sysid, len(dlist), "deleted packages") h = rhnSQL.prepare(""" delete from rhnServerPackage where server_id = :sysid and name_id = :name_id and evr_id = :evr_id and ((:package_arch_id is null and package_arch_id is null) or package_arch_id = :package_arch_id) """) h.execute_bulk({ 'sysid': [sysid] * len(dlist), 'name_id': map(lambda a: a.name_id, dlist), 'evr_id': map(lambda a: a.evr_id, dlist), 'package_arch_id': map(lambda a: a.package_arch_id, dlist), }) commits = commits + len(dlist) del dlist # And now add packages alist = filter(lambda a: a.status in (ADDED, UPDATED), self.__p.values()) if alist: log_debug(4, sysid, len(alist), "added packages") h = rhnSQL.prepare(""" insert into rhnServerPackage (server_id, name_id, evr_id, package_arch_id, installtime) values (:sysid, LOOKUP_PACKAGE_NAME(:n), LOOKUP_EVR(:e, :v, :r), LOOKUP_PACKAGE_ARCH(:a), TO_TIMESTAMP(:instime, 'YYYY-MM-DD HH24:MI:SS') ) """) # some fields are not allowed to contain empty string (varchar) def lambdaae(a): if a.e == '': return None else: return a.e package_data = { 'sysid': [sysid] * len(alist), 'n': map(lambda a: a.n, alist), 'v': map(lambda a: a.v, alist), 'r': map(lambda a: a.r, alist), 'e': map(lambdaae, alist), 'a': map(lambda a: a.a, alist), 'instime': map(lambda a: self.__expand_installtime(a.installtime), alist), } try: h.execute_bulk(package_data) rhnSQL.commit() except rhnSQL.SQLSchemaError, e: # LOOKUP_PACKAGE_ARCH failed if e.errno == 20243: log_debug(2, "Unknown package arch found", e) raise rhnFault( 45, "Unknown package arch found"), None, sys.exc_info()[2] commits = commits + len(alist) del alist if schedule: # queue this server for an errata update update_errata_cache(sysid) # if provisioning box, and there was an actual delta, snapshot ents = check_entitlement(sysid) if commits and ents.has_key("enterprise_entitled"): snapshot_server(sysid, "Package profile changed") # Our new state does not reflect what's on the database anymore self.__loaded = 0 self.__changed = 0 return 0