quota.py 8.86 KB
Newer Older
1
# Copyright 2012, 2013 GRNET S.A. All rights reserved.
Sofia Papagiannaki's avatar
Sofia Papagiannaki committed
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#
# Redistribution and use in source and binary forms, with or
# without modification, are permitted provided that the following
# conditions are met:
#
#   1. Redistributions of source code must retain the above
#      copyright notice, this list of conditions and the following
#      disclaimer.
#
#   2. Redistributions in binary form must reproduce the above
#      copyright notice, this list of conditions and the following
#      disclaimer in the documentation and/or other materials
#      provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
# The views and conclusions contained in the software and
# documentation are those of the authors and should not be
# interpreted as representing official policies, either expressed
# or implied, of GRNET S.A.

34
from optparse import make_option
35
from django.core.management.base import CommandError
Sofia Papagiannaki's avatar
Sofia Papagiannaki committed
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)
Giorgos Korfiatis's avatar
Giorgos Korfiatis committed
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
44
from synnefo.webproject.management.commands import SynnefoCommand
from synnefo.webproject.management import utils
45
from ._common import show_quotas, style_options, check_style, units
Sofia Papagiannaki's avatar
Sofia Papagiannaki committed
46

Olga Brani's avatar
Olga Brani committed
47
48
49
import logging
logger = logging.getLogger(__name__)

50

51
class Command(SynnefoCommand):
52
    help = "List and check the integrity of user quota"
53

54
    option_list = SynnefoCommand.option_list + (
55
56
57
58
        make_option('--list',
                    action='store_true',
                    dest='list',
                    default=False,
59
                    help="List all quota (default)"),
60
61
62
63
        make_option('--unit-style',
                    default='mb',
                    help=("Specify display unit for resource values "
                          "(one of %s); defaults to mb") % style_options),
64
        make_option('--verify',
65
                    action='store_true',
66
67
68
                    dest='verify',
                    default=False,
                    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
75
76
        make_option('--user',
                    metavar='<uuid or email>',
                    dest='user',
77
                    help="List quota for a specified user"),
78
79
        make_option('--import-base-quota',
                    dest='import_base_quota',
80
81
                    metavar='<exported-quota.txt>',
                    help=("Import base quota from file. "
82
83
                          "The file must contain non-empty lines, and each "
                          "line must contain a single-space-separated list "
84
85
86
                          "of values: <user> <resource name> <capacity>. "
                          "Capacity can be followed by a unit with no "
                          "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
96
97
98
99
100
101
102
103
        list_ = options['list']
        import_base_quota = options['import_base_quota']

        if import_base_quota:
            if any([sync, verify, list_]):
                m = "--from-file cannot be combined with other options."
                raise CommandError(m)
            self.import_from_file(import_base_quota)
        else:
104
105
            unit_style = options["unit_style"]
            check_style(unit_style)
106

107
108
109
110
            self.quotas(sync, verify, user_ident, options["output_format"],
                        unit_style)

    def quotas(self, sync, verify, user_ident, output_format, style):
111
112
        list_only = not sync and not verify

113
        if user_ident is not None:
114
            users = [self.get_user(user_ident)]
115
        else:
116
            users = AstakosUser.objects.accepted()
117

118
119
        if list_only:
            qh_quotas, astakos_i = list_user_quotas(users)
120

121
122
123
            info = {}
            for user in users:
                info[user.uuid] = user.email
124

125
126
            print_data, labels = show_quotas(qh_quotas, astakos_i, info,
                                             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
135
            if sync:
                self.print_sync(diff_q)
136

137
    def get_user(self, user_ident):
138
139
140
141
        if is_uuid(user_ident):
            try:
                user = AstakosUser.objects.get(uuid=user_ident)
            except AstakosUser.DoesNotExist:
142
                raise CommandError('There is no user with uuid: %s' %
143
144
145
146
147
                                   user_ident)
        elif is_email(user_ident):
            try:
                user = AstakosUser.objects.get(username=user_ident)
            except AstakosUser.DoesNotExist:
148
                raise CommandError('There is no user with email: %s' %
149
150
151
152
                                   user_ident)
        else:
            raise CommandError('Please specify user by uuid or email')

153
154
        if not user.is_accepted():
            raise CommandError('%s is not an accepted user.' % user.uuid)
155

156
        return user
157

158
159
160
161
162
163
164
165
166
167
    def print_sync(self, diff_quotas):
        size = len(diff_quotas)
        if size == 0:
            self.stdout.write("No sync needed.\n")
        else:
            self.stdout.write("Synced %s users:\n" % size)
            for holder in diff_quotas.keys():
                user = get_user_by_uuid(holder)
                self.stdout.write("%s (%s)\n" % (holder, user.username))

168
169
    def print_verify(self,
                     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)
Giorgos Korfiatis's avatar
Giorgos Korfiatis committed
175
                if registered is None:
176
                    self.stdout.write(
177
                        "No quota for %s (%s) in quotaholder.\n" %
178
179
                        (holder, user.username))
                else:
180
                    self.stdout.write("Quota differ for %s (%s):\n" %
181
                                      (holder, user.username))
182
                    self.stdout.write("Quota according to quotaholder:\n")
Giorgos Korfiatis's avatar
Giorgos Korfiatis committed
183
                    self.stdout.write("%s\n" % (registered))
184
                    self.stdout.write("Quota according to astakos:\n")
Giorgos Korfiatis's avatar
Giorgos Korfiatis committed
185
186
                    self.stdout.write("%s\n\n" % (local))

187
            diffs = len(diff_quotas)
Giorgos Korfiatis's avatar
Giorgos Korfiatis committed
188
            if diffs:
189
                self.stdout.write("Quota differ for %d users.\n" % (diffs))
190
191
192
193
194
195
196
197
198
199

    def import_from_file(self, location):
        users = set()
        with open(location) as f:
            for line in f.readlines():
                try:
                    t = line.rstrip('\n').split(' ')
                    user = t[0]
                    resource = t[1]
                    capacity = t[2]
200
201
202
203
204
205
                    try:
                        capacity = units.parse(capacity)
                    except units.ParseError:
                        m = ("Capacity should be an integer, optionally "
                             "followed by a unit.")
                        raise CommandError(m)
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
                except(IndexError, TypeError):
                    self.stdout.write('Invalid line format: %s:\n' % t)
                    continue
                else:
                    try:
                        user = self.get_user(user)
                        users.add(user.id)
                    except CommandError:
                        self.stdout.write('Not found user: %s\n' % user)
                        continue
                    else:
                        try:
                            add_base_quota(user, resource, capacity)
                        except Exception, e:
                            self.stdout.write('Failed to add quota: %s\n' % e)
                            continue