Statistics
| Branch: | Tag: | Revision:

root / snf-cyclades-app / synnefo / quotas / management / commands / enforce-resources-cyclades.py @ 29e3919d

History | View | Annotate | Download (8 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("--resources",
64
                    help="Specify resources to check, default: %s" %
65
                    ",".join(DEFAULT_RESOURCES)),
66
        make_option("--fix",
67
                    default=False,
68
                    action="store_true",
69
                    help="Fix violations"),
70
        make_option("--force",
71
                    default=False,
72
                    action="store_true",
73
                    help=("Confirm actions that may permanently "
74
                          "remove a vm")),
75
        make_option("--shutdown-timeout",
76
                    help="Force vm shutdown after given seconds."),
77
    )
78

    
79
    def confirm(self):
80
        self.stderr.write("Confirm? [y/N] ")
81
        response = raw_input()
82
        if string.lower(response) not in ['y', 'yes']:
83
            self.stdout.write("Aborted.\n")
84
            exit()
85

    
86
    def get_handlers(self, resources):
87
        def rem(v):
88
            try:
89
                resources.remove(v)
90
                return True
91
            except ValueError:
92
                return False
93

    
94
        if resources is None:
95
            resources = list(DEFAULT_RESOURCES)
96
        else:
97
            resources = resources.split(",")
98

    
99
        handlers = [h for h in enforce.RESOURCE_HANDLING if rem(h[0])]
100
        if resources:
101
            m = "No such resource '%s'" % resources[0]
102
            raise CommandError(m)
103
        return handlers
104

    
105
    @transaction.commit_on_success
106
    def handle(self, *args, **options):
107
        write = self.stderr.write
108
        fix = options["fix"]
109
        force = options["force"]
110
        maxops = options["max_operations"]
111
        if maxops is not None:
112
            try:
113
                maxops = int(maxops)
114
            except ValueError:
115
                m = "Expected integer max operations."
116
                raise CommandError(m)
117

    
118
        shutdown_timeout = options["shutdown_timeout"]
119
        if shutdown_timeout is not None:
120
            try:
121
                shutdown_timeout = int(shutdown_timeout)
122
            except ValueError:
123
                m = "Expected integer shutdown timeout."
124
                raise CommandError(m)
125

    
126
        users = options['users']
127
        if users is not None:
128
            users = users.split(',')
129

    
130
        excluded = options['exclude_users']
131
        excluded = set(excluded.split(',') if excluded is not None else [])
132

    
133
        handlers = self.get_handlers(options["resources"])
134
        try:
135
            qh_holdings = util.get_qh_users_holdings(users)
136
        except errors.AstakosClientException as e:
137
            raise CommandError(e)
138

    
139
        qh_holdings = sorted(qh_holdings.items())
140
        resources = set(h[0] for h in handlers)
141
        dangerous = bool(resources.difference(DEFAULT_RESOURCES))
142

    
143
        opts = {"shutdown_timeout": shutdown_timeout}
144
        actions = {}
145
        overlimit = []
146
        viol_id = 0
147
        for resource, handle_resource, resource_type in handlers:
148
            if resource_type not in actions:
149
                actions[resource_type] = OrderedDict()
150
            actual_resources = enforce.get_actual_resources(resource_type,
151
                                                            users)
152
            for user, user_quota in qh_holdings:
153
                if user in excluded:
154
                    continue
155
                for source, source_quota in user_quota.iteritems():
156
                    try:
157
                        qh = util.transform_quotas(source_quota)
158
                        qh_value, qh_limit, qh_pending = qh[resource]
159
                    except KeyError:
160
                        write("Resource '%s' does not exist in Quotaholder"
161
                              " for user '%s' and source '%s'!\n" %
162
                              (resource, user, source))
163
                        continue
164
                    if qh_pending:
165
                        write("Pending commission for user '%s', source '%s', "
166
                              "resource '%s'. Skipping\n" %
167
                              (user, source, resource))
168
                        continue
169
                    diff = qh_value - qh_limit
170
                    if diff > 0:
171
                        viol_id += 1
172
                        overlimit.append((viol_id, user, source, resource,
173
                                          qh_limit, qh_value))
174
                        relevant_resources = actual_resources[user]
175
                        handle_resource(viol_id, resource, relevant_resources,
176
                                        diff, actions)
177

    
178
        if not overlimit:
179
            write("No violations.\n")
180
            return
181

    
182
        headers = ("#", "User", "Source", "Resource", "Limit", "Usage")
183
        pprint_table(self.stderr, overlimit, headers,
184
                     options["output_format"], title="Violations")
185

    
186
        if any(actions.values()):
187
            write("\n")
188
            if fix:
189
                if dangerous and not force:
190
                    write("You are enforcing resources that may permanently "
191
                          "remove a vm.\n")
192
                    self.confirm()
193
                write("Applying actions. Please wait...\n")
194
            title = "Applied Actions" if fix else "Suggested Actions"
195
            log = enforce.perform_actions(actions, maxops=maxops, fix=fix,
196
                                          options=opts)
197
            headers = ("Type", "ID", "State", "Backend", "Action", "Violation")
198
            if fix:
199
                headers += ("Result",)
200
            pprint_table(self.stderr, log, headers,
201
                         options["output_format"], title=title)