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