Skip to content

Commit

Permalink
workspaces: add default workspace and validate user workspace choice
Browse files Browse the repository at this point in the history
Add default workspace as an environmental variable coming from the cluster deployment choice.
The validation is done centralized in the validate_workspace function

Closes reanahub/reana-client#546
  • Loading branch information
marcdiazsan committed Sep 13, 2021
1 parent 134e943 commit 2fbbac4
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 0 deletions.
14 changes: 14 additions & 0 deletions reana_commons/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,20 @@ def kubernetes_node_label_to_dict(node_label):
SHARED_VOLUME_PATH = os.getenv("SHARED_VOLUME_PATH", "/var/reana")
"""Default shared volume path."""

DEFAULT_WORKSPACE_PATH = os.getenv("DEFAULT_WORKSPACE_PATH", SHARED_VOLUME_PATH)
"""Default workspace path defined by the admin."""


def workspaces(workspaces_list):
"""Load the available workspaces as list."""
workspaces_list = workspaces_list.split(",") if workspaces_list else []
workspaces_list.append(SHARED_VOLUME_PATH)
return workspaces_list


WORKSPACE_PATHS = workspaces(os.getenv("WORKSPACE_PATHS"))
"""List of allowed workspace paths."""

K8S_CERN_EOS_MOUNT_CONFIGURATION = {
"volume": {"name": "eos", "hostPath": {"path": "/var/eos"}},
"volumeMounts": {
Expand Down
46 changes: 46 additions & 0 deletions reana_commons/openapi_specifications/reana_server.json
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,52 @@
"summary": "Ping the server (healthcheck)"
}
},
"/api/workspaces": {
"get":{
"consumes": [
"application/json"
],
"description": "Get the list of available workspaces.",
"operationId": "workspaces",
"produces": [
"application/json"
],
"responses": {
"200": {
"description": "This resource reports the available workspaces in the cluster.",
"examples": {
"application/json": {
"workspaces_available": ["/usr/share","/eos/home","/var/reana"],
"default": "/usr/share"
}
},
"schema": {
"properties": {
"workspaces_available": {
"type": "array",
"items": {
"type":"string"
}
},
"default": {
"type": "string"
}
},
"type": "object"
}
},
"500": {
"description": "Request failed. Internal server error.",
"examples": {
"application/json": {
"message": "Internal server error."
}
}
}
},
"summary": "Get the list of available workspaces."
}
},
"/api/secrets": {
"get": {
"description": "Get user secrets.",
Expand Down
30 changes: 30 additions & 0 deletions reana_commons/workspaces.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# -*- coding: utf-8 -*-
#
# This file is part of REANA.
# Copyright (C) 2021 CERN.
#
# REANA is free software; you can redistribute it and/or modify it
# under the terms of the MIT License; see LICENSE file for more details.
"""REANA-Commons workspaces util."""

from reana_commons.errors import REANAValidationError
from reana_commons.config import WORKSPACE_PATHS
import os


def validate_workspace(workspace_option, available_paths=WORKSPACE_PATHS):
"""Validate and return workspace.
:param workspace_option: A string of the workspace.
:returns: A string of the validated workspace.
"""
if workspace_option:
available = any(
os.path.abspath(workspace_option).startswith(os.path.abspath(path))
for path in available_paths
)
if not available:
raise REANAValidationError(
'==> ERROR: Workspace "{0}" not valid.'.format(workspace_option)
)
return workspace_option

0 comments on commit 2fbbac4

Please sign in to comment.