root / snf-cyclades-app / synnefo / quotas / management / commands / enforce-resources-cyclades.py @ f3bdab04
History | View | Annotate | Download (11.1 kB)
1 |
# Copyright 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 |
import string |
35 |
from optparse import make_option |
36 |
from django.db import transaction |
37 |
|
38 |
from synnefo.lib.ordereddict import OrderedDict |
39 |
from synnefo.quotas import util |
40 |
from synnefo.quotas import enforce |
41 |
from synnefo.quotas import errors |
42 |
from snf_django.management.commands import SynnefoCommand, CommandError |
43 |
from snf_django.management.utils import pprint_table |
44 |
|
45 |
|
46 |
DEFAULT_RESOURCES = ["cyclades.cpu",
|
47 |
"cyclades.ram",
|
48 |
"cyclades.floating_ip",
|
49 |
] |
50 |
|
51 |
|
52 |
class Command(SynnefoCommand): |
53 |
help = """Check and fix quota violations for Cyclades resources.
|
54 |
"""
|
55 |
option_list = SynnefoCommand.option_list + ( |
56 |
make_option("--max-operations",
|
57 |
help="Limit operations per backend."),
|
58 |
make_option("--users", dest="users", |
59 |
help=("Enforce resources only for the specified list "
|
60 |
"of users, e.g uuid1,uuid2")),
|
61 |
make_option("--exclude-users",
|
62 |
help=("Exclude list of users from resource enforcement")),
|
63 |
make_option("--projects",
|
64 |
help=("Enforce resources only for the specified list "
|
65 |
"of projects, e.g uuid1,uuid2")),
|
66 |
make_option("--exclude-projects",
|
67 |
help=("Exclude list of projects from resource enforcement")
|
68 |
), |
69 |
make_option("--resources",
|
70 |
help="Specify resources to check, default: %s" %
|
71 |
",".join(DEFAULT_RESOURCES)),
|
72 |
make_option("--fix",
|
73 |
default=False,
|
74 |
action="store_true",
|
75 |
help="Fix violations"),
|
76 |
make_option("--force",
|
77 |
default=False,
|
78 |
action="store_true",
|
79 |
help=("Confirm actions that may permanently "
|
80 |
"remove a vm")),
|
81 |
make_option("--shutdown-timeout",
|
82 |
help="Force vm shutdown after given seconds."),
|
83 |
) |
84 |
|
85 |
def confirm(self): |
86 |
self.stdout.write("Confirm? [y/N] ") |
87 |
try:
|
88 |
response = raw_input()
|
89 |
except EOFError: |
90 |
response = "ABORT"
|
91 |
if string.lower(response) not in ['y', 'yes']: |
92 |
self.stderr.write("Aborted.\n") |
93 |
exit()
|
94 |
|
95 |
def get_handlers(self, resources): |
96 |
def rem(v): |
97 |
try:
|
98 |
resources.remove(v) |
99 |
return True |
100 |
except ValueError: |
101 |
return False |
102 |
|
103 |
if resources is None: |
104 |
resources = list(DEFAULT_RESOURCES)
|
105 |
else:
|
106 |
resources = resources.split(",")
|
107 |
|
108 |
handlers = [h for h in enforce.RESOURCE_HANDLING if rem(h[0])] |
109 |
if resources:
|
110 |
m = "No such resource '%s'" % resources[0] |
111 |
raise CommandError(m)
|
112 |
return handlers
|
113 |
|
114 |
@transaction.commit_on_success
|
115 |
def handle(self, *args, **options): |
116 |
write = self.stderr.write
|
117 |
fix = options["fix"]
|
118 |
force = options["force"]
|
119 |
handlers = self.get_handlers(options["resources"]) |
120 |
maxops = options["max_operations"]
|
121 |
if maxops is not None: |
122 |
try:
|
123 |
maxops = int(maxops)
|
124 |
except ValueError: |
125 |
m = "Expected integer max operations."
|
126 |
raise CommandError(m)
|
127 |
|
128 |
shutdown_timeout = options["shutdown_timeout"]
|
129 |
if shutdown_timeout is not None: |
130 |
try:
|
131 |
shutdown_timeout = int(shutdown_timeout)
|
132 |
except ValueError: |
133 |
m = "Expected integer shutdown timeout."
|
134 |
raise CommandError(m)
|
135 |
|
136 |
excluded_users = options['exclude_users']
|
137 |
excluded_users = set(excluded_users.split(',') |
138 |
if excluded_users is not None else []) |
139 |
|
140 |
users_to_check = options['users']
|
141 |
if users_to_check is not None: |
142 |
users_to_check = list(set(users_to_check.split(',')) - |
143 |
excluded_users) |
144 |
|
145 |
try:
|
146 |
qh_holdings = util.get_qh_users_holdings(users_to_check) |
147 |
except errors.AstakosClientException as e: |
148 |
raise CommandError(e)
|
149 |
|
150 |
excluded_projects = options["exclude_projects"]
|
151 |
excluded_projects = set(excluded_projects.split(',') |
152 |
if excluded_projects is not None else []) |
153 |
|
154 |
projects_to_check = options["projects"]
|
155 |
if projects_to_check is not None: |
156 |
projects_to_check = list(set(projects_to_check.split(',')) - |
157 |
excluded_projects) |
158 |
|
159 |
try:
|
160 |
qh_project_holdings = util.get_qh_project_holdings( |
161 |
projects_to_check) |
162 |
except errors.AstakosClientException as e: |
163 |
raise CommandError(e)
|
164 |
|
165 |
qh_project_holdings = sorted(qh_project_holdings.items())
|
166 |
qh_holdings = sorted(qh_holdings.items())
|
167 |
resources = set(h[0] for h in handlers) |
168 |
dangerous = bool(resources.difference(DEFAULT_RESOURCES))
|
169 |
|
170 |
opts = {"shutdown_timeout": shutdown_timeout}
|
171 |
actions = {} |
172 |
overlimit = [] |
173 |
viol_id = 0
|
174 |
|
175 |
for resource, handle_resource, resource_type in handlers: |
176 |
if resource_type not in actions: |
177 |
actions[resource_type] = OrderedDict() |
178 |
actual_resources = enforce.get_actual_resources( |
179 |
resource_type, projects=projects_to_check) |
180 |
for project, project_quota in qh_project_holdings: |
181 |
if enforce.skip_check(project, projects_to_check,
|
182 |
excluded_projects): |
183 |
continue
|
184 |
try:
|
185 |
qh = util.transform_project_quotas(project_quota) |
186 |
qh_value, qh_limit, qh_pending = qh[resource] |
187 |
except KeyError: |
188 |
write("Resource '%s' does not exist in Quotaholder"
|
189 |
" for project '%s'!\n" %
|
190 |
(resource, project)) |
191 |
continue
|
192 |
if qh_pending:
|
193 |
write("Pending commission for project '%s', "
|
194 |
"resource '%s'. Skipping\n" %
|
195 |
(project, resource)) |
196 |
continue
|
197 |
diff = qh_value - qh_limit |
198 |
if diff > 0: |
199 |
viol_id += 1
|
200 |
overlimit.append((viol_id, "project", project, "", |
201 |
resource, qh_limit, qh_value)) |
202 |
relevant_resources = enforce.pick_project_resources( |
203 |
actual_resources[project], users=users_to_check, |
204 |
excluded_users=excluded_users) |
205 |
handle_resource(viol_id, resource, relevant_resources, |
206 |
diff, actions) |
207 |
|
208 |
for resource, handle_resource, resource_type in handlers: |
209 |
if resource_type not in actions: |
210 |
actions[resource_type] = OrderedDict() |
211 |
actual_resources = enforce.get_actual_resources(resource_type, |
212 |
users_to_check) |
213 |
for user, user_quota in qh_holdings: |
214 |
if enforce.skip_check(user, users_to_check, excluded_users):
|
215 |
continue
|
216 |
for source, source_quota in user_quota.iteritems(): |
217 |
if enforce.skip_check(source, projects_to_check,
|
218 |
excluded_projects): |
219 |
continue
|
220 |
try:
|
221 |
qh = util.transform_quotas(source_quota) |
222 |
qh_value, qh_limit, qh_pending = qh[resource] |
223 |
except KeyError: |
224 |
write("Resource '%s' does not exist in Quotaholder"
|
225 |
" for user '%s' and source '%s'!\n" %
|
226 |
(resource, user, source)) |
227 |
continue
|
228 |
if qh_pending:
|
229 |
write("Pending commission for user '%s', source '%s', "
|
230 |
"resource '%s'. Skipping\n" %
|
231 |
(user, source, resource)) |
232 |
continue
|
233 |
diff = qh_value - qh_limit |
234 |
if diff > 0: |
235 |
viol_id += 1
|
236 |
overlimit.append((viol_id, "user", user, source,
|
237 |
resource, qh_limit, qh_value)) |
238 |
relevant_resources = actual_resources[source][user] |
239 |
handle_resource(viol_id, resource, relevant_resources, |
240 |
diff, actions) |
241 |
|
242 |
if not overlimit: |
243 |
write("No violations.\n")
|
244 |
return
|
245 |
|
246 |
headers = ("#", "Type", "Holder", "Source", "Resource", "Limit", |
247 |
"Usage")
|
248 |
pprint_table(self.stdout, overlimit, headers,
|
249 |
options["output_format"], title="Violations") |
250 |
|
251 |
if any(actions.values()): |
252 |
self.stdout.write("\n") |
253 |
if fix:
|
254 |
if dangerous and not force: |
255 |
write("You are enforcing resources that may permanently "
|
256 |
"remove a vm.\n")
|
257 |
self.confirm()
|
258 |
write("Applying actions. Please wait...\n")
|
259 |
title = "Applied Actions" if fix else "Suggested Actions" |
260 |
log = enforce.perform_actions(actions, maxops=maxops, fix=fix, |
261 |
options=opts) |
262 |
headers = ("Type", "ID", "State", "Backend", "Action", "Violation") |
263 |
if fix:
|
264 |
headers += ("Result",)
|
265 |
pprint_table(self.stdout, log, headers,
|
266 |
options["output_format"], title=title)
|