def __init__( self, id_, name, owner_id, reana_specification, type_, logs="", input_parameters={}, operational_options={}, status=RunStatus.created, git_ref="", git_repo=None, git_provider=None, workspace_path=None, restart=False, run_number=None, ): """Initialize workflow model.""" self.id_ = id_ self.name = name self.status = status self.owner_id = owner_id self.reana_specification = reana_specification self.input_parameters = input_parameters self.operational_options = operational_options self.type_ = type_ self.logs = logs or "" self.git_ref = git_ref self.git_repo = git_repo self.git_provider = git_provider self.restart = restart self._run_number = self.assign_run_number(run_number) self.workspace_path = workspace_path or build_workspace_path( self.owner_id, self.id_)
def sample_serial_workflow_in_db( app, default_user, session, serial_workflow, sample_workflow_workspace ): """Create a sample workflow in the database. Scope: function Adds a sample serial workflow in the DB. """ from reana_db.models import Workflow workflow_id = uuid4() relative_workspace_path = build_workspace_path(default_user.id_, workflow_id) next(sample_workflow_workspace(relative_workspace_path)) workflow = Workflow( id_=workflow_id, name="sample_serial_workflow_1", owner_id=default_user.id_, reana_specification=serial_workflow["reana_specification"], operational_options={}, type_=serial_workflow["reana_specification"]["workflow"]["type"], logs="", workspace_path=relative_workspace_path, ) session.add(workflow) session.commit() yield workflow for resource in workflow.resources: session.delete(resource) session.delete(workflow) session.commit()
def test_build_workspace_path(user_id, workflow_id, workspace_root_path, workspace_path): """Tests for build_workspace_path().""" from reana_db.utils import build_workspace_path assert (build_workspace_path( user_id=user_id, workflow_id=workflow_id, workspace_root_path=workspace_root_path, ) == workspace_path)
def get_user_workspace(self): """Build user's workspace directory path. :return: Path to the user's workspace directory. """ return build_workspace_path(self.id_)
def get_workspace(self): """Build workflow directory path. :return: Path to the workflow workspace directory. """ return build_workspace_path(self.owner_id, self.id_)
def create_workflow(): # noqa r"""Create workflow and its workspace. --- post: summary: Create workflow and its workspace. description: >- This resource expects all necessary data to represent a workflow so it is stored in database and its workspace is created. operationId: create_workflow produces: - application/json parameters: - name: user in: query description: Required. UUID of workflow owner. required: true type: string - name: workspace_root_path in: query description: A root path under which the workflow workspaces are stored. required: false type: string - name: workflow in: body description: >- JSON object including workflow parameters and workflow specification in JSON format (`yadageschemas.load()` output) with necessary data to instantiate a yadage workflow. required: true schema: type: object properties: operational_options: type: object description: Operational options. reana_specification: type: object description: >- Workflow specification in JSON format. workflow_name: type: string description: Workflow name. If empty name will be generated. git_data: type: object description: >- GitLab data. required: [reana_specification, workflow_name, operational_options] responses: 201: description: >- Request succeeded. The workflow has been created along with its workspace schema: type: object properties: message: type: string workflow_id: type: string workflow_name: type: string examples: application/json: { "message": "Workflow workspace has been created.", "workflow_id": "cdcf48b1-c2f3-4693-8230-b066e088c6ac", "workflow_name": "mytest-1" } 400: description: >- Request failed. The incoming data specification seems malformed 404: description: >- Request failed. User does not exist. examples: application/json: { "message": "User 00000000-0000-0000-0000-000000000000 does not exist" } """ try: user_uuid = request.args["user"] user = User.query.filter(User.id_ == user_uuid).first() if not user: return ( jsonify({ "message": "User with id:{} does not exist".format(user_uuid) }), 404, ) workflow_uuid = str(uuid4()) # Use name prefix user specified or use default name prefix # Actual name is prefix + autoincremented run_number. workflow_name = request.json.get("workflow_name", "") if workflow_name == "": workflow_name = DEFAULT_NAME_FOR_WORKFLOWS else: try: workflow_name.encode("ascii") except UnicodeEncodeError: # `workflow_name` contains something else than just ASCII. raise REANAWorkflowNameError( "Workflow name {} is not valid.".format(workflow_name)) git_ref = "" git_repo = "" if "git_data" in request.json: git_data = request.json["git_data"] git_ref = git_data["git_commit_sha"] git_repo = git_data["git_url"] # add spec and params to DB as JSON workspace_root_path = request.args.get("workspace_root_path", None) workflow = Workflow( id_=workflow_uuid, name=workflow_name, owner_id=request.args["user"], reana_specification=request.json["reana_specification"], operational_options=request.json.get("operational_options", {}), type_=request.json["reana_specification"]["workflow"]["type"], logs="", git_ref=git_ref, git_repo=git_repo, workspace_path=build_workspace_path(request.args["user"], workflow_uuid, workspace_root_path), ) Session.add(workflow) Session.object_session(workflow).commit() if git_ref: create_workflow_workspace( workflow.workspace_path, user_id=user.id_, git_url=git_data["git_url"], git_branch=git_data["git_branch"], git_ref=git_ref, ) else: create_workflow_workspace(workflow.workspace_path) return ( jsonify({ "message": "Workflow workspace created", "workflow_id": workflow.id_, "workflow_name": get_workflow_name(workflow), }), 201, ) except (REANAWorkflowNameError, KeyError) as e: return jsonify({"message": str(e)}), 400 except Exception as e: return jsonify({"message": str(e)}), 500
def test_build_workspace_path(): """Tests for build_workspace_path().""" from reana_db.utils import build_workspace_path assert build_workspace_path(0) == "users/0/workflows"