Пример #1
0
    def kill(self):
        """Force to kill the process.

        The reason can be the fact it was working too long or parent exectuter canceled it.
        """
        self.node.node_running_status = NodeRunningStatus.CANCELED
        for node_id in list(self.monitoring_node_ids):
            run_cancellation_manager.cancel_run(node_id)
Пример #2
0
Файл: node.py Проект: rbax/plynx
def post_node(collection):
    app.logger.debug(request.data)

    data = json.loads(request.data)

    node = Node.from_dict(data['node'])
    node.starred = False
    action = data['action']
    db_node = node_collection_managers[collection].get_db_node(
        node._id, g.user._id)

    if db_node:
        if not node.author:
            node.author = db_node['author']
        if node.author != db_node['author']:
            raise Exception(
                "Author of the node does not match the one in the database")
        is_author = db_node['author'] == g.user._id
    else:
        # assign the author
        node.author = g.user._id
        is_author = True

    is_admin = g.user.check_role(IAMPolicies.IS_ADMIN)
    is_workflow = node.kind in workflow_manager.kind_to_workflow_dict

    can_create_operations = g.user.check_role(
        IAMPolicies.CAN_CREATE_OPERATIONS)
    can_create_workflows = g.user.check_role(IAMPolicies.CAN_CREATE_WORKFLOWS)
    can_modify_others_workflows = g.user.check_role(
        IAMPolicies.CAN_MODIFY_OTHERS_WORKFLOWS)
    can_run_workflows = g.user.check_role(IAMPolicies.CAN_RUN_WORKFLOWS)

    if action == NodePostAction.SAVE:
        if (is_workflow and not can_create_workflows) or (
                not is_workflow and not can_create_operations):
            return make_permission_denied(
                'You do not have permission to save this object')

        if node.node_status != NodeStatus.CREATED:
            return make_fail_response(
                'Cannot save node with status `{}`'.format(node.node_status))

        if is_author or is_admin or (is_workflow
                                     and can_modify_others_workflows):
            node.save(force=True)
        else:
            return make_permission_denied(
                'Only the owners or users with CAN_MODIFY_OTHERS_WORKFLOWS role can save it'
            )

    elif action == NodePostAction.APPROVE:
        if is_workflow:
            return make_fail_response('Invalid action for a workflow'), 400
        if node.node_status != NodeStatus.CREATED:
            return make_fail_response(
                'Node status `{}` expected. Found `{}`'.format(
                    NodeStatus.CREATED, node.node_status))
        validation_error = executor_manager.kind_to_executor_class[node.kind](
            node).validate()
        if validation_error:
            return make_success_response({
                'status':
                NodePostStatus.VALIDATION_FAILED,
                'message':
                'Node validation failed',
                'validation_error':
                validation_error.to_dict()
            })

        node.node_status = NodeStatus.READY

        if is_author or is_admin:
            node.save(force=True)
        else:
            return make_permission_denied()

    elif action == NodePostAction.CREATE_RUN:
        if not is_workflow:
            return make_fail_response('Invalid action for an operation'), 400
        if node.node_status != NodeStatus.CREATED:
            return make_fail_response(
                'Node status `{}` expected. Found `{}`'.format(
                    NodeStatus.CREATED, node.node_status))
        validation_error = executor_manager.kind_to_executor_class[node.kind](
            node).validate()
        if validation_error:
            return make_success_response({
                'status':
                NodePostStatus.VALIDATION_FAILED,
                'message':
                'Node validation failed',
                'validation_error':
                validation_error.to_dict()
            })

        node = node.clone(NodeClonePolicy.NODE_TO_RUN)
        node.author = g.user._id
        if is_admin or can_run_workflows:
            node.save(collection=Collections.RUNS)
        else:
            return make_permission_denied(
                'You do not have CAN_RUN_WORKFLOWS role')

        return make_success_response({
            'status':
            NodePostStatus.SUCCESS,
            'message':
            'Run(_id=`{}`) successfully created'.format(str(node._id)),
            'run_id':
            str(node._id),
            'url':
            '/{}/{}'.format(Collections.RUNS, node._id),
        })

    elif action == NodePostAction.CLONE:
        if (is_workflow and not can_create_workflows) or (
                not is_workflow and not can_create_operations):
            return make_permission_denied(
                'You do not have the role to create an object')
        node_clone_policy = None
        if collection == Collections.TEMPLATES:
            node_clone_policy = NodeClonePolicy.NODE_TO_NODE
        elif collection == Collections.RUNS:
            node_clone_policy = NodeClonePolicy.RUN_TO_NODE

        node = node.clone(node_clone_policy)
        node.save(collection=Collections.TEMPLATES)

        return make_success_response({
            'message':
            'Node(_id=`{}`) successfully created'.format(str(node._id)),
            'node_id':
            str(node._id),
            'url':
            '/{}/{}'.format(Collections.TEMPLATES, node._id),
        })

    elif action == NodePostAction.VALIDATE:
        validation_error = executor_manager.kind_to_executor_class[node.kind](
            node).validate()

        if validation_error:
            return make_success_response({
                'status':
                NodePostStatus.VALIDATION_FAILED,
                'message':
                'Node validation failed',
                'validation_error':
                validation_error.to_dict()
            })
    elif action == NodePostAction.DEPRECATE:
        if node.node_status == NodeStatus.CREATED:
            return make_fail_response('Node status `{}` not expected.'.format(
                node.node_status))

        node.node_status = NodeStatus.DEPRECATED

        if is_author or is_admin:
            node.save(force=True)
        else:
            return make_permission_denied(
                'You are not an author to deprecate it')

    elif action == NodePostAction.MANDATORY_DEPRECATE:
        if node.node_status == NodeStatus.CREATED:
            return make_fail_response('Node status `{}` not expected.'.format(
                node.node_status))

        node.node_status = NodeStatus.MANDATORY_DEPRECATED

        if is_author or is_admin:
            node.save(force=True)
        else:
            return make_permission_denied(
                'You are not an author to deprecate it')

    elif action == NodePostAction.PREVIEW_CMD:

        return make_success_response({
            'message':
            'Successfully created preview',
            'preview_text':
            executor_manager.kind_to_executor_class[node.kind](node).run(
                preview=True)
        })

    elif action == NodePostAction.REARRANGE_NODES:
        node.arrange_auto_layout()
        return make_success_response({
            'message': 'Successfully created preview',
            'node': node.to_dict(),
        })
    elif action == NodePostAction.UPGRADE_NODES:
        upd = node_collection_managers[collection].upgrade_sub_nodes(node)
        return make_success_response({
            'message': 'Successfully updated nodes',
            'node': node.to_dict(),
            'upgraded_nodes_count': upd,
        })
    elif action == NodePostAction.CANCEL:

        if is_author or is_admin:
            run_cancellation_manager.cancel_run(node._id)
        else:
            return make_permission_denied(
                'You are not an author to cancel the run')

    elif action == NodePostAction.GENERATE_CODE:
        raise NotImplementedError()
    else:
        return make_fail_response('Unknown action `{}`'.format(action))

    return make_success_response({
        'message':
        'Node(_id=`{}`) successfully updated'.format(str(node._id))
    })
