Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (6.3 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 astakos.im.models import Resource
39
from astakos.im.resources import update_resource
40
from ._common import show_resource_value, style_options, check_style, units
41

    
42

    
43
class Command(BaseCommand):
44
    args = "<resource name>"
45
    help = ("Modify a resource (currently only change the default base quota)."
46
            "\nIf no resource is specified, all resources are considered.")
47

    
48
    option_list = BaseCommand.option_list + (
49
        make_option('--limit',
50
                    dest='limit',
51
                    help="Specify default base quota"),
52
        make_option('--interactive',
53
                    action='store_true',
54
                    dest='interactive',
55
                    default=None,
56
                    help="Prompt user to change default base quotas"),
57
        make_option('--from-file',
58
                    dest='from_file',
59
                    metavar='<limits_file.json>',
60
                    help="Read default base quotas from a json file"),
61
        make_option('--unit-style',
62
                    default='mb',
63
                    help=("Specify display unit for resource values "
64
                          "(one of %s); defaults to mb") % style_options),
65
    )
66

    
67
    def handle(self, *args, **options):
68
        resource_name = args[0] if len(args) > 0 else None
69

    
70
        actions = {
71
            'limit': self.change_limit,
72
            'interactive': self.change_interactive,
73
            'from_file': self.change_from_file,
74
        }
75

    
76
        opts = [(key, value)
77
                for (key, value) in options.items()
78
                if key in actions and value is not None]
79

    
80
        if len(opts) != 1:
81
            raise CommandError("Please provide exactly one of the options: %s."
82
                               % ", ".join(actions.keys()))
83

    
84
        self.unit_style = options['unit_style']
85
        check_style(self.unit_style)
86

    
87
        key, value = opts[0]
88
        action = actions[key]
89
        action(resource_name, value)
90

    
91
    def get_resource(self, resource_name):
92
        try:
93
            return Resource.objects.get_for_update(name=resource_name)
94
        except Resource.DoesNotExist:
95
            raise CommandError("Resource %s does not exist."
96
                               % resource_name)
97

    
98
    def change_limit(self, resource_name, limit):
99
        if resource_name is None:
100
            raise CommandError("Please provide a resource name.")
101

    
102
        resource = self.get_resource(resource_name)
103
        self.change_resource_limit(resource, limit)
104

    
105
    def change_from_file(self, resource_name, filename):
106
        with open(filename) as file_data:
107
            try:
108
                config = json.load(file_data)
109
            except json.JSONDecodeError:
110
                raise CommandError("Malformed JSON file.")
111
            if not isinstance(config, dict):
112
                raise CommandError("Malformed JSON file.")
113
            self.change_with_conf(resource_name, config)
114

    
115
    def change_with_conf(self, resource_name, config):
116
        if resource_name is None:
117
            resources = Resource.objects.all().select_for_update()
118
        else:
119
            resources = [self.get_resource(resource_name)]
120

    
121
        for resource in resources:
122
            limit = config.get(resource.name)
123
            if limit is not None:
124
                self.change_resource_limit(resource, limit)
125

    
126
    def change_interactive(self, resource_name, _placeholder):
127
        if resource_name is None:
128
            resources = Resource.objects.all().select_for_update()
129
        else:
130
            resources = [self.get_resource(resource_name)]
131

    
132
        for resource in resources:
133
            self.stdout.write("Resource '%s' (%s)\n" %
134
                              (resource.name, resource.desc))
135
            value = show_resource_value(resource.uplimit, resource.name,
136
                                        self.unit_style)
137
            self.stdout.write("Current limit: %s\n" % value)
138
            while True:
139
                self.stdout.write("New limit (leave blank to keep current): ")
140
                response = raw_input()
141
                if response == "":
142
                    break
143
                else:
144
                    try:
145
                        value = units.parse(response)
146
                    except units.ParseError:
147
                        continue
148
                    update_resource(resource, value)
149
                    break
150

    
151
    def change_resource_limit(self, resource, limit):
152
        if not isinstance(limit, (int, long)):
153
            try:
154
                limit = units.parse(limit)
155
            except units.ParseError:
156
                m = ("Limit should be an integer, optionally followed "
157
                     "by a unit.")
158
                raise CommandError(m)
159
            update_resource(resource, limit)