fb84020bf3a24114496e17c11817b3735ba9f506
[sfa.git] / sfa / plc / 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.trust.hierarchy import *
15 from sfa.util.record import *
16 from sfa.util.table import SfaTable
17 from sfa.util.sfalogging import logger
18
19 def main():
20    usage="%prog: trash the registry DB (the 'sfa' table in the 'planetlab5' database)"
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    (options,args)=parser.parse_args()
27    if args:
28       parser.print_help()
29       sys.exit(1)
30    logger.info("Purging SFA records from database")
31    table = SfaTable()
32    table.sfa_records_purge()
33
34    if options.clean_certs:
35       # remove the server certificate and all gids found in /var/lib/sfa/authorities
36       logger.info("Purging cached certificates")
37       for (dir, _, files) in os.walk('/var/lib/sfa/authorities'):
38          for file in files:
39             if file.endswith('.gid') or file == 'server.cert':
40                path=dir+os.sep+file
41                os.unlink(path)
42                if not os.path.exists(path):
43                   logger.info("Unlinked file %s"%path)
44                else:
45                   logger.error("Could not unlink file %s"%path)
46
47    if options.clean_fs:
48       # just remove all files that do not match 'server.key' or 'server.cert'
49       logger.info("Purging registry filesystem cache")
50       preserved_files = [ 'server.key', 'server.cert']
51       for (dir,_,files) in os.walk('/var/lib/sfa/authorities'):
52          for file in files:
53             if file in preserved_files: continue
54             path=dir+os.sep+file
55             os.unlink(path)
56             if not os.path.exists(path):
57                logger.info("Unlinked file %s"%path)
58             else:
59                logger.error("Could not unlink file %s"%path)
60 if __name__ == "__main__":
61    main()