Exemple #1
0
 def test_delete_playbook_invalid_name(self):
     initial_playbooks = flask_server.running_context.controller.get_all_workflows(
     )
     initial_playbook_files = [
         os.path.splitext(playbook)[0]
         for playbook in helpers.locate_workflows_in_directory()
     ]
     response = self.delete_with_status_check(
         '/playbooks/junkPlaybookName',
         error='Playbook does not exist.',
         headers=self.headers,
         status_code=OBJECT_DNE_ERROR)
     self.assertDictEqual(response, {
         'error': 'Playbook does not exist.',
         'playbooks': initial_playbooks
     })
     self.assertFalse(
         flask_server.running_context.controller.is_playbook_registered(
             'junkPlaybookName'))
     final_playbook_files = [
         os.path.splitext(playbook)[0]
         for playbook in helpers.locate_workflows_in_directory()
     ]
     orderless_list_compare(self, final_playbook_files,
                            initial_playbook_files)
Exemple #2
0
    def test_delete_playbook_no_file(self):
        initial_playbooks = flask_server.running_context.controller.get_all_workflows(
        )
        initial_playbook_files = [
            os.path.splitext(playbook)[0]
            for playbook in helpers.locate_workflows_in_directory()
        ]
        self.app.put('/playbooks/test_playbook', headers=self.headers)
        response = self.delete_with_status_check('/playbooks/test_playbook',
                                                 headers=self.headers)
        self.assertDictEqual(response, {'playbooks': initial_playbooks})

        self.assertTrue(
            flask_server.running_context.controller.is_playbook_registered(
                'test'))
        self.assertFalse(
            flask_server.running_context.controller.is_playbook_registered(
                'test_playbook'))

        final_playbook_files = [
            os.path.splitext(playbook)[0]
            for playbook in helpers.locate_workflows_in_directory()
        ]
        orderless_list_compare(self, final_playbook_files,
                               initial_playbook_files)
 def test_delete_playbook_invalid_name(self):
     initial_playbooks = flask_server.running_context.controller.get_all_workflows()
     initial_playbook_files = [os.path.splitext(playbook)[0] for playbook in helpers.locate_workflows_in_directory()]
     response = self.app.post('/playbook/junkPlaybookName/delete', headers=self.headers)
     self.assertEqual(response.status_code, 200)
     response = json.loads(response.get_data(as_text=True))
     self.assertDictEqual(response, {'status': 'success', 'playbooks': initial_playbooks})
     self.assertFalse(flask_server.running_context.controller.is_playbook_registerd('junkPlaybookName'))
     final_playbook_files = [os.path.splitext(playbook)[0] for playbook in helpers.locate_workflows_in_directory()]
     orderless_list_compare(self, final_playbook_files, initial_playbook_files)
Exemple #4
0
def display_available_workflow_templates():
    templates = {
        os.path.splitext(workflow)[0]: helpers.get_workflow_names_from_file(
            os.path.join(config.templatesPath, workflow))
        for workflow in locate_workflows_in_directory(config.templatesPath)
    }
    return json.dumps({"templates": templates})
Exemple #5
0
def create_playbook(playbook_name):
    form = forms.AddPlaybookForm(request.form)
    if form.validate():
        status = 'success'
        template_playbook = form.playbook_template.data
        if template_playbook:
            if template_playbook in [
                    os.path.splitext(workflow)[0]
                    for workflow in helpers.locate_workflows_in_directory(
                        core.config.paths.templates_path)
            ]:
                running_context.controller.create_playbook_from_template(
                    playbook_name=playbook_name,
                    template_playbook=template_playbook)
            else:
                running_context.controller.create_playbook_from_template(
                    playbook_name=playbook_name)
                status = 'warning: template playbook not found. Using default template'
        else:
            running_context.controller.create_playbook_from_template(
                playbook_name=playbook_name)

        return json.dumps({
            "status":
            status,
            "playbooks":
            running_context.controller.get_all_workflows()
        })

    else:
        return json.dumps({'status': 'error: invalid form'})
