Exemplo n.º 1
0
def check_stack_status(app_instance_id):
    """
    check_stack_status
    Args:
        app_instance_id:
    """
    app_ins_mapper = AppInsMapper.get(app_instance_id=app_instance_id)
    if not app_ins_mapper:
        LOG.debug('app ins: %s db record not found', app_instance_id)
        return
    heat = openstack_utils.create_heat_client(app_ins_mapper.host_ip)
    stack_resp = heat.stacks.get(app_ins_mapper.stack_id)
    if stack_resp is None and app_ins_mapper.operational_status == 'Terminating':
        app_ins_mapper.delete()
        LOG.debug('finish terminate app ins %s', app_instance_id)
        return
    if stack_resp.status == 'COMPLETE' or stack_resp.status == 'FAILED':
        LOG.debug('app ins: %s, stack_status: %s, reason: %s', app_instance_id,
                  stack_resp.stack_status, stack_resp.stack_status_reason)
        if stack_resp.action == 'CREATE' and stack_resp.stack_status == 'CREATE_COMPLETE':
            app_ins_mapper.operational_status = utils.INSTANTIATED
            app_ins_mapper.operation_info = stack_resp.stack_status_reason
            LOG.debug('finish instantiate app ins %s', app_instance_id)
        elif stack_resp.action == 'DELETE' and stack_resp.stack_status == 'DELETE_COMPLETE':
            app_ins_mapper.delete()
            LOG.debug('finish terminate app ins %s', app_instance_id)
        else:
            app_ins_mapper.operation_info = stack_resp.stack_status_reason
            app_ins_mapper.operational_status = utils.FAILURE
            LOG.debug('failed action %s app ins %s', stack_resp.action,
                      app_instance_id)
    else:
        LOG.debug('app ins %s status not updated, waite next...')
        start_check_stack_status(app_instance_id)
Exemplo n.º 2
0
    def workloadEvents(self, request, context):
        """
        工作负载事件查询
        :param request:
        :param context:
        :return:
        """
        LOG.info('receive workload describe msg...')
        res = WorkloadEventsResponse(response='{"code":500}')

        host_ip = validate_input_params(request)
        if host_ip is None:
            return res

        app_instance_id = request.appInstanceId
        if app_instance_id is None:
            return res

        app_ins_mapper = AppInsMapper.get(app_instance_id=app_instance_id)
        if app_ins_mapper is None:
            LOG.debug('app实例 %s 不存在', app_instance_id)
            res.response = '{"code":404}'
            return res

        heat = openstack_utils.create_heat_client(host_ip)

        events = heat.events.list(stack_id=app_ins_mapper.stack_id)
        vm_describe_info = {}
        for event in events:
            if event.resource_name in vm_describe_info:
                vm_describe_info[event.resource_name]['events'].append({
                    'eventTime':
                    event.event_time,
                    'resourceStatus':
                    event.resource_status,
                    'resourceStatusReason':
                    event.resource_status_reason
                })
            else:
                vm_describe_info[event.resource_name] = {
                    'resourceName':
                    event.resource_name,
                    'logicalResourceId':
                    event.logical_resource_id,
                    'physicalResourceId':
                    event.physical_resource_id,
                    'events': [{
                        'eventTime':
                        event.event_time,
                        'resourceStatus':
                        event.resource_status,
                        'resourceStatusReason':
                        event.resource_status_reason
                    }]
                }
        response_data = []
        for key, value in vm_describe_info.items():
            response_data.append(value)
        res.response = json.dumps(response_data)
        return res
