Skip to content

Commit

Permalink
Add cmdline args, dev mode, k8s config
Browse files Browse the repository at this point in the history
  • Loading branch information
dperl-dls committed May 24, 2024
1 parent efe1dd3 commit d2c1228
Show file tree
Hide file tree
Showing 7 changed files with 401 additions and 10 deletions.
5 changes: 2 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ ENV PATH=/venv/bin:$PATH
FROM developer as build
COPY . /context
WORKDIR /context
RUN pip install .
RUN pip install .[server]

# The runtime stage copies the built venv into a slim runtime container
FROM python:${PYTHON_VERSION}-slim as runtime
Expand All @@ -25,5 +25,4 @@ COPY --from=build /venv/ /venv/
ENV PATH=/venv/bin:$PATH

# change this entrypoint if it is not the same as the repo
ENTRYPOINT ["config-service"]
CMD ["--version"]
ENTRYPOINT ["sh"]
22 changes: 22 additions & 0 deletions k8s-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
apiVersion: v1
kind: Pod
metadata:
name: container-service
spec:
containers:
- name: config-service
image:
command: ["container_service"]
args: []
resources:
limits:
cpu: "1"
memory: 300M
- name: config-service-database
image: valkey/valkey:7.2.5-alpine3.19
command: ["container_service"]
args: []
resources:
limits:
cpu: "1"
memory: 300M
1 change: 1 addition & 0 deletions run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
fastapi dev src/config_service/__main__.py
46 changes: 39 additions & 7 deletions src/config_service/__main__.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,57 @@
from argparse import ArgumentParser

import redis
import uvicorn
from dodal.common.beamlines.beamline_parameters import (
BEAMLINE_PARAMETER_PATHS,
GDABeamlineParameters,
)
from fastapi import FastAPI

from . import __version__
from .constants import ENDPOINTS

__all__ = ["main"]


BEAMLINE_PARAM_PATH = ""
BEAMLINE_PARAMS: GDABeamlineParameters | None = None

app = FastAPI()
valkey = redis.Redis(host="localhost", port=6379, decode_responses=True)
beamline_params = GDABeamlineParameters.from_file(BEAMLINE_PARAMETER_PATHS["i03"])


@app.get("/beamlineparameters/{item_id}")
@app.get(ENDPOINTS.BL_PARAM + "{item_id}")
def beamlineparameter(item_id: str):
return {item_id: beamline_params.params.get(item_id)}


@app.post("/featureflag/{item_id}")
def set_featureflag(item_id: str, value):
return {item_id: valkey.set(item_id, value)}
@app.post(ENDPOINTS.FEATURE + "{item_id}")
def set_featureflag(item_id: str, value: bool):
return {"success": valkey.set(item_id, int(value))}


@app.get("/featureflag/{item_id}")
@app.get(ENDPOINTS.FEATURE + "{item_id}")
def get_featureflag(item_id: str):
return {item_id: valkey.get(item_id)}
ret = valkey.get(item_id)
return {item_id: bool(ret) if ret is not None else None}


def main():
global BEAMLINE_PARAM_PATH
global BEAMLINE_PARAMS
parser = ArgumentParser()
parser.add_argument("-v", "--version", action="version", version=__version__)
parser.add_argument("-d", "--dev", action="store_true")
args = parser.parse_args()
if args.dev:
BEAMLINE_PARAM_PATH = "tests/test_data/beamline_parameters.txt"
else:
BEAMLINE_PARAM_PATH = BEAMLINE_PARAMETER_PATHS["i03"]
BEAMLINE_PARAMS = GDABeamlineParameters.from_file(BEAMLINE_PARAM_PATH)
uvicorn.run(app="config_service.__main__:app", host="localhost", port=8000)


# test with: python -m config_service
if __name__ == "__main__":
main()
28 changes: 28 additions & 0 deletions src/config_service/client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import json
from http.client import HTTPConnection

from .constants import ENDPOINTS


class ConfigService:
def __init__(self, address: str, port: int) -> None:
self.address = address
self.port = port

def _get(self, endpoint: str, param: str):
conn = HTTPConnection(self.address, self.port)
conn.connect()
conn.request("GET", f"{endpoint}{param}")
resp = conn.getresponse()
assert resp.status == 200, f"Failed to get response: {resp}"
body = json.loads(resp.read())
assert param in body, f"Malformed response: {body} does not contain {param}"
resp.close()
conn.close()
return body[param]

def get_beamline_param(self, param: str) -> str | int | float | bool | None:
return self._get(ENDPOINTS.BL_PARAM, param)

def get_feature_flag(self, param: str) -> bool | None:
return self._get(ENDPOINTS.FEATURE, param)
10 changes: 10 additions & 0 deletions src/config_service/constants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from dataclasses import dataclass


@dataclass(frozen=True)
class Endpoints:
FEATURE = "/featureflag/"
BL_PARAM = "/beamlineparameters/"


ENDPOINTS = Endpoints()
Loading

0 comments on commit d2c1228

Please sign in to comment.