def execute_workflow(self,
                         deployment_id,
                         workflow_id,
                         parameters=None,
                         allow_custom_parameters=False,
                         force=False):
        deployment = self.get_deployment(deployment_id)

        if workflow_id not in deployment.workflows:
            raise manager_exceptions.NonexistentWorkflowError(
                'Workflow {0} does not exist in deployment {1}'.format(
                    workflow_id, deployment_id))
        workflow = deployment.workflows[workflow_id]

        self._verify_deployment_environment_created_successfully(deployment_id)

        # validate no execution is currently in progress
        if not force:
            executions = get_storage_manager().executions_list(
                deployment_id=deployment_id)
            running = [
                e.id for e in executions
                if get_storage_manager().get_execution(e.id).status not in
                models.Execution.END_STATES
            ]
            if len(running) > 0:
                raise manager_exceptions.ExistingRunningExecutionError(
                    'The following executions are currently running for this '
                    'deployment: {0}. To execute this workflow anyway, pass '
                    '"force=true" as a query parameter to this request'.format(
                        running))

        execution_parameters = \
            BlueprintsManager._merge_and_validate_execution_parameters(
                workflow, workflow_id, parameters, allow_custom_parameters)

        execution_id = str(uuid.uuid4())

        new_execution = models.Execution(
            id=execution_id,
            status=models.Execution.PENDING,
            created_at=str(datetime.now()),
            blueprint_id=deployment.blueprint_id,
            workflow_id=workflow_id,
            deployment_id=deployment_id,
            error='',
            parameters=self._get_only_user_execution_parameters(
                execution_parameters))

        get_storage_manager().put_execution(new_execution.id, new_execution)

        workflow_client().execute_workflow(
            workflow_id,
            workflow,
            blueprint_id=deployment.blueprint_id,
            deployment_id=deployment_id,
            execution_id=execution_id,
            execution_parameters=execution_parameters)

        return new_execution
 def _check_for_active_system_wide_execution(self):
     filters = {'status': models.Execution.ACTIVE_STATES}
     for e in self.executions_list(is_include_system_workflows=True,
                                   filters=filters).items:
         if e.deployment_id is None:
             raise manager_exceptions.ExistingRunningExecutionError(
                 'You cannot start an execution if there is a running '
                 'system-wide execution (id: {0})'.format(e.id))
    def _check_for_active_executions(self, deployment_id, force,
                                     transient_workers_config):
        is_transient_workers_enabled = transient_workers_config['enabled']

        def _get_running_executions(deployment_id=None, include_system=True):
            deplyment_id_filter = self.create_filters_dict(
                deployment_id=deployment_id)
            executions = self.executions_list(
                filters=deplyment_id_filter,
                is_include_system_workflows=include_system).items
            running = [
                e.id for e in executions if self.sm.get_execution(e.id).status
                not in models.Execution.END_STATES
            ]
            return running

        # validate no execution is currently in progress
        if not force:
            running = _get_running_executions(deployment_id)
            if len(running) > 0:
                raise manager_exceptions.ExistingRunningExecutionError(
                    'The following executions are currently running for this '
                    'deployment: {0}. To execute this workflow anyway, pass '
                    '"force=true" as a query parameter to this request'.format(
                        running))
        elif is_transient_workers_enabled:
            raise manager_exceptions.ExistingRunningExecutionError(
                'Forcing parallel executions in a single deployment is '
                'disabled in transient deployment workers mode')

        if is_transient_workers_enabled:
            global_parallel_executions_limit = \
                transient_workers_config['global_parallel_executions_limit']

            if global_parallel_executions_limit != \
                    LIMITLESS_GLOBAL_PARALLEL_EXECUTIONS_VALUE:
                running = _get_running_executions()
                if len(running) >= global_parallel_executions_limit:
                    raise manager_exceptions. \
                        GlobalParallelRunningExecutionsLimitReachedError(
                            'New workflows may not be executed at this time,'
                            'because global parallel running executions limit '
                            'has been reached ({0} running executions; '
                            'global limit {1}). Please try again soon'
                            .format(len(running),
                                    global_parallel_executions_limit))
    def _check_for_any_active_executions(self):
        filters = {'status': models.Execution.ACTIVE_STATES}
        executions = [
            e.id
            for e in self.executions_list(is_include_system_workflows=True,
                                          filters=filters).items
        ]

        if executions:
            raise manager_exceptions.ExistingRunningExecutionError(
                'You cannot start a system-wide execution if there are '
                'other executions running. '
                'Currently running executions: {0}'.format(executions))
    def _check_for_active_executions(self, deployment_id, force):

        def _get_running_executions(deployment_id=None, include_system=True):
            deployment_id_filter = self.create_filters_dict(
                deployment_id=deployment_id)
            executions = self.executions_list(
                filters=deployment_id_filter,
                is_include_system_workflows=include_system).items
            running = [
                e.id for e in executions if
                self.sm.get_execution(e.id).status
                not in models.Execution.END_STATES]
            return running

        # validate no execution is currently in progress
        if not force:
            running = _get_running_executions(deployment_id)
            if len(running) > 0:
                raise manager_exceptions.ExistingRunningExecutionError(
                    'The following executions are currently running for this '
                    'deployment: {0}. To execute this workflow anyway, pass '
                    '"force=true" as a query parameter to this request'.format(
                        running))