Statistics
| Branch: | Tag: | Revision:

root / snf-astakos-app / astakos / im / management / commands / quota-verify.py @ 4220c336

History | View | Annotate | Download (4.1 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.db import transaction
36

    
37
from astakos.im.models import AstakosUser
38
from astakos.im.quotas import (
39
    qh_sync_users_diffs,)
40
from astakos.im.functions import get_user_by_uuid
41
from snf_django.management.commands import SynnefoCommand
42
from astakos.im.management.commands import _common as common
43

    
44
import logging
45
logger = logging.getLogger(__name__)
46

    
47

    
48
class Command(SynnefoCommand):
49
    help = "Check the integrity of user quota"
50

    
51
    option_list = SynnefoCommand.option_list + (
52
        make_option('--sync',
53
                    action='store_true',
54
                    dest='sync',
55
                    default=False,
56
                    help="Sync quotaholder"),
57
        make_option('--user',
58
                    metavar='<uuid or email>',
59
                    dest='user',
60
                    help="Check for a specified user"),
61
    )
62

    
63
    @transaction.commit_on_success
64
    def handle(self, *args, **options):
65
        sync = options['sync']
66
        user_ident = options['user']
67

    
68
        if user_ident is not None:
69
            users = [common.get_accepted_user(user_ident)]
70
        else:
71
            users = AstakosUser.objects.accepted()
72

    
73
        qh_limits, diff_q = qh_sync_users_diffs(users, sync=sync)
74
        if sync:
75
            self.print_sync(diff_q)
76
        else:
77
            self.print_verify(qh_limits, diff_q)
78

    
79
    def print_sync(self, diff_quotas):
80
        size = len(diff_quotas)
81
        if size == 0:
82
            self.stderr.write("No sync needed.\n")
83
        else:
84
            self.stderr.write("Synced %s users:\n" % size)
85
            uuids = diff_quotas.keys()
86
            users = AstakosUser.objects.filter(uuid__in=uuids)
87
            for user in users:
88
                self.stderr.write("%s (%s)\n" % (user.uuid, user.username))
89

    
90
    def print_verify(self, qh_limits, diff_quotas):
91
        for holder, local in diff_quotas.iteritems():
92
            registered = qh_limits.pop(holder, None)
93
            user = get_user_by_uuid(holder)
94
            if registered is None:
95
                self.stderr.write(
96
                    "No quota for %s (%s) in quotaholder.\n" %
97
                    (holder, user.username))
98
            else:
99
                self.stdout.write("Quota differ for %s (%s):\n" %
100
                                  (holder, user.username))
101
                self.stdout.write("Quota according to quotaholder:\n")
102
                self.stdout.write("%s\n" % (registered))
103
                self.stdout.write("Quota according to astakos:\n")
104
                self.stdout.write("%s\n\n" % (local))
105

    
106
        diffs = len(diff_quotas)
107
        if diffs:
108
            self.stderr.write("Quota differ for %d users.\n" % (diffs))