trimmed useless imports, unstarred all imports
[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.util.table import SfaTable
15 from sfa.util.sfalogging import logger
16
17 def main():
18    usage="%prog: trash the registry DB (the 'sfa' table in the 'planetlab5' database)"
19    parser = OptionParser(usage=usage)
20    parser.add_option('-f','--file-system',dest='clean_fs',action='store_true',default=False,
21                      help='Clean up the /var/lib/sfa/authorities area as well')
22    parser.add_option('-c','--certs',dest='clean_certs',action='store_true',default=False,
23                      help='Remove all cached certs/gids found in /var/lib/sfa/authorities area as well')
24    (options,args)=parser.parse_args()
25    if args:
26       parser.print_help()
27       sys.exit(1)
28    logger.info("Purging SFA records from database")
29    table = SfaTable()
30    table.sfa_records_purge()
31
32    if options.clean_certs:
33       # remove the server certificate and all gids found in /var/lib/sfa/authorities
34       logger.info("Purging cached certificates")
35       for (dir, _, files) in os.walk('/var/lib/sfa/authorities'):
36          for file in files:
37             if file.endswith('.gid') or file == 'server.cert':
38                path=dir+os.sep+file
39                os.unlink(path)
40                if not os.path.exists(path):
41                   logger.info("Unlinked file %s"%path)
42                else:
43                   logger.error("Could not unlink file %s"%path)
44
45    if options.clean_fs:
46       # just remove all files that do not match 'server.key' or 'server.cert'
47       logger.info("Purging registry filesystem cache")
48       preserved_files = [ 'server.key', 'server.cert']
49       for (dir,_,files) in os.walk('/var/lib/sfa/authorities'):
50          for file in files:
51             if file in preserved_files: continue
52             path=dir+os.sep+file
53             os.unlink(path)
54             if not os.path.exists(path):
55                logger.info("Unlinked file %s"%path)
56             else:
57                logger.error("Could not unlink file %s"%path)
58 if __name__ == "__main__":
59    main()