Exemplo n.º 1
0
    def get(self, task_id):

        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')

        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)
Exemplo n.º 2
0
    def delete(self, container_id):
        try:
            uuid.UUID(container_id)
        except ValueError:
            return result_handler(consts.API_ERROR, 'invalid container id')

        try:
            container = container_handler.get_by_uuid(container_id)
        except ValueError:
            return result_handler(consts.API_ERROR, 'no such container id')

        environment_id = container.environment_id

        client = Client(base_url=consts.DOCKER_URL)
        LOG.info('delete container: %s', container.name)
        try:
            client.remove_container(container.name, force=True)
        except Exception:
            LOG.exception('delete container failed')
            return result_handler(consts.API_ERROR, 'delete container failed')

        LOG.info('delete container in database')
        container_handler.delete_by_uuid(container_id)

        LOG.info('update container in environment')
        environment = environment_handler.get_by_uuid(environment_id)
        container_info = jsonutils.loads(environment.container_id)
        key = next((k for k, v in container_info.items() if v == container_id))
        container_info.pop(key)
        environment_delete_data = {
            'container_id': jsonutils.dumps(container_info)
        }
        environment_handler.update_attr(environment_id, environment_delete_data)

        return result_handler(consts.API_SUCCESS, {'container': container_id})
Exemplo n.º 3
0
    def delete(self, container_id):
        try:
            uuid.UUID(container_id)
        except ValueError:
            return result_handler(consts.API_ERROR, 'invalid container id')

        try:
            container = container_handler.get_by_uuid(container_id)
        except ValueError:
            return result_handler(consts.API_ERROR, 'no such container id')

        environment_id = container.environment_id

        client = Client(base_url=consts.DOCKER_URL)
        LOG.info('delete container: %s', container.name)
        try:
            client.remove_container(container.name, force=True)
        except Exception:
            LOG.exception('delete container failed')
            return result_handler(consts.API_ERROR, 'delete container failed')

        LOG.info('delete container in database')
        container_handler.delete_by_uuid(container_id)

        LOG.info('update container in environment')
        environment = environment_handler.get_by_uuid(environment_id)
        container_info = jsonutils.loads(environment.container_id)
        key = next((k for k, v in container_info.items() if v == container_id))
        container_info.pop(key)
        environment_delete_data = {
            'container_id': jsonutils.dumps(container_info)
        }
        environment_handler.update_attr(environment_id, environment_delete_data)

        return result_handler(consts.API_SUCCESS, {'container': container_id})
Exemplo n.º 4
0
    def get(self, task_id):

        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')

        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)
Exemplo n.º 5
0
    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})
Exemplo n.º 6
0
    def delete(self, project_id):
        try:
            uuid.UUID(project_id)
        except ValueError:
            return result_handler(consts.API_ERROR, 'invalid project id')

        project_handler = V2ProjectHandler()
        try:
            project = project_handler.get_by_uuid(project_id)
        except ValueError:
            return result_handler(consts.API_ERROR, 'no such project id')

        if project.tasks:
            LOG.info('delete related task')
            task_handler = V2TaskHandler()
            for task_id in project.tasks.split(','):
                LOG.debug('delete task: %s', task_id)
                try:
                    task_handler.delete_by_uuid(task_id)
                except ValueError:
                    LOG.exception('no such task id: %s', task_id)

        LOG.info('delete project in database')
        project_handler.delete_by_uuid(project_id)

        return result_handler(consts.API_SUCCESS, {'project': project_id})
Exemplo n.º 7
0
    def update_openrc(self, 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()]
        LOG.debug('Writing: %s', ''.join(lines))

        LOG.info('Writing openrc: Writing')
        utils.makedirs(consts.CONF_DIR)

        with open(consts.OPENRC, 'w') as f:
            f.writelines(lines)
        LOG.info('Writing openrc: Done')

        LOG.info('Source openrc: Sourcing')
        try:
            self._source_file(consts.OPENRC)
        except subprocess.CalledProcessError as e:
            LOG.exception('Failed to source openrc')
            return result_handler(consts.API_ERROR, str(e))
        LOG.info('Source openrc: Done')

        return result_handler(consts.API_SUCCESS, {'openrc': openrc_vars})
