Skip to content

Commit

Permalink
Merge d6c6588 into 75933d5
Browse files Browse the repository at this point in the history
  • Loading branch information
leticiawanderley committed Aug 14, 2019
2 parents 75933d5 + d6c6588 commit 73b7c5b
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 4 deletions.
1 change: 1 addition & 0 deletions AUTHORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ The list of contributors in alphabetical order:
- `Dinos Kousidis <https://orcid.org/0000-0002-4914-4289>`_
- `Harri Hirvonsalo <https://orcid.org/0000-0002-5503-510X>`_
- `Jan Okraska <https://orcid.org/0000-0002-1416-3244>`_
- `Leticia Wanderley <https://orcid.org/0000-0003-4649-6630>`_
- `Lukas Heinrich <https://orcid.org/0000-0002-4048-7584>`_
- `Rokas Maciulaitis <https://orcid.org/0000-0003-1064-6967>`_
- `Sinclert Perez <https://www.linkedin.com/in/sinclert>`_
Expand Down
4 changes: 4 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ RUN apt-get update && \
vim-tiny && \
pip install --upgrade pip

RUN apt-get update && \
apt-get upgrade -y && \
apt-get install -y git

COPY CHANGES.rst README.rst setup.py /code/
COPY reana_workflow_controller/version.py /code/reana_workflow_controller/

