api_endpoint.py 8.38 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# Copyright (c) 2016, 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.

"""The real API, exposed as web pages or REST API."""

import logging
19
import os
20

21
22
import zoe_api.exceptions
import zoe_api.master_api
23
24
import zoe_lib.applications
import zoe_lib.exceptions
25
26
import zoe_lib.state
from zoe_lib.config import get_conf
27
28
29

log = logging.getLogger(__name__)

30
31
GUEST_QUOTA_MAX_EXECUTIONS = 1

32
33
34
35
36
37
38
39

class APIEndpoint:
    """
    The APIEndpoint class.

    :type master: zoe_api.master_api.APIManager
    :type sql: zoe_lib.sql_manager.SQLManager
    """
40
    def __init__(self, master_api, sql_manager: zoe_lib.state.SQLManager):
41
42
        self.master = master_api
        self.sql = sql_manager
43

44
    def execution_by_id(self, uid, role, execution_id) -> zoe_lib.state.Execution:
45
        """Lookup an execution by its ID."""
46
        e = self.sql.executions.select(id=execution_id, only_one=True)
47
48
        if e is None:
            raise zoe_api.exceptions.ZoeNotFoundException('No such execution')
49
        assert isinstance(e, zoe_lib.state.Execution)
50
51
52
53
54
55
        if e.user_id != uid and role != 'admin':
            raise zoe_api.exceptions.ZoeAuthException()
        return e

    def execution_list(self, uid, role, **filters):
        """Generate a optionally filtered list of executions."""
56
        execs = self.sql.executions.select(**filters)
57
58
59
        ret = [e for e in execs if e.user_id == uid or role == 'admin']
        return ret

60
61
62
63
64
65
66
    def zapp_validate(self, application_description):
        """Validates the passed ZApp description against the supported schema."""
        try:
            zoe_lib.applications.app_validate(application_description)
        except zoe_lib.exceptions.InvalidApplicationDescription as e:
            raise zoe_api.exceptions.ZoeException('Invalid application description: ' + e.message)

67
    def execution_start(self, uid, role, exec_name, application_description):  # pylint: disable=unused-argument
68
69
70
71
72
73
        """Start an execution."""
        try:
            zoe_lib.applications.app_validate(application_description)
        except zoe_lib.exceptions.InvalidApplicationDescription as e:
            raise zoe_api.exceptions.ZoeException('Invalid application description: ' + e.message)

74
75
76
77
78
        # quota check
        if role == "guest":
            running_execs = self.execution_list(uid, role, **{'status': 'running'})
            running_execs += self.execution_list(uid, role, **{'status': 'starting'})
            running_execs += self.execution_list(uid, role, **{'status': 'scheduled'})
Daniele Venzano's avatar
Daniele Venzano committed
79
            running_execs += self.execution_list(uid, role, **{'status': 'image download'})
80
            running_execs += self.execution_list(uid, role, **{'status': 'submitted'})
Daniele Venzano's avatar
Daniele Venzano committed
81
            if len(running_execs) >= GUEST_QUOTA_MAX_EXECUTIONS:
82
                raise zoe_api.exceptions.ZoeException('Guest users cannot run more than one execution at a time, quota exceeded.')
83

84
        new_id = self.sql.executions.insert(exec_name, uid, application_description)
85
86
87
        success, message = self.master.execution_start(new_id)
        if not success:
            raise zoe_api.exceptions.ZoeException('The Zoe master is unavailable, execution will be submitted automatically when the master is back up ({}).'.format(message))
hxquangnhat's avatar
hxquangnhat committed
88

89
90
91
92
        return new_id

    def execution_terminate(self, uid, role, exec_id):
        """Terminate an execution."""
93
94
        e = self.sql.executions.select(id=exec_id, only_one=True)
        assert isinstance(e, zoe_lib.state.Execution)
95
96
97
98
99
100
        if e is None:
            raise zoe_api.exceptions.ZoeNotFoundException('No such execution')

        if e.user_id != uid and role != 'admin':
            raise zoe_api.exceptions.ZoeAuthException()

101
        if e.is_active:
102
103
104
105
106
107
            return self.master.execution_terminate(exec_id)
        else:
            raise zoe_api.exceptions.ZoeException('Execution is not running')

    def execution_delete(self, uid, role, exec_id):
        """Delete an execution."""
108
109
110
        if role != "admin":
            raise zoe_api.exceptions.ZoeAuthException()

111
112
        e = self.sql.executions.select(id=exec_id, only_one=True)
        assert isinstance(e, zoe_lib.state.Execution)
113
114
115
116
117
118
        if e is None:
            raise zoe_api.exceptions.ZoeNotFoundException('No such execution')

        if e.user_id != uid and role != 'admin':
            raise zoe_api.exceptions.ZoeAuthException()

119
        if e.is_active:
120
            raise zoe_api.exceptions.ZoeException('Cannot delete an active execution')
121
122
123

        status, message = self.master.execution_delete(exec_id)
        if status:
124
            self.sql.executions.delete(exec_id)
125
126
127
128
            return True, ''
        else:
            raise zoe_api.exceptions.ZoeException(message)

129
    def service_by_id(self, uid, role, service_id) -> zoe_lib.state.Service:
130
        """Lookup a service by its ID."""
131
        service = self.sql.services.select(id=service_id, only_one=True)
132
133
134
135
136
137
138
139
        if service is None:
            raise zoe_api.exceptions.ZoeNotFoundException('No such execution')
        if service.user_id != uid and role != 'admin':
            raise zoe_api.exceptions.ZoeAuthException()
        return service

    def service_list(self, uid, role, **filters):
        """Generate a optionally filtered list of services."""
140
        services = self.sql.services.select(**filters)
141
142
143
        ret = [s for s in services if s.user_id == uid or role == 'admin']
        return ret

144
    def service_logs(self, uid, role, service_id):
145
146
147
        """Retrieve the logs for the given service.
        If stream is True, a file object is returned, otherwise the log contents as a str object.
        """
148
        service = self.sql.services.select(id=service_id, only_one=True)
149
150
151
152
        if service is None:
            raise zoe_api.exceptions.ZoeNotFoundException('No such service')
        if service.user_id != uid and role != 'admin':
            raise zoe_api.exceptions.ZoeAuthException()
153
154
155
156

        path = os.path.join(get_conf().service_logs_base_path, get_conf().deployment_name, str(service.execution_id), service.name + '.txt')
        if not os.path.exists(path):
            raise zoe_api.exceptions.ZoeNotFoundException('Service log not available')
157
        return open(path, encoding='utf-8')
158

159
160
    def statistics_scheduler(self, uid_, role_):
        """Retrieve statistics about the scheduler."""
161
162
163
        success, message = self.master.scheduler_statistics()
        if success:
            return message
164
165
166
167

    def cleanup_dead_executions(self):
        """Terminates all executions with dead "monitor" services."""
        log.debug('Starting dead execution cleanup task')
168
        all_execs = self.sql.executions.select(status='running')
169
        for execution in all_execs:
170
            for service in execution.services:
171
                if service.description['monitor'] and service.backend_status == service.BACKEND_DIE_STATUS:
172
173
174
                    log.info("Service {} ({}) of execution {} died, terminating execution".format(service.id, service.name, execution.id))
                    self.master.execution_terminate(execution.id)
                    break
175

176
        log.debug('Cleanup task finished')
177

178
    def execution_endpoints(self, uid: str, role: str, execution: zoe_lib.state.Execution):
179
180
181
182
183
184
        """Return a list of the services and public endpoints available for a certain execution."""
        services_info = []
        endpoints = []
        for service in execution.services:
            services_info.append(self.service_by_id(uid, role, service.id))
            for port in service.description['ports']:
185
                port_key = str(port['port_number']) + "/" + port['protocol']
186
                backend_port = self.sql.ports.select(only_one=True, service_id=service.id, internal_name=port_key)
187
                if backend_port is not None and backend_port.external_ip is not None:
188
                    endpoint = port['url_template'].format(**{"ip_port": backend_port.external_ip + ":" + str(backend_port.external_port)})
189
                    endpoints.append((port['name'], endpoint))
190
191

        return services_info, endpoints