Exemplo n.º 8
0
    def create_suite(self, args):
        try:
            suite_name = args['name']
        except KeyError:
            return result_handler(consts.API_ERROR, 'name must be provided')

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

        testcases = [{'file_name': '{}.yaml'.format(t)} for t in testcases]

        suite = os.path.join(consts.TESTSUITE_DIR, '{}.yaml'.format(suite_name))
        suite_content = {
            'schema': 'yardstick:suite:0.1',
            'name': suite_name,
            'test_cases_dir': 'tests/opnfv/test_cases/',
            'test_cases': testcases
        }

        LOG.info('write test suite')
        with open(suite, 'w') as f:
            yaml.dump(suite_content, f, default_flow_style=False)

        return result_handler(consts.API_SUCCESS, {'suite': suite_name})
Exemplo n.º 9
0
    def get(self, case_name):
        case_path = os.path.join(consts.TESTCASE_DIR,
                                 '{}.yaml'.format(case_name))

        try:
            with open(case_path) as f:
                data = f.read()
        except IOError as e:
            if e.errno == errno.ENOENT:
                return result_handler(consts.API_ERROR, 'case does not exist')

        options = {
            k: {
                'description': '',
                'type': v.__class__.__name__
            }
            for k, v in jinja2schema.infer(data).items()
        }
        # [('segmentation_id', < scalar >), ('image', < string >), ('provider', < scalar >),
        # ('physical_network', < string >), ('packetsize', < number >)]
        for k, v in options.items():
            if k == 'segmentation_id':
                options[k]['type'] = 'Number'
            if k == 'provider':
                options[k]['type'] = 'String'
        return result_handler(consts.API_SUCCESS, {
            'testcase': data,
            'args': options
        })
Exemplo n.º 10
0
    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})
Exemplo n.º 11
0
    def delete(self, project_id):
        try:
            uuid.UUID(project_id)
        except ValueError:
            return result_handler(consts.API_ERROR, 'invalid project id')

        project_handler = V2ProjectHandler()
        try:
            project = project_handler.get_by_uuid(project_id)
        except ValueError:
            return result_handler(consts.API_ERROR, 'no such project id')

        if project.tasks:
            LOG.info('delete related task')
            task_handler = V2TaskHandler()
            for task_id in project.tasks.split(','):
                LOG.debug('delete task: %s', task_id)
                try:
                    task_handler.delete_by_uuid(task_id)
                except ValueError:
                    LOG.exception('no such task id: %s', task_id)

        LOG.info('delete project in database')
        project_handler.delete_by_uuid(project_id)

        return result_handler(consts.API_SUCCESS, {'project': project_id})
Exemplo n.º 12
0
    def create_suite(self, args):
        try:
            suite_name = args['name']
        except KeyError:
            return result_handler(consts.API_ERROR, 'name must be provided')

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

        testcases = [{'file_name': '{}.yaml'.format(t)} for t in testcases]

        suite = os.path.join(consts.TESTSUITE_DIR,
                             '{}.yaml'.format(suite_name))
        suite_content = {
            'schema': 'yardstick:suite:0.1',
            'name': suite_name,
            'test_cases_dir': 'tests/opnfv/test_cases/',
            'test_cases': testcases
        }

        LOG.info('write test suite')
        with open(suite, 'w') as f:
            yaml.dump(suite_content, f, default_flow_style=False)

        return result_handler(consts.API_SUCCESS, {'suite': suite_name})
Exemplo n.º 13
0
    def update_openrc(self, 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()]
        LOG.debug('Writing: %s', ''.join(lines))

        LOG.info('Writing openrc: Writing')
        utils.makedirs(consts.CONF_DIR)

        with open(consts.OPENRC, 'w') as f:
            f.writelines(lines)
        LOG.info('Writing openrc: Done')

        LOG.info('Source openrc: Sourcing')
        try:
            self._source_file(consts.OPENRC)
        except subprocess.CalledProcessError as e:
            LOG.exception('Failed to source openrc')
            return result_handler(consts.API_ERROR, str(e))
        LOG.info('Source openrc: Done')

        return result_handler(consts.API_SUCCESS, {'openrc': openrc_vars})
