a4967c3692aba28bdd6462948be5197222a9b755
[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.alchemy import engine
17 from sfa.storage.dbschema import DBSchema
18
19 def main():
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="By default a new DB schema is installed after the cleanup; this option prevents that")
28    (options,args)=parser.parse_args()
29    if args:
30       parser.print_help()
31       sys.exit(1)
32    dbschema=DBSchema()
33    logger.info("Purging SFA records from database")
34    dbschema.nuke()
35    # for convenience we re-create the schema here, so there's no need for an explicit
36    # service sfa restart
37    # however in some (upgrade) scenarios this might be wrong
38    if options.reinit:
39       logger.info("re-creating empty schema")
40       dbschema.init_or_upgrade()
41
42    if options.clean_certs:
43       # remove the server certificate and all gids found in /var/lib/sfa/authorities
44       logger.info("Purging cached certificates")
45       for (dir, _, files) in os.walk('/var/lib/sfa/authorities'):
46          for file in files:
47             if file.endswith('.gid') or file == 'server.cert':
48                path=dir+os.sep+file
49                os.unlink(path)
50                if not os.path.exists(path):
51                   logger.info("Unlinked file %s"%path)
52                else:
53                   logger.error("Could not unlink file %s"%path)
54
55    if options.clean_fs:
56       # just remove all files that do not match 'server.key' or 'server.cert'
57       logger.info("Purging registry filesystem cache")
58       preserved_files = [ 'server.key', 'server.cert']
59       for (dir,_,files) in os.walk('/var/lib/sfa/authorities'):
60          for file in files:
61             if file in preserved_files: continue
62             path=dir+os.sep+file
63             os.unlink(path)
64             if not os.path.exists(path):
65                logger.info("Unlinked file %s"%path)
66             else:
67                logger.error("Could not unlink file %s"%path)
68 if __name__ == "__main__":
69    main()