checkpoint commit - import, register, create_slice all work fine
[sfa.git] / sfa / importer / sfa-nuke-plc.py
1 #!/usr/bin/python
2 ##
3 # Delete all the database records for SFA. This tool is used to clean out SFA
4 # records during testing.
5 #
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'.
8 ##
9
10 import sys
11 import os
12 from optparse import OptionParser
13
14 from sfa.util.sfalogging import logger
15
16 from sfa.storage.persistentobjs import init_tables,drop_tables
17
18 def main():
19    usage="%prog: trash the registry DB"
20    parser = OptionParser(usage=usage)
21    parser.add_option("-f","--file-system",dest='clean_fs',action='store_true',default=False,
22                      help="Clean up the /var/lib/sfa/authorities area as well")
23    parser.add_option("-c","--certs",dest='clean_certs',action='store_true',default=False,
24                      help="Remove all cached certs/gids found in /var/lib/sfa/authorities area as well")
25    parser.add_option("-0","--no-reinit",dest='reinit',action='store_false',default=True,
26                      help="Do not reinitialize the database schema")
27    (options,args)=parser.parse_args()
28    if args:
29       parser.print_help()
30       sys.exit(1)
31    logger.info("Purging SFA records from database")
32    drop_tables()
33    # for convenience we re-create the schema here, so there's no need for an explicit
34    # service sfa restart
35    # however in some (upgrade) scenarios this might be wrong
36    if options.reinit:
37       logger.info("re-creating empty schema")
38       init_tables()
39
40    if options.clean_certs:
41       # remove the server certificate and all gids found in /var/lib/sfa/authorities
42       logger.info("Purging cached certificates")
43       for (dir, _, files) in os.walk('/var/lib/sfa/authorities'):
44          for file in files:
45             if file.endswith('.gid') or file == 'server.cert':
46                path=dir+os.sep+file
47                os.unlink(path)
48                if not os.path.exists(path):
49                   logger.info("Unlinked file %s"%path)
50                else:
51                   logger.error("Could not unlink file %s"%path)
52
53    if options.clean_fs:
54       # just remove all files that do not match 'server.key' or 'server.cert'
55       logger.info("Purging registry filesystem cache")
56       preserved_files = [ 'server.key', 'server.cert']
57       for (dir,_,files) in os.walk('/var/lib/sfa/authorities'):
58          for file in files:
59             if file in preserved_files: continue
60             path=dir+os.sep+file
61             os.unlink(path)
62             if not os.path.exists(path):
63                logger.info("Unlinked file %s"%path)
64             else:
65                logger.error("Could not unlink file %s"%path)
66 if __name__ == "__main__":
67    main()