Exemple #6
0
    def test_delete_playbook(self):
        response = self.delete_with_status_check('/playbooks/test', 'success', headers=self.headers)
        self.assertDictEqual(response, {'status': 'success', 'playbooks': {}})

        self.assertFalse(flask_server.running_context.controller.is_playbook_registered('test'))

        playbooks = [os.path.splitext(playbook)[0]
                     for playbook in helpers.locate_workflows_in_directory(core.config.paths.workflows_path)]
        self.assertEqual(len(playbooks), 0)
    def test_delete_playbook(self):
        response = self.app.post('/playbook/test/delete', headers=self.headers)
        self.assertEqual(response.status_code, 200)
        response = json.loads(response.get_data(as_text=True))
        self.assertDictEqual(response, {'status': 'success', 'playbooks': {}})

        self.assertFalse(flask_server.running_context.controller.is_playbook_registerd('test'))

        playbooks = [os.path.splitext(playbook)[0] for playbook in helpers.locate_workflows_in_directory()]
        self.assertEqual(len(playbooks), 0)
Exemple #8
0
 def load_all_workflows_from_directory(self, path=None):
     """Loads all workflows from a directory.
     
     Args:
         path (str, optional): Path to the directory to load from. Defaults to the configuration workflows_path. 
     """
     if path is None:
         path = core.config.paths.workflows_path
     for workflow in locate_workflows_in_directory(path):
         self.load_workflows_from_file(os.path.join(path, workflow))
Exemple #9
0
def display_available_playbooks():
    try:
        workflows = {
            os.path.splitext(workflow)[0]:
            helpers.get_workflow_names_from_file(
                os.path.join(config.workflowsPath, workflow))
            for workflow in locate_workflows_in_directory()
        }
        return json.dumps({"status": "success", "playbooks": workflows})
    except Exception as e:
        return json.dumps({"status": "error: {0}".format(e)})
 def test_load_all_workflows_from_directory(self):
     path = config.test_workflows_path
     workflows = helpers.locate_workflows_in_directory(path)
     keys = []
     for workflow in workflows:
         for name in helpers.get_workflow_names_from_file(
                 os.path.join(config.test_workflows_path, workflow)):
             playbook_name = workflow.split('.')[0]
             keys.append(_WorkflowKey(playbook_name, name))
     self.controller.load_all_workflows_from_directory(path=path)
     for key in keys:
         self.assertTrue(key in self.controller.workflows)
Exemple #11
0
    def __func():
        if running_context.controller.is_playbook_registered(playbook_name):
            data = request.get_json()
            if 'new_name' in data and data['new_name']:

                if running_context.controller.is_playbook_registered(
                        data['new_name']):
                    current_app.logger.warning(
                        'Could not update playbook {0}. Playbook already exists.'
                        .format(playbook_name))
                    return {
                        "error": "Playbook already exists."
                    }, OBJECT_EXISTS_ERROR

                new_name = data['new_name']
                running_context.controller.update_playbook_name(
                    playbook_name, new_name)
                running_context.Triggers.update_playbook(
                    old_playbook=playbook_name, new_playbook=new_name)
                saved_playbooks = [
                    os.path.splitext(playbook)[0]
                    for playbook in helpers.locate_workflows_in_directory(
                        core.config.paths.workflows_path)
                ]
                if playbook_name in saved_playbooks:
                    os.rename(
                        os.path.join(core.config.paths.workflows_path,
                                     '{0}.workflow'.format(playbook_name)),
                        os.path.join(core.config.paths.workflows_path,
                                     '{0}.workflow'.format(new_name)))
                current_app.logger.info(
                    'Playbook renamed from {0} to {1}'.format(
                        playbook_name, new_name))
                return {
                    "playbooks":
                    running_context.controller.get_all_workflows()
                }, SUCCESS
            else:
                current_app.logger.error(
                    'No new name provided to update playbook')
                return {
                    "error": 'No new name provided to update playbook.',
                    "playbooks":
                    running_context.controller.get_all_workflows()
                }, INVALID_INPUT_ERROR
        else:
            current_app.logger.error(
                'Could not edit playbook {0}. Playbook does not exist.'.format(
                    playbook_name))
            return {
                "error": 'Playbook does not exist.'.format(playbook_name)
            }, OBJECT_DNE_ERROR