Exemplo n.º 14
0
    def get(self, environment_id):
        try:
            uuid.UUID(environment_id)
        except ValueError:
            return result_handler(consts.API_ERROR, 'invalid environment id')

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

        if not environment.pod_id:
            return result_handler(consts.API_SUCCESS, {'sut': {}})

        pod_handler = V2PodHandler()
        try:
            pod = pod_handler.get_by_uuid(environment.pod_id)
        except ValueError:
            return result_handler(consts.API_ERROR, 'no such pod id')
        else:
            pod_content = pod.content

        env = Environment(pod=pod_content)
        sut_info = env.get_sut_info()

        return result_handler(consts.API_SUCCESS, {'sut': sut_info})
Exemplo n.º 15
0
    def delete(self, image_id):
        try:
            uuid.UUID(image_id)
        except ValueError:
            return result_handler(consts.API_ERROR, 'invalid image id')

        image_handler = V2ImageHandler()
        try:
            image = image_handler.get_by_uuid(image_id)
        except ValueError:
            return result_handler(consts.API_ERROR, 'no such image id')

        LOG.info('delete image in openstack')
        glance_client = get_glance_client()
        try:
            image_o = next((i for i in glance_client.images.list()
                            if i.name == image.name))
        except StopIteration:
            return result_handler(consts.API_ERROR, 'can not find image')

        glance_client.images.delete(image_o.id)

        LOG.info('delete image in environment')
        environment_id = image.environment_id
        environment_handler = V2EnvironmentHandler()
        environment = environment_handler.get_by_uuid(environment_id)
        image_list = environment.image_id.split(',')
        image_list.remove(image_id)
        environment_handler.update_attr(environment_id,
                                        {'image_id': ','.join(image_list)})

        LOG.info('delete image in DB')
        image_handler.delete_by_uuid(image_id)

        return result_handler(consts.API_SUCCESS, {'image': image_id})
Exemplo n.º 16
0
    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})
Exemplo n.º 17
0
    def delete(self, suite_name):
        suite_path = os.path.join(consts.TESTSUITE_DIR, '{}.yaml'.format(suite_name))
        try:
            os.remove(suite_path)
        except IOError as e:
            if e.errno == errno.ENOENT:
                return result_handler(consts.API_ERROR, 'suite does not exist')

        return result_handler(consts.API_SUCCESS, {'testsuite': suite_name})
Exemplo n.º 18
0
    def delete(self, suite_name):
        suite_path = os.path.join(consts.TESTSUITE_DIR,
                                  '{}.yaml'.format(suite_name))
        try:
            os.remove(suite_path)
        except IOError as e:
            if e.errno == errno.ENOENT:
                return result_handler(consts.API_ERROR, 'suite does not exist')

        return result_handler(consts.API_SUCCESS, {'testsuite': suite_name})
Exemplo n.º 19
0
    def get(self, suite_name):
        suite_path = os.path.join(consts.TESTSUITE_DIR, '{}.yaml'.format(suite_name))
        try:
            with open(suite_path) as f:
                data = f.read()
        except IOError as e:
            if e.errno == errno.ENOENT:
                return result_handler(consts.API_ERROR, 'suite does not exist')

        return result_handler(consts.API_SUCCESS, {'testsuite': data})
Exemplo n.º 20
0
    def get(self, case_name):
        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.º 21
0
    def get(self, suite_name):
        suite_path = os.path.join(consts.TESTSUITE_DIR,
                                  '{}.yaml'.format(suite_name))
        try:
            with open(suite_path) as f:
                data = f.read()
        except IOError as e:
            if e.errno == errno.ENOENT:
                return result_handler(consts.API_ERROR, 'suite does not exist')

        return result_handler(consts.API_SUCCESS, {'testsuite': data})
