1 | #!/usr/bin/env python |
---|
2 | |
---|
3 | """ |
---|
4 | invirt-quota allows an administrator to set memory, disk, and VM quotas |
---|
5 | for an owner. Invoking with only an owner name returns the current quotas for |
---|
6 | that owner. Setting a parameter to -1 restores the default. |
---|
7 | """ |
---|
8 | |
---|
9 | import sys |
---|
10 | import optparse |
---|
11 | |
---|
12 | from invirt import database |
---|
13 | |
---|
14 | def main(argv): |
---|
15 | parser = optparse.OptionParser(usage = '%prog <owner> [options]', |
---|
16 | description = __doc__.strip()) |
---|
17 | parser.add_option('-m', '--ram-total', |
---|
18 | type = 'int', |
---|
19 | dest = 'ramtotal', |
---|
20 | help = 'set total concurrent RAM quota') |
---|
21 | parser.add_option('-n', '--ram-single', |
---|
22 | type = 'int', |
---|
23 | dest = 'ramsingle', |
---|
24 | help = 'set single VM RAM quota') |
---|
25 | parser.add_option('-d', '--disk-total', |
---|
26 | type = 'int', |
---|
27 | dest = 'disktotal', |
---|
28 | help = 'set total disk quota') |
---|
29 | parser.add_option('-e', '--disk-single', |
---|
30 | type = 'int', |
---|
31 | dest = 'disksingle', |
---|
32 | help = 'set single VM disk quota') |
---|
33 | parser.add_option('-v', '--vms-total', |
---|
34 | type = 'int', |
---|
35 | dest = 'vmstotal', |
---|
36 | help = 'set total VM quota') |
---|
37 | parser.add_option('-w', '--vms-active', |
---|
38 | type = 'int', |
---|
39 | dest = 'vmsactive', |
---|
40 | help = 'set active VM quota') |
---|
41 | opts, args = parser.parse_args() |
---|
42 | |
---|
43 | if len(args) != 1: |
---|
44 | parser.print_help(sys.stderr) |
---|
45 | return 1 |
---|
46 | owner = args[0] |
---|
47 | database.connect() |
---|
48 | database.session.begin() |
---|
49 | |
---|
50 | x = database.Owner.query().filter_by(owner_id=owner).first() |
---|
51 | if x is None: |
---|
52 | x = database.Owner(owner_id=owner) |
---|
53 | |
---|
54 | for resource, scope in [('ram', 'total'), ('ram', 'single'), |
---|
55 | ('disk', 'total'), ('disk', 'single'), |
---|
56 | ('vms', 'total'), ('vms', 'active')]: |
---|
57 | val = getattr(opts, resource+scope) |
---|
58 | if val is not None: |
---|
59 | setattr(x, resource+'_quota_'+scope, val if val >= 0 else None) |
---|
60 | |
---|
61 | database.session.commit() |
---|
62 | print str(x) |
---|
63 | return 0 |
---|
64 | |
---|
65 | if __name__ == '__main__': |
---|
66 | sys.exit(main(sys.argv)) |
---|