Exemple #12
0
 def test_load_all_workflows_from_directory(self):
     path = config.test_workflows_path
     workflows = helpers.locate_workflows_in_directory(path)
     keys = []
     invalid_workflows = [
         'invalidActionWorkflow', 'invalidAppWorkflow',
         'tooManyStepInputsWorkflow', 'invalidInputWorkflow'
     ]
     for workflow in workflows:
         for name in helpers.get_workflow_names_from_file(
                 os.path.join(config.test_workflows_path, workflow)):
             playbook_name = workflow.split('.')[0]
             if playbook_name not in invalid_workflows:
                 keys.append(_WorkflowKey(playbook_name, name))
     self.controller.load_all_workflows_from_directory(path=path)
     for key in keys:
         self.assertTrue(key in self.controller.workflows)
Exemple #13
0
def update_playbook(playbook_name):
    if running_context.controller.is_playbook_registered(playbook_name):
        if request.get_json():
            data = request.get_json()
            if 'new_name' in data and data['new_name']:
                new_name = data['new_name']
                running_context.controller.update_playbook_name(
                    playbook_name, new_name)
                saved_playbooks = [
                    os.path.splitext(playbook)[0]
                    for playbook in helpers.locate_workflows_in_directory(
                        core.config.paths.workflows_path)
                ]
                if playbook_name in saved_playbooks:
                    os.rename(
                        os.path.join(core.config.paths.workflows_path,
                                     '{0}.workflow'.format(playbook_name)),
                        os.path.join(core.config.paths.workflows_path,
                                     '{0}.workflow'.format(new_name)))
                return json.dumps({
                    "status":
                    'success',
                    "playbooks":
                    running_context.controller.get_all_workflows()
                })
            else:
                return json.dumps({
                    "status":
                    'error: no name provided',
                    "playbooks":
                    running_context.controller.get_all_workflows()
                })
        else:
            return json.dumps({
                "status":
                'error: invalid json',
                "playbooks":
                running_context.controller.get_all_workflows()
            })
    else:
        return json.dumps({
            "status":
            'error: playbook name not found',
            "playbooks":
            running_context.controller.get_all_workflows()
        })
Exemple #14
0
def display_playbook_workflows(name):
    try:
        workflows = {
            os.path.splitext(workflow)[0]:
            helpers.get_workflow_names_from_file(
                os.path.join(config.workflowsPath, workflow))
            for workflow in locate_workflows_in_directory()
        }
        if name in workflows:
            return json.dumps({
                "status": "success",
                "workflows": workflows[name]
            })
        else:
            return json.dumps({"status": "error: name not found"})
    except Exception as e:
        return json.dumps({"status": "error: {0}".format(e)})
Exemple #15
0
 def __func():
     if playbook_name in running_context.controller.get_all_playbooks():
         return {"error": "Playbook already exists."}, OBJECT_EXISTS_ERROR
     form = forms.AddPlaybookForm(request.form)
     template_playbook = form.playbook_template.data
     if template_playbook:
         if template_playbook in [
                 os.path.splitext(workflow)[0]
                 for workflow in helpers.locate_workflows_in_directory(
                     core.config.paths.templates_path)
         ]:
             running_context.controller.create_playbook_from_template(
                 playbook_name=playbook_name,
                 template_playbook=template_playbook)
             current_app.logger.info(
                 'Playbook {0} created from template {1}'.format(
                     playbook_name, template_playbook))
             return {
                 "playbooks":
                 running_context.controller.get_all_workflows()
             }, OBJECT_CREATED
         else:
             running_context.controller.create_playbook_from_template(
                 playbook_name=playbook_name)
             current_app.logger.info(
                 'Playbook {0} cannot be created from template {1} because it doesn\'t exist. '
                 'Using default template instead'.format(
                     playbook_name, template_playbook))
             return {
                 "playbooks":
                 running_context.controller.get_all_workflows()
             }, SUCCESS_WITH_WARNING
     else:
         running_context.controller.create_playbook_from_template(
             playbook_name=playbook_name)
         current_app.logger.info(
             'Playbook {0} created from default template'.format(
                 playbook_name))
         return {
             "playbooks": running_context.controller.get_all_workflows()
         }, OBJECT_CREATED
