Exemplo n.º 1
0
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})
Exemplo n.º 2
0
    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')
Exemplo n.º 3
0
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')
Exemplo n.º 4
0
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})
Exemplo n.º 5
0
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})
Exemplo n.º 6
0
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})
Exemplo n.º 7
0
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})
Exemplo n.º 8
0
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})
Exemplo n.º 9
0
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})
Exemplo n.º 10
0
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)
Exemplo n.º 11
0
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})
Exemplo n.º 12
0
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})
Exemplo n.º 13
0
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})
Exemplo n.º 14
0
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)()
Exemplo n.º 15
0
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)()
Exemplo n.º 16
0
    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)
Exemplo n.º 17
0
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')
Exemplo n.º 18
0
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)
Exemplo n.º 19
0
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)
Exemplo n.º 20
0
def listAllTestcases(args):
    param = Param(args)
    testcase_list = Testcase().list_all(param)
    return common_utils.result_handler(1, testcase_list)
Exemplo n.º 21
0
 def _unfinished():
     return result_handler(consts.TASK_NOT_DONE, {})
Exemplo n.º 22
0
 def _finished():
     if task.result:
         return result_handler(consts.TASK_DONE, json.loads(task.result))
     else:
         return result_handler(consts.TASK_DONE, {})
Exemplo n.º 23
0
 def _error():
     return result_handler(consts.TASK_FAILED, task.error)
Exemplo n.º 24
0
 def _error():
     return common_utils.result_handler(2, task.error)
Exemplo n.º 25
0
 def _unfinished():
     return common_utils.result_handler(0, [])