Skip to content

Commit

Permalink
dashboard server: allow configuration through env variables
Browse files Browse the repository at this point in the history
  • Loading branch information
HolecekM committed Dec 16, 2024
1 parent a39aaa6 commit 369434e
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 5 deletions.
4 changes: 4 additions & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
CHALLENGE_DIR=/challenges
CLASS_DIR=/classes
HOST=0.0.0.0
PORT=80
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,6 @@ celerybeat.pid
*.sage.py

# Environments
.env
.venv
env/
venv/
Expand All @@ -133,4 +132,4 @@ dmypy.json
.pyre/

# Ollama folder with models
ollama/
ollama/
2 changes: 1 addition & 1 deletion dashboard/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
3 changes: 3 additions & 0 deletions dashboard/server/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions dashboard/server/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import db
import docker
import llm
from config import getenv


def eprint(*args, **kwargs):
Expand All @@ -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")
Expand Down Expand Up @@ -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)
15 changes: 15 additions & 0 deletions dashboard/server/config.py
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions dashboard/server/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ flask-socketio
eventlet
paramiko
ollama==0.3.3
python-dotenv

0 comments on commit 369434e

Please sign in to comment.