Exemple #16
0
def create_workflow(playbook_name, workflow_name):
    form = forms.AddWorkflowForm(request.form)
    if form.validate():
        status = 'success'
        template_playbook = form.playbook.data
        template = form.template.data
        if template and template_playbook:
            if template_playbook in [
                    os.path.splitext(workflow)[0]
                    for workflow in helpers.locate_workflows_in_directory(
                        core.config.paths.templates_path)
            ]:
                res = running_context.controller.create_workflow_from_template(
                    playbook_name=playbook_name,
                    workflow_name=workflow_name,
                    template_playbook=template_playbook,
                    template_name=template)
                if not res:
                    add_default_template(playbook_name, workflow_name)
                    status = 'warning: template not found in playbook. Using default template'
            else:
                add_default_template(playbook_name, workflow_name)
                status = 'warning: template playbook not found. Using default template'
        else:
            add_default_template(playbook_name, workflow_name)
        if running_context.controller.is_workflow_registered(
                playbook_name, workflow_name):
            workflow = running_context.controller.get_workflow(
                playbook_name, workflow_name)
            return json.dumps({
                'workflow': {
                    'name': workflow_name,
                    'steps': workflow.get_cytoscape_data(),
                    'options': workflow.options.as_json()
                },
                'status': status
            })
        else:
            return json.dumps({'status': 'error: could not add workflow'})
    else:
        return json.dumps({'status': 'error: invalid form'})
Exemple #17
0
    def __func():
        if running_context.controller.is_playbook_registered(playbook_name):
            running_context.controller.remove_playbook(playbook_name)
            current_app.logger.info(
                'Deleted playbook {0} from controller'.format(playbook_name))
            if playbook_name in [
                    os.path.splitext(playbook)[0]
                    for playbook in helpers.locate_workflows_in_directory()
            ]:
                try:
                    os.remove(
                        os.path.join(core.config.paths.workflows_path,
                                     '{0}.workflow'.format(playbook_name)))
                    current_app.logger.info(
                        'Deleted playbook {0} from workflow directory'.format(
                            playbook_name))
                except (IOError, OSError) as e:
                    current_app.logger.error(
                        'Error deleting playbook {0}: {1}'.format(
                            playbook_name, e))
                    return {
                        'error':
                        'Error occurred while remove playbook file: {0}.'.
                        format(e),
                        'playbooks':
                        running_context.controller.get_all_workflows()
                    }, IO_ERROR
        else:
            current_app.logger.error(
                'Could not delete playbook {0}. Playbook does not exist.'.
                format(playbook_name))
            return {
                'error': 'Playbook does not exist.',
                'playbooks': running_context.controller.get_all_workflows()
            }, OBJECT_DNE_ERROR

        return {
            'playbooks': running_context.controller.get_all_workflows()
        }, SUCCESS
Exemple #18
0
def read_playbook_get_templates(playbook_name):
    if playbook_name == "templates":
        templates = {
            os.path.splitext(workflow)[0]:
            helpers.get_workflow_names_from_file(
                os.path.join(core.config.paths.templates_path, workflow))
            for workflow in helpers.locate_workflows_in_directory(
                core.config.paths.templates_path)
        }
        return json.dumps({"status": "success", "templates": templates})
    else:
        try:
            workflows = running_context.controller.get_all_workflows()
            if playbook_name in workflows:
                return json.dumps({
                    "status": "success",
                    "workflows": workflows[playbook_name]
                })
            else:
                return json.dumps({"status": "error: name not found"})
        except Exception as e:
            return json.dumps({"status": "error: {0}".format(e)})
Exemple #19
0
def delete_playbook(playbook_name):
    status = 'success'
    if running_context.controller.is_playbook_registered(playbook_name):
        running_context.controller.remove_playbook(playbook_name)
    if playbook_name in [
            os.path.splitext(playbook)[0]
            for playbook in helpers.locate_workflows_in_directory()
    ]:
        try:
            os.remove(
                os.path.join(core.config.paths.workflows_path,
                             '{0}.workflow'.format(playbook_name)))
        except OSError as e:
            status = 'error: error occurred while remove playbook file: {0}'.format(
                e)

    return json.dumps({
        'status':
        status,
        'playbooks':
        running_context.controller.get_all_workflows()
    })
