[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 | |
---|
| 9 | from invirt.database import * |
---|
| 10 | from sys import argv, exit, stderr, stdout |
---|
| 11 | from optparse import OptionParser |
---|
| 12 | |
---|
| 13 | def main(argv): |
---|
[2194] | 14 | parser = OptionParser(usage = '%prog <owner> [options]', |
---|
| 15 | description = __doc__.strip()) |
---|
| 16 | parser.add_option('-m', '--ram-total', |
---|
[2135] | 17 | type = 'int', |
---|
[2194] | 18 | dest = 'ramtotal', |
---|
[2135] | 19 | help = 'set total concurrent RAM quota') |
---|
[2194] | 20 | parser.add_option('-n', '--ram-single', |
---|
[2135] | 21 | type = 'int', |
---|
[2194] | 22 | dest = 'ramsingle', |
---|
[2135] | 23 | help = 'set single VM RAM quota') |
---|
| 24 | parser.add_option('-d', '--disk-total', |
---|
| 25 | type = 'int', |
---|
| 26 | dest = 'disktotal', |
---|
| 27 | help = 'set total disk quota') |
---|
| 28 | parser.add_option('-e', '--disk-single', |
---|
| 29 | type = 'int', |
---|
| 30 | dest = 'disksingle', |
---|
| 31 | help = 'set single VM disk quota') |
---|
| 32 | parser.add_option('-v', '--vms-total', |
---|
| 33 | type = 'int', |
---|
| 34 | dest = 'vmstotal', |
---|
| 35 | help = 'set total VM quota') |
---|
| 36 | parser.add_option('-w', '--vms-active', |
---|
| 37 | type = 'int', |
---|
| 38 | dest = 'vmsactive', |
---|
| 39 | help = 'set active VM quota') |
---|
| 40 | opts, args = parser.parse_args() |
---|
[2132] | 41 | |
---|
[2135] | 42 | if len(args) != 1: |
---|
[2194] | 43 | parser.print_help(stderr) |
---|
[2135] | 44 | return 1 |
---|
| 45 | owner = args[0] |
---|
| 46 | connect() |
---|
| 47 | session.begin() |
---|
| 48 | |
---|
| 49 | x = Owner.query().filter_by(owner_id=owner).first() |
---|
[2194] | 50 | if x is None: |
---|
[2152] | 51 | x = Owner(owner_id=owner) |
---|
[2132] | 52 | |
---|
[2194] | 53 | for resource, scope in [('ram', 'total'), ('ram', 'single'), |
---|
| 54 | ('disk', 'total'), ('disk', 'single'), |
---|
| 55 | ('vms', 'total'), ('vms', 'active')]: |
---|
| 56 | opt = getattr(opts, resource+scope) |
---|
| 57 | if opt is not None: |
---|
| 58 | val = int(opt) |
---|
| 59 | setattr(x, resource+'_quota_'+scope, val if val >= 0 else None) |
---|
[2132] | 60 | |
---|
[2135] | 61 | session.commit() |
---|
| 62 | print str(x) |
---|
| 63 | return 0 |
---|
[2132] | 64 | |
---|
| 65 | if __name__ == '__main__': |
---|
| 66 | exit(main(argv)) |
---|