| 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 | from invirt.database import * |
|---|
| 10 | from sys import argv, exit, stderr, stdout |
|---|
| 11 | from optparse import OptionParser |
|---|
| 12 | |
|---|
| 13 | def main(argv): |
|---|
| 14 | parser = OptionParser(usage = '%prog <owner> [options]', |
|---|
| 15 | description = __doc__.strip()) |
|---|
| 16 | parser.add_option('-m', '--ram-total', |
|---|
| 17 | type = 'int', |
|---|
| 18 | dest = 'ramtotal', |
|---|
| 19 | help = 'set total concurrent RAM quota') |
|---|
| 20 | parser.add_option('-n', '--ram-single', |
|---|
| 21 | type = 'int', |
|---|
| 22 | dest = 'ramsingle', |
|---|
| 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() |
|---|
| 41 | |
|---|
| 42 | if len(args) != 1: |
|---|
| 43 | parser.print_help(stderr) |
|---|
| 44 | return 1 |
|---|
| 45 | owner = args[0] |
|---|
| 46 | connect() |
|---|
| 47 | session.begin() |
|---|
| 48 | |
|---|
| 49 | x = Owner.query().filter_by(owner_id=owner).first() |
|---|
| 50 | if x is None: |
|---|
| 51 | x = Owner(owner_id=owner) |
|---|
| 52 | |
|---|
| 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) |
|---|
| 60 | |
|---|
| 61 | session.commit() |
|---|
| 62 | print str(x) |
|---|
| 63 | return 0 |
|---|
| 64 | |
|---|
| 65 | if __name__ == '__main__': |
|---|
| 66 | exit(main(argv)) |
|---|