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 t['params'] = jsonutils.loads(params) if params else None
44 return result_handler(consts.API_SUCCESS, {'tasks': tasks})
47 return self._dispatch_post()
49 def create_task(self, args):
53 return result_handler(consts.API_ERROR, 'name must be provided')
56 project_id = args['project_id']
58 return result_handler(consts.API_ERROR, 'project_id must be provided')
60 task_id = str(uuid.uuid4())
61 create_time = datetime.now()
62 task_handler = V2TaskHandler()
64 LOG.info('create task in database')
67 'project_id': project_id,
72 task_handler.insert(task_init_data)
74 LOG.info('create task in project')
75 project_handler = V2ProjectHandler()
76 project_handler.append_attr(project_id, {'tasks': task_id})
78 return result_handler(consts.API_SUCCESS, {'uuid': task_id})
81 class V2Task(ApiResource):
83 def get(self, task_id):
87 return result_handler(consts.API_ERROR, 'invalid task id')
89 task_handler = V2TaskHandler()
91 task = task_handler.get_by_uuid(task_id)
93 return result_handler(consts.API_ERROR, 'no such task id')
95 task_info = change_obj_to_dict(task)
96 result = task_info['result']
97 task_info['result'] = jsonutils.loads(result) if result else None
99 params = task_info['params']
100 task_info['params'] = jsonutils.loads(params) if params else None
102 return result_handler(consts.API_SUCCESS, {'task': task_info})
104 def delete(self, task_id):
108 return result_handler(consts.API_ERROR, 'invalid task id')
110 task_handler = V2TaskHandler()
112 project_id = task_handler.get_by_uuid(task_id).project_id
114 return result_handler(consts.API_ERROR, 'no such task id')
116 LOG.info('delete task in database')
117 task_handler.delete_by_uuid(task_id)
119 project_handler = V2ProjectHandler()
120 project = project_handler.get_by_uuid(project_id)
123 LOG.info('update tasks in project')
124 new_task_list = project.tasks.split(',')
125 new_task_list.remove(task_id)
127 new_tasks = ','.join(new_task_list)
130 project_handler.update_attr(project_id, {'tasks': new_tasks})
132 return result_handler(consts.API_SUCCESS, {'task': task_id})
134 def put(self, task_id):
138 return result_handler(consts.API_ERROR, 'invalid task id')
140 task_handler = V2TaskHandler()
142 task_handler.get_by_uuid(task_id)
144 return result_handler(consts.API_ERROR, 'no such task id')
146 return self._dispatch_post(task_id=task_id)
148 def add_environment(self, args):
150 task_id = args['task_id']
152 environment_id = args['environment_id']
154 return result_handler(consts.API_ERROR, 'environment_id must be provided')
157 uuid.UUID(environment_id)
159 return result_handler(consts.API_ERROR, 'invalid environment id')
161 environment_handler = V2EnvironmentHandler()
163 environment_handler.get_by_uuid(environment_id)
165 return result_handler(consts.API_ERROR, 'no such environment id')
167 LOG.info('update environment_id in task')
168 task_handler = V2TaskHandler()
169 task_handler.update_attr(task_id, {'environment_id': environment_id})
171 return result_handler(consts.API_SUCCESS, {'uuid': task_id})
173 def add_params(self, args):
174 task_id = args['task_id']
176 params = args['params']
178 return result_handler(consts.API_ERROR, 'params must be provided')
180 LOG.info('update params info in task')
182 task_handler = V2TaskHandler()
183 task_update_data = {'params': jsonutils.dumps(params)}
184 task_handler.update_attr(task_id, task_update_data)
186 return result_handler(consts.API_SUCCESS, {'uuid': task_id})
188 def add_case(self, args):
189 task_id = args['task_id']
191 name = args['case_name']
193 return result_handler(consts.API_ERROR, 'case_name must be provided')
196 content = args['case_content']
198 return result_handler(consts.API_ERROR, 'case_content must be provided')
200 LOG.info('update case info in task')
201 task_handler = V2TaskHandler()
207 task_handler.update_attr(task_id, task_update_data)
209 return result_handler(consts.API_SUCCESS, {'uuid': task_id})
211 def add_suite(self, args):
212 task_id = args['task_id']
214 name = args['suite_name']
216 return result_handler(consts.API_ERROR, 'suite_name must be provided')
219 content = args['suite_content']
221 return result_handler(consts.API_ERROR, 'suite_content must be provided')
223 LOG.info('update suite info in task')
224 task_handler = V2TaskHandler()
230 task_handler.update_attr(task_id, task_update_data)
232 return result_handler(consts.API_SUCCESS, {'uuid': task_id})
236 task_id = args['task_id']
238 return result_handler(consts.API_ERROR, 'task_id must be provided')
243 return result_handler(consts.API_ERROR, 'invalid task id')
245 task_handler = V2TaskHandler()
247 task = task_handler.get_by_uuid(task_id)
249 return result_handler(consts.API_ERROR, 'no such task id')
251 if not task.environment_id:
252 return result_handler(consts.API_ERROR, 'environment not set')
254 if not task.case_name or not task.content:
255 return result_handler(consts.API_ERROR, 'case not set')
258 return result_handler(consts.API_ERROR, 'task is already running')
260 with open('/tmp/{}.yaml'.format(task.case_name), 'w') as f:
261 f.write(task.content)
264 'inputfile': ['/tmp/{}.yaml'.format(task.case_name)],
266 'task-args': task.params
269 data.update({'suite': True})
271 LOG.info('start task thread')
273 task_thread = TaskThread(Task().start, param, task_handler)
276 return result_handler(consts.API_SUCCESS, {'uuid': task_id})
279 class V2TaskLog(ApiResource):
281 def get(self, task_id):
285 return result_handler(consts.API_ERROR, 'invalid task id')
287 task_handler = V2TaskHandler()
289 task = task_handler.get_by_uuid(task_id)
291 return result_handler(consts.API_ERROR, 'no such task id')
293 index = int(self._get_args().get('index', 0))
296 with open(os.path.join(consts.TASK_LOG_DIR, '{}.log'.format(task_id))) as f:
301 if e.errno == errno.ENOENT:
302 return result_handler(consts.API_ERROR, 'log file does not exist')
303 return result_handler(consts.API_ERROR, 'error with log file')
310 return result_handler(task.status, return_data)