root / snf-astakos-app / astakos / im / management / commands / astakos-quota.py @ 0da5e49a
History | View | Annotate | Download (7.2 kB)
1 |
# Copyright 2012, 2013 GRNET S.A. All rights reserved.
|
---|---|
2 |
#
|
3 |
# Redistribution and use in source and binary forms, with or
|
4 |
# without modification, are permitted provided that the following
|
5 |
# conditions are met:
|
6 |
#
|
7 |
# 1. Redistributions of source code must retain the above
|
8 |
# copyright notice, this list of conditions and the following
|
9 |
# disclaimer.
|
10 |
#
|
11 |
# 2. Redistributions in binary form must reproduce the above
|
12 |
# copyright notice, this list of conditions and the following
|
13 |
# disclaimer in the documentation and/or other materials
|
14 |
# provided with the distribution.
|
15 |
#
|
16 |
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
|
17 |
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
18 |
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
19 |
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
|
20 |
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
21 |
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
22 |
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
23 |
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
24 |
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
25 |
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
26 |
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
27 |
# POSSIBILITY OF SUCH DAMAGE.
|
28 |
#
|
29 |
# The views and conclusions contained in the software and
|
30 |
# documentation are those of the authors and should not be
|
31 |
# interpreted as representing official policies, either expressed
|
32 |
# or implied, of GRNET S.A.
|
33 |
|
34 |
from optparse import make_option |
35 |
from django.core.management.base import BaseCommand, CommandError |
36 |
|
37 |
from astakos.im.models import sync_all_users, sync_users, AstakosUser |
38 |
from astakos.im.functions import get_user_by_uuid |
39 |
from astakos.im.management.commands._common import is_uuid, is_email |
40 |
from synnefo.lib.db.transaction import commit_on_success_strict |
41 |
|
42 |
import logging |
43 |
logger = logging.getLogger(__name__) |
44 |
|
45 |
|
46 |
class Command(BaseCommand): |
47 |
help = "Inspect quotaholder status"
|
48 |
|
49 |
option_list = BaseCommand.option_list + ( |
50 |
make_option('--list',
|
51 |
action='store_true',
|
52 |
dest='list',
|
53 |
default=False,
|
54 |
help="List all quotas (default)"),
|
55 |
make_option('--verify',
|
56 |
action='store_true',
|
57 |
dest='verify',
|
58 |
default=False,
|
59 |
help="Check if quotaholder is in sync with astakos"),
|
60 |
make_option('--sync',
|
61 |
action='store_true',
|
62 |
dest='sync',
|
63 |
default=False,
|
64 |
help="Sync quotaholder"),
|
65 |
make_option('--user',
|
66 |
metavar='<uuid or email>',
|
67 |
dest='user',
|
68 |
help="List quotas for a specified user"),
|
69 |
) |
70 |
|
71 |
def handle(self, *args, **options): |
72 |
sync = options['sync']
|
73 |
verify = options['verify']
|
74 |
user_ident = options['user']
|
75 |
list_only = not sync and not verify |
76 |
|
77 |
|
78 |
if user_ident is not None: |
79 |
log = self.run_sync_user(user_ident, sync)
|
80 |
else:
|
81 |
log = self.run(sync)
|
82 |
|
83 |
qh_limits, qh_quotas, astakos_i, diff_q, info = log |
84 |
|
85 |
if list_only:
|
86 |
self.list_quotas(qh_quotas, astakos_i, info)
|
87 |
else:
|
88 |
if verify:
|
89 |
self.print_verify(qh_limits, diff_q)
|
90 |
if sync:
|
91 |
self.print_sync(diff_q)
|
92 |
|
93 |
@commit_on_success_strict()
|
94 |
def run_sync_user(self, user_ident, sync): |
95 |
if is_uuid(user_ident):
|
96 |
try:
|
97 |
user = AstakosUser.objects.get(uuid=user_ident) |
98 |
except AstakosUser.DoesNotExist:
|
99 |
raise CommandError('Not found user having uuid: %s' % |
100 |
user_ident) |
101 |
elif is_email(user_ident):
|
102 |
try:
|
103 |
user = AstakosUser.objects.get(username=user_ident) |
104 |
except AstakosUser.DoesNotExist:
|
105 |
raise CommandError('Not found user having email: %s' % |
106 |
user_ident) |
107 |
else:
|
108 |
raise CommandError('Please specify user by uuid or email') |
109 |
|
110 |
if not user.email_verified and sync: |
111 |
raise CommandError('User %s is not verified.' % user.uuid) |
112 |
|
113 |
try:
|
114 |
return sync_users([user], sync=sync)
|
115 |
except BaseException, e: |
116 |
logger.exception(e) |
117 |
raise CommandError("Failed to compute quotas.") |
118 |
|
119 |
@commit_on_success_strict()
|
120 |
def run(self, sync): |
121 |
try:
|
122 |
self.stderr.write("Calculating all quotas...\n") |
123 |
return sync_all_users(sync=sync)
|
124 |
except BaseException, e: |
125 |
logger.exception(e) |
126 |
raise CommandError("Syncing failed.") |
127 |
|
128 |
def list_quotas(self, qh_quotas, astakos_initial, info): |
129 |
labels = ('uuid', 'email', 'source', 'resource', 'initial', 'total', 'usage') |
130 |
columns = (36, 30, 20, 24, 12, 12, 12) |
131 |
|
132 |
line = ' '.join(l.rjust(w) for l, w in zip(labels, columns)) |
133 |
self.stdout.write(line + '\n') |
134 |
sep = '-' * len(line) |
135 |
self.stdout.write(sep + '\n') |
136 |
|
137 |
for holder, holder_quotas in qh_quotas.iteritems(): |
138 |
h_initial = astakos_initial[holder] |
139 |
email = info[holder] |
140 |
for source, source_quotas in holder_quotas.iteritems(): |
141 |
s_initial = h_initial[source] |
142 |
for resource, values in source_quotas.iteritems(): |
143 |
initial = str(s_initial[resource])
|
144 |
capacity = str(values['limit']) |
145 |
used = str(values['used']) |
146 |
|
147 |
fields = holder, email, source, resource, initial, capacity, used |
148 |
output = [] |
149 |
for field, width in zip(fields, columns): |
150 |
s = field.rjust(width) |
151 |
output.append(s) |
152 |
|
153 |
line = ' '.join(output)
|
154 |
self.stdout.write(line + '\n') |
155 |
|
156 |
def print_sync(self, diff_quotas): |
157 |
size = len(diff_quotas)
|
158 |
if size == 0: |
159 |
self.stdout.write("No sync needed.\n") |
160 |
else:
|
161 |
self.stdout.write("Synced %s users:\n" % size) |
162 |
for holder in diff_quotas.keys(): |
163 |
user = get_user_by_uuid(holder) |
164 |
self.stdout.write("%s (%s)\n" % (holder, user.username)) |
165 |
|
166 |
def print_verify(self, |
167 |
qh_limits, |
168 |
diff_quotas): |
169 |
|
170 |
for holder, local in diff_quotas.iteritems(): |
171 |
registered = qh_limits.pop(holder, None)
|
172 |
user = get_user_by_uuid(holder) |
173 |
if registered is None: |
174 |
self.stdout.write(
|
175 |
"No quotas for %s (%s) in quotaholder.\n" %
|
176 |
(holder, user.username)) |
177 |
else:
|
178 |
self.stdout.write("Quotas differ for %s (%s):\n" % |
179 |
(holder, user.username)) |
180 |
self.stdout.write("Quotas according to quotaholder:\n") |
181 |
self.stdout.write("%s\n" % (registered)) |
182 |
self.stdout.write("Quotas according to astakos:\n") |
183 |
self.stdout.write("%s\n\n" % (local)) |
184 |
|
185 |
diffs = len(diff_quotas)
|
186 |
if diffs:
|
187 |
self.stdout.write("Quotas differ for %d users.\n" % (diffs)) |