root / snf-astakos-app / astakos / im / management / commands / user-set-initial-quota.py @ 2ec5a35c
History | View | Annotate | Download (7.7 kB)
1 |
# Copyright 2012, 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 os |
35 |
import uuid |
36 |
import string |
37 |
|
38 |
from optparse import make_option |
39 |
from collections import namedtuple |
40 |
|
41 |
from django.core.management.base import BaseCommand, CommandError |
42 |
from django.core.validators import validate_email |
43 |
|
44 |
from astakos.im.models import AstakosUser, AstakosUserQuota, Resource |
45 |
|
46 |
AddResourceArgs = namedtuple('AddQuotaArgs', ('resource', |
47 |
'capacity',
|
48 |
)) |
49 |
|
50 |
class Command(BaseCommand): |
51 |
help = """Import user quota limits from file or set quota
|
52 |
for a single user from the command line
|
53 |
|
54 |
The file must contain non-empty lines, and each line must
|
55 |
contain a single-space-separated list of values:
|
56 |
|
57 |
<user> <resource name> <capacity>
|
58 |
|
59 |
For example to grant the following user with 10 private networks
|
60 |
(independent of any he receives from projects):
|
61 |
|
62 |
6119a50b-cbc7-42c0-bafc-4b6570e3f6ac cyclades.network.private 10
|
63 |
|
64 |
Similar syntax is used when setting quota from the command line:
|
65 |
|
66 |
--set-capacity 6119a50b-cbc7-42c0-bafc-4b6570e3f6ac cyclades.vm 10
|
67 |
|
68 |
The special value of 'default' sets the user setting to the default.
|
69 |
"""
|
70 |
|
71 |
option_list = BaseCommand.option_list + ( |
72 |
make_option('--from-file',
|
73 |
dest='from_file',
|
74 |
metavar='<exported-quotas.txt>',
|
75 |
help="Import quotas from file"),
|
76 |
make_option('--set-capacity',
|
77 |
dest='set_capacity',
|
78 |
metavar='<uuid or email> <resource> <capacity>',
|
79 |
nargs=3,
|
80 |
help="Set capacity for a specified user/resource pair"),
|
81 |
|
82 |
make_option('-f', '--no-confirm', |
83 |
action='store_true',
|
84 |
default=False,
|
85 |
dest='force',
|
86 |
help="Do not ask for confirmation"),
|
87 |
) |
88 |
|
89 |
def handle(self, *args, **options): |
90 |
from_file = options['from_file']
|
91 |
set_capacity = options['set_capacity']
|
92 |
force = options['force']
|
93 |
|
94 |
if from_file is not None: |
95 |
if set_capacity is not None: |
96 |
raise CommandError("Cannot combine option `--from-file' with " |
97 |
"`--set-capacity'.")
|
98 |
self.import_from_file(from_file)
|
99 |
return
|
100 |
|
101 |
if set_capacity is not None: |
102 |
user, resource, capacity = set_capacity |
103 |
self.set_limit(user, resource, capacity, force)
|
104 |
return
|
105 |
|
106 |
m = "Please use either `--from-file' or `--set-capacity' options"
|
107 |
raise CommandError(m)
|
108 |
|
109 |
def set_limit(self, user_ident, resource, capacity, force): |
110 |
if is_uuid(user_ident):
|
111 |
try:
|
112 |
user = AstakosUser.objects.get(uuid=user_ident) |
113 |
except AstakosUser.DoesNotExist:
|
114 |
raise CommandError('Not found user having uuid: %s' % |
115 |
user_ident) |
116 |
elif is_email(user_ident):
|
117 |
try:
|
118 |
user = AstakosUser.objects.get(username=user_ident) |
119 |
except AstakosUser.DoesNotExist:
|
120 |
raise CommandError('Not found user having email: %s' % |
121 |
user_ident) |
122 |
else:
|
123 |
raise CommandError('Please specify user by uuid or email') |
124 |
|
125 |
if capacity != 'default': |
126 |
try:
|
127 |
capacity = int(capacity)
|
128 |
except ValueError: |
129 |
m = "Please specify capacity as a decimal integer or 'default'"
|
130 |
raise CommandError(m)
|
131 |
|
132 |
args = AddResourceArgs(resource=resource, |
133 |
capacity=capacity, |
134 |
) |
135 |
|
136 |
try:
|
137 |
quota, default_capacity = user.get_resource_policy(resource) |
138 |
except Resource.DoesNotExist:
|
139 |
raise CommandError("No such resource: %s" % resource) |
140 |
|
141 |
current = quota.capacity if quota is not None else 'default' |
142 |
|
143 |
if not force: |
144 |
self.stdout.write("user: %s (%s)\n" % (user.uuid, user.username)) |
145 |
self.stdout.write("default capacity: %s\n" % default_capacity) |
146 |
self.stdout.write("current capacity: %s\n" % current) |
147 |
self.stdout.write("new capacity: %s\n" % capacity) |
148 |
self.stdout.write("Confirm? (y/n) ") |
149 |
response = raw_input()
|
150 |
if string.lower(response) not in ['y', 'yes']: |
151 |
self.stdout.write("Aborted.\n") |
152 |
return
|
153 |
|
154 |
if capacity == 'default': |
155 |
try:
|
156 |
q = AstakosUserQuota.objects.get(user=user, |
157 |
resource__name=resource) |
158 |
q.delete() |
159 |
except Exception as e: |
160 |
import traceback |
161 |
traceback.print_exc() |
162 |
raise CommandError("Failed to remove policy: %s" % e) |
163 |
else:
|
164 |
try:
|
165 |
user.add_resource_policy(*args) |
166 |
except Exception as e: |
167 |
raise CommandError("Failed to add policy: %s" % e) |
168 |
|
169 |
def import_from_file(self, location): |
170 |
try:
|
171 |
f = open(location, 'r') |
172 |
except IOError, e: |
173 |
raise CommandError(e)
|
174 |
|
175 |
for line in f.readlines(): |
176 |
try:
|
177 |
t = line.rstrip('\n').split(' ') |
178 |
user = t[0]
|
179 |
args = AddResourceArgs(*t[1:])
|
180 |
except(IndexError, TypeError): |
181 |
self.stdout.write('Invalid line format: %s:\n' % t) |
182 |
continue
|
183 |
else:
|
184 |
try:
|
185 |
user = AstakosUser.objects.get(uuid=user) |
186 |
except AstakosUser.DoesNotExist:
|
187 |
self.stdout.write('Not found user having uuid: %s\n' % user) |
188 |
continue
|
189 |
else:
|
190 |
try:
|
191 |
user.add_resource_policy(*args) |
192 |
except Exception, e: |
193 |
self.stdout.write('Failed to policy: %s\n' % e) |
194 |
continue
|
195 |
finally:
|
196 |
f.close() |
197 |
|
198 |
|
199 |
def is_uuid(s): |
200 |
if s is None: |
201 |
return False |
202 |
try:
|
203 |
uuid.UUID(s) |
204 |
except ValueError: |
205 |
return False |
206 |
else:
|
207 |
return True |
208 |
|
209 |
|
210 |
def is_email(s): |
211 |
if s is None: |
212 |
return False |
213 |
try:
|
214 |
validate_email(s) |
215 |
except:
|
216 |
return False |
217 |
else:
|
218 |
return True |