Expand Down
4 changes: 4 additions & 0 deletions docs/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,10 @@
"required": true,
"schema": {
"properties": {
"git_data": {
"description": "GitLab data.",
"type": "object"
},
"operational_options": {
"description": "Operational options.",
"type": "object"
Expand Down
9 changes: 9 additions & 0 deletions reana_workflow_controller/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,3 +114,12 @@

WORKFLOW_ENGINE_NAME = 'workflow-engine'
"""Default workflow engine container name."""

REANA_GITLAB_HOST = os.getenv('REANA_GITLAB_HOST', 'CHANGE_ME')
"""GitLab API HOST"""

REANA_GITLAB_URL = 'https://{}'.format(REANA_GITLAB_HOST)
"""GitLab API URL"""

REANA_URL = os.getenv('REANA_URL', 'CHANGE_ME')
"""REANA URL"""
30 changes: 29 additions & 1 deletion reana_workflow_controller/consumer.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,20 @@
import json
import uuid

import requests
from kubernetes.client.rest import ApiException
from reana_commons.consumer import BaseConsumer
from reana_commons.k8s.api_client import current_k8s_batchv1_api_client
from reana_commons.k8s.secrets import REANAUserSecretsStore
from reana_commons.utils import (calculate_file_access_time,
calculate_hash_of_dir,
calculate_job_input_hash)
from reana_db.database import Session
from reana_db.models import Job, JobCache, Workflow, WorkflowStatus
from sqlalchemy.orm.attributes import flag_modified

from reana_workflow_controller.config import PROGRESS_STATUSES
from reana_workflow_controller.config import (PROGRESS_STATUSES,
REANA_GITLAB_URL, REANA_URL)
from reana_workflow_controller.errors import REANAWorkflowControllerError


Expand Down Expand Up @@ -67,12 +70,37 @@ def _update_workflow_status(workflow_uuid, status, logs):
"""Update workflow status in DB."""
Workflow.update_workflow_status(Session, workflow_uuid,
status, logs, None)
workflow = Session.query(Workflow).filter_by(id_=workflow_uuid)\
.one_or_none()
if workflow.git_ref:
_update_commit_status(workflow, status)
alive_statuses = \
[WorkflowStatus.created, WorkflowStatus.running, WorkflowStatus.queued]
if status not in alive_statuses:
_delete_workflow_engine_pod(workflow_uuid)


def _update_commit_status(workflow, status):
if status == WorkflowStatus.finished:
state = "success"
elif status == WorkflowStatus.failed:
state = "failed"
elif status == WorkflowStatus.stopped or status == WorkflowStatus.deleted:
state = "canceled"
else:
state = "running"
secret_store = REANAUserSecretsStore(workflow.owner_id)
gitlab_access_token = secret_store.get_secret_value('gitlab_access_token')
target_url = REANA_URL + "/api/workflows/{0}/logs".format(workflow.id_)
commit_status_url = REANA_GITLAB_URL + "/api/v4/projects/{0}/" + \
"statuses/{1}?access_token={2}&state={3}&target_url={4}"
requests.post(commit_status_url.format(workflow.name,
workflow.git_ref,
gitlab_access_token,
state,
target_url))


def _update_run_progress(workflow_uuid, msg):
"""Register succeeded Jobs to DB."""
workflow = Session.query(Workflow).filter_by(id_=workflow_uuid).\
Expand Down
18 changes: 17 additions & 1 deletion reana_workflow_controller/rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,10 @@ def create_workflow(): # noqa
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]
Expand Down Expand Up @@ -306,6 +310,10 @@ def create_workflow(): # noqa
# `workflow_name` contains something else than just ASCII.
raise REANAWorkflowNameError('Workflow name {} is not valid.'.
format(workflow_name))
git_ref = ''
if 'git_data' in request.json:
git_data = request.json['git_data']
git_ref = git_data['git_commit_sha']
# add spec and params to DB as JSON
workflow = Workflow(id_=workflow_uuid,
name=workflow_name,
Expand All @@ -317,9 +325,17 @@ def create_workflow(): # noqa
type_=request.json[
'reana_specification']['workflow']['type'],
logs='')
workflow.git_ref = git_ref
Session.add(workflow)
Session.object_session(workflow).commit()
create_workflow_workspace(workflow.get_workspace())
if git_ref:
create_workflow_workspace(workflow.get_workspace(),
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.get_workspace())
return jsonify({'message': 'Workflow workspace created',
'workflow_id': workflow.id_,
'workflow_name': _get_workflow_name(workflow)}), 201
Expand Down
19 changes: 17 additions & 2 deletions reana_workflow_controller/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,18 @@

import fs
from flask import current_app as app
from git import Repo
from reana_commons.config import REANA_WORKFLOW_UMASK
from reana_commons.k8s.secrets import REANAUserSecretsStore
from reana_db.database import Session
from reana_db.models import Job, JobCache

from reana_workflow_controller.config import WORKFLOW_TIME_FORMAT
from reana_workflow_controller.config import (REANA_GITLAB_HOST,
WORKFLOW_TIME_FORMAT)


def create_workflow_workspace(path):
def create_workflow_workspace(path, user_id=None,
git_url=None, git_branch=None, git_ref=None):
"""Create workflow workspace.
:param path: Relative path to workspace directory.
Expand All @@ -28,6 +32,17 @@ def create_workflow_workspace(path):
os.umask(REANA_WORKFLOW_UMASK)
reana_fs = fs.open_fs(app.config['SHARED_VOLUME_PATH'])
reana_fs.makedirs(path, recreate=True)
if git_url and git_ref:
secret_store = REANAUserSecretsStore(user_id)
gitlab_access_token = secret_store\
.get_secret_value('gitlab_access_token')
url = "https://oauth2:{0}@{1}/{2}.git"\
.format(gitlab_access_token, REANA_GITLAB_HOST, git_url)
repo = Repo.clone_from(url=url,
to_path=os.path.abspath(
reana_fs.root_path + '/' + path),
branch=git_branch)
repo.head.reset(commit=git_ref)


def list_directory_files(directory):
Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
'Flask-SQLAlchemy>=2.2',
'Flask>=0.12',
'fs>=2.0',
'gitpython>=2.1',
'jsonpickle>=0.9.6',
'marshmallow>=2.13',
'packaging>=18.0',
Expand Down

0 comments on commit 73b7c5b

Please sign in to comment.