1 ##############################################################################
2 # Copyright (c) 2017 Huawei Technologies Co.,Ltd.
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
13 from datetime import datetime
15 from oslo_serialization import jsonutils
17 from api import ApiResource
18 from api.database.v2.handlers import V2TaskHandler
19 from api.database.v2.handlers import V2ProjectHandler
20 from api.database.v2.handlers import V2EnvironmentHandler
21 from api.utils.thread import TaskThread
22 from yardstick.common.utils import result_handler
23 from yardstick.common.utils import change_obj_to_dict
24 from yardstick.common import constants as consts
25 from yardstick.benchmark.core.task import Task
26 from yardstick.benchmark.core import Param
28 LOG = logging.getLogger(__name__)
29 LOG.setLevel(logging.DEBUG)
32 class V2Tasks(ApiResource):
35 task_handler = V2TaskHandler()
36 tasks = [change_obj_to_dict(t) for t in task_handler.list_all()]
40 t['result'] = jsonutils.loads(result) if result else None
42 return result_handler(consts.API_SUCCESS, {'tasks': tasks})
45 return self._dispatch_post()
47 def create_task(self, args):
51 return result_handler(consts.API_ERROR, 'name must be provided')
54 project_id = args['project_id']
56 return result_handler(consts.API_ERROR, 'project_id must be provided')
58 task_id = str(uuid.uuid4())
59 create_time = datetime.now()
60 task_handler = V2TaskHandler()
62 LOG.info('create task in database')
65 'project_id': project_id,
70 task_handler.insert(task_init_data)
72 LOG.info('create task in project')
73 project_handler = V2ProjectHandler()
74 project_handler.append_attr(project_id, {'tasks': task_id})
76 return result_handler(consts.API_SUCCESS, {'uuid': task_id})
79 class V2Task(ApiResource):
81 def get(self, task_id):
85 return result_handler(consts.API_ERROR, 'invalid task id')
87 task_handler = V2TaskHandler()
89 task = task_handler.get_by_uuid(task_id)
91 return result_handler(consts.API_ERROR, 'no such task id')
93 task_info = change_obj_to_dict(task)
94 result = task_info['result']
95 task_info['result'] = jsonutils.loads(result) if result else None
97 return result_handler(consts.API_SUCCESS, {'task': task_info})
99 def delete(self, task_id):
103 return result_handler(consts.API_ERROR, 'invalid task id')
105 task_handler = V2TaskHandler()
107 project_id = task_handler.get_by_uuid(task_id).project_id
109 return result_handler(consts.API_ERROR, 'no such task id')
111 LOG.info('delete task in database')
112 task_handler.delete_by_uuid(task_id)
114 project_handler = V2ProjectHandler()
115 project = project_handler.get_by_uuid(project_id)
118 LOG.info('update tasks in project')
119 new_task_list = project.tasks.split(',')
120 new_task_list.remove(task_id)
122 new_tasks = ','.join(new_task_list)
125 project_handler.update_attr(project_id, {'tasks': new_tasks})
127 return result_handler(consts.API_SUCCESS, {'task': task_id})
129 def put(self, task_id):
134 return result_handler(consts.API_ERROR, 'invalid task id')
136 task_handler = V2TaskHandler()
138 task_handler.get_by_uuid(task_id)
140 return result_handler(consts.API_ERROR, 'no such task id')
142 return self._dispatch_post(task_id=task_id)
144 def add_environment(self, args):
146 task_id = args['task_id']
148 environment_id = args['environment_id']
150 return result_handler(consts.API_ERROR, 'environment_id must be provided')
153 uuid.UUID(environment_id)
155 return result_handler(consts.API_ERROR, 'invalid environment id')
157 environment_handler = V2EnvironmentHandler()
159 environment_handler.get_by_uuid(environment_id)
161 return result_handler(consts.API_ERROR, 'no such environment id')
163 LOG.info('update environment_id in task')
164 task_handler = V2TaskHandler()
165 task_handler.update_attr(task_id, {'environment_id': environment_id})
167 return result_handler(consts.API_SUCCESS, {'uuid': task_id})
169 def add_case(self, args):
170 task_id = args['task_id']
172 name = args['case_name']
174 return result_handler(consts.API_ERROR, 'case_name must be provided')
177 content = args['case_content']
179 return result_handler(consts.API_ERROR, 'case_content must be provided')
181 LOG.info('update case info in task')
182 task_handler = V2TaskHandler()
188 task_handler.update_attr(task_id, task_update_data)
190 return result_handler(consts.API_SUCCESS, {'uuid': task_id})
192 def add_suite(self, args):
193 task_id = args['task_id']
195 name = args['suite_name']
197 return result_handler(consts.API_ERROR, 'suite_name must be provided')
200 content = args['suite_content']
202 return result_handler(consts.API_ERROR, 'suite_content must be provided')
204 LOG.info('update suite info in task')
205 task_handler = V2TaskHandler()
211 task_handler.update_attr(task_id, task_update_data)
213 return result_handler(consts.API_SUCCESS, {'uuid': task_id})
217 task_id = args['task_id']
219 return result_handler(consts.API_ERROR, 'task_id must be provided')
224 return result_handler(consts.API_ERROR, 'invalid task id')
226 task_handler = V2TaskHandler()
228 task = task_handler.get_by_uuid(task_id)
230 return result_handler(consts.API_ERROR, 'no such task id')
232 if not task.environment_id:
233 return result_handler(consts.API_ERROR, 'environment not set')
235 if not task.case_name or not task.content:
236 return result_handler(consts.API_ERROR, 'case not set')
239 return result_handler(consts.API_ERROR, 'task is already running')
241 with open('/tmp/{}.yaml'.format(task.case_name), 'w') as f:
242 f.write(task.content)
245 'inputfile': ['/tmp/{}.yaml'.format(task.case_name)],
249 data.update({'suite': True})
251 LOG.info('start task thread')
253 task_thread = TaskThread(Task().start, param, task_handler)
256 return result_handler(consts.API_SUCCESS, {'uuid': task_id})
259 class V2TaskLog(ApiResource):
261 def get(self, task_id):
265 return result_handler(consts.API_ERROR, 'invalid task id')
267 task_handler = V2TaskHandler()
269 task = task_handler.get_by_uuid(task_id)
271 return result_handler(consts.API_ERROR, 'no such task id')
273 index = int(self._get_args().get('index', 0))
276 with open(os.path.join(consts.TASK_LOG_DIR, '{}.log'.format(task_id))) as f:
281 if e.errno == errno.ENOENT:
282 return result_handler(consts.API_ERROR, 'log file does not exist')
283 return result_handler(consts.API_ERROR, 'error with log file')
290 return result_handler(task.status, return_data)