Statistics
| Branch: | Tag: | Revision:

root / snf-astakos-app / astakos / im / management / commands / project-control.py @ 39b2cb50

History | View | Annotate | Download (5.8 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
from optparse import make_option
35

    
36
from django.core.management.base import BaseCommand, CommandError
37
from astakos.im.functions import (terminate, suspend, resume, check_expiration,
38
                                  approve_application, deny_application)
39
from synnefo.lib.db.transaction import commit_on_success_strict
40

    
41

    
42
class Command(BaseCommand):
43
    help = "Manage projects and applications"
44

    
45
    option_list = BaseCommand.option_list + (
46
        make_option('--approve',
47
                    dest='approve',
48
                    metavar='<application id>',
49
                    help="Approve a project application"),
50
        make_option('--deny',
51
                    dest='deny',
52
                    metavar='<application id>',
53
                    help="Deny a project application"),
54
        make_option('--terminate',
55
                    dest='terminate',
56
                    metavar='<project id>',
57
                    help="Terminate a project"),
58
        make_option('--suspend',
59
                    dest='suspend',
60
                    metavar='<project id>',
61
                    help="Suspend a project"),
62
        make_option('--unsuspend',
63
                    dest='resume',
64
                    metavar='<project id>',
65
                    help="Resume a suspended project"),
66
        make_option('--check-expired',
67
                    action='store_true',
68
                    dest='check_expired',
69
                    default=False,
70
                    help="Check projects for expiration"),
71
        make_option('--terminate-expired',
72
                    action='store_true',
73
                    dest='terminate_expired',
74
                    default=False,
75
                    help="Terminate all expired projects"),
76
        make_option('--message', '-m',
77
                    dest='message',
78
                    metavar='<msg>',
79
                    help=("Specify reason of action, "
80
                          "e.g. when denying a project")),
81
    )
82

    
83
    def handle(self, *args, **options):
84

    
85
        message = options['message']
86

    
87
        pid = options['terminate']
88
        if pid is not None:
89
            self.run_command(terminate, pid)
90
            return
91

    
92
        pid = options['resume']
93
        if pid is not None:
94
            self.run_command(resume, pid)
95
            return
96

    
97
        pid = options['suspend']
98
        if pid is not None:
99
            self.run_command(suspend, pid)
100
            return
101

    
102
        appid = options['approve']
103
        if appid is not None:
104
            self.run_command(approve_application, appid)
105
            return
106

    
107
        appid = options['deny']
108
        if appid is not None:
109
            self.run_command(deny_application, appid, message)
110
            return
111

    
112
        if options['check_expired']:
113
            self.expire(execute=False)
114
            return
115

    
116
        if options['terminate_expired']:
117
            self.expire(execute=True)
118

    
119
    def run_command(self, func, *args):
120
        @commit_on_success_strict()
121
        def inner():
122
            try:
123
                func(*args)
124
            except BaseException as e:
125
                raise CommandError(e)
126
        inner()
127

    
128
    def print_expired(self, projects, execute):
129
        length = len(projects)
130
        if length == 0:
131
            s = 'No expired projects.\n'
132
        elif length == 1:
133
            s = '1 expired project:\n'
134
        else:
135
            s = '%d expired projects:\n' % (length,)
136
        self.stdout.write(s)
137

    
138
        if length > 0:
139
            labels = ('Project', 'Name', 'Status', 'Expiration date')
140
            columns = (10, 30, 14, 30)
141

    
142
            line = ' '.join(l.rjust(w) for l, w in zip(labels, columns))
143
            self.stdout.write(line + '\n')
144
            sep = '-' * len(line)
145
            self.stdout.write(sep + '\n')
146

    
147
            for project in projects:
148
                line = ' '.join(f.rjust(w) for f, w in zip(project, columns))
149
                self.stdout.write(line + '\n')
150

    
151
            if execute:
152
                self.stdout.write('%d projects have been terminated.\n' % (
153
                    length,))
154

    
155
    @commit_on_success_strict()
156
    def expire(self, execute=False, ctx=None):
157
        try:
158
            projects = check_expiration(execute=execute)
159
            self.print_expired(projects, execute)
160
        except BaseException as e:
161
            if ctx:
162
                ctx.mark_rollback()
163
            raise CommandError(e)