root / snf-cyclades-app / synnefo / quotas / management / commands / reconcile-resources-cyclades.py @ 8c911970
History | View | Annotate | Download (5.2 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 datetime import datetime |
35 |
from snf_django.management.commands import SynnefoCommand |
36 |
from optparse import make_option |
37 |
|
38 |
from synnefo import quotas |
39 |
from synnefo.quotas import util |
40 |
from snf_django.management.utils import pprint_table |
41 |
from snf_django.utils import reconcile |
42 |
|
43 |
|
44 |
class Command(SynnefoCommand): |
45 |
help = """Reconcile resource usage of Astakos with Cyclades DB.
|
46 |
|
47 |
Detect unsynchronized usage between Astakos and Cyclades DB resources and
|
48 |
synchronize them if specified so.
|
49 |
|
50 |
"""
|
51 |
option_list = SynnefoCommand.option_list + ( |
52 |
make_option("--userid", dest="userid", |
53 |
default=None,
|
54 |
help="Reconcile resources only for this user"),
|
55 |
make_option("--project",
|
56 |
help="Reconcile resources only for this project"),
|
57 |
make_option("--fix", dest="fix", |
58 |
default=False,
|
59 |
action="store_true",
|
60 |
help="Synchronize Astakos quotas with Cyclades DB."),
|
61 |
make_option("--force",
|
62 |
default=False,
|
63 |
action="store_true",
|
64 |
help="Override Astakos quotas. Force Astakos to impose"
|
65 |
" the Cyclades quota, independently of their value.")
|
66 |
) |
67 |
|
68 |
def handle(self, *args, **options): |
69 |
write = self.stderr.write
|
70 |
userid = options['userid']
|
71 |
project = options["project"]
|
72 |
|
73 |
# Get holdings from Cyclades DB
|
74 |
db_holdings = util.get_db_holdings(userid, project) |
75 |
db_project_holdings = util.get_db_project_holdings(project) |
76 |
|
77 |
# Get holdings from QuotaHolder
|
78 |
qh_holdings = util.get_qh_users_holdings( |
79 |
[userid] if userid is not None else None) |
80 |
qh_project_holdings = util.get_qh_project_holdings( |
81 |
[project] if project is not None else None) |
82 |
|
83 |
unsynced_users, users_pending, users_unknown =\ |
84 |
reconcile.check_users(self.stderr, quotas.RESOURCES,
|
85 |
db_holdings, qh_holdings) |
86 |
|
87 |
unsynced_projects, projects_pending, projects_unknown =\ |
88 |
reconcile.check_projects(self.stderr, quotas.RESOURCES,
|
89 |
db_project_holdings, qh_project_holdings) |
90 |
pending_exists = users_pending or projects_pending
|
91 |
unknown_exists = users_unknown or projects_unknown
|
92 |
|
93 |
headers = ("Type", "Holder", "Source", "Resource", |
94 |
"Database", "Quotaholder") |
95 |
unsynced = unsynced_users + unsynced_projects |
96 |
if unsynced:
|
97 |
pprint_table(self.stdout, unsynced, headers)
|
98 |
if options["fix"]: |
99 |
qh = quotas.Quotaholder.get() |
100 |
force = options["force"]
|
101 |
name = ("client: reconcile-resources-cyclades, time: %s"
|
102 |
% datetime.now()) |
103 |
user_provisions = reconcile.create_user_provisions( |
104 |
unsynced_users) |
105 |
project_provisions = reconcile.create_project_provisions( |
106 |
unsynced_projects) |
107 |
try:
|
108 |
qh.issue_commission_generic( |
109 |
user_provisions, project_provisions, |
110 |
name=name, force=force, |
111 |
auto_accept=True)
|
112 |
except quotas.errors.QuotaLimit:
|
113 |
write("Reconciling failed because a limit has been "
|
114 |
"reached. Use --force to ignore the check.\n")
|
115 |
return
|
116 |
write("Fixed unsynced resources\n")
|
117 |
|
118 |
if pending_exists:
|
119 |
write("Found pending commissions. Run 'snf-manage"
|
120 |
" reconcile-commissions-cyclades'\n")
|
121 |
elif not (unsynced or unknown_exists): |
122 |
write("Everything in sync.\n")
|