1 | #!/usr/bin/env python |
---|
2 | |
---|
3 | """ |
---|
4 | invirt-setquota 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 | Examples: |
---|
9 | |
---|
10 | invirt-setquota joeuser -mt 512 -ms -1 |
---|
11 | """ |
---|
12 | |
---|
13 | from invirt.database import * |
---|
14 | from sys import argv, exit, stderr, stdout |
---|
15 | from optparse import OptionParser |
---|
16 | |
---|
17 | def main(argv): |
---|
18 | parser = OptionParser(usage = '%prog owner [options]', |
---|
19 | description = __doc__.strip().split('\n\n')[0]) |
---|
20 | parser.add_option('-m', '--mem-total', |
---|
21 | type = 'int', |
---|
22 | dest = 'memtotal', |
---|
23 | help = 'set total concurrent RAM quota') |
---|
24 | parser.add_option('-n', '--mem-single', |
---|
25 | type = 'int', |
---|
26 | dest = 'memsingle', |
---|
27 | help = 'set single VM RAM quota') |
---|
28 | parser.add_option('-d', '--disk-total', |
---|
29 | type = 'int', |
---|
30 | dest = 'disktotal', |
---|
31 | help = 'set total disk quota') |
---|
32 | parser.add_option('-e', '--disk-single', |
---|
33 | type = 'int', |
---|
34 | dest = 'disksingle', |
---|
35 | help = 'set single VM disk quota') |
---|
36 | parser.add_option('-v', '--vms-total', |
---|
37 | type = 'int', |
---|
38 | dest = 'vmstotal', |
---|
39 | help = 'set total VM quota') |
---|
40 | parser.add_option('-w', '--vms-active', |
---|
41 | type = 'int', |
---|
42 | dest = 'vmsactive', |
---|
43 | help = 'set active VM quota') |
---|
44 | opts, args = parser.parse_args() |
---|
45 | |
---|
46 | if len(args) != 1: |
---|
47 | print >> stderr, __doc__.strip() |
---|
48 | return 1 |
---|
49 | owner = args[0] |
---|
50 | connect() |
---|
51 | session.begin() |
---|
52 | |
---|
53 | x = Owner.query().filter_by(owner_id=owner).first() |
---|
54 | if x == None: |
---|
55 | x = Owner(owner_id=owner, ram_quota_total=None, ram_quota_single=None, |
---|
56 | disk_quota_total=None, disk_quota_single=None, |
---|
57 | vms_quota_total=None, vms_quota_active=None) |
---|
58 | |
---|
59 | if opts.memtotal != None: |
---|
60 | total = int(opts.memtotal) |
---|
61 | if total == -1: |
---|
62 | x.ram_quota_total = None |
---|
63 | else: |
---|
64 | x.ram_quota_total = total |
---|
65 | |
---|
66 | if opts.memsingle != None: |
---|
67 | single = int(opts.memsingle) |
---|
68 | if single == -1: |
---|
69 | x.ram_quota_single = None |
---|
70 | else: |
---|
71 | x.ram_quota_single = single |
---|
72 | |
---|
73 | if opts.disktotal != None: |
---|
74 | total = int(opts.disktotal) |
---|
75 | if total == -1: |
---|
76 | x.disk_quota_total = None |
---|
77 | else: |
---|
78 | x.disk_quota_total = total |
---|
79 | |
---|
80 | if opts.disksingle != None: |
---|
81 | single = int(opts.disksingle) |
---|
82 | if single == -1: |
---|
83 | x.disk_quota_single = None |
---|
84 | else: |
---|
85 | x.disk_quota_single = single |
---|
86 | |
---|
87 | if opts.vmstotal != None: |
---|
88 | total = int(opts.vmstotal) |
---|
89 | if total == -1: |
---|
90 | x.vms_quota_total = None |
---|
91 | else: |
---|
92 | x.vms_quota_total = total |
---|
93 | |
---|
94 | if opts.vmsactive != None: |
---|
95 | active = int(opts.vmsactive) |
---|
96 | if active == -1: |
---|
97 | x.vms_quota_active = None |
---|
98 | else: |
---|
99 | x.vms_quota_active = active |
---|
100 | |
---|
101 | session.commit() |
---|
102 | print str(x) |
---|
103 | return 0 |
---|
104 | |
---|
105 | if __name__ == '__main__': |
---|
106 | exit(main(argv)) |
---|