Statistics
| Branch: | Tag: | Revision:

root / snf-astakos-app / astakos / im / management / commands / resource-modify.py @ f72ba65d

History | View | Annotate | Download (7.7 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
from optparse import make_option
35
from django.core.management.base import BaseCommand, CommandError
36
from django.utils import simplejson as json
37

    
38
from snf_django.management import utils
39
from astakos.im.models import Resource
40
from astakos.im.register import update_resources
41
from ._common import show_resource_value, style_options, check_style, units
42

    
43

    
44
class Command(BaseCommand):
45
    args = "<resource name>"
46
    help = "Modify a resource's default base quota and boolean flags."
47

    
48
    option_list = BaseCommand.option_list + (
49
        make_option('--default-quota',
50
                    metavar='<limit>',
51
                    help="Specify default base quota"),
52
        make_option('--default-quota-interactive',
53
                    action='store_true',
54
                    default=None,
55
                    help=("Prompt user to change default base quota. "
56
                          "If no resource is given, prompts for all "
57
                          "resources.")),
58
        make_option('--default-quota-from-file',
59
                    metavar='<limits_file.json>',
60
                    help=("Read default base quota from a file. "
61
                          "File should contain a json dict mapping resource "
62
                          "names to limits")),
63
        make_option('--unit-style',
64
                    default='mb',
65
                    help=("Specify display unit for resource values "
66
                          "(one of %s); defaults to mb") % style_options),
67
        make_option('--allow-in-projects',
68
                    metavar='True|False',
69
                    help=("Specify whether to allow this resource "
70
                          "in projects.")),
71
    )
72

    
73
    def handle(self, *args, **options):
74
        resource_name = args[0] if len(args) > 0 else None
75

    
76
        actions = {
77
            'default_quota': self.change_limit,
78
            'default_quota_interactive': self.change_interactive,
79
            'default_quota_from_file': self.change_from_file,
80
            'allow_in_projects': self.set_allow_in_projects,
81
        }
82

    
83
        opts = [(key, value)
84
                for (key, value) in options.items()
85
                if key in actions and value is not None]
86

    
87
        if len(opts) != 1:
88
            raise CommandError("Please provide exactly one of the options: "
89
                               "--default-quota, --default-quota-interactive, "
90
                               "--default-quota-from-file, "
91
                               "--allow-in-projects.")
92

    
93
        self.unit_style = options['unit_style']
94
        check_style(self.unit_style)
95

    
96
        key, value = opts[0]
97
        action = actions[key]
98
        action(resource_name, value)
99

    
100
    def set_allow_in_projects(self, resource_name, allow):
101
        if resource_name is None:
102
            raise CommandError("Please provide a resource name.")
103

    
104
        try:
105
            allow = utils.parse_bool(allow)
106
        except ValueError:
107
            raise CommandError("Expecting a boolean value.")
108
        resource = self.get_resource(resource_name)
109
        resource.allow_in_projects = allow
110
        resource.save()
111

    
112
    def get_resource(self, resource_name):
113
        try:
114
            return Resource.objects.select_for_update().get(name=resource_name)
115
        except Resource.DoesNotExist:
116
            raise CommandError("Resource %s does not exist."
117
                               % resource_name)
118

    
119
    def change_limit(self, resource_name, limit):
120
        if resource_name is None:
121
            raise CommandError("Please provide a resource name.")
122

    
123
        resource = self.get_resource(resource_name)
124
        self.change_resource_limit(resource, limit)
125

    
126
    def change_from_file(self, resource_name, filename):
127
        with open(filename) as file_data:
128
            try:
129
                config = json.load(file_data)
130
            except json.JSONDecodeError:
131
                raise CommandError("Malformed JSON file.")
132
            if not isinstance(config, dict):
133
                raise CommandError("Malformed JSON file.")
134
            self.change_with_conf(resource_name, config)
135

    
136
    def change_with_conf(self, resource_name, config):
137
        if resource_name is None:
138
            resources = Resource.objects.all().select_for_update()
139
        else:
140
            resources = [self.get_resource(resource_name)]
141

    
142
        updates = []
143
        for resource in resources:
144
            limit = config.get(resource.name)
145
            if limit is not None:
146
                limit = self.parse_limit(limit)
147
                updates.append((resource, limit))
148
        if updates:
149
            update_resources(updates)
150

    
151
    def change_interactive(self, resource_name, _placeholder):
152
        if resource_name is None:
153
            resources = Resource.objects.all().select_for_update()
154
        else:
155
            resources = [self.get_resource(resource_name)]
156

    
157
        updates = []
158
        for resource in resources:
159
            self.stdout.write("Resource '%s' (%s)\n" %
160
                              (resource.name, resource.desc))
161
            value = show_resource_value(resource.uplimit, resource.name,
162
                                        self.unit_style)
163
            self.stdout.write("Current limit: %s\n" % value)
164
            while True:
165
                self.stdout.write("New limit (leave blank to keep current): ")
166
                response = raw_input()
167
                if response == "":
168
                    break
169
                else:
170
                    try:
171
                        value = units.parse(response)
172
                    except units.ParseError:
173
                        continue
174
                    updates.append((resource, value))
175
                    break
176
        if updates:
177
            self.stdout.write("Updating...\n")
178
            update_resources(updates)
179

    
180
    def parse_limit(self, limit):
181
        try:
182
            if isinstance(limit, (int, long)):
183
                return limit
184
            if isinstance(limit, basestring):
185
                return units.parse(limit)
186
            raise units.ParseError()
187
        except units.ParseError:
188
            m = ("Limit should be an integer, optionally followed by a unit,"
189
                 " or 'inf'.")
190
            raise CommandError(m)
191

    
192
    def change_resource_limit(self, resource, limit):
193
        limit = self.parse_limit(limit)
194
        update_resources([(resource, limit)])