Exemple #20
0
 def __func():
     if playbook_name == "templates":
         templates = {
             os.path.splitext(workflow)[0]:
             helpers.get_workflow_names_from_file(
                 os.path.join(core.config.paths.templates_path, workflow))
             for workflow in helpers.locate_workflows_in_directory(
                 core.config.paths.templates_path)
         }
         return {"templates": templates}, SUCCESS
     else:
         try:
             workflows = running_context.controller.get_all_workflows()
             if playbook_name in workflows:
                 return {"workflows": workflows[playbook_name]}, SUCCESS
             else:
                 current_app.logger.error(
                     'Playbook {0} was not found'.format(playbook_name))
                 return {
                     "error": "Playbook does not exist."
                 }, OBJECT_DNE_ERROR
         except Exception as e:
             return {"error": "{0}".format(e)}, INVALID_INPUT_ERROR
Exemple #21
0
 def load_all_workflows_from_directory(self, path=config.workflowsPath):
     for workflow in locate_workflows_in_directory(path):
         self.loadWorkflowsFromFile(
             os.path.join(config.workflowsPath, workflow))
Exemple #22
0
def crud_playbook(playbook_name, action):
    if action == 'add':
        form = forms.AddPlaybookForm(request.form)
        if form.validate():
            status = 'success'
            template_playbook = form.playbook_template.data
            if template_playbook:
                if template_playbook in [
                        os.path.splitext(workflow)[0] for workflow in
                        locate_workflows_in_directory(config.templatesPath)
                ]:
                    running_context.controller.create_playbook_from_template(
                        playbook_name=playbook_name,
                        template_playbook=template_playbook)
                else:
                    running_context.controller.create_playbook_from_template(
                        playbook_name=playbook_name)
                    status = 'warning: template playbook not found. Using default template'
            else:
                running_context.controller.create_playbook_from_template(
                    playbook_name=playbook_name)
            return json.dumps({
                "status":
                status,
                "playbooks":
                running_context.controller.get_all_workflows()
            })

        else:
            return json.dumps({'status': 'error: invalid form'})
    elif action == 'edit':
        if running_context.controller.is_playbook_registerd(playbook_name):
            form = forms.EditPlaybookForm(request.form)
            if form.validate():
                new_name = form.new_name.data
                if new_name:
                    running_context.controller.update_playbook_name(
                        playbook_name, new_name)
                    saved_playbooks = [
                        os.path.splitext(playbook)[0]
                        for playbook in locate_workflows_in_directory()
                    ]
                    if playbook_name in saved_playbooks:

                        os.rename(
                            os.path.join(config.workflowsPath,
                                         '{0}.workflow'.format(playbook_name)),
                            os.path.join(config.workflowsPath,
                                         '{0}.workflow'.format(new_name)))
                    return json.dumps({
                        "status":
                        'success',
                        "playbooks":
                        running_context.controller.get_all_workflows()
                    })
                else:
                    return json.dumps({
                        "status":
                        'error: no name provided',
                        "playbooks":
                        running_context.controller.get_all_workflows()
                    })
            else:
                return json.dumps({
                    "status":
                    'error: invalid form',
                    "playbooks":
                    running_context.controller.get_all_workflows()
                })
        else:
            return json.dumps({
                "status":
                'error: playbook name not found',
                "playbooks":
                running_context.controller.get_all_workflows()
            })
    elif action == 'delete':
        status = 'success'
        if running_context.controller.is_playbook_registerd(playbook_name):
            running_context.controller.remove_playbook(playbook_name)
        if playbook_name in [
                os.path.splitext(playbook)[0]
                for playbook in locate_workflows_in_directory()
        ]:
            try:
                os.remove(
                    os.path.join(config.workflowsPath,
                                 '{0}.workflow'.format(playbook_name)))
            except OSError as e:
                status = 'error: error occurred while remove playbook file: {0}'.format(
                    e)

        return json.dumps({
            'status':
            status,
            'playbooks':
            running_context.controller.get_all_workflows()
        })
    else:
        return json.dumps({"status": 'error: invalid operation'})
