|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +# |
| 3 | +# Copyright 2021 - Swiss Data Science Center (SDSC) |
| 4 | +# A partnership between École Polytechnique Fédérale de Lausanne (EPFL) and |
| 5 | +# Eidgenössische Technische Hochschule Zürich (ETHZ). |
| 6 | +# |
| 7 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | +# you may not use this file except in compliance with the License. |
| 9 | +# You may obtain a copy of the License at |
| 10 | +# |
| 11 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | +# |
| 13 | +# Unless required by applicable law or agreed to in writing, software |
| 14 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | +# See the License for the specific language governing permissions and |
| 17 | +# limitations under the License. |
| 18 | +"""Renku service project lock status controller.""" |
| 19 | + |
| 20 | +import portalocker |
| 21 | + |
| 22 | +from renku.core import errors |
| 23 | +from renku.service.cache.models.project import Project |
| 24 | +from renku.service.controllers.api.abstract import ServiceCtrl |
| 25 | +from renku.service.controllers.api.mixins import RenkuOperationMixin |
| 26 | +from renku.service.errors import ProjectNotFound |
| 27 | +from renku.service.serializers.project import ProjectLockStatusRequest, ProjectLockStatusResponseRPC |
| 28 | +from renku.service.views import result_response |
| 29 | + |
| 30 | + |
| 31 | +class ProjectLockStatusCtrl(ServiceCtrl, RenkuOperationMixin): |
| 32 | + """Controller for project lock status endpoint.""" |
| 33 | + |
| 34 | + REQUEST_SERIALIZER = ProjectLockStatusRequest() |
| 35 | + RESPONSE_SERIALIZER = ProjectLockStatusResponseRPC() |
| 36 | + |
| 37 | + def __init__(self, cache, user_data, request_data): |
| 38 | + """Construct a project edit controller.""" |
| 39 | + self.ctx = self.REQUEST_SERIALIZER.load(request_data) |
| 40 | + |
| 41 | + super().__init__(cache, user_data, request_data) |
| 42 | + |
| 43 | + @property |
| 44 | + def context(self): |
| 45 | + """Controller operation context.""" |
| 46 | + return self.ctx |
| 47 | + |
| 48 | + def get_lock_status(self) -> bool: |
| 49 | + """Return True if a project is write-locked.""" |
| 50 | + if "project_id" in self.context: |
| 51 | + try: |
| 52 | + project = self.cache.get_project(self.user, self.context["project_id"]) |
| 53 | + except ProjectNotFound: |
| 54 | + return False |
| 55 | + elif "git_url" in self.context and "user_id" in self.user_data: |
| 56 | + try: |
| 57 | + project = Project.get( |
| 58 | + (Project.user_id == self.user_data["user_id"]) & (Project.git_url == self.context["git_url"]) |
| 59 | + ) |
| 60 | + except ValueError: |
| 61 | + return False |
| 62 | + else: |
| 63 | + raise errors.RenkuException("context does not contain `project_id` or `git_url` or missing `user_id`") |
| 64 | + |
| 65 | + try: |
| 66 | + with project.read_lock(timeout=0): |
| 67 | + return False |
| 68 | + except (portalocker.LockException, portalocker.AlreadyLocked): |
| 69 | + return True |
| 70 | + |
| 71 | + def renku_op(self): |
| 72 | + """Renku operation for the controller.""" |
| 73 | + # NOTE: We leave it empty since it does not execute renku operation. |
| 74 | + pass |
| 75 | + |
| 76 | + def to_response(self): |
| 77 | + """Execute controller flow and serialize to service response.""" |
| 78 | + is_locked = self.get_lock_status() |
| 79 | + return result_response(self.RESPONSE_SERIALIZER, data={"locked": is_locked}) |
0 commit comments