Statistics
| Branch: | Tag: | Revision:

root / snf-pithos-app / pithos / api / management / commands / reconcile-resources-pithos.py @ bab17726

History | View | Annotate | Download (6.4 kB)

1
# Copyright 2012 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 NoArgsCommand, CommandError
35

    
36
from optparse import make_option
37

    
38
from pithos.api.util import get_backend
39
from pithos.api.resources import resources
40
from pithos.backends.modular import CLUSTER_NORMAL, DEFAULT_SOURCE
41

    
42
from synnefo.webproject.management import utils
43

    
44
from astakosclient.errors import QuotaLimit
45

    
46
backend = get_backend()
47

    
48

    
49
class Command(NoArgsCommand):
50
    help = """Reconcile resource usage of Astakos with Pithos DB.
51

52
    Detect unsynchronized usage between Astakos and Pithos DB resources and
53
    synchronize them if specified so.
54

55
    """
56
    option_list = NoArgsCommand.option_list + (
57
        make_option("--userid", dest="userid",
58
                    default=None,
59
                    help="Reconcile resources only for this user"),
60
        make_option("--fix", dest="fix",
61
                    default=False,
62
                    action="store_true",
63
                    help="Synchronize Astakos quotas with Pithos DB."),
64
        make_option("--force",
65
                    default=False,
66
                    action="store_true",
67
                    help="Override Astakos quotas. Force Astakos to impose "
68
                         "the Pithos quota, independently of their value.")
69
    )
70

    
71
    def handle_noargs(self, **options):
72
        try:
73
            qh_result = backend.astakosclient.service_get_quotas(
74
                backend.service_token)
75

    
76
            users = (options['userid'],) if options['userid'] else None
77
            account_nodes = backend.node.node_accounts(users)
78
            if not account_nodes:
79
                raise CommandError('No users found.')
80

    
81
            db_usage = {}
82
            for path, node in account_nodes:
83
                size = backend.node.node_account_usage(node, CLUSTER_NORMAL)
84
                db_usage[path] = size or 0
85

    
86
            users = set(qh_result.keys())
87
            users.update(db_usage.keys())
88

    
89
            pending_exists = False
90
            unknown_user_exists = False
91
            unsynced = []
92
            for uuid in users:
93
                db_value = db_usage.get(uuid, 0)
94
                try:
95
                    qh_all = qh_result[uuid]
96
                except KeyError:
97
                    self.stdout.write(
98
                        "User '%s' does not exist in Quotaholder!\n" % uuid
99
                    )
100
                    unknown_user_exists = True
101
                    continue
102
                else:
103
                    qh = qh_all.get(DEFAULT_SOURCE, {})
104
                    for resource in [r['name'] for r in resources]:
105
                        try:
106
                            qh_resource = qh[resource]
107
                        except KeyError:
108
                            self.stdout.write(
109
                                "Resource '%s' does not exist in Quotaholder "
110
                                "for user '%s'!\n" % (resource, uuid))
111
                            continue
112

    
113
                        if qh_resource['pending']:
114
                            self.stdout.write(
115
                                "Pending commission. "
116
                                "User '%s', resource '%s'.\n" %
117
                                (uuid, resource)
118
                            )
119
                            pending_exists = True
120
                            continue
121

    
122
                        qh_value = qh_resource['usage']
123

    
124
                        if  db_value != qh_value:
125
                            data = (uuid, resource, db_value, qh_value)
126
                            unsynced.append(data)
127

    
128
            if unsynced:
129
                headers = ("User", "Resource", "Database", "Quotaholder")
130
                utils.pprint_table(self.stdout, unsynced, headers)
131
                if options['fix']:
132
                    request = {}
133
                    request['force'] = options['force']
134
                    request['auto_accept'] = True
135
                    request['name'] = "RECONCILE"
136
                    request['provisions'] = map(create_provision, unsynced)
137
                    try:
138
                        backend.astakosclient.issue_commission(
139
                            backend.service_token, request)
140
                    except QuotaLimit:
141
                        self.stdout.write(
142
                            "Reconciling failed because a limit has been "
143
                            "reached. Use --force to ignore the check.\n")
144
                        return
145

    
146
            if pending_exists:
147
                self.stdout.write(
148
                    "Found pending commissions. Run 'snf-manage"
149
                    " reconcile-commissions-pithos'\n"
150
                )
151
            elif not (unsynced or unknown_user_exists):
152
                self.stdout.write("Everything in sync.\n")
153
        finally:
154
            backend.close()
155

    
156

    
157
def create_provision(provision_info):
158
    user, resource, db_value, qh_value = provision_info
159
    return {"holder": user,
160
            "source": DEFAULT_SOURCE,
161
            "resource": resource,
162
            "quantity": db_value - qh_value}