Exemplo n.º 22
0
    def get(self, case_name):
        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.º 23
0
    def upload_image(self, args):
        try:
            image_file = args['file']
        except KeyError:
            return result_handler(consts.API_ERROR, 'file must be provided')

        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 = environment_handler.get_by_uuid(environment_id)
        except ValueError:
            return result_handler(consts.API_ERROR, 'no such environment')

        file_path = os.path.join(consts.IMAGE_DIR, image_file.filename)
        LOG.info('saving file')
        image_file.save(file_path)

        LOG.info('loading image')
        self._load_image(image_file.filename, file_path)

        LOG.info('creating image in DB')
        image_handler = V2ImageHandler()
        image_id = str(uuid.uuid4())
        image_init_data = {
            'uuid': image_id,
            'name': image_file.filename,
            'environment_id': environment_id
        }
        image_handler.insert(image_init_data)

        LOG.info('update image in environment')
        if environment.image_id:
            image_list = environment.image_id.split(',')
            image_list.append(image_id)
            new_image_id = ','.join(image_list)
        else:
            new_image_id = image_id

        environment_handler.update_attr(environment_id,
                                        {'image_id': new_image_id})

        return result_handler(consts.API_SUCCESS, {'uuid': image_id})
Exemplo n.º 24
0
    def load_image(self, args):
        try:
            image_name = args['name']
        except KeyError:
            return result_handler(consts.API_ERROR, 'image name must provided')

        if image_name not in IMAGE_MAP:
            return result_handler(consts.API_ERROR, 'wrong image name')

        thread = threading.Thread(target=self._do_load_image,
                                  args=(image_name, ))
        thread.start()
        return result_handler(consts.API_SUCCESS, {'image': image_name})
Exemplo n.º 25
0
    def upload_case(self, args):
        try:
            upload_file = args['file']
        except KeyError:
            return result_handler(consts.API_ERROR, 'file must be provided')

        case_name = os.path.join(consts.TESTCASE_DIR, upload_file.filename)

        LOG.info('save case file')
        upload_file.save(case_name)

        return result_handler(consts.API_SUCCESS,
                              {'testcase': upload_file.filename})
Exemplo n.º 26
0
    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)
Exemplo n.º 27
0
    def upload_image_by_url(self, args):
        try:
            url = args['url']
        except KeyError:
            return result_handler(consts.API_ERROR, 'url must be provided')

        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 = environment_handler.get_by_uuid(environment_id)
        except ValueError:
            return result_handler(consts.API_ERROR, 'no such environment')

        thread = threading.Thread(target=self._do_upload_image_by_url,
                                  args=(url, ))
        thread.start()

        file_name = url.split('/')[-1]

        LOG.info('creating image in DB')
        image_handler = V2ImageHandler()
        image_id = str(uuid.uuid4())
        image_init_data = {
            'uuid': image_id,
            'name': file_name,
            'environment_id': environment_id
        }
        image_handler.insert(image_init_data)

        LOG.info('update image in environment')
        if environment.image_id:
            image_list = environment.image_id.split(',')
            image_list.append(image_id)
            new_image_id = ','.join(image_list)
        else:
            new_image_id = image_id

        environment_handler.update_attr(environment_id,
                                        {'image_id': new_image_id})

        return result_handler(consts.API_SUCCESS, {'uuid': image_id})
Exemplo n.º 28
0
    def create_environment(self, args):
        try:
            name = args['name']
        except KeyError:
            return result_handler(consts.API_ERROR, 'name must be provided')

        env_id = str(uuid.uuid4())

        environment_handler = V2EnvironmentHandler()

        env_init_data = {'name': name, 'uuid': env_id}
        environment_handler.insert(env_init_data)

        return result_handler(consts.API_SUCCESS, {'uuid': env_id})
Exemplo n.º 29
0
    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})
