aboutsummaryrefslogtreecommitdiffstats
path: root/api/resources/v2/tasks.py
blob: 17241ed63c88d9e7a89146951ed6f9031fb66e1c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
##############################################################################
# Copyright (c) 2017 Huawei Technologies Co.,Ltd.
#
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Apache License, Version 2.0
# which accompanies this distribution, and is available at
# http://www.apache.org/licenses/LICENSE-2.0
##############################################################################
import uuid
import logging
import os
import errno
from datetime import datetime

from oslo_serialization import jsonutils

from api import ApiResource
from api.database.v2.handlers import V2TaskHandler
from api.database.v2.handlers import V2ProjectHandler
from api.database.v2.handlers import V2EnvironmentHandler
from api.utils.thread import TaskThread
from yardstick.common.utils import result_handler
from yardstick.common.utils import change_obj_to_dict
from yardstick.common import constants as consts
from yardstick.benchmark.core.task import Task
from yardstick.benchmark.core import Param

LOG = logging.getLogger(__name__)
LOG.setLevel(logging.DEBUG)


class V2Tasks(ApiResource):

    def get(self):
        task_handler = V2TaskHandler()
        tasks = [change_obj_to_dict(t) for t in task_handler.list_all()]

        for t in tasks:
            result = t['result']
            t['result'] = jsonutils.loads(result) if result else None
            params = t['params']
            t['params'] = jsonutils.loads(params) if params else None

        return result_handler(consts.API_SUCCESS, {'tasks': tasks})

    def post(self):
        return self._dispatch_post()

    def create_task(self, args):
        try:
            name = args['name']
        except KeyError:
            return result_handler(consts.API_ERROR, 'name must be provided')

        try:
            project_id = args['project_id']
        except KeyError:
            return result_handler(consts.API_ERROR, 'project_id must be provided')

        task_id = str(uuid.uuid4())
        create_time = datetime.now()
        task_handler = V2TaskHandler()

        LOG.info('create task in database')
        task_init_data = {
            'uuid': task_id,
            'project_id': project_id,
            'name': name,
            'time': create_time,
            'status': -1
        }
        task_handler.insert(task_init_data)

        LOG.info('create task in project')
        project_handler = V2ProjectHandler()
        project_handler.append_attr(project_id, {'tasks': task_id})

        return result_handler(consts.API_SUCCESS, {'uuid': task_id})


