def guest_registered(self, host_sid, guest_sid): host_system_slots = server_lib.check_entitlement(host_sid) host_system_slots = list(host_system_slots.keys()) try: host_system_slots.remove("virtualization_host") except ValueError: pass guest_system_slots = server_lib.check_entitlement(guest_sid) guest_system_slots = list(guest_system_slots.keys()) for entitlement in host_system_slots: if entitlement not in guest_system_slots: try: rhnSQL.transaction(entitlement) procedure.rhn_entitlements.entitle_server(guest_sid, entitlement) except rhnSQL.SQLError: e = sys.exc_info()[1] rhnSQL.rollback(entitlement) log_error("Error adding entitlement %s to host ID-%s: %s" % (entitlement, guest_sid, str(e))) # rhnSQL.rollback() return
def test_statement_prepare_error(self): rhnSQL.transaction("test_statement_prepare_error") query = "aaa bbb ccc" cursor = rhnSQL.prepare(query) self.assertRaises(rhnSQL.SQLStatementPrepareError, cursor.execute) rhnSQL.rollback("test_statement_prepare_error")
def test_statement_prepare_error(): rhnSQL.transaction("test_statement_prepare_error") query = "aaa bbb ccc" cursor = rhnSQL.prepare(query) with pytest.raises(sql_base.SQLError): cursor.execute() rhnSQL.rollback("test_statement_prepare_error")
def test_statement_prepare_error(self): rhnSQL.transaction("test_statement_prepare_error") query = "aaa bbb ccc" cursor = rhnSQL.prepare(query) self.assertRaises(rhnSQL.SQLError, cursor.execute) rhnSQL.rollback("test_statement_prepare_error")
def check_token_limits(server_id, tokens_obj): assert(isinstance(tokens_obj, ActivationTokens)) rhnSQL.transaction("check_token_limits") for token in tokens_obj.tokens: try: _check_token_limits(server_id, token) except: log_debug(4, "Rolling back transaction") rhnSQL.rollback("check_token_limits") raise return 0
def check_token_limits(server_id, tokens_obj): """ check the token registration limits """ # XXX: would be nice to have those done with triggers in the database # land... assert(isinstance(tokens_obj, ActivationTokens)) rhnSQL.transaction("check_token_limits") for token in tokens_obj.tokens: try: _check_token_limits(server_id, token) except: log_debug(4, "Rolling back transaction") rhnSQL.rollback("check_token_limits") raise return 0
def save(self, commit=1, channel=None): log_debug(3) # attempt to preserve pending changes before we were called, # so we set up our own transaction checkpoint rhnSQL.transaction("save_server") try: self.__save(channel) except: # roll back to what we have before and raise again rhnSQL.rollback("save_server") # shoot the exception up the chain raise else: # if we want to commit, commit all pending changes if commit: rhnSQL.commit() try: search = SearchNotify() search.notify() except Exception, e: log_error("Exception caught from SearchNotify.notify().", e)
def change_base_channel(self, new_rel): log_debug(3, self.server["id"], new_rel) old_rel = self.server["release"] current_channels = rhnChannel.channels_for_server(self.server["id"]) # Extract the base channel off of old_base = [x for x in current_channels if not x['parent_channel']] # Quick sanity check base_channels_count = len(old_base) if base_channels_count == 1: old_base = old_base[0] elif base_channels_count == 0: old_base = None else: raise rhnException("Server %s subscribed to multiple base channels" % (self.server["id"], )) # bz 442355 # Leave custom base channels alone, don't alter any of the channel subscriptions if not CFG.RESET_BASE_CHANNEL and old_base and rhnChannel.isCustomChannel(old_base["id"]): log_debug(3, "Custom base channel detected, will not alter channel subscriptions") self.server["release"] = new_rel self.server.save() msg = """The Red Hat Satellite Update Agent has detected a change in the base version of the operating system running on your system, additionaly you are subscribed to a custom channel as your base channel. Due to this configuration your channel subscriptions will not be altered. """ self.add_history("Updated system release from %s to %s" % ( old_rel, new_rel), msg) self.save_history_byid(self.server["id"]) return 1 s = rhnChannel.LiteServer().init_from_server(self) s.release = new_rel s.arch = self.archname # Let get_server_channels deal with the errors and raise rhnFault target_channels = rhnChannel.guess_channels_for_server(s, none_ok=True) if target_channels: target_base = filter(lambda x: not x['parent_channel'], target_channels)[0] else: target_base = None channels_to_subscribe = [] channels_to_unsubscribe = [] if old_base and target_base and old_base['id'] == target_base['id']: # Same base channel. Preserve the currently subscribed child # channels, just add the ones that are missing hash = {} for c in current_channels: hash[c['id']] = c for c in target_channels: channel_id = c['id'] if channel_id in hash: # Already subscribed to this one del hash[channel_id] continue # Have to subscribe to this one channels_to_subscribe.append(c) # We don't want to lose subscriptions to prior channels, so don't # do anything with hash.values() else: # Different base channel channels_to_unsubscribe = current_channels channels_to_subscribe = target_channels rhnSQL.transaction("change_base_channel") self.server["release"] = new_rel self.server.save() if not (channels_to_subscribe or channels_to_unsubscribe): # Nothing to do, just add the history entry self.add_history("Updated system release from %s to %s" % ( old_rel, new_rel)) self.save_history_byid(self.server["id"]) return 1 # XXX: need a way to preserve existing subscriptions to # families so we can restore access to non-public ones. rhnChannel.unsubscribe_channels(self.server["id"], channels_to_unsubscribe) rhnChannel.subscribe_channels(self.server["id"], channels_to_subscribe) # now that we changed, recompute the errata cache for this one rhnSQL.Procedure("queue_server")(self.server["id"]) # Make a history note sub_channels = rhnChannel.channels_for_server(self.server["id"]) if sub_channels: channel_list = [a["name"] for a in sub_channels] msg = """The Red Hat Satellite Update Agent has detected a change in the base version of the operating system running on your system and has updated your channel subscriptions to reflect that. Your server has been automatically subscribed to the following channels:\n%s\n""" % (string.join(channel_list, "\n"),) else: msg = """*** ERROR: *** While trying to subscribe this server to software channels: There are no channels serving release %s""" % new_rel self.add_history("Updated system release from %s to %s" % ( old_rel, new_rel), msg) self.save_history_byid(self.server["id"]) return 1
def change_base_channel(self, new_rel, suse_products=None): log_debug(3, self.server["id"], new_rel) old_rel = self.server["release"] current_channels = rhnChannel.channels_for_server(self.server["id"]) # Extract the base channel off of old_base = [x for x in current_channels if not x['parent_channel']] # Quick sanity check base_channels_count = len(old_base) if base_channels_count == 1: old_base = old_base[0] elif base_channels_count == 0: old_base = None else: raise rhnException( "Server %s subscribed to multiple base channels" % (self.server["id"], )) # bz 442355 # Leave custom base channels alone, don't alter any of the channel subscriptions if not CFG.RESET_BASE_CHANNEL and old_base and rhnChannel.isCustomChannel( old_base["id"]): log_debug( 3, "Custom base channel detected, will not alter channel subscriptions" ) self.server["release"] = new_rel self.server.save() msg = """The SUSE Manager Update Agent has detected a change in the base version of the operating system running on your system, additionally you are subscribed to a custom channel as your base channel. Due to this configuration your channel subscriptions will not be altered. """ self.add_history( "Updated system release from %s to %s" % (old_rel, new_rel), msg) self.save_history_byid(self.server["id"]) return 1 s = rhnChannel.LiteServer().init_from_server(self) s.release = new_rel s.arch = self.archname if suse_products: s.suse_products = suse_products # Let get_server_channels deal with the errors and raise rhnFault target_channels = rhnChannel.guess_channels_for_server(s, none_ok=True) if target_channels: target_base = [ x for x in target_channels if not x['parent_channel'] ][0] else: target_base = None channels_to_subscribe = [] channels_to_unsubscribe = [] if old_base and target_base and old_base['id'] == target_base['id']: # Same base channel. Preserve the currently subscribed child # channels, just add the ones that are missing hash = {} for c in current_channels: hash[c['id']] = c for c in target_channels: channel_id = c['id'] if channel_id in hash: # Already subscribed to this one del hash[channel_id] continue # Have to subscribe to this one channels_to_subscribe.append(c) # We don't want to lose subscriptions to prior channels, so don't # do anything with hash.values() else: # Different base channel channels_to_unsubscribe = current_channels channels_to_subscribe = target_channels rhnSQL.transaction("change_base_channel") self.server["release"] = new_rel self.server.save() if not (channels_to_subscribe or channels_to_unsubscribe): # Nothing to do, just add the history entry self.add_history("Updated system release from %s to %s" % (old_rel, new_rel)) self.save_history_byid(self.server["id"]) return 1 # XXX: need a way to preserve existing subscriptions to # families so we can restore access to non-public ones. rhnChannel.unsubscribe_channels(self.server["id"], channels_to_unsubscribe) rhnChannel.subscribe_channels(self.server["id"], channels_to_subscribe) # now that we changed, recompute the errata cache for this one rhnSQL.Procedure("queue_server")(self.server["id"]) # Make a history note sub_channels = rhnChannel.channels_for_server(self.server["id"]) if sub_channels: channel_list = [a["name"] for a in sub_channels] msg = """The SUSE Manager Update Agent has detected a change in the base version of the operating system running on your system and has updated your channel subscriptions to reflect that. Your server has been automatically subscribed to the following channels:\n%s\n""" % (string.join(channel_list, "\n"), ) else: msg = """*** ERROR: *** While trying to subscribe this server to software channels: There are no channels serving release %s""" % new_rel self.add_history( "Updated system release from %s to %s" % (old_rel, new_rel), msg) self.save_history_byid(self.server["id"]) return 1
def setUp(self): misc_functions.setup_db_connection() self.__transaction_name = 'eula_test_%d' % (int(time.time())) rhnSQL.transaction(self.__transaction_name)
def channelCreateTransaction(self, username, password, transaction_name): log_debug(3) self._auth(username, password) rhnSQL.transaction(transaction_name)