Statistics
| Branch: | Tag: | Revision:

root / snf-cyclades-app / synnefo / quotas / management / commands / cyclades-usage-verify.py @ 629acc65

History | View | Annotate | Download (3.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 django.core.management.base import BaseCommand
35
from optparse import make_option
36

    
37
from synnefo.quotas import DEFAULT_SOURCE
38
from synnefo.quotas.util import (get_db_holdings, get_quotaholder_holdings,
39
                                 transform_quotas)
40
from synnefo.webproject.management.utils import pprint_table
41

    
42

    
43
class Command(BaseCommand):
44
    help = """
45
    Verify that cyclades.* resource usage.
46

47
    Verify that usage calculated from Cyclades DB agrees with the usage
48
    recorded in the effective quota database (Quotaholder)
49

50
    """
51
    output_transaction = True
52
    option_list = BaseCommand.option_list + (
53
        make_option("--userid", dest="userid",
54
                    default=None,
55
                    help="Verify usage only for this user"),
56
    )
57

    
58
    def handle(self, *args, **options):
59
        write = self.stdout.write
60
        userid = options['userid']
61

    
62
        users = [userid] if userid else None
63
        # Get info from DB
64
        db_holdings = get_db_holdings(users)
65
        users = db_holdings.keys()
66
        qh_holdings = get_quotaholder_holdings(userid)
67
        qh_users = qh_holdings.keys()
68

    
69
        if len(qh_users) < len(users):
70
            for u in set(users) - set(qh_users):
71
                write("Unknown entity: %s\n" % u)
72
                users = qh_users
73

    
74
        headers = ("User", "Resource", "Database", "Quotaholder")
75
        unsynced = []
76
        for user in users:
77
            db = db_holdings[user]
78
            qh_all = qh_holdings[user]
79
            # Assuming only one source
80
            qh = qh_all[DEFAULT_SOURCE]
81
            qh = transform_quotas(qh)
82

    
83
            for resource, (value, value1) in qh.iteritems:
84
                db_value = db.pop(resource, None)
85
                if value != value1:
86
                    write("Commission pending for %s"
87
                          % str((user, resource)))
88
                    continue
89
                if db_value is None:
90
                    write("Resource %s exists in QH for %s but not in DB\n"
91
                          % (resource, user))
92
                elif db_value != value:
93
                    data = (user, resource, str(db_value), str(value))
94
                    unsynced.append(data)
95

    
96
            for resource, db_value in db.iteritems():
97
                write("Resource %s exists in DB for %s but not in QH\n"
98
                      % (resource, user))
99

    
100
        if unsynced:
101
            pprint_table(self.stderr, unsynced, headers)