def update_openrc(args): try: openrc_vars = args['openrc'] except KeyError: return result_handler(consts.API_ERROR, 'openrc must be provided') else: if not isinstance(openrc_vars, collections.Mapping): return result_handler(consts.API_ERROR, 'args should be a dict') lines = ['export {}={}\n'.format(k, v) for k, v in openrc_vars.items()] logger.debug('Writing: %s', ''.join(lines)) logger.info('Writing openrc: Writing') common_utils.makedirs(consts.CONF_DIR) with open(consts.OPENRC, 'w') as f: f.writelines(lines) logger.info('Writing openrc: Done') logger.info('Source openrc: Sourcing') try: _source_file(consts.OPENRC) except Exception as e: logger.exception('Failed to source openrc') return result_handler(consts.API_ERROR, str(e)) logger.info('Source openrc: Done') return result_handler(consts.API_SUCCESS, {'openrc': openrc_vars})
def _dispatch(self, args, action='default'): module_name = re.sub(r'([A-Z][a-z]*)', r'_\1', self.__class__.__name__)[1:].lower() module_name = 'api.resources.%s' % module_name resources = importlib.import_module(module_name) try: return getattr(resources, action)(args) except AttributeError: common_utils.result_handler(consts.API_ERROR, 'No such action')
def update_hosts(hosts_ip): if not isinstance(hosts_ip, dict): return result_handler(consts.API_ERROR, 'Error, args should be a dict') LOG.info('Writing hosts: Writing') hosts_list = [ '\n{} {}'.format(ip, host_name) for host_name, ip in hosts_ip.items() ] LOG.debug('Writing: %s', hosts_list) with open(consts.ETC_HOSTS, 'a') as f: f.writelines(hosts_list) LOG.info('Writing hosts: Done') return result_handler(consts.API_SUCCESS, 'success')
def update_pod_file(args): try: pod_dic = args['pod'] except KeyError: return result_handler(consts.API_ERROR, 'pod must be provided') else: if not isinstance(pod_dic, collections.Mapping): return result_handler(consts.API_ERROR, 'pod should be a dict') LOG.info('Writing file') with open(consts.POD_FILE, 'w') as f: yaml.dump(pod_dic, f, default_flow_style=False) LOG.info('Writing finished') return result_handler(consts.API_SUCCESS, {'pod_info': pod_dic})
def get_case_docs(args): try: case_name = args['case_name'] except KeyError: return result_handler(consts.API_ERROR, 'case_name must be provided') docs_path = os.path.join(consts.DOCS_DIR, '{}.rst'.format(case_name)) if not os.path.exists(docs_path): return result_handler(consts.API_ERROR, 'case not exists') LOG.info('Reading %s', case_name) with open(docs_path) as f: content = f.read() return result_handler(consts.API_SUCCESS, {'docs': content})
def prepare_env(args): task_id = str(uuid.uuid4()) thread = threading.Thread(target=_prepare_env_daemon, args=(task_id, )) thread.start() return result_handler(consts.API_SUCCESS, {'task_id': task_id})
def createInfluxDBContainer(args): task_id = str(uuid.uuid4()) thread = threading.Thread(target=_create_influxdb, args=(task_id,)) thread.start() return result_handler('success', {'task_id': task_id})
def prepareYardstickEnv(args): task_id = str(uuid.uuid4()) thread = threading.Thread(target=_prepare_env_daemon, args=(task_id,)) thread.start() return result_handler('success', {'task_id': task_id})
def createGrafanaContainer(args): task_id = str(uuid.uuid4()) thread = threading.Thread(target=_create_grafana, args=(task_id,)) thread.start() return result_handler('success', {'task_id': task_id})
def runTestSuite(args): try: opts = args.get('opts', {}) testsuite = args['testsuite'] except KeyError: return common_utils.error_handler('Lack of testsuite argument') if 'suite' not in opts: opts['suite'] = 'true' testsuite = os.path.join(consts.TESTSUITE_DIR, '{}.yaml'.format(testsuite)) task_id = str(uuid.uuid4()) command_list = ['task', 'start'] command_list = common_utils.get_command_list(command_list, opts, testsuite) logger.debug('The command_list is: %s', command_list) logger.debug('Start to execute command list') task_dic = { 'task_id': task_id, 'details': _get_cases_from_suite_file(testsuite) } common_utils.exec_command_task(command_list, task_dic) return common_utils.result_handler('success', task_id)
def create_grafana(args): task_id = str(uuid.uuid4()) thread = threading.Thread(target=_create_grafana, args=(task_id, )) thread.start() return result_handler(consts.API_SUCCESS, {'task_id': task_id})
def upload_pod_file(args): try: pod_file = args['file'] except KeyError: return result_handler(consts.API_ERROR, 'file must be provided') LOG.info('Checking file') data = yaml.load(pod_file.read()) if not isinstance(data, collections.Mapping): return result_handler(consts.API_ERROR, 'invalid yaml file') LOG.info('Writing file') with open(consts.POD_FILE, 'w') as f: yaml.dump(data, f, default_flow_style=False) LOG.info('Writing finished') return result_handler(consts.API_SUCCESS, {'pod_info': data})
def run_test_case(args): try: case_name = args['testcase'] except KeyError: return result_handler(consts.API_ERROR, 'testcase must be provided') testcase = os.path.join(consts.TESTCASE_DIR, '{}.yaml'.format(case_name)) task_id = str(uuid.uuid4()) task_args = {'inputfile': [testcase], 'task_id': task_id} task_args.update(args.get('opts', {})) param = Param(task_args) task_thread = TaskThread(Task().start, param) task_thread.start() return result_handler(consts.API_SUCCESS, {'task_id': task_id})
def getResult(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 = TasksHandler() try: task = task_handler.get_task_by_taskid(task_id) except ValueError: return result_handler(consts.API_ERROR, 'invalid task_id') def _unfinished(): return result_handler(consts.TASK_NOT_DONE, {}) def _finished(): if task.result: return result_handler(consts.TASK_DONE, json.loads(task.result)) else: return result_handler(consts.TASK_DONE, {}) def _error(): return result_handler(consts.TASK_FAILED, task.error) status = task.status logger.debug('Task status is: %s', status) if status not in [ consts.TASK_NOT_DONE, consts.TASK_DONE, consts.TASK_FAILED ]: return result_handler(consts.API_ERROR, 'internal server error') switcher = { consts.TASK_NOT_DONE: _unfinished, consts.TASK_DONE: _finished, consts.TASK_FAILED: _error } return switcher.get(status)()
def _get_status(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') asynctask_handler = AsyncTaskHandler() try: asynctask = asynctask_handler.get_task_by_taskid(task_id) except ValueError: return result_handler(consts.API_ERROR, 'invalid task_id') def _unfinished(): return result_handler(consts.TASK_NOT_DONE, {}) def _finished(): return result_handler(consts.TASK_DONE, {}) def _error(): return result_handler(consts.TASK_FAILED, asynctask.error) status = asynctask.status LOG.debug('Task status is: %s', status) if status not in [ consts.TASK_NOT_DONE, consts.TASK_DONE, consts.TASK_FAILED ]: return result_handler(consts.API_ERROR, 'internal server error') switcher = { consts.TASK_NOT_DONE: _unfinished, consts.TASK_DONE: _finished, consts.TASK_FAILED: _error } return switcher.get(status)()
def _finished(): testcases = task.details.split(',') def get_data(testcase): query_template = "select * from %s where task_id='%s'" query_sql = query_template % (testcase, task_id) data = common_utils.translate_to_str(influx_utils.query(query_sql)) return data result = {k: get_data(k) for k in testcases} return common_utils.result_handler(1, result)
def _get_status(args): try: task_id = args['task_id'] uuid.UUID(task_id) except KeyError: message = 'measurement and task_id must be provided' return common_utils.error_handler(message) asynctask = AsyncTasks.query.filter_by(task_id=task_id).first() try: status = asynctask.status error = asynctask.error if asynctask.error else [] return common_utils.result_handler(status, error) except AttributeError: return common_utils.error_handler('no such task')
def runTestCase(args): try: opts = args.get('opts', {}) testcase = args['testcase'] except KeyError: return common_utils.error_handler('Lack of testcase argument') testcase_name = conf.TEST_CASE_PRE + testcase testcase = os.path.join(conf.TEST_CASE_PATH, testcase_name + '.yaml') task_id = str(uuid.uuid4()) command_list = ['task', 'start'] command_list = common_utils.get_command_list(command_list, opts, testcase) logger.debug('The command_list is: %s', command_list) logger.debug('Start to execute command list') task_dict = {'task_id': task_id, 'details': testcase_name} common_utils.exec_command_task(command_list, task_dict) return common_utils.result_handler('success', task_id)
def runTestSuite(args): try: opts = args.get('opts', {}) testsuite = args['testsuite'] except KeyError: return common_utils.error_handler('Lack of testsuite argument') if 'suite' not in opts: opts['suite'] = 'true' testsuite = os.path.join(conf.TEST_SUITE_PATH, conf.TEST_SUITE_PRE + testsuite + '.yaml') task_id = str(uuid.uuid4()) command_list = ['task', 'start'] command_list = common_utils.get_command_list(command_list, opts, testsuite) logger.debug('The command_list is: %s', command_list) logger.debug('Start to execute command list') common_utils.exec_command_task(command_list, task_id) return common_utils.result_handler('success', task_id)
def listAllTestcases(args): param = Param(args) testcase_list = Testcase().list_all(param) return common_utils.result_handler(1, testcase_list)
def _unfinished(): return result_handler(consts.TASK_NOT_DONE, {})
def _finished(): if task.result: return result_handler(consts.TASK_DONE, json.loads(task.result)) else: return result_handler(consts.TASK_DONE, {})
def _error(): return result_handler(consts.TASK_FAILED, task.error)
def _error(): return common_utils.result_handler(2, task.error)
def _unfinished(): return common_utils.result_handler(0, [])