Rename invirt-setquotas to invirt-quota.
[invirt/packages/invirt-database.git] / scripts / invirt-quota
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)
56
57     if opts.memtotal != None:
58         total = int(opts.memtotal)
59         if total == -1:
60             x.ram_quota_total = None
61         else:
62             x.ram_quota_total = total
63
64     if opts.memsingle != None:
65         single = int(opts.memsingle)
66         if single == -1:
67             x.ram_quota_single = None
68         else:
69             x.ram_quota_single = single
70
71     if opts.disktotal != None:
72         total = int(opts.disktotal)
73         if total == -1:
74             x.disk_quota_total = None
75         else:
76             x.disk_quota_total = total
77
78     if opts.disksingle != None:
79         single = int(opts.disksingle)
80         if single == -1:
81             x.disk_quota_single = None
82         else:
83             x.disk_quota_single = single
84
85     if opts.vmstotal != None:
86         total = int(opts.vmstotal)
87         if total == -1:
88             x.vms_quota_total = None
89         else:
90             x.vms_quota_total = total
91
92     if opts.vmsactive != None:
93         active = int(opts.vmsactive)
94         if active == -1:
95             x.vms_quota_active = None
96         else:
97             x.vms_quota_active = active
98
99     session.commit()
100     print str(x)
101     return 0
102
103 if __name__ == '__main__':
104     exit(main(argv))