Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (5.5 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 astakos.im.project_xctx import cmd_project_transaction_context
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
    )
77

    
78
    def handle(self, *args, **options):
79

    
80
        pid = options['terminate']
81
        if pid is not None:
82
            self.run_command(terminate, pid)
83
            return
84

    
85
        pid = options['resume']
86
        if pid is not None:
87
            self.run_command(resume, pid)
88
            return
89

    
90
        pid = options['suspend']
91
        if pid is not None:
92
            self.run_command(suspend, pid)
93
            return
94

    
95
        appid = options['approve']
96
        if appid is not None:
97
            self.run_command(approve_application, appid)
98
            return
99

    
100
        appid = options['deny']
101
        if appid is not None:
102
            self.run_command(deny_application, appid)
103
            return
104

    
105
        if options['check_expired']:
106
            self.expire(execute=False)
107
            return
108

    
109
        if options['terminate_expired']:
110
            self.expire(execute=True)
111

    
112
    @cmd_project_transaction_context(sync=True)
113
    def run_command(self, func, id, ctx=None):
114
        try:
115
            func(id)
116
        except BaseException as e:
117
            if ctx:
118
                ctx.mark_rollback()
119
            raise CommandError(e)
120

    
121
    def print_expired(self, projects, execute):
122
        length = len(projects)
123
        if length == 0:
124
            s = 'No expired projects.\n'
125
        elif length == 1:
126
            s = '1 expired project:\n'
127
        else:
128
            s = '%d expired projects:\n' % (length,)
129
        self.stdout.write(s)
130

    
131
        if length > 0:
132
            labels = ('Project', 'Name', 'Status', 'Expiration date')
133
            columns = (10, 30, 14, 30)
134

    
135
            line = ' '.join(l.rjust(w) for l, w in zip(labels, columns))
136
            self.stdout.write(line + '\n')
137
            sep = '-' * len(line)
138
            self.stdout.write(sep + '\n')
139

    
140
            for project in projects:
141
                line = ' '.join(f.rjust(w) for f, w in zip(project, columns))
142
                self.stdout.write(line + '\n')
143

    
144
            if execute:
145
                self.stdout.write('%d projects have been terminated.\n' % (
146
                    length,))
147

    
148
    @cmd_project_transaction_context(sync=True)
149
    def expire(self, execute=False, ctx=None):
150
        try:
151
            projects = check_expiration(execute=execute)
152
            self.print_expired(projects, execute)
153
        except BaseException as e:
154
            if ctx:
155
                ctx.mark_rollback()
156
            raise CommandError(e)