Revision 4c03c942
b/snf-pithos-app/pithos/api/management/commands/pithos-set-quota.py | ||
---|---|---|
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 collections import namedtuple |
|
37 |
from sqlalchemy import func |
|
38 |
from sqlalchemy.sql import select, and_, or_ |
|
39 |
|
|
40 |
from pithos.api.util import get_backend |
|
41 |
from pithos.backends.modular import ( |
|
42 |
CLUSTER_NORMAL, CLUSTER_HISTORY, CLUSTER_DELETED |
|
43 |
) |
|
44 |
clusters = (CLUSTER_NORMAL, CLUSTER_HISTORY, CLUSTER_DELETED) |
|
45 |
|
|
46 |
Statistics = namedtuple('Statistics', ('node', 'path', 'size', 'cluster')) |
|
47 |
|
|
48 |
AddQuotaPayload = namedtuple('AddQuotaPayload', ('holder', |
|
49 |
'resource', |
|
50 |
'key', |
|
51 |
'quantity', |
|
52 |
'capacity', |
|
53 |
'import_limit', |
|
54 |
'export_limit')) |
|
55 |
|
|
56 |
ENTITY_KEY= '1' |
|
57 |
|
|
58 |
backend = get_backend() |
|
59 |
table = {} |
|
60 |
table['nodes'] = backend.node.nodes |
|
61 |
table['versions'] = backend.node.versions |
|
62 |
table['statistics'] = backend.node.statistics |
|
63 |
conn = backend.node.conn |
|
64 |
|
|
65 |
def _compute_statistics(nodes): |
|
66 |
statistics = [] |
|
67 |
append = statistics.append |
|
68 |
for path, node in nodes: |
|
69 |
select_children = select( |
|
70 |
[table['nodes'].c.node]).where(table['nodes'].c.parent==node) |
|
71 |
select_descendants = select([table['nodes'].c.node]).where( |
|
72 |
or_(table['nodes'].c.parent.in_(select_children), |
|
73 |
table['nodes'].c.node.in_(select_children))) |
|
74 |
s = select([table['versions'].c.cluster, |
|
75 |
func.sum(table['versions'].c.size)]) |
|
76 |
s = s.group_by(table['versions'].c.cluster) |
|
77 |
s = s.where(table['nodes'].c.node == table['versions'].c.node) |
|
78 |
s = s.where(table['nodes'].c.node.in_(select_descendants)) |
|
79 |
d2 = dict(conn.execute(s).fetchall()) |
|
80 |
|
|
81 |
for cluster in clusters: |
|
82 |
try: |
|
83 |
size = d2[cluster] |
|
84 |
except KeyError: |
|
85 |
size = 0 |
|
86 |
append(Statistics( |
|
87 |
node=node, |
|
88 |
path=path, |
|
89 |
size=size, |
|
90 |
cluster=cluster)) |
|
91 |
return statistics |
|
92 |
|
|
93 |
def _get_verified_quota(statistics): |
|
94 |
""" Verify statistics and set quotaholder account quota """ |
|
95 |
add_quota = [] |
|
96 |
append = add_quota.append |
|
97 |
for item in statistics: |
|
98 |
s = select([table['statistics'].c.size]) |
|
99 |
s = s.where(table['statistics'].c.node == item.node) |
|
100 |
s = s.where(table['statistics'].c.cluster == item.cluster) |
|
101 |
db_item = conn.execute(s).fetchone() |
|
102 |
if not db_item: |
|
103 |
continue |
|
104 |
try: |
|
105 |
assert item.size == db_item.size, \ |
|
106 |
'%d[%d], size: %d != %d' % ( |
|
107 |
item.node, item.cluster, item.size, db_item.size) |
|
108 |
except AssertionError, e: |
|
109 |
print e |
|
110 |
continue |
|
111 |
else: |
|
112 |
append(AddQuotaPayload( |
|
113 |
holder=item.path, |
|
114 |
resource='pithos+.diskspace', |
|
115 |
key=ENTITY_KEY, |
|
116 |
quantity=db_item.size, |
|
117 |
capacity=0, |
|
118 |
import_limit=0, |
|
119 |
export_limit=0)) |
|
120 |
return add_quota |
|
121 |
|
|
122 |
|
|
123 |
class Command(NoArgsCommand): |
|
124 |
help = "Set quotaholder account quotas" |
|
125 |
|
|
126 |
def handle_noargs(self, **options): |
|
127 |
try: |
|
128 |
if not backend.quotaholder_url: |
|
129 |
raise CommandError('Quotaholder component url is not set') |
|
130 |
|
|
131 |
if not backend.quotaholder_token: |
|
132 |
raise CommandError('Quotaholder component token is not set') |
|
133 |
|
|
134 |
# retrieve account nodes |
|
135 |
s = select([table['nodes'].c.path, table['nodes'].c.node]) |
|
136 |
s = s.where(and_(table['nodes'].c.node != 0, |
|
137 |
table['nodes'].c.parent == 0)) |
|
138 |
account_nodes = conn.execute(s).fetchall() |
|
139 |
|
|
140 |
# compute account statistics |
|
141 |
statistics = _compute_statistics(account_nodes) |
|
142 |
|
|
143 |
# verify and send quota |
|
144 |
add_quota = _get_verified_quota(statistics) |
|
145 |
result = backend.quotaholder.add_quota( |
|
146 |
context={}, |
|
147 |
clientkey='pithos', |
|
148 |
serial=42, |
|
149 |
sub_quota=[], |
|
150 |
add_quota=add_quota) |
|
151 |
if result: |
|
152 |
raise CommandError(result) |
|
153 |
finally: |
|
154 |
backend.close() |
Also available in: Unified diff