def take_snapshot(self, reason): return server_lib.snapshot_server(self.server['id'], reason)
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