Statistics
| Branch: | Tag: | Revision:

root / snf-astakos-app / astakos / im / management / commands / user-set-initial-quota.py @ 88f4d3b3

History | View | Annotate | Download (8.4 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
from synnefo.lib.quotaholder.api import QH_PRACTICALLY_INFINITE
44

    
45
from astakos.im.models import AstakosUser, AstakosUserQuota, Resource
46

    
47
AddResourceArgs = namedtuple('AddQuotaArgs', ('resource',
48
                                              'capacity',
49
                                              'quantity',
50
                                              'import_limit',
51
                                              'export_limit'))
52

    
53
class Command(BaseCommand):
54
    help = """Import user quota limits from file or set quota
55
for a single user from the command line
56

57
    The file must contain non-empty lines, and each line must
58
    contain a single-space-separated list of values:
59

60
    <user> <resource name> <capacity> <quantity> <import_limit> <export_limit>
61

62
    For example to grant the following user with 10 private networks
63
    (independent of any he receives from projects):
64

65
    6119a50b-cbc7-42c0-bafc-4b6570e3f6ac cyclades.network.private 10 0 1000000 1000000
66

67
    The last two values are arbitrarily large to represent no
68
    import/export limit at all.
69

70
    When setting quota from the command line, specify only capacity.
71
    Quantity and import/export limit will get default values. Example:
72

73
    --set-capacity 6119a50b-cbc7-42c0-bafc-4b6570e3f6ac cyclades.vm 10
74

75
    The special value of 'default' sets the user setting to the default.
76
    """
77

    
78
    option_list = BaseCommand.option_list + (
79
        make_option('--from-file',
80
                    dest='from_file',
81
                    metavar='<exported-quotas.txt>',
82
                    help="Import quotas from file"),
83
        make_option('--set-capacity',
84
                    dest='set_capacity',
85
                    metavar='<uuid or email> <resource> <capacity>',
86
                    nargs=3,
87
                    help="Set capacity for a specified user/resource pair"),
88

    
89
        make_option('-f', '--no-confirm',
90
                    action='store_true',
91
                    default=False,
92
                    dest='force',
93
                    help="Do not ask for confirmation"),
94
    )
95

    
96
    def handle(self, *args, **options):
97
        from_file = options['from_file']
98
        set_capacity = options['set_capacity']
99
        force = options['force']
100

    
101
        if from_file is not None:
102
            if set_capacity is not None:
103
                raise CommandError("Cannot combine option `--from-file' with "
104
                                   "`--set-capacity'.")
105
            self.import_from_file(from_file)
106

    
107
        if set_capacity is not None:
108
            user, resource, capacity = set_capacity
109
            self.set_limit(user, resource, capacity, force)
110
            return
111

    
112
        m = "Please use either `--from-file' or `--set-capacity' options"
113
        raise CommandError(m)
114

    
115
    def set_limit(self, user_ident, resource, capacity, force):
116
        if is_uuid(user_ident):
117
            try:
118
                user = AstakosUser.objects.get(uuid=user_ident)
119
            except AstakosUser.DoesNotExist:
120
                raise CommandError('Not found user having uuid: %s' %
121
                                   user_ident)
122
        elif is_email(user_ident):
123
            try:
124
                user = AstakosUser.objects.get(username=user_ident)
125
            except AstakosUser.DoesNotExist:
126
                raise CommandError('Not found user having email: %s' %
127
                                   user_ident)
128
        else:
129
            raise CommandError('Please specify user by uuid or email')
130

    
131
        if capacity != 'default':
132
            try:
133
                capacity = int(capacity)
134
            except ValueError:
135
                m = "Please specify capacity as a decimal integer or 'default'"
136
                raise CommandError(m)
137

    
138
        args = AddResourceArgs(resource=resource,
139
                               capacity=capacity,
140
                               quantity=0,
141
                               import_limit=QH_PRACTICALLY_INFINITE,
142
                               export_limit=QH_PRACTICALLY_INFINITE,
143
                               )
144

    
145
        try:
146
            quota, default_capacity = user.get_resource_policy(resource)
147
        except Resource.DoesNotExist:
148
            raise CommandError("No such resource: %s" % resource)
149

    
150
        current = quota.capacity if quota is not None else 'default'
151

    
152
        if not force:
153
            self.stdout.write("user: %s (%s)\n" % (user.uuid, user.username))
154
            self.stdout.write("default capacity: %s\n" % default_capacity)
155
            self.stdout.write("current capacity: %s\n" % current)
156
            self.stdout.write("new capacity: %s\n" % capacity)
157
            self.stdout.write("Confirm? (y/n) ")
158
            response = raw_input()
159
            if string.lower(response) not in ['y', 'yes']:
160
                self.stdout.write("Aborted.\n")
161
                return
162

    
163
        if capacity == 'default':
164
            try:
165
                service, sep, name = resource.partition('.')
166
                q = AstakosUserQuota.objects.get(
167
                        user=user,
168
                        resource__service__name=service,
169
                        resource__name=name)
170
                q.delete()
171
            except Exception as e:
172
                import traceback
173
                traceback.print_exc()
174
                raise CommandError("Failed to remove policy: %s" % e)
175
        else:
176
            try:
177
                user.add_resource_policy(*args)
178
            except Exception as e:
179
                raise CommandError("Failed to add policy: %s" % e)
180

    
181
    def import_from_file(self, location):
182
        try:
183
            f = open(location, 'r')
184
        except IOError, e:
185
            raise CommandError(e)
186

    
187
        for line in f.readlines():
188
            try:
189
                t = line.rstrip('\n').split(' ')
190
                user = t[0]
191
                args = AddResourceArgs(*t[1:])
192
            except(IndexError, TypeError):
193
                self.stdout.write('Invalid line format: %s:\n' % t)
194
                continue
195
            else:
196
                try:
197
                    user = AstakosUser.objects.get(uuid=user)
198
                except AstakosUser.DoesNotExist:
199
                    self.stdout.write('Not found user having uuid: %s\n' % user)
200
                    continue
201
                else:
202
                    try:
203
                        user.add_resource_policy(*args)
204
                    except Exception, e:
205
                        self.stdout.write('Failed to policy: %s\n' % e)
206
                        continue
207
            finally:
208
                f.close()
209

    
210

    
211
def is_uuid(s):
212
    if s is None:
213
        return False
214
    try:
215
        uuid.UUID(s)
216
    except ValueError:
217
        return False
218
    else:
219
        return True
220

    
221

    
222
def is_email(s):
223
    if s is None:
224
        return False
225
    try:
226
        validate_email(s)
227
    except:
228
        return False
229
    else:
230
        return True