[2132] | 1 | #!/usr/bin/env python |
---|
| 2 | |
---|
| 3 | """ |
---|
[2176] | 4 | invirt-quota allows an administrator to set memory, disk, and VM quotas |
---|
[2134] | 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. |
---|
[2132] | 7 | """ |
---|
| 8 | |
---|
[2198] | 9 | import sys |
---|
| 10 | import optparse |
---|
[2132] | 11 | |
---|
[2198] | 12 | from invirt import database |
---|
| 13 | |
---|
[2132] | 14 | def main(argv): |
---|
[2198] | 15 | parser = optparse.OptionParser(usage = '%prog <owner> [options]', |
---|
[2194] | 16 | description = __doc__.strip()) |
---|
| 17 | parser.add_option('-m', '--ram-total', |
---|
[2135] | 18 | type = 'int', |
---|
[2194] | 19 | dest = 'ramtotal', |
---|
[2135] | 20 | help = 'set total concurrent RAM quota') |
---|
[2194] | 21 | parser.add_option('-n', '--ram-single', |
---|
[2135] | 22 | type = 'int', |
---|
[2194] | 23 | dest = 'ramsingle', |
---|
[2135] | 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() |
---|
[2132] | 42 | |
---|
[2135] | 43 | if len(args) != 1: |
---|
[2198] | 44 | parser.print_help(sys.stderr) |
---|
[2135] | 45 | return 1 |
---|
| 46 | owner = args[0] |
---|
[2198] | 47 | database.connect() |
---|
| 48 | database.session.begin() |
---|
[2135] | 49 | |
---|
[2198] | 50 | x = database.Owner.query().filter_by(owner_id=owner).first() |
---|
[2194] | 51 | if x is None: |
---|
[2198] | 52 | x = database.Owner(owner_id=owner) |
---|
[2132] | 53 | |
---|
[2194] | 54 | for resource, scope in [('ram', 'total'), ('ram', 'single'), |
---|
| 55 | ('disk', 'total'), ('disk', 'single'), |
---|
| 56 | ('vms', 'total'), ('vms', 'active')]: |
---|
[2198] | 57 | val = getattr(opts, resource+scope) |
---|
| 58 | if val is not None: |
---|
[2194] | 59 | setattr(x, resource+'_quota_'+scope, val if val >= 0 else None) |
---|
[2132] | 60 | |
---|
[2198] | 61 | database.session.commit() |
---|
[2135] | 62 | print str(x) |
---|
| 63 | return 0 |
---|
[2132] | 64 | |
---|
| 65 | if __name__ == '__main__': |
---|
[2198] | 66 | sys.exit(main(sys.argv)) |
---|