3 # Delete all the database records for SFA. This tool is used to clean out SFA
4 # records during testing.
6 # Authority info (maintained by the hierarchy module in a subdirectory tree)
7 # is not purged by this tool and may be deleted by a command like 'rm'.
12 from optparse import OptionParser
14 from sfa.util.sfalogging import logger
16 from sfa.storage.alchemy import dbsession
17 from sfa.storage.model import init_tables,drop_tables
20 usage="%prog: trash the registry DB"
21 parser = OptionParser(usage=usage)
22 parser.add_option("-f","--file-system",dest='clean_fs',action='store_true',default=False,
23 help="Clean up the /var/lib/sfa/authorities area as well")
24 parser.add_option("-c","--certs",dest='clean_certs',action='store_true',default=False,
25 help="Remove all cached certs/gids found in /var/lib/sfa/authorities area as well")
26 parser.add_option("-0","--no-reinit",dest='reinit',action='store_false',default=True,
27 help="Do not reinitialize the database schema")
28 (options,args)=parser.parse_args()
32 logger.info("Purging SFA records from database")
33 drop_tables(dbsession)
34 # for convenience we re-create the schema here, so there's no need for an explicit
36 # however in some (upgrade) scenarios this might be wrong
38 logger.info("re-creating empty schema")
39 init_tables(dbsession)
41 if options.clean_certs:
42 # remove the server certificate and all gids found in /var/lib/sfa/authorities
43 logger.info("Purging cached certificates")
44 for (dir, _, files) in os.walk('/var/lib/sfa/authorities'):
46 if file.endswith('.gid') or file == 'server.cert':
49 if not os.path.exists(path):
50 logger.info("Unlinked file %s"%path)
52 logger.error("Could not unlink file %s"%path)
55 # just remove all files that do not match 'server.key' or 'server.cert'
56 logger.info("Purging registry filesystem cache")
57 preserved_files = [ 'server.key', 'server.cert']
58 for (dir,_,files) in os.walk('/var/lib/sfa/authorities'):
60 if file in preserved_files: continue
63 if not os.path.exists(path):
64 logger.info("Unlinked file %s"%path)
66 logger.error("Could not unlink file %s"%path)
67 if __name__ == "__main__":