Exemple #23
0
def workflow(playbook_name, workflow_name, action):
    if action == 'add':
        form = forms.AddWorkflowForm(request.form)
        if form.validate():
            status = 'success'
            template_playbook = form.playbook.data
            template = form.template.data
            if template and template_playbook:
                if template_playbook in [
                        os.path.splitext(workflow)[0] for workflow in
                        locate_workflows_in_directory(config.templatesPath)
                ]:
                    res = running_context.controller.create_workflow_from_template(
                        playbook_name=playbook_name,
                        workflow_name=workflow_name,
                        template_playbook=template_playbook,
                        template_name=template)
                    if not res:
                        add_default_template(playbook_name, workflow_name)
                        status = 'warning: template not found in playbook. Using default template'
                else:
                    add_default_template(playbook_name, workflow_name)
                    status = 'warning: template playbook not found. Using default template'
            else:
                add_default_template(playbook_name, workflow_name)
            if running_context.controller.is_workflow_registered(
                    playbook_name, workflow_name):
                workflow = running_context.controller.get_workflow(
                    playbook_name, workflow_name)
                return json.dumps({
                    'workflow': {
                        'name': workflow_name,
                        'steps': workflow.get_cytoscape_data(),
                        'options': workflow.options.as_json()
                    },
                    'status': status
                })
            else:
                return json.dumps({'status': 'error: could not add workflow'})
        else:
            return json.dumps({'status': 'error: invalid form'})

    elif action == 'edit':
        if running_context.controller.is_workflow_registered(
                playbook_name, workflow_name):
            form = forms.EditPlayNameForm(request.form)
            if form.validate():
                enabled = form.enabled.data if form.enabled.data else False
                scheduler = {
                    'type':
                    form.scheduler_type.data
                    if form.scheduler_type.data else 'chron',
                    'autorun':
                    str(form.autoRun.data).lower()
                    if form.autoRun.data else 'false',
                    'args':
                    json.loads(form.scheduler_args.data)
                    if form.scheduler_args.data else {}
                }
                running_context.controller.get_workflow(playbook_name, workflow_name).options = \
                    Options(scheduler=scheduler, enabled=enabled)
                if form.new_name.data:
                    running_context.controller.update_workflow_name(
                        playbook_name, workflow_name, playbook_name,
                        form.new_name.data)
                    workflow_name = form.new_name.data
                workflow = running_context.controller.get_workflow(
                    playbook_name, workflow_name)
                if workflow:
                    return json.dumps({
                        'workflow': {
                            'name': workflow_name,
                            'options': workflow.options.as_json()
                        },
                        'status': 'success'
                    })
                else:
                    json.dumps({
                        'status':
                        'error: altered workflow can no longer be located'
                    })
            else:
                return json.dumps({'status': 'error: invalid form'})
        else:
            return json.dumps({'status': 'error: workflow name is not valid'})

    elif action == 'save':
        if running_context.controller.is_workflow_registered(
                playbook_name, workflow_name):
            if request.get_json():
                if 'cytoscape' in request.get_json():
                    workflow = running_context.controller.get_workflow(
                        playbook_name, workflow_name)
                    workflow.from_cytoscape_data(
                        json.loads(request.get_json()['cytoscape']))
                    try:
                        write_format = 'w' if sys.version_info[0] == 2 else 'wb'
                        workflow_filename = os.path.join(
                            config.workflowsPath,
                            '{0}.workflow'.format(playbook_name))
                        with open(workflow_filename,
                                  write_format) as workflow_out:
                            xml = ElementTree.tostring(
                                running_context.controller.playbook_to_xml(
                                    playbook_name))
                            workflow_out.write(xml)
                        return json.dumps({
                            "status":
                            "success",
                            "steps":
                            workflow.get_cytoscape_data()
                        })
                    except (OSError, IOError) as e:
                        return json.dumps({
                            "status":
                            "Error saving: {0}".format(e.message),
                            "steps":
                            workflow.get_cytoscape_data()
                        })
                else:
                    return json.dumps({"status": "error: malformed json"})
            else:
                return json.dumps({"status": "error: no information received"})
        else:
            return json.dumps({'status': 'error: workflow name is not valid'})

    elif action == 'delete':
        if running_context.controller.is_workflow_registered(
                playbook_name, workflow_name):
            running_context.controller.removeWorkflow(playbook_name,
                                                      workflow_name)
            status = 'success'
        else:
            status = 'error: invalid workflow name'
        return json.dumps({
            "status":
            status,
            "playbooks":
            running_context.controller.get_all_workflows()
        })

    elif action == 'execute':
        if running_context.controller.is_workflow_registered(
                playbook_name, workflow_name):
            running_context.controller.executeWorkflow(playbook_name,
                                                       workflow_name)
            status = 'success'
        else:
            status = 'error: invalid workflow name'
        return json.dumps({"status": status})

    else:
        return json.dumps({"status": 'error: invalid operation'})
