-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add cmdline args, dev mode, k8s config
- Loading branch information
Showing
7 changed files
with
401 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
fastapi dev src/config_service/__main__.py |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
Oops, something went wrong.