[2132] | 1 | #!/usr/bin/env python |
---|
| 2 | |
---|
| 3 | """ |
---|
| 4 | invirt-setquota allows an administrator to set the RAM quotas for an owner. |
---|
| 5 | Invoking with only an owner name returns the current quotas for that owner. |
---|
| 6 | Setting a parameter to -1 restores the default. |
---|
| 7 | |
---|
| 8 | Examples: |
---|
| 9 | |
---|
| 10 | invirt-setquota joeuser -t 512 -s None |
---|
| 11 | """ |
---|
| 12 | |
---|
| 13 | from invirt.database import * |
---|
| 14 | from sys import argv, exit, stderr, stdout |
---|
| 15 | from optparse import OptionParser |
---|
| 16 | |
---|
| 17 | class invirt_exception(Exception): pass |
---|
| 18 | |
---|
| 19 | def main(argv): |
---|
| 20 | try: |
---|
| 21 | parser = OptionParser(usage = '%prog owner [options]', |
---|
| 22 | description = __doc__.strip().split('\n\n')[0]) |
---|
| 23 | parser.add_option('-t', '--total', |
---|
| 24 | type = 'int', |
---|
| 25 | dest = 'total', |
---|
| 26 | help = 'set the total concurrent RAM quota') |
---|
| 27 | parser.add_option('-s', '--single', |
---|
| 28 | type = 'int', |
---|
| 29 | dest = 'single', |
---|
| 30 | help = 'set the single VM RAM quota') |
---|
| 31 | opts, args = parser.parse_args() |
---|
| 32 | |
---|
| 33 | if len(args) != 1: |
---|
| 34 | raise invirt_exception(__doc__.strip()) |
---|
| 35 | owner = args[0] |
---|
| 36 | connect() |
---|
| 37 | session.begin() |
---|
| 38 | |
---|
| 39 | x = Owner.query().filter_by(owner_id=owner).first() |
---|
| 40 | |
---|
| 41 | edited = False |
---|
| 42 | if opts.total != None: |
---|
| 43 | total = int(opts.total) |
---|
| 44 | if total == -1: |
---|
| 45 | x.ram_quota_total = None |
---|
| 46 | else: |
---|
| 47 | x.ram_quota_total = total |
---|
| 48 | edited = True |
---|
| 49 | |
---|
| 50 | if opts.single != None: |
---|
| 51 | single = int(opts.single) |
---|
| 52 | if single == -1: |
---|
| 53 | x.ram_quota_single = None |
---|
| 54 | else: |
---|
| 55 | x.ram_quota_single = single |
---|
| 56 | edited = True |
---|
| 57 | |
---|
| 58 | if edited: |
---|
| 59 | session.commit() |
---|
| 60 | print str(x) |
---|
| 61 | |
---|
| 62 | except invirt_exception, ex: |
---|
| 63 | print >> stderr, ex |
---|
| 64 | return 1 |
---|
| 65 | |
---|
| 66 | if __name__ == '__main__': |
---|
| 67 | exit(main(argv)) |
---|