Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (5.9 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.backends.modular import CLUSTER_NORMAL, DEFAULT_SOURCE
40
from synnefo.webproject.management import utils
41

    
42
backend = get_backend()
43

    
44

    
45
class Command(NoArgsCommand):
46
    help = """Reconcile resource usage of Astakos with Pithos DB.
47

48
    Detect unsynchronized usage between Astakos and Pithos DB resources and
49
    synchronize them if specified so.
50

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

    
67
    def handle_noargs(self, **options):
68
        try:
69
            users = (options['userid'],) if options['userid'] else None
70
            account_nodes = backend.node.node_accounts(users)
71
            if not account_nodes:
72
                raise CommandError('No users found.')
73

    
74
            db_usage = {}
75
            for path, node in account_nodes:
76
                size = backend.node.node_account_usage(node, CLUSTER_NORMAL)
77
                db_usage[path] = size or 0
78

    
79
            qh_result = backend.astakosclient.service_get_quotas(
80
                backend.service_token,
81
                users
82
            )
83

    
84
            resource = 'pithos.diskspace'
85
            pending_exists = False
86
            unknown_user_exists = False
87
            unsynced = []
88
            for uuid in db_usage.keys():
89
                db_value = db_usage[uuid]
90
                try:
91
                    qh_all = qh_result[uuid]
92
                except KeyError:
93
                    self.stdout.write(
94
                        "User '%s' does not exist in Quotaholder!\n" % uuid
95
                    )
96
                    unknown_user_exists = True
97
                    continue
98
                else:
99
                    qh = qh_all.get(DEFAULT_SOURCE, {})
100
                    try:
101
                        qh_resource = qh[resource]
102
                    except KeyError:
103
                        self.stdout.write(
104
                            "Resource '%s' does not exist in Quotaholder"
105
                            " for user '%s'!\n" % (resource, uuid))
106
                        continue
107

    
108
                    if qh_resource['pending']:
109
                        self.stdout.write(
110
                            "Pending commission. User '%s', resource '%s'.\n" %
111
                            (uuid, resource)
112
                        )
113
                        pending_exists = True
114
                        continue
115

    
116
                    qh_value = qh_resource['usage']
117

    
118
                    if  db_value != qh_value:
119
                        data = (uuid, resource, db_value, qh_value)
120
                        unsynced.append(data)
121

    
122
            if unsynced:
123
                headers = ("User", "Resource", "Database", "Quotaholder")
124
                utils.pprint_table(self.stdout, unsynced, headers)
125
                if options['fix']:
126
                    request = {}
127
                    request['force'] = options['force']
128
                    request['auto_accept'] = True
129
                    request['name'] = "RECONCILE"
130
                    request['provisions'] = map(create_provision, unsynced)
131
                    backend.astakosclient.issue_commission(
132
                        backend.service_token, request
133
                    )
134

    
135
            if pending_exists:
136
                self.stdout.write(
137
                    "Found pending commissions. Run 'snf-manage"
138
                    " reconcile-commissions-pithos'\n"
139
                )
140
            elif not (unsynced or unknown_user_exists):
141
                self.stdout.write("Everything in sync.\n")
142
        finally:
143
            backend.close()
144

    
145

    
146
def create_provision(provision_info):
147
    user, resource, db_value, qh_value = provision_info
148
    return {"holder": user,
149
            "source": DEFAULT_SOURCE,
150
            "resource": resource,
151
            "quantity": db_value - qh_value}