Exemple #24
0
    def __func():
        form = forms.AddWorkflowForm(request.form)
        template_playbook = form.playbook.data
        template = form.template.data

        # TODO: UNCOMMENT THIS
        # if not running_context.controller.is_playbook_registered(playbook_name):
        #     current_app.logger.error('Could not create workflow {0}. Playbook does not exist.'.format(playbook_name))
        #     return {"error": 'Playbook does not exist.'}, OBJECT_DNE_ERROR

        if running_context.controller.is_workflow_registered(
                playbook_name, workflow_name):
            current_app.logger.warning(
                'Could not create workflow {0}. Workflow already exists.'.
                format(workflow_name))
            return {"error": "Workflow already exists."}, OBJECT_EXISTS_ERROR

        if template and template_playbook:
            if template_playbook in [
                    os.path.splitext(workflow)[0]
                    for workflow in helpers.locate_workflows_in_directory(
                        core.config.paths.templates_path)
            ]:
                res = running_context.controller.create_workflow_from_template(
                    playbook_name=playbook_name,
                    workflow_name=workflow_name,
                    template_playbook=template_playbook,
                    template_name=template)
                if not res:
                    add_default_template(playbook_name, workflow_name)
                    current_app.logger.warning(
                        'Workflow {0}-{1} could not be created from template {2}-{3}. '
                        'Using default template'.format(
                            playbook_name, workflow_name, template_playbook,
                            template))
                    workflow = running_context.controller.get_workflow(
                        playbook_name, workflow_name)
                    return {
                        'workflow': {
                            'name': workflow_name,
                            'steps': workflow.get_cytoscape_data(),
                            'options': workflow.options.as_json(),
                            'start': workflow.start_step
                        }
                    }, SUCCESS_WITH_WARNING
                else:
                    current_app.logger.info(
                        'Workflow {0}-{1} created from template {2}-{3}. '
                        'Using default template'.format(
                            playbook_name, workflow_name, template_playbook,
                            template))
            else:
                add_default_template(playbook_name, workflow_name)
                current_app.logger.info(
                    'Workflow {0}-{1} could not be created from template playbook {0} '
                    'because template playbook is not found. '
                    'Using default template'.format(playbook_name,
                                                    workflow_name))
                workflow = running_context.controller.get_workflow(
                    playbook_name, workflow_name)
                return {
                    'workflow': {
                        'name': workflow_name,
                        'steps': workflow.get_cytoscape_data(),
                        'options': workflow.options.as_json(),
                        'start': workflow.start_step
                    }
                }, SUCCESS_WITH_WARNING
        else:
            add_default_template(playbook_name, workflow_name)
            current_app.logger.info(
                'Workflow {0}-{1} created from default template'.format(
                    playbook_name, workflow_name))
        if running_context.controller.is_workflow_registered(
                playbook_name, workflow_name):
            workflow = running_context.controller.get_workflow(
                playbook_name, workflow_name)
            return {
                'workflow': {
                    'name': workflow_name,
                    'steps': workflow.get_cytoscape_data(),
                    'options': workflow.options.as_json(),
                    'start': workflow.start_step
                }
            }, OBJECT_CREATED
        else:
            current_app.logger.error('Could not add workflow {0}-{1}'.format(
                playbook_name, workflow_name))
            return {'error': 'Could not add workflow.'}, INVALID_INPUT_ERROR