Пример #3
0
def post_node(collection):
    app.logger.debug(request.data)

    data = json.loads(request.data)

    node = Node.from_dict(data['node'])
    node.author = g.user._id
    node.starred = False
    db_node = node_collection_managers[collection].get_db_node(
        node._id, g.user._id)
    action = data['action']
    if db_node and db_node[
            '_readonly'] and action not in PERMITTED_READONLY_POST_ACTIONS:
        return make_fail_response('Permission denied'), 403

    if action == NodePostAction.SAVE:
        if node.node_status != NodeStatus.CREATED and node.base_node_name != 'file':
            return make_fail_response(
                'Cannot save node with status `{}`'.format(node.node_status))

        node.save(force=True)

    elif action == NodePostAction.APPROVE:
        if node.node_status != NodeStatus.CREATED:
            return make_fail_response(
                'Node status `{}` expected. Found `{}`'.format(
                    NodeStatus.CREATED, node.node_status))
        validation_error = executor_manager.kind_to_executor_class[node.kind](
            node).validate()
        if validation_error:
            return JSONEncoder().encode({
                'status':
                NodePostStatus.VALIDATION_FAILED,
                'message':
                'Node validation failed',
                'validation_error':
                validation_error.to_dict()
            })

        node.node_status = NodeStatus.READY
        node.save(force=True)

    elif action == NodePostAction.CREATE_RUN:
        if node.node_status != NodeStatus.CREATED:
            return make_fail_response(
                'Node status `{}` expected. Found `{}`'.format(
                    NodeStatus.CREATED, node.node_status))
        validation_error = executor_manager.kind_to_executor_class[node.kind](
            node).validate()
        if validation_error:
            return JSONEncoder().encode({
                'status':
                NodePostStatus.VALIDATION_FAILED,
                'message':
                'Node validation failed',
                'validation_error':
                validation_error.to_dict()
            })

        node = node.clone(NodeClonePolicy.NODE_TO_RUN)
        node.save(collection=Collections.RUNS)
        return JSONEncoder().encode({
            'status':
            NodePostStatus.SUCCESS,
            'message':
            'Run(_id=`{}`) successfully created'.format(str(node._id)),
            'run_id':
            str(node._id),
            'url':
            '/{}/{}'.format(Collections.RUNS, node._id),
        })

    elif action == NodePostAction.CLONE:
        node_clone_policy = None
        if collection == Collections.TEMPLATES:
            node_clone_policy = NodeClonePolicy.NODE_TO_NODE
        elif collection == Collections.RUNS:
            node_clone_policy = NodeClonePolicy.RUN_TO_NODE

        node = node.clone(node_clone_policy)
        node.save(collection=Collections.TEMPLATES)

        return JSONEncoder().encode({
            'status':
            NodePostStatus.SUCCESS,
            'message':
            'Node(_id=`{}`) successfully created'.format(str(node._id)),
            'node_id':
            str(node._id),
            'url':
            '/{}/{}'.format(Collections.TEMPLATES, node._id),
        })

    elif action == NodePostAction.VALIDATE:
        validation_error = executor_manager.kind_to_executor_class[node.kind](
            node).validate()

        if validation_error:
            return JSONEncoder().encode({
                'status':
                NodePostStatus.VALIDATION_FAILED,
                'message':
                'Node validation failed',
                'validation_error':
                validation_error.to_dict()
            })
    elif action == NodePostAction.DEPRECATE:
        if node.node_status == NodeStatus.CREATED:
            return make_fail_response('Node status `{}` not expected.'.format(
                node.node_status))

        node.node_status = NodeStatus.DEPRECATED
        node.save(force=True)
    elif action == NodePostAction.MANDATORY_DEPRECATE:
        if node.node_status == NodeStatus.CREATED:
            return make_fail_response('Node status `{}` not expected.'.format(
                node.node_status))

        node.node_status = NodeStatus.MANDATORY_DEPRECATED
        node.save(force=True)
    elif action == NodePostAction.PREVIEW_CMD:

        return JSONEncoder().encode({
            'status':
            NodePostStatus.SUCCESS,
            'message':
            'Successfully created preview',
            'preview_text':
            executor_manager.kind_to_executor_class[node.kind](node).run(
                preview=True)
        })

    elif action == NodePostAction.REARRANGE_NODES:
        node.arrange_auto_layout()
        return JSONEncoder().encode(
            dict({
                'status': NodePostStatus.SUCCESS,
                'message': 'Successfully created preview',
                'node': node.to_dict(),
            }))
    elif action == NodePostAction.UPGRADE_NODES:
        upd = node_collection_managers[collection].upgrade_sub_nodes(node)
        return JSONEncoder().encode(
            dict({
                'status': NodePostStatus.SUCCESS,
                'message': 'Successfully updated nodes',
                'node': node.to_dict(),
                'upgraded_nodes_count': upd,
            }))
    elif action == NodePostAction.CANCEL:
        run_cancellation_manager.cancel_run(node._id)
    elif action == NodePostAction.GENERATE_CODE:
        raise NotImplementedError()
    else:
        return make_fail_response('Unknown action `{}`'.format(action))

    return JSONEncoder().encode({
        'status':
        NodePostStatus.SUCCESS,
        'message':
        'Node(_id=`{}`) successfully updated'.format(str(node._id))
    })