Exemplo n.º 30
0
    def update_openrc(self, args):
        try:
            openrc_vars = args['openrc']
        except KeyError:
            return result_handler(consts.API_ERROR, 'openrc must be provided')

        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')

        LOG.info('writing openrc: %s', consts.OPENRC)
        makedirs(consts.CONF_DIR)

        lines = ['export {}={}\n'.format(k, v) for k, v in openrc_vars.items()]
        LOG.debug('writing: %s', ''.join(lines))
        with open(consts.OPENRC, 'w') as f:
            f.writelines(lines)
        LOG.info('writing openrc: Done')

        LOG.info('source openrc: %s', consts.OPENRC)
        try:
            source_env(consts.OPENRC)
        except Exception:
            LOG.exception('source openrc failed')
            raise y_exc.UpdateOpenrcError()
        LOG.info('source openrc: Done')

        openrc_id = str(uuid.uuid4())
        self._write_into_database(environment_id, openrc_id, openrc_vars)

        LOG.info('writing ansible cloud conf')
        try:
            self._generate_ansible_conf_file(openrc_vars)
        except Exception:
            LOG.exception('write cloud conf failed')
            raise y_exc.UpdateOpenrcError()
        LOG.info('finish writing ansible cloud conf')

        return result_handler(consts.API_SUCCESS, {
            'openrc': openrc_vars,
            'uuid': openrc_id
        })
Exemplo n.º 31
0
    def get(self, pod_id):
        try:
            uuid.UUID(pod_id)
        except ValueError:
            return result_handler(consts.API_ERROR, 'invalid pod id')

        pod_handler = V2PodHandler()
        try:
            pod = pod_handler.get_by_uuid(pod_id)
        except ValueError:
            return result_handler(consts.API_ERROR, 'no such pod')

        content = jsonutils.loads(pod.content)

        return result_handler(consts.API_SUCCESS, {'pod': content})
