executions.py 5.52 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

20
import zoe_api.exceptions
21 22 23 24 25 26 27 28 29 30 31
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 ExecutionStartWeb(ZoeRequestHandler):
    """Handler class"""
    def initialize(self, **kwargs):
        """Initializes the request handler."""
        super().initialize(**kwargs)
        self.api_endpoint = kwargs['api_endpoint']  # type: APIEndpoint
32

33 34 35 36
    @catch_exceptions
    def post(self):
        """Start an execution."""
        uid, role = get_auth(self)
37 38
        if uid is None:
            return self.redirect(self.get_argument('next', u'/login'))
39

40 41 42
        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')
43

44
        new_id = self.api_endpoint.execution_start(uid, role, exec_name, app_descr)
45

46
        self.redirect(self.reverse_url('execution_inspect', new_id))
47 48


49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
class ExecutionListWeb(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):
        """Home page with authentication."""
        uid, role = get_auth(self)
        if uid is None:
            return self.redirect(self.get_argument('next', u'/login'))

        executions = self.api_endpoint.execution_list(uid, role)

        template_vars = {
            "uid": uid,
            "role": role,
68
            'executions': sorted(executions, key=lambda e: e.id, reverse=True)
69 70 71 72
        }
        self.render('execution_list.html', **template_vars)


73 74 75 76 77 78
class ExecutionRestartWeb(ZoeRequestHandler):
    """Handler class"""
    def initialize(self, **kwargs):
        """Initializes the request handler."""
        super().initialize(**kwargs)
        self.api_endpoint = kwargs['api_endpoint']  # type: APIEndpoint
79

80 81 82 83
    @catch_exceptions
    def get(self, execution_id: int):
        """Restart an already defined (and not running) execution."""
        uid, role = get_auth(self)
84 85
        if uid is None:
            return self.redirect(self.get_argument('next', u'/login'))
86

87 88
        e = self.api_endpoint.execution_by_id(uid, role, execution_id)
        new_id = self.api_endpoint.execution_start(uid, role, e.name, e.description)
89

90
        self.redirect(self.reverse_url('execution_inspect', new_id))
91 92


93 94 95 96 97 98
class ExecutionTerminateWeb(ZoeRequestHandler):
    """Handler class"""
    def initialize(self, **kwargs):
        """Initializes the request handler."""
        super().initialize(**kwargs)
        self.api_endpoint = kwargs['api_endpoint']  # type: APIEndpoint
99

100 101 102 103
    @catch_exceptions
    def get(self, execution_id: int):
        """Terminate an execution."""
        uid, role = get_auth(self)
104 105
        if uid is None:
            return self.redirect(self.get_argument('next', u'/login'))
106

107 108 109
        success, message = self.api_endpoint.execution_terminate(uid, role, execution_id)
        if not success:
            raise zoe_api.exceptions.ZoeException(message)
110

111
        self.redirect(self.reverse_url('home_user'))
112 113


114 115 116 117 118 119
class ExecutionInspectWeb(ZoeRequestHandler):
    """Handler class"""
    def initialize(self, **kwargs):
        """Initializes the request handler."""
        super().initialize(**kwargs)
        self.api_endpoint = kwargs['api_endpoint']  # type: APIEndpoint
120

121 122 123 124
    @catch_exceptions
    def get(self, execution_id):
        """Gather details about an execution."""
        uid, role = get_auth(self)
125 126
        if uid is None:
            return self.redirect(self.get_argument('next', u'/login'))
127

128
        e = self.api_endpoint.execution_by_id(uid, role, execution_id)
129

130
        services_info, endpoints = self.api_endpoint.execution_endpoints(uid, role, e)
131

132 133
        endpoints = self.api_endpoint.execution_endpoints(uid, role, e)[1]

134
        template_vars = {
135 136
            "uid": uid,
            "role": role,
137
            "e": e,
138
            "services_info": services_info,
139
            "endpoints": endpoints,
140 141
        }
        self.render('execution_inspect.html', **template_vars)
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160


class ServiceLogsWeb(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, service_id):
        """Gather details about an execution."""
        uid, role = get_auth(self)
        if uid is None:
            return self.redirect(self.get_argument('next', u'/login'))

        service = self.api_endpoint.service_by_id(uid, role, service_id)

        template_vars = {
161 162
            "uid": uid,
            "role": role,
163 164 165
            "service": service,
        }
        self.render('service_logs.html', **template_vars)