class V2Task(ApiResource):

    def get(self, task_id):
        try:
            uuid.UUID(task_id)
        except ValueError:
            return result_handler(consts.API_ERROR, 'invalid task id')

        task_handler = V2TaskHandler()
        try:
            task = task_handler.get_by_uuid(task_id)
        except ValueError:
            return result_handler(consts.API_ERROR, 'no such task id')

        task_info = change_obj_to_dict(task)
        result = task_info['result']
        task_info['result'] = jsonutils.loads(result) if result else None

        params = task_info['params']
        task_info['params'] = jsonutils.loads(params) if params else None

        return result_handler(consts.API_SUCCESS, {'task': task_info})

    def delete(self, task_id):
        try:
            uuid.UUID(task_id)
        except ValueError:
            return result_handler(consts.API_ERROR, 'invalid task id')

        task_handler = V2TaskHandler()
        try:
            project_id = task_handler.get_by_uuid(task_id).project_id
        except ValueError:
            return result_handler(consts.API_ERROR, 'no such task id')

        LOG.info('delete task in database')
        task_handler.delete_by_uuid(task_id)

        project_handler = V2ProjectHandler()
        project = project_handler.get_by_uuid(project_id)

        if project.tasks:
            LOG.info('update tasks in project')
            new_task_list = project.tasks.split(',')
            new_task_list.remove(task_id)
            if new_task_list:
                new_tasks = ','.join(new_task_list)
            else:
                new_tasks = None
            project_handler.update_attr(project_id, {'tasks': new_tasks})

        return result_handler(consts.API_SUCCESS, {'task': task_id})

    def put(self, task_id):
        try:
            uuid.UUID(task_id)
        except ValueError:
            return result_handler(consts.API_ERROR, 'invalid task id')

        task_handler = V2TaskHandler()
        try:
            task_handler.get_by_uuid(task_id)
        except ValueError:
            return result_handler(consts.API_ERROR, 'no such task id')

        return self._dispatch_post(task_id=task_id)

    def add_environment(self, args):

        task_id = args['task_id']
        try:
            environment_id = args['environment_id']
        except KeyError:
            return result_handler(consts.API_ERROR, 'environment_id must be provided')

        try:
            uuid.UUID(environment_id)
        except ValueError:
            return result_handler(consts.API_ERROR, 'invalid environment id')

        environment_handler = V2EnvironmentHandler()
        try:
            environment_handler.get_by_uuid(environment_id)
        except ValueError:
            return result_handler(consts.API_ERROR, 'no such environment id')

        LOG.info('update environment_id in task')
        task_handler = V2TaskHandler()
        task_handler.update_attr(task_id, {'environment_id': environment_id})

        return result_handler(consts.API_SUCCESS, {'uuid': task_id})

    def add_params(self, args):
        task_id = args['task_id']
        try:
            params = args['params']
        except KeyError:
            return result_handler(consts.API_ERROR, 'params must be provided')

        LOG.info('update params info in task')

        task_handler = V2TaskHandler()
        task_update_data = {'params': jsonutils.dumps(params)}
        task_handler.update_attr(task_id, task_update_data)

        return result_handler(consts.API_SUCCESS, {'uuid': task_id})

    def add_case(self, args):
        task_id = args['task_id']
        try:
            name = args['case_name']
        except KeyError:
            return result_handler(consts.API_ERROR, 'case_name must be provided')

        try:
            content = args['case_content']
        except KeyError:
            return result_handler(consts.API_ERROR, 'case_content must be provided')

        LOG.info('update case info in task')
        task_handler = V2TaskHandler()
        task_update_data = {
            'case_name': name,
            'content': content,
            'suite': False
        }
        task_handler.update_attr(task_id, task_update_data)

        return result_handler(consts.API_SUCCESS, {'uuid': task_id})

    def add_suite(self, args):
        task_id = args['task_id']
        try:
            name = args['suite_name']
        except KeyError:
            return result_handler(consts.API_ERROR, 'suite_name must be provided')

        try:
            content = args['suite_content']
        except KeyError:
            return result_handler(consts.API_ERROR, 'suite_content must be provided')

        LOG.info('update suite info in task')
        task_handler = V2TaskHandler()
        task_update_data = {
            'case_name': name,
            'content': content,
            'suite': True
        }
        task_handler.update_attr(task_id, task_update_data)

        return result_handler(consts.API_SUCCESS, {'uuid': task_id})

    def run(self, args):
        try:
            task_id = args['task_id']
        except KeyError:
            return result_handler(consts.API_ERROR, 'task_id must be provided')

        try:
            uuid.UUID(task_id)
        except ValueError:
            return result_handler(consts.API_ERROR, 'invalid task id')

        task_handler = V2TaskHandler()
        try:
            task = task_handler.get_by_uuid(task_id)
        except ValueError:
            return result_handler(consts.API_ERROR, 'no such task id')

        if not task.environment_id:
            return result_handler(consts.API_ERROR, 'environment not set')

        if not task.case_name or not task.content:
            return result_handler(consts.API_ERROR, 'case not set')

        if task.status == 0:
            return result_handler(consts.API_ERROR, 'task is already running')

        with open('/tmp/{}.yaml'.format(task.case_name), 'w') as f:
            f.write(task.content)

        data = {
            'inputfile': ['/tmp/{}.yaml'.format(task.case_name)],
            'task_id': task_id,
            'task-args': task.params
        }
        if task.suite:
            data.update({'suite': True})

        LOG.info('start task thread')
        param = Param(data)
        task_thread = TaskThread(Task().start, param, task_handler)
        task_thread.start()

        return result_handler(consts.API_SUCCESS, {'uuid': task_id})


class V2TaskLog(ApiResource):

    def get(self, task_id):
        try:
            uuid.UUID(task_id)
        except ValueError:
            return result_handler(consts.API_ERROR, 'invalid task id')

        task_handler = V2TaskHandler()
        try:
            task = task_handler.get_by_uuid(task_id)
        except ValueError:
            return result_handler(consts.API_ERROR, 'no such task id')

        index = int(self._get_args().get('index', 0))

        try:
            with open(os.path.join(consts.TASK_LOG_DIR, '{}.log'.format(task_id))) as f:
                f.seek(index)
                data = f.readlines()
                index = f.tell()
        except OSError as e:
            if e.errno == errno.ENOENT:
                return result_handler(consts.API_ERROR, 'log file does not exist')
            return result_handler(consts.API_ERROR, 'error with log file')

        return_data = {
            'index': index,
            'data': data
        }

        return result_handler(task.status, return_data)