root / snf-astakos-app / astakos / im / management / commands / quota.py @ 1b27f8a2
History | View | Annotate | Download (8.9 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 CommandError |
36 |
|
37 |
from astakos.im.models import AstakosUser |
38 |
from astakos.im.quotas import ( |
39 |
qh_sync_users_diffs, list_user_quotas, add_base_quota) |
40 |
from astakos.im.functions import get_user_by_uuid |
41 |
from astakos.im.management.commands._common import is_uuid, is_email |
42 |
from snf_django.lib.db.transaction import commit_on_success_strict |
43 |
from synnefo.webproject.management.commands import SynnefoCommand |
44 |
from synnefo.webproject.management import utils |
45 |
from ._common import show_quotas, style_options, check_style, units |
46 |
|
47 |
import logging |
48 |
logger = logging.getLogger(__name__) |
49 |
|
50 |
|
51 |
class Command(SynnefoCommand): |
52 |
help = "List and check the integrity of user quota"
|
53 |
|
54 |
option_list = SynnefoCommand.option_list + ( |
55 |
make_option('--list',
|
56 |
action='store_true',
|
57 |
dest='list',
|
58 |
default=False,
|
59 |
help="List all quota (default)"),
|
60 |
make_option('--unit-style',
|
61 |
default='mb',
|
62 |
help=("Specify display unit for resource values "
|
63 |
"(one of %s); defaults to mb") % style_options),
|
64 |
make_option('--verify',
|
65 |
action='store_true',
|
66 |
dest='verify',
|
67 |
default=False,
|
68 |
help="Check if quotaholder is in sync with astakos"),
|
69 |
make_option('--sync',
|
70 |
action='store_true',
|
71 |
dest='sync',
|
72 |
default=False,
|
73 |
help="Sync quotaholder"),
|
74 |
make_option('--user',
|
75 |
metavar='<uuid or email>',
|
76 |
dest='user',
|
77 |
help="List quota for a specified user"),
|
78 |
make_option('--import-base-quota',
|
79 |
dest='import_base_quota',
|
80 |
metavar='<exported-quota.txt>',
|
81 |
help=("Import base quota from file. "
|
82 |
"The file must contain non-empty lines, and each "
|
83 |
"line must contain a single-space-separated list "
|
84 |
"of values: <user> <resource name> <capacity>. "
|
85 |
"Capacity can be followed by a unit with no "
|
86 |
"separating space (e.g 10GB).")
|
87 |
), |
88 |
) |
89 |
|
90 |
@commit_on_success_strict()
|
91 |
def handle(self, *args, **options): |
92 |
sync = options['sync']
|
93 |
verify = options['verify']
|
94 |
user_ident = options['user']
|
95 |
list_ = options['list']
|
96 |
import_base_quota = options['import_base_quota']
|
97 |
|
98 |
if import_base_quota:
|
99 |
if any([sync, verify, list_]): |
100 |
m = "--from-file cannot be combined with other options."
|
101 |
raise CommandError(m)
|
102 |
self.import_from_file(import_base_quota)
|
103 |
else:
|
104 |
unit_style = options["unit_style"]
|
105 |
check_style(unit_style) |
106 |
|
107 |
self.quotas(sync, verify, user_ident, options["output_format"], |
108 |
unit_style) |
109 |
|
110 |
def quotas(self, sync, verify, user_ident, output_format, style): |
111 |
list_only = not sync and not verify |
112 |
|
113 |
if user_ident is not None: |
114 |
users = [self.get_user(user_ident)]
|
115 |
else:
|
116 |
users = AstakosUser.objects.moderated() |
117 |
|
118 |
if list_only:
|
119 |
qh_quotas, astakos_i = list_user_quotas(users) |
120 |
|
121 |
info = {} |
122 |
for user in users: |
123 |
info[user.uuid] = user.email |
124 |
|
125 |
print_data, labels = show_quotas(qh_quotas, astakos_i, info, |
126 |
style=style) |
127 |
utils.pprint_table(self.stdout, print_data, labels,
|
128 |
output_format) |
129 |
|
130 |
elif verify or sync: |
131 |
qh_limits, diff_q = qh_sync_users_diffs(users, sync=sync) |
132 |
if verify:
|
133 |
self.print_verify(qh_limits, diff_q)
|
134 |
if sync:
|
135 |
self.print_sync(diff_q)
|
136 |
|
137 |
def get_user(self, user_ident): |
138 |
if is_uuid(user_ident):
|
139 |
try:
|
140 |
user = AstakosUser.objects.get(uuid=user_ident) |
141 |
except AstakosUser.DoesNotExist:
|
142 |
raise CommandError('There is no user with uuid: %s' % |
143 |
user_ident) |
144 |
elif is_email(user_ident):
|
145 |
try:
|
146 |
user = AstakosUser.objects.get(username=user_ident) |
147 |
except AstakosUser.DoesNotExist:
|
148 |
raise CommandError('There is no user with email: %s' % |
149 |
user_ident) |
150 |
else:
|
151 |
raise CommandError('Please specify user by uuid or email') |
152 |
|
153 |
if not user.moderated: |
154 |
raise CommandError('%s is not an accepted user.\n' % user.uuid) |
155 |
|
156 |
return user
|
157 |
|
158 |
def print_sync(self, diff_quotas): |
159 |
size = len(diff_quotas)
|
160 |
if size == 0: |
161 |
self.stdout.write("No sync needed.\n") |
162 |
else:
|
163 |
self.stdout.write("Synced %s users:\n" % size) |
164 |
for holder in diff_quotas.keys(): |
165 |
user = get_user_by_uuid(holder) |
166 |
self.stdout.write("%s (%s)\n" % (holder, user.username)) |
167 |
|
168 |
def print_verify(self, |
169 |
qh_limits, |
170 |
diff_quotas): |
171 |
|
172 |
for holder, local in diff_quotas.iteritems(): |
173 |
registered = qh_limits.pop(holder, None)
|
174 |
user = get_user_by_uuid(holder) |
175 |
if registered is None: |
176 |
self.stdout.write(
|
177 |
"No quota for %s (%s) in quotaholder.\n" %
|
178 |
(holder, user.username)) |
179 |
else:
|
180 |
self.stdout.write("Quota differ for %s (%s):\n" % |
181 |
(holder, user.username)) |
182 |
self.stdout.write("Quota according to quotaholder:\n") |
183 |
self.stdout.write("%s\n" % (registered)) |
184 |
self.stdout.write("Quota according to astakos:\n") |
185 |
self.stdout.write("%s\n\n" % (local)) |
186 |
|
187 |
diffs = len(diff_quotas)
|
188 |
if diffs:
|
189 |
self.stdout.write("Quota differ for %d users.\n" % (diffs)) |
190 |
|
191 |
def import_from_file(self, location): |
192 |
users = set()
|
193 |
with open(location) as f: |
194 |
for line in f.readlines(): |
195 |
try:
|
196 |
t = line.rstrip('\n').split(' ') |
197 |
user = t[0]
|
198 |
resource = t[1]
|
199 |
capacity = t[2]
|
200 |
try:
|
201 |
capacity = units.parse(capacity) |
202 |
except units.ParseError:
|
203 |
m = ("Capacity should be an integer, optionally "
|
204 |
"followed by a unit.")
|
205 |
raise CommandError(m)
|
206 |
except(IndexError, TypeError): |
207 |
self.stdout.write('Invalid line format: %s:\n' % t) |
208 |
continue
|
209 |
else:
|
210 |
try:
|
211 |
user = self.get_user(user)
|
212 |
users.add(user.id) |
213 |
except CommandError:
|
214 |
self.stdout.write('Not found user: %s\n' % user) |
215 |
continue
|
216 |
else:
|
217 |
try:
|
218 |
add_base_quota(user, resource, capacity) |
219 |
except Exception, e: |
220 |
self.stdout.write('Failed to add quota: %s\n' % e) |
221 |
continue
|