executions.py 4.55 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# Copyright (c) 2015, Daniele Venzano
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied.
# See the License for the specific language governing permissions and
# limitations under the License.

16
17
"""Web pages and functions related to executions."""

18
19
import json

Daniele Venzano's avatar
Daniele Venzano committed
20
import zoe_api.exceptions
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
from zoe_api.web.utils import get_auth, catch_exceptions
from zoe_api.api_endpoint import APIEndpoint  # pylint: disable=unused-import
from zoe_api.web.custom_request_handler import ZoeRequestHandler


class ExecutionDefineWeb(ZoeRequestHandler):
    """Handler class"""
    def initialize(self, **kwargs):
        """Initializes the request handler."""
        super().initialize(**kwargs)
        self.api_endpoint = kwargs['api_endpoint']  # type: APIEndpoint

    @catch_exceptions
    def get(self):
        """Define a new execution."""
        get_auth(self)

        self.render('execution_new.html')


class ExecutionStartWeb(ZoeRequestHandler):
    """Handler class"""
    def initialize(self, **kwargs):
        """Initializes the request handler."""
        super().initialize(**kwargs)
        self.api_endpoint = kwargs['api_endpoint']  # type: APIEndpoint
47

48
49
50
51
    @catch_exceptions
    def post(self):
        """Start an execution."""
        uid, role = get_auth(self)
52

53
54
55
        app_descr_json = self.request.files['file'][0]['body'].decode('utf-8')
        app_descr = json.loads(app_descr_json)
        exec_name = self.get_argument('exec_name')
56

57
        new_id = self.api_endpoint.execution_start(uid, role, exec_name, app_descr)
58

59
        self.redirect(self.reverse_url('execution_inspect', new_id))
60
61


62
63
64
65
66
67
class ExecutionRestartWeb(ZoeRequestHandler):
    """Handler class"""
    def initialize(self, **kwargs):
        """Initializes the request handler."""
        super().initialize(**kwargs)
        self.api_endpoint = kwargs['api_endpoint']  # type: APIEndpoint
68

69
70
71
72
    @catch_exceptions
    def get(self, execution_id: int):
        """Restart an already defined (and not running) execution."""
        uid, role = get_auth(self)
73

74
75
        e = self.api_endpoint.execution_by_id(uid, role, execution_id)
        new_id = self.api_endpoint.execution_start(uid, role, e.name, e.description)
76

77
        self.redirect(self.reverse_url('execution_inspect', new_id))
78
79


80
81
82
83
84
85
class ExecutionTerminateWeb(ZoeRequestHandler):
    """Handler class"""
    def initialize(self, **kwargs):
        """Initializes the request handler."""
        super().initialize(**kwargs)
        self.api_endpoint = kwargs['api_endpoint']  # type: APIEndpoint
86

87
88
89
90
    @catch_exceptions
    def get(self, execution_id: int):
        """Terminate an execution."""
        uid, role = get_auth(self)
91

92
93
94
        success, message = self.api_endpoint.execution_terminate(uid, role, execution_id)
        if not success:
            raise zoe_api.exceptions.ZoeException(message)
95

96
        self.redirect(self.reverse_url('home_user'))
97
98


99
100
101
102
103
104
class ExecutionDeleteWeb(ZoeRequestHandler):
    """Handler class"""
    def initialize(self, **kwargs):
        """Initializes the request handler."""
        super().initialize(**kwargs)
        self.api_endpoint = kwargs['api_endpoint']  # type: APIEndpoint
105

106
107
108
109
    @catch_exceptions
    def get(self, execution_id: int):
        """Delete an execution."""
        uid, role = get_auth(self)
110

111
112
113
        success, message = self.api_endpoint.execution_delete(uid, role, execution_id)
        if not success:
            raise zoe_api.exceptions.ZoeException(message)
114

115
        self.redirect(self.reverse_url('home_user'))
116
117


118
119
120
121
122
123
class ExecutionInspectWeb(ZoeRequestHandler):
    """Handler class"""
    def initialize(self, **kwargs):
        """Initializes the request handler."""
        super().initialize(**kwargs)
        self.api_endpoint = kwargs['api_endpoint']  # type: APIEndpoint
124

125
126
127
128
    @catch_exceptions
    def get(self, execution_id):
        """Gather details about an execution."""
        uid, role = get_auth(self)
129

130
        e = self.api_endpoint.execution_by_id(uid, role, execution_id)
131

132
        services_info, endpoints = self.api_endpoint.execution_endpoints(uid, role, e)
133

134
135
        template_vars = {
            "e": e,
136
            "endpoints": endpoints,
137
138
139
            "services_info": services_info
        }
        self.render('execution_inspect.html', **template_vars)