root / snf-astakos-app / astakos / im / management / commands / resource-modify.py @ d758784b
History | View | Annotate | Download (7.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 |
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_resource |
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('--limit',
|
50 |
help="Specify default base quota"),
|
51 |
make_option('--limit-interactive',
|
52 |
action='store_true',
|
53 |
default=None,
|
54 |
help=("Prompt user to change default base quota. "
|
55 |
"If no resource is given, prompts for all "
|
56 |
"resources.")),
|
57 |
make_option('--limit-from-file',
|
58 |
metavar='<limits_file.json>',
|
59 |
help=("Read default base quota from a file. "
|
60 |
"File should contain a json dict mapping resource "
|
61 |
"names to limits")),
|
62 |
make_option('--unit-style',
|
63 |
default='mb',
|
64 |
help=("Specify display unit for resource values "
|
65 |
"(one of %s); defaults to mb") % style_options),
|
66 |
make_option('--allow-in-projects',
|
67 |
metavar='True|False',
|
68 |
help=("Specify whether to allow this resource "
|
69 |
"in projects.")),
|
70 |
) |
71 |
|
72 |
def handle(self, *args, **options): |
73 |
resource_name = args[0] if len(args) > 0 else None |
74 |
|
75 |
actions = { |
76 |
'limit': self.change_limit, |
77 |
'limit_interactive': self.change_interactive, |
78 |
'limit_from_file': self.change_from_file, |
79 |
'allow_in_projects': self.set_allow_in_projects, |
80 |
} |
81 |
|
82 |
opts = [(key, value) |
83 |
for (key, value) in options.items() |
84 |
if key in actions and value is not None] |
85 |
|
86 |
if len(opts) != 1: |
87 |
raise CommandError("Please provide exactly one of the options: " |
88 |
"--limit, --limit-interactive, "
|
89 |
"--limit-from-file, --allow-in-projects.")
|
90 |
|
91 |
self.unit_style = options['unit_style'] |
92 |
check_style(self.unit_style)
|
93 |
|
94 |
key, value = opts[0]
|
95 |
action = actions[key] |
96 |
action(resource_name, value) |
97 |
|
98 |
def set_allow_in_projects(self, resource_name, allow): |
99 |
if resource_name is None: |
100 |
raise CommandError("Please provide a resource name.") |
101 |
|
102 |
try:
|
103 |
allow = utils.parse_bool(allow) |
104 |
except ValueError: |
105 |
raise CommandError("Expecting a boolean value.") |
106 |
resource = self.get_resource(resource_name)
|
107 |
resource.allow_in_projects = allow |
108 |
resource.save() |
109 |
|
110 |
def get_resource(self, resource_name): |
111 |
try:
|
112 |
return Resource.objects.get_for_update(name=resource_name)
|
113 |
except Resource.DoesNotExist:
|
114 |
raise CommandError("Resource %s does not exist." |
115 |
% resource_name) |
116 |
|
117 |
def change_limit(self, resource_name, limit): |
118 |
if resource_name is None: |
119 |
raise CommandError("Please provide a resource name.") |
120 |
|
121 |
resource = self.get_resource(resource_name)
|
122 |
self.change_resource_limit(resource, limit)
|
123 |
|
124 |
def change_from_file(self, resource_name, filename): |
125 |
with open(filename) as file_data: |
126 |
try:
|
127 |
config = json.load(file_data) |
128 |
except json.JSONDecodeError:
|
129 |
raise CommandError("Malformed JSON file.") |
130 |
if not isinstance(config, dict): |
131 |
raise CommandError("Malformed JSON file.") |
132 |
self.change_with_conf(resource_name, config)
|
133 |
|
134 |
def change_with_conf(self, resource_name, config): |
135 |
if resource_name is None: |
136 |
resources = Resource.objects.all().select_for_update() |
137 |
else:
|
138 |
resources = [self.get_resource(resource_name)]
|
139 |
|
140 |
for resource in resources: |
141 |
limit = config.get(resource.name) |
142 |
if limit is not None: |
143 |
self.change_resource_limit(resource, limit)
|
144 |
|
145 |
def change_interactive(self, resource_name, _placeholder): |
146 |
if resource_name is None: |
147 |
resources = Resource.objects.all().select_for_update() |
148 |
else:
|
149 |
resources = [self.get_resource(resource_name)]
|
150 |
|
151 |
for resource in resources: |
152 |
self.stdout.write("Resource '%s' (%s)\n" % |
153 |
(resource.name, resource.desc)) |
154 |
value = show_resource_value(resource.uplimit, resource.name, |
155 |
self.unit_style)
|
156 |
self.stdout.write("Current limit: %s\n" % value) |
157 |
while True: |
158 |
self.stdout.write("New limit (leave blank to keep current): ") |
159 |
response = raw_input()
|
160 |
if response == "": |
161 |
break
|
162 |
else:
|
163 |
try:
|
164 |
value = units.parse(response) |
165 |
except units.ParseError:
|
166 |
continue
|
167 |
update_resource(resource, value) |
168 |
break
|
169 |
|
170 |
def change_resource_limit(self, resource, limit): |
171 |
if not isinstance(limit, (int, long)): |
172 |
try:
|
173 |
limit = units.parse(limit) |
174 |
except units.ParseError:
|
175 |
m = ("Limit should be an integer, optionally followed "
|
176 |
"by a unit.")
|
177 |
raise CommandError(m)
|
178 |
update_resource(resource, limit) |