From fdc4bda8bd1a2fbe851868dbf6a47064f2897878 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miroslav=20Hole=C4=8Dek?= Date: Tue, 10 Dec 2024 19:34:39 +0100 Subject: [PATCH] dashboard server: allow configuration through env variables fixes #34 --- .env | 4 ++++ .gitignore | 3 +-- dashboard/Dockerfile | 2 +- dashboard/server/README.md | 3 +++ dashboard/server/app.py | 5 +++-- dashboard/server/config.py | 15 +++++++++++++++ dashboard/server/requirements.txt | 1 + 7 files changed, 28 insertions(+), 5 deletions(-) create mode 100644 .env create mode 100644 dashboard/server/config.py diff --git a/.env b/.env new file mode 100644 index 0000000..682f4dc --- /dev/null +++ b/.env @@ -0,0 +1,4 @@ +CHALLENGE_DIR=/challenges +CLASS_DIR=/classes +HOST=0.0.0.0 +PORT=80 diff --git a/.gitignore b/.gitignore index 3697a71..2119bbb 100644 --- a/.gitignore +++ b/.gitignore @@ -106,7 +106,6 @@ celerybeat.pid *.sage.py # Environments -.env .venv env/ venv/ @@ -133,4 +132,4 @@ dmypy.json .pyre/ # Ollama folder with models -ollama/ \ No newline at end of file +ollama/ diff --git a/dashboard/Dockerfile b/dashboard/Dockerfile index 0f34369..310d64b 100644 --- a/dashboard/Dockerfile +++ b/dashboard/Dockerfile @@ -28,7 +28,7 @@ COPY --from=download-python /root/.local/lib/python3.12/site-packages /root/.loc # Copy built Svelte files COPY --from=build-svelte /app/public $APP/public -COPY server/docker.py server/db.py server/app.py server/ws_ssh.py server/llm.py $APP/ +COPY server/*.py $APP/ # exec is important to receive sigterm and perform graceful shutdown when container is being terminated CMD ["bash", "-c", "python ws_ssh.py & exec python app.py"] diff --git a/dashboard/server/README.md b/dashboard/server/README.md index 331dcc0..fd0e552 100644 --- a/dashboard/server/README.md +++ b/dashboard/server/README.md @@ -4,6 +4,9 @@ - `pip install -r requirements.txt && pip install -r requirements-dev.txt` to install dependencies (including development tools) +- Locally edit `.env` (do not commit it!) to remove the leading slash from + `CHALLENGE_DIR` and `CLASS_DIR`, so that relative paths are used, unlike + in the Docker where both the directories are indeed located at `/` - `python app.py` to run the development server (also done in Docker) - `python ws_ssh.py` to run the websocket server - The reason for 2 servers is a conflict of threading (used for SSH diff --git a/dashboard/server/app.py b/dashboard/server/app.py index 16710c5..065cd3c 100644 --- a/dashboard/server/app.py +++ b/dashboard/server/app.py @@ -13,6 +13,7 @@ import db import docker import llm +from config import getenv def eprint(*args, **kwargs): @@ -25,7 +26,7 @@ def get_dirs(parent: str) -> List[str]: # load challenges from files and bootstrap DB -def init(parent_ch_dir='/challenges', parent_cl_dir='/classes'): +def init(parent_ch_dir=getenv('CHALLENGE_DIR') or '/challenges', parent_cl_dir=getenv('CLASS_DIR') or '/classes'): # this file works as a check to know if DB was already bootstrapped or not # because otherwise this code could run multiple times if the container was restarted file = Path(".was_db_inited") @@ -453,4 +454,4 @@ async def llm_chat(): if __name__ == '__main__': init() - app.run(debug=True, host='0.0.0.0', port=80) + app.run(debug=True, host=getenv('HOST') or '0.0.0.0', port=getenv('PORT') or 80) diff --git a/dashboard/server/config.py b/dashboard/server/config.py new file mode 100644 index 0000000..9c9cc5d --- /dev/null +++ b/dashboard/server/config.py @@ -0,0 +1,15 @@ +import os + +from dotenv import dotenv_values, find_dotenv + +config = { + **dotenv_values(find_dotenv()), + **os.environ +} + + +def getenv(key: str) -> str | None: + if key in config: + return config[key] + + return None diff --git a/dashboard/server/requirements.txt b/dashboard/server/requirements.txt index 5062666..e78316c 100644 --- a/dashboard/server/requirements.txt +++ b/dashboard/server/requirements.txt @@ -4,3 +4,4 @@ flask-socketio eventlet paramiko ollama==0.3.3 +python-dotenv