Skip to content

Commit

Permalink
Add scratch endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
ZohebShaikh committed Feb 27, 2025
1 parent bec48e6 commit 545e066
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 9 deletions.
8 changes: 4 additions & 4 deletions helm/blueapi/templates/statefulset.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ spec:
sizeLimit: 5Gi
{{- end }}
initContainers:
{{- if .Values.scratchHostPath }}
{{- if .Values.initContainer.scratch.root }}
- name: setup-scratch
image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}"
imagePullPolicy: {{ .Values.image.pullPolicy }}
Expand All @@ -69,7 +69,7 @@ spec:
mountPath: "/config"
readOnly: true
- name: scratch-host
mountPath: {{ .Values.worker.scratch.root }}
mountPath: {{ .Values.initContainer.scratch.root }}
mountPropagation: HostToContainer
- name: venv
mountPath: /artefacts
Expand All @@ -86,9 +86,9 @@ spec:
- name: worker-config
mountPath: "/config"
readOnly: true
{{- if .Values.scratchHostPath }}
{{- if .Values.initContainer.scratch.root }}
- name: scratch-host
mountPath: {{ .Values.worker.scratch.root }}
mountPath: {{ .Values.initContainer.scratch.root }}
mountPropagation: HostToContainer
- name: venv
mountPath: /venv
Expand Down
4 changes: 0 additions & 4 deletions helm/blueapi/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,3 @@ initContainer:
repositories: []
# - name: "dodal"
# remote_url: https://github.com/DiamondLightSource/dodal.git

# Mount path for scratch area from host machine, setting
# this effectively enables scratch area management
scratchHostPath: "" # example: /usr/local/blueapi-software-scratch
21 changes: 21 additions & 0 deletions src/blueapi/cli/scratch.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from git import Repo

from blueapi.config import ScratchConfig
from blueapi.service.model import ScratchResponse
from blueapi.utils import get_owner_gid, is_sgid_set

_DEFAULT_INSTALL_TIMEOUT: float = 300.0
Expand Down Expand Up @@ -128,3 +129,23 @@ def _validate_directory(path: Path) -> None:
raise KeyError(f"{path}: No such file or directory")
elif path.is_file():
raise KeyError(f"{path}: Is a file, not a directory")


def get_scratch_info(config: ScratchConfig) -> ScratchResponse:
_validate_directory(config.root)
scratch_responses = ScratchResponse(package_name=[], version=[], is_dirty=[])
for repo in config.repositories:
local_directory = config.root / repo.name
repo = Repo(local_directory)
try:
branch = repo.active_branch.name
except TypeError:
branch = repo.head.commit.hexsha

is_dirty = repo.is_dirty()

scratch_responses.package_name.append(repo.remotes.origin.url)
scratch_responses.version.append(branch)
scratch_responses.is_dirty.append(is_dirty)

return scratch_responses
3 changes: 3 additions & 0 deletions src/blueapi/client/rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,9 @@ def delete_environment(self) -> EnvironmentResponse:
def get_oidc_config(self) -> OIDCConfig:
return self._request_and_deserialize("/config/oidc", OIDCConfig)

def get_scratch_packages(self) -> dict:
return self._request_and_deserialize("/scratch/", dict)

@start_as_current_span(TRACER, "method", "data", "suffix")
def _request_and_deserialize(
self,
Expand Down
10 changes: 9 additions & 1 deletion src/blueapi/service/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@
from bluesky_stomp.messaging import StompClient
from bluesky_stomp.models import Broker, DestinationBase, MessageTopic

from blueapi.cli.scratch import get_scratch_info
from blueapi.config import ApplicationConfig, OIDCConfig, StompConfig
from blueapi.core.context import BlueskyContext
from blueapi.core.event import EventStream
from blueapi.service.model import DeviceModel, PlanModel, WorkerTask
from blueapi.service.model import DeviceModel, PlanModel, ScratchResponse, WorkerTask
from blueapi.worker.event import TaskStatusEnum, WorkerState
from blueapi.worker.task import Task
from blueapi.worker.task_worker import TaskWorker, TrackableTask
Expand Down Expand Up @@ -196,3 +197,10 @@ def get_task_by_id(task_id: str) -> TrackableTask | None:

def get_oidc_config() -> OIDCConfig | None:
return config().oidc


def get_scratch_packages() -> ScratchResponse:
if _CONFIG.scratch is None:
return ScratchResponse(package_name=[], version=[], is_dirty=[])
else:
return get_scratch_info(config=_CONFIG.scratch)
7 changes: 7 additions & 0 deletions src/blueapi/service/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
EnvironmentResponse,
PlanModel,
PlanResponse,
ScratchResponse,
StateChangeRequest,
TaskResponse,
TasksListResponse,
Expand Down Expand Up @@ -420,6 +421,12 @@ def set_state(
return runner.run(interface.get_worker_state)


@router.get("/scratch", response_model=ScratchResponse)
@start_as_current_span(TRACER)
def get_scratch_packages(runner: WorkerDispatcher = Depends(_runner)):
return runner.run(interface.get_scratch_packages)


@start_as_current_span(TRACER, "config")
def start(config: ApplicationConfig):
import uvicorn
Expand Down
12 changes: 12 additions & 0 deletions src/blueapi/service/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,18 @@ class EnvironmentResponse(BlueapiBaseModel):
)


class ScratchResponse(BlueapiBaseModel):
"""
State of the scratch area.
"""

package_name: list[str] = Field(description="Name of the package")
version: list[str] = Field(description="Version of the package")
is_dirty: list[bool] = Field(
description="Does the package have uncommitted changes"
)


class Cache(BlueapiBaseModel):
"""
Represents the cached data required for managing authentication.
Expand Down

0 comments on commit 545e066

Please sign in to comment.