Exemplo n.º 32
0
    def update_pod_file(self, 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.º 33
0
    def get(self, pod_id):
        try:
            uuid.UUID(pod_id)
        except ValueError:
            return result_handler(consts.API_ERROR, 'invalid pod id')

        pod_handler = V2PodHandler()
        try:
            pod = pod_handler.get_by_uuid(pod_id)
        except ValueError:
            return result_handler(consts.API_ERROR, 'no such pod')

        content = jsonutils.loads(pod.content)

        return result_handler(consts.API_SUCCESS, {'pod': content})
Exemplo n.º 34
0
    def update_pod_file(self, 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.º 35
0
    def create_influxdb(self, args):
        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')

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

        container_info = environment.container_id
        container_info = jsonutils.loads(container_info) if container_info else {}

        if container_info.get('influxdb'):
            return result_handler(consts.API_ERROR, 'influxdb container already exist')

        name = 'influxdb-{}'.format(environment_id[:8])
        port = get_free_port(consts.SERVER_IP)
        container_id = str(uuid.uuid4())
        LOG.info('%s will launch on : %s', name, port)

        LOG.info('launch influxdb background')
        args = (name, port, container_id)
        thread = threading.Thread(target=self._create_influxdb, args=args)
        thread.start()

        LOG.info('record container in database')
        container_init_data = {
            'uuid': container_id,
            'environment_id': environment_id,
            'name': name,
            'port': port,
            'status': 0
        }
        container_handler.insert(container_init_data)

        LOG.info('update container in environment')
        container_info['influxdb'] = container_id
        environment_info = {'container_id': jsonutils.dumps(container_info)}
        environment_handler.update_attr(environment_id, environment_info)

        return result_handler(consts.API_SUCCESS, {'uuid': container_id})
Exemplo n.º 36
0
    def create_influxdb(self, args):
        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')

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

        container_info = environment.container_id
        container_info = jsonutils.loads(container_info) if container_info else {}

        if container_info.get('influxdb'):
            return result_handler(consts.API_ERROR, 'influxdb container already exist')

        name = 'influxdb-{}'.format(environment_id[:8])
        port = get_free_port(consts.SERVER_IP)
        container_id = str(uuid.uuid4())
        LOG.info('%s will launch on : %s', name, port)

        LOG.info('launch influxdb background')
        args = (name, port, container_id)
        thread = threading.Thread(target=self._create_influxdb, args=args)
        thread.start()

        LOG.info('record container in database')
        container_init_data = {
            'uuid': container_id,
            'environment_id': environment_id,
            'name': name,
            'port': port,
            'status': 0
        }
        container_handler.insert(container_init_data)

        LOG.info('update container in environment')
        container_info['influxdb'] = container_id
        environment_info = {'container_id': jsonutils.dumps(container_info)}
        environment_handler.update_attr(environment_id, environment_info)

        return result_handler(consts.API_SUCCESS, {'uuid': container_id})
Exemplo n.º 37
0
    def create_grafana(self, *args):
        task_id = str(uuid.uuid4())

        thread = threading.Thread(target=self._create_grafana, args=(task_id,))
        thread.start()

        return result_handler(consts.API_SUCCESS, {'task_id': task_id})
Exemplo n.º 38
0
    def get(self):
        param = Param({})
        testsuite_list = Testsuite().list_all(param)

        data = {'testsuites': testsuite_list}

        return result_handler(consts.API_SUCCESS, data)
Exemplo n.º 39
0
    def get(self):
        args = self._get_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
        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.º 40
0
    def get(self):
        args = self._get_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
        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.º 41
0
 def update_hosts(self, hosts_ip):
     if not isinstance(hosts_ip, collections.Mapping):
         return result_handler(consts.API_ERROR, 'args should be a dict')
     LOG.info('Writing hosts: Writing')
     LOG.debug('Writing: %s', hosts_ip)
     cmd = ["sudo", "python", "write_hosts.py"]
     p = subprocess.Popen(cmd,
                          stdin=subprocess.PIPE,
                          stdout=subprocess.PIPE,
                          stderr=subprocess.PIPE,
                          cwd=os.path.join(consts.REPOS_DIR,
                                           "api/resources"))
     _, err = p.communicate(jsonutils.dumps(hosts_ip))
     if p.returncode != 0:
         return result_handler(consts.API_ERROR, err)
     LOG.info('Writing hosts: Done')
     return result_handler(consts.API_SUCCESS, 'success')
Exemplo n.º 42
0
    def upload_pod_file(self, 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.º 43
0
    def prepare_env(self, *args):
        task_id = str(uuid.uuid4())

        thread = threading.Thread(target=self._prepare_env_daemon,
                                  args=(task_id,))
        thread.start()

        return result_handler(consts.API_SUCCESS, {'task_id': task_id})
Exemplo n.º 44
0
    def get(self, project_id):
        try:
            uuid.UUID(project_id)
        except ValueError:
            return result_handler(consts.API_ERROR, 'invalid project id')

        project_handler = V2ProjectHandler()
        try:
            project = project_handler.get_by_uuid(project_id)
        except ValueError:
            return result_handler(consts.API_ERROR, 'no such project id')

        project_info = change_obj_to_dict(project)
        tasks = project_info['tasks']
        project_info['tasks'] = tasks.split(',') if tasks else []

        return result_handler(consts.API_SUCCESS, {'project': project_info})
Exemplo n.º 45
0
    def create_environment(self, args):
        try:
            name = args['name']
        except KeyError:
            return result_handler(consts.API_ERROR, 'name must be provided')

        env_id = str(uuid.uuid4())

        environment_handler = V2EnvironmentHandler()

        env_init_data = {
            'name': name,
            'uuid': env_id
        }
        environment_handler.insert(env_init_data)

        return result_handler(consts.API_SUCCESS, {'uuid': env_id})
Exemplo n.º 46
0
    def get(self, openrc_id):
        try:
            uuid.UUID(openrc_id)
        except ValueError:
            return result_handler(consts.API_ERROR, 'invalid openrc id')

        LOG.info('Geting openrc: %s', openrc_id)
        openrc_handler = V2OpenrcHandler()
        try:
            openrc = openrc_handler.get_by_uuid(openrc_id)
        except ValueError:
            return result_handler(consts.API_ERROR, 'no such openrc id')

        LOG.info('load openrc content')
        content = jsonutils.loads(openrc.content)

        return result_handler(consts.API_ERROR, {'openrc': content})
Exemplo n.º 47
0
    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

        return result_handler(consts.API_SUCCESS, {'task': task_info})
Exemplo n.º 48
0
    def get(self, openrc_id):
        try:
            uuid.UUID(openrc_id)
        except ValueError:
            return result_handler(consts.API_ERROR, 'invalid openrc id')

        LOG.info('Geting openrc: %s', openrc_id)
        openrc_handler = V2OpenrcHandler()
        try:
            openrc = openrc_handler.get_by_uuid(openrc_id)
        except ValueError:
            return result_handler(consts.API_ERROR, 'no such openrc id')

        LOG.info('load openrc content')
        content = jsonutils.loads(openrc.content)

        return result_handler(consts.API_ERROR, {'openrc': content})
Exemplo n.º 49
0
    def create_grafana(self, *args):
        task_id = str(uuid.uuid4())

        thread = threading.Thread(target=self._create_grafana,
                                  args=(task_id, ))
        thread.start()

        return result_handler(consts.API_SUCCESS, {'task_id': task_id})
Exemplo n.º 50
0
    def get(self):
        param = Param({})
        testsuite_list = Testsuite().list_all(param)

        data = {
            'testsuites': testsuite_list
        }

        return result_handler(consts.API_SUCCESS, data)
Exemplo n.º 51
0
    def get(self):
        project_handler = V2ProjectHandler()
        projects = [change_obj_to_dict(p) for p in project_handler.list_all()]

        for p in projects:
            tasks = p['tasks']
            p['tasks'] = tasks.split(',') if tasks else []

        return result_handler(consts.API_SUCCESS, {'projects': projects})
Exemplo n.º 52
0
    def create_project(self, args):
        try:
            name = args['name']
        except KeyError:
            return result_handler(consts.API_ERROR, 'name must be provided')

        project_id = str(uuid.uuid4())
        create_time = datetime.now()
        project_handler = V2ProjectHandler()

        project_init_data = {
            'uuid': project_id,
            'name': name,
            'time': create_time
        }
        project_handler.insert(project_init_data)

        return result_handler(consts.API_SUCCESS, {'uuid': project_id})
Exemplo n.º 53
0
    def delete(self, pod_id):
        try:
            uuid.UUID(pod_id)
        except ValueError:
            return result_handler(consts.API_ERROR, 'invalid pod id')

        pod_handler = V2PodHandler()
        try:
            pod = pod_handler.get_by_uuid(pod_id)
        except ValueError:
            return result_handler(consts.API_ERROR, 'no such pod')

        LOG.info('update pod in environment')
        environment_handler = V2EnvironmentHandler()
        environment_handler.update_attr(pod.environment_id, {'pod_id': None})

        LOG.info('delete pod in database')
        pod_handler.delete_by_uuid(pod_id)

        return result_handler(consts.API_SUCCESS, {'pod': pod_id})
Exemplo n.º 54
0
    def delete(self, openrc_id):
        try:
            uuid.UUID(openrc_id)
        except ValueError:
            return result_handler(consts.API_ERROR, 'invalid openrc id')

        LOG.info('Geting openrc: %s', openrc_id)
        openrc_handler = V2OpenrcHandler()
        try:
            openrc = openrc_handler.get_by_uuid(openrc_id)
        except ValueError:
            return result_handler(consts.API_ERROR, 'no such openrc id')

        LOG.info('update openrc in environment')
        environment_handler = V2EnvironmentHandler()
        environment_handler.update_attr(openrc.environment_id, {'openrc_id': None})

        openrc_handler.delete_by_uuid(openrc_id)

        return result_handler(consts.API_SUCCESS, {'openrc': openrc_id})
Exemplo n.º 55
0
    def delete(self, environment_id):
        try:
            uuid.UUID(environment_id)
        except ValueError:
            return result_handler(consts.API_ERROR, 'invalid environment id')

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

        if environment.openrc_id:
            LOG.info('delete openrc: %s', environment.openrc_id)
            openrc_handler = V2OpenrcHandler()
            openrc_handler.delete_by_uuid(environment.openrc_id)

        if environment.pod_id:
            LOG.info('delete pod: %s', environment.pod_id)
            pod_handler = V2PodHandler()
            pod_handler.delete_by_uuid(environment.pod_id)

        if environment.container_id:
            LOG.info('delete containers')
            container_info = jsonutils.loads(environment.container_id)

            container_handler = V2ContainerHandler()
            client = Client(base_url=consts.DOCKER_URL)
            for k, v in container_info.items():
                LOG.info('start delete: %s', k)
                container = container_handler.get_by_uuid(v)
                LOG.debug('container name: %s', container.name)
                try:
                    client.remove_container(container.name, force=True)
                except Exception:
                    LOG.exception('remove container failed')
                container_handler.delete_by_uuid(v)

        environment_handler.delete_by_uuid(environment_id)

        return result_handler(consts.API_SUCCESS, {'environment': environment_id})
Exemplo n.º 56
0
    def run_test_case(self, args):
        try:
            name = args['testcase']
        except KeyError:
            return result_handler(consts.API_ERROR, 'testcase must be provided')

        testcase = os.path.join(consts.SAMPLE_CASE_DIR, '{}.yaml'.format(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, TasksHandler())
        task_thread.start()

        return result_handler(consts.API_SUCCESS, {'task_id': task_id})
Exemplo n.º 57
0
    def get(self, environment_id):
        try:
            uuid.UUID(environment_id)
        except ValueError:
            return result_handler(consts.API_ERROR, 'invalid environment id')

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

        environment = change_obj_to_dict(environment)

        container_id = environment['container_id']
        environment['container_id'] = jsonutils.loads(container_id) if container_id else {}

        image_id = environment['image_id']
        environment['image_id'] = image_id.split(',') if image_id else []

        return result_handler(consts.API_SUCCESS, {'environment': environment})
Exemplo n.º 58
0
    def upload_pod_file(self, args):
        try:
            upload_file = args['file']
        except KeyError:
            return result_handler(consts.API_ERROR, 'file must be provided')

        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')

        LOG.info('writing pod file: %s', consts.POD_FILE)
        upload_file.save(consts.POD_FILE)

        with open(consts.POD_FILE) as f:
            data = yaml_load(TaskTemplate.render(f.read()))
        LOG.debug('pod content is: %s', data)

        LOG.info('create pod in database')
        pod_id = str(uuid.uuid4())
        pod_handler = V2PodHandler()
        pod_init_data = {
            'uuid': pod_id,
            'environment_id': environment_id,
            'content': jsonutils.dumps(data)
        }
        pod_handler.insert(pod_init_data)

        LOG.info('update pod in environment')
        environment_handler = V2EnvironmentHandler()
        environment_handler.update_attr(environment_id, {'pod_id': pod_id})

        return result_handler(consts.API_SUCCESS, {'uuid': pod_id, 'pod': data})
Exemplo n.º 59
0
    def get(self, container_id):
        try:
            uuid.UUID(container_id)
        except ValueError:
            return result_handler(consts.API_ERROR, 'invalid container id')

        try:
            container = container_handler.get_by_uuid(container_id)
        except ValueError:
            return result_handler(consts.API_ERROR, 'no such container id')

        name = container.name
        client = Client(base_url=consts.DOCKER_URL)
        info = client.inspect_container(name)

        data = {
            'name': name,
            'status': info.get('State', {}).get('Status', 'error'),
            'time': info.get('Created'),
            'port': container.port
        }

        return result_handler(consts.API_SUCCESS, {'container': data})
Exemplo n.º 60
0
    def get(self):
        environment_handler = V2EnvironmentHandler()
        environments = [change_obj_to_dict(e) for e in environment_handler.list_all()]

        for e in environments:
            container_info = e['container_id']
            e['container_id'] = jsonutils.loads(container_info) if container_info else {}

            image_id = e['image_id']
            e['image_id'] = image_id.split(',') if image_id else []

        data = {
            'environments': environments
        }

        return result_handler(consts.API_SUCCESS, data)