Preview of a new Python-based management tool; includes segment cleaning.
[cumulus.git] / lbs-util.py
1 #!/usr/bin/python
2 #
3 # Utility for managing LBS archives.
4
5 import sys
6 from optparse import OptionParser
7 import lbs
8
9 parser = OptionParser(usage="%prog [option]... command [arg]...")
10 parser.add_option("-v", action="store_true", dest="verbose", default=False,
11                   help="increase verbosity")
12 parser.add_option("--localdb", dest="localdb",
13                   help="specify path to local database")
14 (options, args) = parser.parse_args(sys.argv[1:])
15
16 # Run the segment cleaner.
17 # Syntax: $0 --localdb=LOCALDB clean
18 def cmd_clean(clean_threshold=7.0):
19     db = lbs.LocalDatabase(options.localdb)
20
21     # Delete old snapshots from the local database.
22     db.garbage_collect()
23
24     # Expire segments which are poorly-utilized.
25     for s in db.get_segment_cleaning_list():
26         if s.cleaning_benefit > clean_threshold:
27             print "Cleaning segment %d (benefit %.2f)" % (s.id,
28                                                           s.cleaning_benefit)
29             db.mark_segment_expired(s)
30         else:
31             break
32     db.balance_expired_objects()
33     db.commit()
34
35 if len(args) == 0:
36     parser.print_usage()
37     sys.exit(1)
38 cmd = args[0]
39 args = args[1:]
40 if cmd == 'clean':
41     cmd_clean()
42 else:
43     print "Unknown command:", cmd
44     parser.print_usage()
45     sys.exit(1)