Exemplo n.º 3
0
    def query(self, request, context):
        """
        实例信息查询
        :param request:
        :param context:
        :return:
        """
        LOG.info('receive query msg...')
        res = QueryResponse(response='{"code": 500, "msg": "server error"}')

        host_ip = validate_input_params(request)
        if host_ip is None:
            res.response = '{"code":400}'
            return res

        app_instance_id = request.appInstanceId
        if app_instance_id is None:
            res.response = '{"code":400}'
            return res

        app_ins_mapper = AppInsMapper.get(app_instance_id=app_instance_id)
        if app_ins_mapper is None:
            res.response = '{"code":404}'
            return res

        heat = openstack_utils.create_heat_client(host_ip)
        output_list = heat.stacks.output_list(app_ins_mapper.stack_id)

        response = _get_output_data(output_list, heat, app_ins_mapper.stack_id)

        res.response = json.dumps(response)
        return res
Exemplo n.º 4
0
    def terminate(self, request, context):
        """
        销毁实例
        :param request:
        :param context:
        :return:
        """
        LOG.info('receive terminate msg...')
        res = TerminateResponse(status=utils.FAILURE)

        LOG.debug('校验token, host ip')
        host_ip = validate_input_params(request)
        if host_ip is None:
            return res

        LOG.debug('获取实例ID')
        app_instance_id = request.appInstanceId
        if app_instance_id is None:
            return res

        LOG.debug('查询数据库')
        app_ins_mapper = AppInsMapper.get(app_instance_id=app_instance_id)
        if app_ins_mapper is None:
            res.status = utils.SUCCESS
            return res

        LOG.debug('初始化openstack客户端')
        heat = openstack_utils.create_heat_client(host_ip)
        try:
            LOG.debug('发送删除请求')
            heat.stacks.delete(app_ins_mapper.stack_id)
        except HTTPNotFound:
            LOG.debug('stack不存在')
        except Exception as exception:
            LOG.error(exception, exc_info=True)
            return res

        app_ins_mapper.operational_status = utils.TERMINATING
        LOG.debug('更新数据库状态')
        commit()

        LOG.debug('开始状态更新定时任务')
        start_check_stack_status(app_instance_id=app_instance_id)

        res.status = utils.SUCCESS
        LOG.debug('处理请求完成')
        return res
Exemplo n.º 5
0
    def instantiate(self, request, context):
        """
        app 实例化
        :param request:
        :param context:
        :return:
        """
        LOG.info('receive instantiate msg...')
        res = TerminateResponse(status=utils.FAILURE)

        parameter = InstantiateRequest(request)

        LOG.debug('校验access token, host ip')
        host_ip = validate_input_params(parameter)
        if host_ip is None:
            return res

        LOG.debug('获取实例ID')
        app_instance_id = parameter.app_instance_id
        if app_instance_id is None:
            return res

        LOG.debug('查询数据库是否存在相同记录')
        app_ins_mapper = AppInsMapper.get(app_instance_id=app_instance_id)
        if app_ins_mapper is not None:
            LOG.info('app ins %s exist', app_instance_id)
            return res

        LOG.debug('读取包的hot文件')
        hot_yaml_path = get_hot_yaml_path(parameter.app_package_path)
        if hot_yaml_path is None:
            return res

        LOG.debug('构建heat参数')
        tpl_files, template = template_utils.get_template_contents(
            template_file=hot_yaml_path)
        fields = {
            'stack_name': 'eg-' + ''.join(str(uuid.uuid4()).split('-'))[0:8],
            'template': template,
            'files': dict(list(tpl_files.items()))
        }
        LOG.debug('init heat client')
        heat = openstack_utils.create_heat_client(host_ip)
        try:
            LOG.debug('发送创建stack请求')
            stack_resp = heat.stacks.create(**fields)
        except Exception as exception:
            LOG.error(exception, exc_info=True)
            return res
        AppInsMapper(app_instance_id=app_instance_id,
                     host_ip=host_ip,
                     stack_id=stack_resp['stack']['id'],
                     operational_status=utils.INSTANTIATING)
        LOG.debug('更新数据库')
        commit()

        LOG.debug('开始更新状态定时任务')
        start_check_stack_status(app_instance_id=app_instance_id)

        res.status = utils.SUCCESS
        LOG.debug('消息处理完成')
        return res