def main(self): parser = OptionParser(option_list=options_table) (self.options, _args) = parser.parse_args() rhnSQL.initDB() self._channels_hash = self._get_channels() package_ids = self._get_packages() if package_ids is None: return 1 if self.options.backup_file: self._backup_packages(package_ids, self.options.backup_file) try: self._add_package_header_values(package_ids) except: rhnSQL.rollback() raise if self.options.commit: print "Commiting work" rhnSQL.commit() else: print "Rolling back" rhnSQL.rollback()
def db_clean(bkp,pid): answer = ask("Did you take a backup of your database?") if not answer: print "Taking a backup now" db_backup(bkp,pid) bkp.save() rhnSQL.initDB() pids=[] queryA = """ delete from rhnchannelpackage where package_id = :pid """ queryB = """ delete from rhnpackage where id = :pid """ answer = ask("Continue with the deletion of the entries?") if not answer : print "leaving..." else: try: cursor = rhnSQL.prepare(queryA) cursor.execute(pid=pid) cursor = rhnSQL.prepare(queryB) cursor.execute(pid=pid) rhnSQL.commit() except: rhnSQL.rollback() raise print "entries deleted"
def delete_guests(server_id): """ Callback used after a successful kickstart to remove any guest virtual instances, as well as their associated servers. """ # First delete all the guest server objects: h = rhnSQL.prepare(_query_lookup_guests_for_host) h.execute(server_id=server_id) delete_server = rhnSQL.Procedure("delete_server") log_debug(4, "Deleting guests") while 1: row = h.fetchone_dict() if not row: break guest_id = row['virtual_system_id'] log_debug(4, 'Deleting guest server: %s' % guest_id) try: if guest_id is not None: delete_server(guest_id) except rhnSQL.SQLError: log_error("Error deleting server: %s" % guest_id) # Finally delete all the virtual instances: log_debug(4, "Deleting all virtual instances for host") h = rhnSQL.prepare(_query_delete_virtual_instances) h.execute(server_id=server_id) # Commit all changes: try: rhnSQL.commit() except rhnSQL.SQLError: e = sys.exc_info()[1] log_error("Error committing transaction: %s" % e) rhnSQL.rollback()
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 updateDist(self, kwargs, username, password): log_debug(3) self._auth(username, password) if not kwargs.get('release'): raise rhnFault(23, "Insufficient data, release missing to update dist", explain=0) if not kwargs.get('os'): kwargs['os'] = 'Red Hat Linux' if kwargs.get('channel_id') is None: # Missing stuff raise rhnFault(23, "Insufficient data, channel_id missing to update dist", explain=0) if kwargs.get('channel_arch_id') is None: # Missing stuff raise rhnFault(23, "Insufficient data, channel arch id missing to update dist", explain=0) try: rhnSQL.execute(""" insert into rhnDistChannelMap (channel_id, channel_arch_id, os, release) values (:channel_id, :channel_arch_id, :os, :release) """, kwargs) except rhnSQL.SQLError, e: rhnSQL.rollback() raise rhnFault(23, str(e.args[1]), explain=0 )
def moveChannelDownloads(self, channel_id, old_channel_family_id, new_channel_family_id, username, password): log_debug(3) self._auth(username, password) if old_channel_family_id is None or \ old_channel_family_id == new_channel_family_id: # Nothing to be done here, same channel family return 0 log_debug(3, " Migrating downloads") try: rhnSQL.execute(""" update rhnDownloads set channel_family_id = :new_channel_family_id where channel_family_id = :old_channel_family_id and id in ( select downloads_id from rhnChannelDownloads where channel_id = :channel_id) """, channel_id=channel_id, old_channel_family_id=old_channel_family_id, new_channel_family_id=new_channel_family_id, ) except rhnSQL.SQLError, e: rhnSQL.rollback() raise rhnFault(23, str(e.args[1]), explain=0 )
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_1(): try: _test_1() except: rhnSQL.rollback() raise rhnSQL.commit()
def _channelPermission(self, label, role, commit, username, password, query): log_debug(3) self._auth(username, password) try: rhnSQL.execute(query, username = username, label = label, role_label = role) except rhnSQL.SQLError, e: rhnSQL.rollback() raise rhnFault(23, str(e.args[1]), explain=0 )
def save(self): if self._token is None: self.generate_token() try: return self._save() except: rhnSQL.rollback() raise
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 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 sync(self): """Trigger a reposync""" start_time = datetime.now() for (repo_id, url, repo_label, channel_family_id) in self.urls: print("") self.print_msg("Repo URL: %s" % url) plugin = None # If the repository uses a uln:// URL, switch to the ULN plugin, overriding the command-line if url.startswith("uln://"): self.repo_plugin = self.load_plugin("uln") # pylint: disable=W0703 try: plugin = self.repo_plugin(url, self.channel_label) if repo_id is not None: keys = rhnSQL.fetchone_dict(""" select k1.key as ca_cert, k2.key as client_cert, k3.key as client_key from rhncontentssl join rhncryptokey k1 on rhncontentssl.ssl_ca_cert_id = k1.id left outer join rhncryptokey k2 on rhncontentssl.ssl_client_cert_id = k2.id left outer join rhncryptokey k3 on rhncontentssl.ssl_client_key_id = k3.id where rhncontentssl.content_source_id = :repo_id or rhncontentssl.channel_family_id = :channel_family_id """, repo_id=int(repo_id), channel_family_id=int(channel_family_id)) if keys and ('ca_cert' in keys): plugin.set_ssl_options(keys['ca_cert'], keys['client_cert'], keys['client_key']) self.import_packages(plugin, repo_id, url) self.import_groups(plugin, url) if not self.no_errata: self.import_updates(plugin, url) # only for repos obtained from the DB if self.sync_kickstart and repo_label: try: self.import_kickstart(plugin, url, repo_label) except: rhnSQL.rollback() raise except Exception: e = sys.exc_info()[1] self.error_msg("ERROR: %s" % e) if plugin is not None: plugin.clear_ssl_cache() if self.regen: taskomatic.add_to_repodata_queue_for_channel_package_subscription( [self.channel_label], [], "server.app.yumreposync") taskomatic.add_to_erratacache_queue(self.channel_label) self.update_date() rhnSQL.commit() total_time = datetime.now() - start_time self.print_msg("Sync completed.") self.print_msg("Total time: %s" % str(total_time).split('.')[0])
def _push_file(self, config_channel_id, file): if not file: # Nothing to do return {} # Check for full path on the file path = file.get('path') if not (path[0] == os.sep): raise ConfigFilePathIncomplete(file) if not file.has_key('config_file_type_id'): log_debug(4, "Client does not support config directories, so set file_type_id to 1") file['config_file_type_id'] = '1' # Check if delimiters are present if self._is_file(file) and \ not (file.get('delim_start') and file.get('delim_end')): # Need delimiters raise ConfigFileMissingDelimError(file) if not (file.get('user') and file.get('group') and file.get('mode')) and not self._is_link(file) : raise ConfigFileMissingInfoError(file) # Oracle doesn't like certain binding variables file['username'] = file.get('user','') file['groupname'] = file.get('group','') file['file_mode'] = file.get('mode','') file['selinux_ctx'] = file.get('selinux_ctx','') result = {} try: if self._is_file(file): self._push_contents(file) elif self._is_link(file): file['symlink'] = file.get('symlink') or '' except ConfigFileTooLargeError: result['file_too_large'] = 1 t = rhnSQL.Table('rhnConfigFileState', 'label') state_id_alive = t['alive']['id'] file['state_id'] = state_id_alive file['config_channel_id'] = config_channel_id try: self._push_config_file(file) self._push_revision(file) except rhnSQL.SQLSchemaError, e: log_debug(4, "schema error", e) rhnSQL.rollback() # blow away the contents that got inserted if e.errno == 20267: # ORA-20267: (not_enough_quota) - Insufficient available quota # for the specified action raise ConfigFileExceedsQuota(file) raise
def sync(self): """Trigger a reposync""" start_time = datetime.now() for (repo_id, url, repo_label) in self.urls: print() self.print_msg("Repo URL: %s" % url) plugin = None # If the repository uses a uln:// URL, switch to the ULN plugin, overriding the command-line if url.startswith("uln://"): self.repo_plugin = self.load_plugin("uln") # pylint: disable=W0703 try: plugin = self.repo_plugin(url, self.channel_label) if repo_id is not None: keys = rhnSQL.fetchone_dict(""" select k1.key as ca_cert, k2.key as client_cert, k3.key as client_key from rhncontentsourcessl join rhncryptokey k1 on rhncontentsourcessl.ssl_ca_cert_id = k1.id left outer join rhncryptokey k2 on rhncontentsourcessl.ssl_client_cert_id = k2.id left outer join rhncryptokey k3 on rhncontentsourcessl.ssl_client_key_id = k3.id where rhncontentsourcessl.content_source_id = :repo_id """, repo_id=int(repo_id)) if keys and ('ca_cert' in keys): plugin.set_ssl_options(keys['ca_cert'], keys['client_cert'], keys['client_key']) self.import_packages(plugin, repo_id, url) self.import_groups(plugin, url) if not self.no_errata: self.import_updates(plugin, url) # only for repos obtained from the DB if self.sync_kickstart and repo_label: try: self.import_kickstart(plugin, url, repo_label) except: rhnSQL.rollback() raise except Exception: e = sys.exc_info()[1] self.error_msg("ERROR: %s" % e) if plugin is not None: plugin.clear_ssl_cache() if self.regen: taskomatic.add_to_repodata_queue_for_channel_package_subscription( [self.channel_label], [], "server.app.yumreposync") taskomatic.add_to_erratacache_queue(self.channel_label) self.update_date() rhnSQL.commit() total_time = datetime.now() - start_time self.print_msg("Sync completed.") self.print_msg("Total time: %s" % str(total_time).split('.')[0])
def addKSTree(self, username, password, channel_label, ks_label, path, install_type, tree_type, clear, files, pkgs, ignore_lint_errors, commit): log_debug(3) self._auth(username, password) # channel = rhnChannel.channel_info(channel_label) # if channel is '': # raise rhnFault(40, 'Could not lookup channel ' + channel_label) # # channel_id = channel['id'] kstree = rhnKickstart.lookup_tree(ks_label, pkgs) if kstree != None and clear: kstree.delete_tree() kstree = None if kstree == None: boot_image = ks_label kstree = rhnKickstart.create_tree(ks_label, channel_label, path, boot_image, tree_type, install_type, pkgs) # Firstly, we should lint the tree, and if we're not set to ignore linting errors # we error. lint_results = kstree.lint_tree() if lint_results != None and not ignore_lint_errors: rhnSQL.rollback() raise rhnFault (2102, """ The following packages in the kickstart tree were not found in the channel: %s """ % lint_results, explain=0) kstree.clear_files() for file in files: if kstree.has_file(file): continue else: log_debug(3, 'trying to insert ' + file['last_modified'] + ' as last_modified') kstree.add_file(file) if commit: rhnSQL.commit() message = 'Success. Committing transaction.' else: rhnSQL.rollback() message = 'Success. Rolling back transaction. --commit not specified' return message
def deleteChannel(self, channel_id, commit, username, password): log_debug(3) authobj = self._auth(username, password) authobj.isChannelAdmin() try: p = rhnSQL.Procedure("delete_channel") p(channel_id) except rhnSQL.SQLError, e: rhnSQL.rollback() raise rhnFault(23, str(e.args[1]), explain=0 )
def deleteDist(self, channel_id, username, password): log_debug(3) self._auth(username, password) try: rhnSQL.execute(""" delete from rhnDistChannelMap where channel_id = :channel_id """, channel_id=channel_id) except rhnSQL.SQLError, e: rhnSQL.rollback() raise rhnFault(23, str(e.args[1]), explain=0 )
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 sync(self): """Trigger a reposync""" start_time = datetime.now() for (repo_id, url, repo_label) in self.urls: print self.print_msg("Repo URL: %s" % url) plugin = None # If the repository uses a uln:// URL, switch to the ULN plugin, overriding the command-line if url.startswith("uln://"): self.repo_plugin = self.load_plugin("uln") # pylint: disable=W0703 try: plugin = self.repo_plugin(url, self.channel_label) if repo_id is not None: keys = rhnSQL.fetchone_dict(""" select k1.key as ca_cert, k2.key as client_cert, k3.key as client_key from rhncontentsourcessl join rhncryptokey k1 on rhncontentsourcessl.ssl_ca_cert_id = k1.id left outer join rhncryptokey k2 on rhncontentsourcessl.ssl_client_cert_id = k2.id left outer join rhncryptokey k3 on rhncontentsourcessl.ssl_client_key_id = k3.id where rhncontentsourcessl.content_source_id = :repo_id """, repo_id=int(repo_id)) if keys and keys.has_key('ca_cert'): plugin.set_ssl_options(keys['ca_cert'], keys['client_cert'], keys['client_key']) self.import_packages(plugin, repo_id, url) self.import_groups(plugin, url) if not self.no_errata: self.import_updates(plugin, url) # only for repos obtained from the DB if self.sync_kickstart and repo_label: try: self.import_kickstart(plugin, url, repo_label) except: rhnSQL.rollback() raise except Exception, e: self.error_msg("ERROR: %s" % e) if plugin is not None: plugin.clear_ssl_cache()
def sync(self): """Trigger a reposync""" start_time = datetime.now() for (repo_id, url, repo_label) in self.urls: print self.print_msg("Repo URL: %s" % url) plugin = None # If the repository uses a uln:// URL, switch to the ULN plugin, overriding the command-line if url.startswith("uln://"): self.repo_plugin = self.load_plugin("uln") # pylint: disable=W0703 try: plugin = self.repo_plugin(url, self.channel_label) if repo_id is not None: keys = rhnSQL.fetchone_dict( """ select k1.key as ca_cert, k2.key as client_cert, k3.key as client_key from rhncontentsourcessl join rhncryptokey k1 on rhncontentsourcessl.ssl_ca_cert_id = k1.id left outer join rhncryptokey k2 on rhncontentsourcessl.ssl_client_cert_id = k2.id left outer join rhncryptokey k3 on rhncontentsourcessl.ssl_client_key_id = k3.id where rhncontentsourcessl.content_source_id = :repo_id """, repo_id=int(repo_id), ) if keys and keys.has_key("ca_cert"): plugin.set_ssl_options(keys["ca_cert"], keys["client_cert"], keys["client_key"]) self.import_packages(plugin, repo_id, url) self.import_groups(plugin, url) if not self.no_errata: self.import_updates(plugin, url) # only for repos obtained from the DB if self.sync_kickstart and repo_label: try: self.import_kickstart(plugin, url, repo_label) except: rhnSQL.rollback() raise except Exception, e: self.error_msg("ERROR: %s" % e) if plugin is not None: plugin.clear_ssl_cache()
def db_clean(bkp): db_backup(bkp) bkp.save() rhnSQL.initDB() # delete from rhnchannelpackage entries that have an unknown provider and are packages in a RH channel queryA = """ delete from rhnchannelpackage where package_id in ( select distinct rp.id as "pid" from rhnpackage rp left outer join rhnchannelpackage rcp on rcp.package_id = rp.id left outer join rhnchannel rc on rc.id = rcp.channel_id left outer join rhnpackagekeyassociation rpka on rpka.package_id = rp.id left outer join rhnpackagekey rpk on rpk.id = rpka.key_id where rpka.key_id is null and rc.channel_product_id is not null ) """ # delete rhnpackage entries not in any channel queryB = """ delete from rhnpackage where id in ( select distinct rp.id as "pid" from rhnpackage rp left outer join rhnchannelpackage rcp on rcp.package_id = rp.id left outer join rhnchannel rc on rc.id = rcp.channel_id where rcp.channel_id is null ) """ answer = ask("Continue with the deletion of the entries?") if not answer: print "leaving..." else: answer = ask("Did you take a backup of your database?") if not answer: print "you need to take one to be able to roll back" else: try: cursor = rhnSQL.prepare(queryA) cursor.execute() cursor = rhnSQL.prepare(queryB) cursor.execute() rhnSQL.commit() except: rhnSQL.rollback() raise print "entries deleted"
def db_clean(bkp): db_backup(bkp) bkp.save() rhnSQL.initDB() # delete from rhnchannelpackage entries that have an unknown provider and are packages in a RH channel queryA = """ delete from rhnchannelpackage where package_id in ( select distinct rp.id as "pid" from rhnpackage rp left outer join rhnchannelpackage rcp on rcp.package_id = rp.id left outer join rhnchannel rc on rc.id = rcp.channel_id left outer join rhnpackagekeyassociation rpka on rpka.package_id = rp.id left outer join rhnpackagekey rpk on rpk.id = rpka.key_id where rpka.key_id is null and rc.channel_product_id is not null ) """ # delete rhnpackage entries not in any channel queryB = """ delete from rhnpackage where id in ( select distinct rp.id as "pid" from rhnpackage rp left outer join rhnchannelpackage rcp on rcp.package_id = rp.id left outer join rhnchannel rc on rc.id = rcp.channel_id where rcp.channel_id is null ) """ answer = ask("Continue with the deletion of the entries?") if not answer : print "leaving..." else: answer = ask("Did you take a backup of your database?") if not answer: print "you need to take one to be able to roll back" else: try: cursor = rhnSQL.prepare(queryA) cursor.execute() cursor = rhnSQL.prepare(queryB) cursor.execute() rhnSQL.commit() except: rhnSQL.rollback() raise print "entries deleted"
def updateChannelMembership(self, channel_id, channel_family_id, kargs, commit, username, password): log_debug(3) authobj = self._auth(username, password) authobj.isChannelAdmin() rhnSQL.execute(""" delete from rhnChannelFamilyMembers where channel_id = :channel_id""", channel_id=channel_id) try: rhnSQL.execute(""" insert into rhnChannelFamilyMembers (channel_id, channel_family_id ) values (:channel_id, :channel_family_id) """, channel_id=channel_id, channel_family_id=channel_family_id) except rhnSQL.SQLError, e: rhnSQL.rollback() raise rhnFault(23, str(e.args[1]), explain=0 )
def createChannel(self, params, commit, username, password): log_debug(3) self._auth(username, password) params['id'] = rhnSQL.Sequence("rhn_channel_id_seq").next() fields = [] for f in rhnChannel_fields: if params.has_key(f): fields.append(f) field_names = string.join(fields, ", ") bind_vars = string.join(map(lambda x: ':' + x, fields), ', ') try: rhnSQL.execute("insert into rhnChannel (%s) values (%s)" % (field_names, bind_vars), **params) except rhnSQL.SQLError, e: rhnSQL.rollback() raise rhnFault(23, str(e.args[1]), explain=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 db_clean(bkp, pid): answer = ask("Did you take a backup of your database?") if not answer: print "Taking a backup now" db_backup(bkp, pid) bkp.save() rhnSQL.initDB() pids=[] if isinstance(pid, list): queryA = """ delete from rhnchannelpackage where package_id in (:pid) """ queryB = """ delete from rhnpackage where id in (:pid) """ else: queryA = """ delete from rhnchannelpackage where package_id = :pid """ queryB = """ delete from rhnpackage where id = :pid """ answer = ask("Continue with the deletion of the entries?") if not answer : print "leaving..." else: try: if isinstance(pid, list): cursor = rhnSQL.prepare(queryA) cursor.execute(pid=','.join(pid)) cursor = rhnSQL.prepare(queryB) cursor.execute(pid=','.join(pid)) rhnSQL.commit() else: cursor = rhnSQL.prepare(queryA) cursor.execute(pid=pid) cursor = rhnSQL.prepare(queryB) cursor.execute(pid=pid) rhnSQL.commit() except: rhnSQL.rollback() raise print "entries deleted"
def delKSTree(self, username, password, ks_label, commit): log_debug(3) self._auth(username, password) kstree = rhnKickstart.lookup_tree(ks_label) if not isinstance(kstree, rhnKickstart.Kickstart): message = 'Kickstart tree not found' return message else: kstree.delete_tree() if commit: rhnSQL.commit() message = 'Success. Committing transaction.' else: rhnSQL.rollback() message = 'Success. Rolling back transaction. --commit not specified' return message
def sync(self): """Trigger a reposync""" start_time = datetime.now() for (repo_id, url, repo_label) in self.urls: self.print_msg("Repo URL: %s" % url) plugin = None try: plugin = self.repo_plugin(url, self.channel_label) if repo_id is not None: keys = rhnSQL.fetchone_dict( """ select k1.key as ca_cert, k2.key as client_cert, k3.key as client_key from rhncontentsourcessl join rhncryptokey k1 on rhncontentsourcessl.ssl_ca_cert_id = k1.id left outer join rhncryptokey k2 on rhncontentsourcessl.ssl_client_cert_id = k2.id left outer join rhncryptokey k3 on rhncontentsourcessl.ssl_client_key_id = k3.id where rhncontentsourcessl.content_source_id = :repo_id """, repo_id=int(repo_id), ) if keys and keys.has_key("ca_cert"): plugin.set_ssl_options(keys["ca_cert"], keys["client_cert"], keys["client_key"]) self.import_packages(plugin, repo_id, url) self.import_groups(plugin, url) if not self.no_errata: self.import_updates(plugin, url) if self.sync_kickstart: try: self.import_kickstart(plugin, url, repo_label) except: rhnSQL.rollback() raise except Exception, e: self.error_msg("ERROR: %s" % e) if plugin is not None: plugin.clear_ssl_cache()
def sync(self): """Trigger a reposync""" start_time = datetime.now() for (repo_id, url, repo_label) in self.urls: self.print_msg("Repo URL: %s" % url) plugin = None try: plugin = self.repo_plugin(url, self.channel_label) if repo_id is not None: keys = rhnSQL.fetchone_dict(""" select k1.key as ca_cert, k2.key as client_cert, k3.key as client_key from rhncontentsourcessl join rhncryptokey k1 on rhncontentsourcessl.ssl_ca_cert_id = k1.id left outer join rhncryptokey k2 on rhncontentsourcessl.ssl_client_cert_id = k2.id left outer join rhncryptokey k3 on rhncontentsourcessl.ssl_client_key_id = k3.id where rhncontentsourcessl.content_source_id = :repo_id """, repo_id=int(repo_id)) if keys and keys.has_key('ca_cert'): plugin.set_ssl_options(keys['ca_cert'], keys['client_cert'], keys['client_key']) self.import_packages(plugin, repo_id, url) self.import_groups(plugin, url) if not self.no_errata: self.import_updates(plugin, url) if self.sync_kickstart: try: self.import_kickstart(plugin, url, repo_label) except: rhnSQL.rollback() raise except Exception, e: self.error_msg("ERROR: %s" % e) if plugin is not None: plugin.clear_ssl_cache()
def updateChannel(self, params, channel_id, old_channel_family_id, new_channel_family_id, commit, username, password): log_debug(3) global rhnChannel_fields authobj = self._auth(username, password) authobj.isChannelAdmin() fields = [] for f in rhnChannel_fields: if params.has_key(f): fields.append(f) set_clause = string.join( map(lambda x: "%s = :%s" % (x, x), fields), ', ') try: rhnSQL.execute("update rhnChannel set %s where id = :id" % set_clause, **params) except rhnSQL.SQLError, e: rhnSQL.rollback() raise rhnFault(23, str(e.args[1]), explain=0 )
def rollback(): logger.warning("Rolling back rhnSQL transaction.") rhnSQL.rollback()
myserver = TestServer() #myserver.upload_packages('/home/devel/wregglej/rpmtest') #handler = rhnHandler() #print handler.auth_system( myserver.getSystemId() ) #up2date = myserver.getUp2date() #id = myserver.getSystemId() #print up2date.solvedep( id, ['libcaps.so'] ) #print "Done!" #rhnserver = rhnServer.Server(myserver.testuser, org_id=myserver.org_id) fake_key = create_activation_key(org_id=myserver.org_id, user_id=myserver.testuser.getid(), channels=[myserver.label], server_id=myserver.getServerId()) fake_action = rhnAction.schedule_server_action(myserver.getServerId(), "packages.update", action_name="Testing", delta_time=9999, org_id=myserver.org_id) fake_token = rhnServer.search_token(fake_key._token) print look_at_actions(myserver.getServerId()) rhnFlags.set("registration_token", fake_token) myserver.testserver.use_token() print look_at_actions(myserver.getServerId()) rhnAction.invalidate_action(myserver.getServerId(), fake_action) rhnSQL.rollback()
def tearDown(self): rhnSQL.rollback()
def sync(self, update_repodata=False): """Trigger a reposync""" start_time = datetime.now() for (repo_id, url, repo_label) in self.urls: log(0, "Repo URL: %s" % url) plugin = None # If the repository uses a uln:// URL, switch to the ULN plugin, overriding the command-line if url.startswith("uln://"): self.repo_plugin = self.load_plugin("uln") # pylint: disable=W0703 try: # use modified relative_url as name of repo plugin, because # it used as name of cache directory as well relative_url = '_'.join(url.split('://')[1].split('/')[1:]) plugin_name = relative_url.replace("?", "_").replace("&", "_").replace("=", "_") plugin = self.repo_plugin(url, plugin_name) if update_repodata: plugin.clear_cache() if repo_id is not None: keys = rhnSQL.fetchone_dict(""" select k1.key as ca_cert, k2.key as client_cert, k3.key as client_key from rhncontentsource cs join rhncryptokey k1 on cs.ssl_ca_cert_id = k1.id left outer join rhncryptokey k2 on cs.ssl_client_cert_id = k2.id left outer join rhncryptokey k3 on cs.ssl_client_key_id = k3.id where cs.id = :repo_id """, repo_id=int(repo_id)) if keys and ('ca_cert' in keys): plugin.set_ssl_options(keys['ca_cert'], keys['client_cert'], keys['client_key']) if not self.no_packages: self.import_packages(plugin, repo_id, url) self.import_groups(plugin, url) if not self.no_errata: self.import_updates(plugin, url) # only for repos obtained from the DB if self.sync_kickstart and repo_label: try: self.import_kickstart(plugin, repo_label) except: rhnSQL.rollback() raise except Exception: e = sys.exc_info()[1] log2stderr(0, "ERROR: %s" % e) if plugin is not None: plugin.clear_ssl_cache() if self.regen: taskomatic.add_to_repodata_queue_for_channel_package_subscription( [self.channel_label], [], "server.app.yumreposync") taskomatic.add_to_erratacache_queue(self.channel_label) self.update_date() rhnSQL.commit() elapsed_time = datetime.now() - start_time log(0, "Sync of channel completed in %s." % str(elapsed_time).split('.')[0]) return elapsed_time
def look_at_actions(server_id): h = rhnSQL.prepare(_query_action_lookup) h.execute(server_id=server_id) return h.fetchall_dict() if __name__ == "__main__": myserver = TestServer() #myserver.upload_packages('/home/devel/wregglej/rpmtest') #handler = rhnHandler() #print handler.auth_system( myserver.getSystemId() ) #up2date = myserver.getUp2date() #id = myserver.getSystemId() #print up2date.solvedep( id, ['libcaps.so'] ) #print "Done!" #rhnserver = rhnServer.Server(myserver.testuser, org_id=myserver.org_id) fake_key = create_activation_key(org_id=myserver.org_id, user_id=myserver.testuser.getid(), channels=[myserver.label], server_id=myserver.getServerId()) fake_action = rhnAction.schedule_server_action(myserver.getServerId(), "packages.update", action_name="Testing", delta_time=9999, org_id=myserver.org_id) fake_token = rhnServer.search_token(fake_key._token) print look_at_actions(myserver.getServerId()) rhnFlags.set("registration_token", fake_token) myserver.testserver.use_token() print look_at_actions(myserver.getServerId()) rhnAction.invalidate_action(myserver.getServerId(), fake_action) rhnSQL.rollback()
def tearDown(self): # Roll back any unsaved data rhnSQL.rollback()
def sync(self, update_repodata=False): """Trigger a reposync""" start_time = datetime.now() for (repo_id, url, repo_label, channel_family_id) in self.urls: log(0, "Repo URL: %s" % url) plugin = None # If the repository uses a uln:// URL, switch to the ULN plugin, overriding the command-line if url.startswith("uln://"): self.repo_plugin = self.load_plugin("uln") # pylint: disable=W0703 try: # use modified relative_url as name of repo plugin, because # it used as name of cache directory as well relative_url = '_'.join(url.split('://')[1].split('/')[1:]) plugin_name = relative_url.replace("?", "_").replace("&", "_").replace("=", "_") plugin = self.repo_plugin(url, plugin_name) if update_repodata: plugin.clear_cache() if repo_id is not None: keys = rhnSQL.fetchone_dict(""" select k1.key as ca_cert, k2.key as client_cert, k3.key as client_key from rhncontentssl join rhncryptokey k1 on rhncontentssl.ssl_ca_cert_id = k1.id left outer join rhncryptokey k2 on rhncontentssl.ssl_client_cert_id = k2.id left outer join rhncryptokey k3 on rhncontentssl.ssl_client_key_id = k3.id where rhncontentssl.content_source_id = :repo_id or rhncontentssl.channel_family_id = :channel_family_id """, repo_id=int(repo_id), channel_family_id=int(channel_family_id)) if keys and ('ca_cert' in keys): plugin.set_ssl_options(keys['ca_cert'], keys['client_cert'], keys['client_key']) if not self.no_packages: self.import_packages(plugin, repo_id, url) self.import_groups(plugin, url) if not self.no_errata: self.import_updates(plugin, url) # only for repos obtained from the DB if self.sync_kickstart and repo_label: try: self.import_kickstart(plugin, repo_label) except: rhnSQL.rollback() raise except Exception: e = sys.exc_info()[1] log2stderr(0, "ERROR: %s" % e) if plugin is not None: plugin.clear_ssl_cache() if self.regen: taskomatic.add_to_repodata_queue_for_channel_package_subscription( [self.channel_label], [], "server.app.yumreposync") taskomatic.add_to_erratacache_queue(self.channel_label) self.update_date() rhnSQL.commit() elapsed_time = datetime.now() - start_time log(0, "Sync of channel completed in %s." % str(elapsed_time).split('.')[0]) return elapsed_time
def _push_file(self, config_channel_id, file): if not file: # Nothing to do return {} # Check for full path on the file path = file.get('path') if not (path[0] == os.sep): raise ConfigFilePathIncomplete(file) if 'config_file_type_id' not in file: log_debug(4, "Client does not support config directories, so set file_type_id to 1") file['config_file_type_id'] = '1' # Check if delimiters are present if self._is_file(file) and \ not (file.get('delim_start') and file.get('delim_end')): # Need delimiters raise ConfigFileMissingDelimError(file) if not (file.get('user') and file.get('group') and file.get('mode') is not None) and not self._is_link(file): raise ConfigFileMissingInfoError(file) # Oracle doesn't like certain binding variables file['username'] = file.get('user', '') file['groupname'] = file.get('group', '') file['file_mode'] = str(file.get('mode', '')) # if the selinux flag is not sent by the client it is set to the last file # revision (or to None (i.e. NULL) in case of first revision) - see the bug # 644985 - SELinux context cleared from RHEL4 rhncfg-client file['selinux_ctx'] = file.get('selinux_ctx', None) if not file['selinux_ctx']: # RHEL4 or RHEL5+ with disabled selinux - set from the last revision h = rhnSQL.prepare(self._query_current_selinux_lookup) h.execute(**file) row = h.fetchone_dict() if row: file['selinux_ctx'] = row['selinux_ctx'] else: file['selinux_ctx'] = None result = {} try: if self._is_file(file): self._push_contents(file) elif self._is_link(file): file['symlink'] = file.get('symlink') or '' except ConfigFileTooLargeError: result['file_too_large'] = 1 t = rhnSQL.Table('rhnConfigFileState', 'label') state_id_alive = t['alive']['id'] file['state_id'] = state_id_alive file['config_channel_id'] = config_channel_id try: self._push_config_file(file) self._push_revision(file) except rhnSQL.SQLSchemaError, e: log_debug(4, "schema error", e) rhnSQL.rollback() # blow away the contents that got inserted if e.errno == 20267: # ORA-20267: (not_enough_quota) - Insufficient available quota # for the specified action raise ConfigFileExceedsQuota(file) raise
def sync(self, update_repodata=True): """Trigger a reposync""" failed_packages = 0 sync_error = 0 if not self.urls: sync_error = -1 start_time = datetime.now() for (repo_id, url, repo_label) in self.urls: log(0, "Repo URL: %s" % url) plugin = None # If the repository uses a uln:// URL, switch to the ULN plugin, overriding the command-line if url.startswith("uln://"): self.repo_plugin = self.load_plugin("uln") # pylint: disable=W0703 try: if repo_label: repo_name = repo_label else: # use modified relative_url as name of repo plugin, because # it used as name of cache directory as well relative_url = '_'.join(url.split('://')[1].split('/')[1:]) repo_name = relative_url.replace("?", "_").replace( "&", "_").replace("=", "_") plugin = self.repo_plugin(url, repo_name, org=str(self.org_id or ''), channel_label=self.channel_label) if update_repodata: plugin.clear_cache() if repo_id is not None: keys = rhnSQL.fetchall_dict(""" select k1.key as ca_cert, k2.key as client_cert, k3.key as client_key from rhncontentsource cs inner join rhncontentsourcessl csssl on cs.id = csssl.content_source_id inner join rhncryptokey k1 on csssl.ssl_ca_cert_id = k1.id left outer join rhncryptokey k2 on csssl.ssl_client_cert_id = k2.id left outer join rhncryptokey k3 on csssl.ssl_client_key_id = k3.id where cs.id = :repo_id """, repo_id=int(repo_id)) if keys: ssl_set = get_single_ssl_set( keys, check_dates=self.check_ssl_dates) if ssl_set: plugin.set_ssl_options(ssl_set['ca_cert'], ssl_set['client_cert'], ssl_set['client_key']) else: raise ValueError( "No valid SSL certificates were found for repository." ) if not self.no_packages: ret = self.import_packages(plugin, repo_id, url) failed_packages += ret self.import_groups(plugin, url) if not self.no_errata: self.import_updates(plugin, url) # only for repos obtained from the DB if self.sync_kickstart and repo_label: try: self.import_kickstart(plugin, repo_label) except: rhnSQL.rollback() raise except Exception: e = sys.exc_info()[1] log2(0, 0, "ERROR: %s" % e, stream=sys.stderr) log2disk(0, "ERROR: %s" % e) # pylint: disable=W0104 sync_error = -1 if plugin is not None: plugin.clear_ssl_cache() # Update cache with package checksums rhnCache.set(checksum_cache_filename, self.checksum_cache) if self.regen: taskomatic.add_to_repodata_queue_for_channel_package_subscription( [self.channel_label], [], "server.app.yumreposync") taskomatic.add_to_erratacache_queue(self.channel_label) self.update_date() rhnSQL.commit() # update permissions fileutils.createPath(os.path.join( CFG.MOUNT_POINT, 'rhn')) # if the directory exists update ownership only for root, dirs, files in os.walk(os.path.join(CFG.MOUNT_POINT, 'rhn')): for d in dirs: fileutils.setPermsPath(os.path.join(root, d), group='apache') for f in files: fileutils.setPermsPath(os.path.join(root, f), group='apache') elapsed_time = datetime.now() - start_time log( 0, "Sync of channel completed in %s." % str(elapsed_time).split('.')[0]) # if there is no global problems, but some packages weren't synced if sync_error == 0 and failed_packages > 0: sync_error = failed_packages return elapsed_time, sync_error
def _push_file(self, config_channel_id, file): if not file: # Nothing to do return {} # Check for full path on the file path = file.get('path') if not (path[0] == os.sep): raise ConfigFilePathIncomplete(file) if 'config_file_type_id' not in file: log_debug( 4, "Client does not support config directories, so set file_type_id to 1" ) file['config_file_type_id'] = '1' # Check if delimiters are present if self._is_file(file) and \ not (file.get('delim_start') and file.get('delim_end')): # Need delimiters raise ConfigFileMissingDelimError(file) if not (file.get('user') and file.get('group') and file.get('mode') is not None) and not self._is_link(file): raise ConfigFileMissingInfoError(file) # Oracle doesn't like certain binding variables file['username'] = file.get('user', '') file['groupname'] = file.get('group', '') file['file_mode'] = str(file.get('mode', '')) # if the selinux flag is not sent by the client it is set to the last file # revision (or to None (i.e. NULL) in case of first revision) - see the bug # 644985 - SELinux context cleared from RHEL4 rhncfg-client file['selinux_ctx'] = file.get('selinux_ctx', None) if not file['selinux_ctx']: # RHEL4 or RHEL5+ with disabled selinux - set from the last revision h = rhnSQL.prepare(self._query_current_selinux_lookup) h.execute(**file) row = h.fetchone_dict() if row: file['selinux_ctx'] = row['selinux_ctx'] else: file['selinux_ctx'] = None result = {} try: if self._is_file(file): self._push_contents(file) elif self._is_link(file): file['symlink'] = file.get('symlink') or '' except ConfigFileTooLargeError: result['file_too_large'] = 1 t = rhnSQL.Table('rhnConfigFileState', 'label') state_id_alive = t['alive']['id'] file['state_id'] = state_id_alive file['config_channel_id'] = config_channel_id try: self._push_config_file(file) self._push_revision(file) except rhnSQL.SQLSchemaError, e: log_debug(4, "schema error", e) rhnSQL.rollback() # blow away the contents that got inserted if e.errno == 20267: # ORA-20267: (not_enough_quota) - Insufficient available quota # for the specified action raise ConfigFileExceedsQuota(file) raise
def tearDown(self): # Roll back any unsaved data rhnSQL.rollback(self.__transaction_name)