Statistics
| Branch: | Tag: | Revision:

root / snf-cyclades-app / synnefo / quotas / management / commands / enforce-resources-cyclades.py @ 12e8c717

History | View | Annotate | Download (7.5 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
    )
76

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

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

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

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

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

    
116
        users = options['users']
117
        if users is not None:
118
            users = users.split(',')
119

    
120
        excluded = options['exclude_users']
121
        excluded = set(excluded.split(',') if excluded is not None else [])
122

    
123
        handlers = self.get_handlers(options["resources"])
124
        try:
125
            qh_holdings = util.get_qh_users_holdings(users)
126
        except errors.AstakosClientException as e:
127
            raise CommandError(e)
128

    
129
        qh_holdings = sorted(qh_holdings.items())
130
        resources = set(h[0] for h in handlers)
131
        dangerous = bool(resources.difference(DEFAULT_RESOURCES))
132

    
133
        actions = {}
134
        overlimit = []
135
        viol_id = 0
136
        for resource, handle_resource, resource_type in handlers:
137
            if resource_type not in actions:
138
                actions[resource_type] = OrderedDict()
139
            actual_resources = enforce.get_actual_resources(resource_type,
140
                                                            users)
141
            for user, user_quota in qh_holdings:
142
                if user in excluded:
143
                    continue
144
                for source, source_quota in user_quota.iteritems():
145
                    try:
146
                        qh = util.transform_quotas(source_quota)
147
                        qh_value, qh_limit, qh_pending = qh[resource]
148
                    except KeyError:
149
                        write("Resource '%s' does not exist in Quotaholder"
150
                              " for user '%s' and source '%s'!\n" %
151
                              (resource, user, source))
152
                        continue
153
                    if qh_pending:
154
                        write("Pending commission for user '%s', source '%s', "
155
                              "resource '%s'. Skipping\n" %
156
                              (user, source, resource))
157
                        continue
158
                    diff = qh_value - qh_limit
159
                    if diff > 0:
160
                        viol_id += 1
161
                        overlimit.append((viol_id, user, source, resource,
162
                                          qh_limit, qh_value))
163
                        relevant_resources = actual_resources[user]
164
                        handle_resource(viol_id, resource, relevant_resources,
165
                                        diff, actions)
166

    
167
        if not overlimit:
168
            write("No violations.\n")
169
            return
170

    
171
        headers = ("#", "User", "Source", "Resource", "Limit", "Usage")
172
        pprint_table(self.stderr, overlimit, headers,
173
                     options["output_format"], title="Violations")
174

    
175
        if any(actions.values()):
176
            write("\n")
177
            if fix:
178
                if dangerous and not force:
179
                    write("You are enforcing resources that may permanently "
180
                          "remove a vm.\n")
181
                    self.confirm()
182
                write("Applying actions. Please wait...\n")
183
            title = "Applied Actions" if fix else "Suggested Actions"
184
            log = enforce.perform_actions(actions, maxops=maxops, fix=fix)
185
            headers = ("Type", "ID", "State", "Backend", "Action", "Violation")
186
            if fix:
187
                headers += ("Result",)
188
            pprint_table(self.stderr, log, headers,
189
                         options["output_format"], title=title)