sts-sponsors team mailing list archive
-
sts-sponsors team
-
Mailing list archive
-
Message #05584
[Merge] ~thorsten-merten/maas-site-manager:MAASENG-1451-setup-docker-compose into maas-site-manager:main
Thorsten Merten has proposed merging ~thorsten-merten/maas-site-manager:MAASENG-1451-setup-docker-compose into maas-site-manager:main.
Commit message:
feat: add docker-compose to easily run backend + db
* add docker-compose.yaml
* add .env.dev file to share db secrets
* add backend Dockerfiles and .dockerignore
* change backend to use environment variables instead of hardcoded db
* change uvcorn run to accept all connections
* change tox to forward env vars
Requested reviews:
MAAS Committers (maas-committers)
For more details, see:
https://code.launchpad.net/~thorsten-merten/maas-site-manager/+git/maas-site-manager/+merge/438281
--
Your team MAAS Committers is requested to review the proposed merge of ~thorsten-merten/maas-site-manager:MAASENG-1451-setup-docker-compose into maas-site-manager:main.
diff --git a/.env.dev b/.env.dev
new file mode 100644
index 0000000..c59d0d9
--- /dev/null
+++ b/.env.dev
@@ -0,0 +1,11 @@
+APP_CORES=1
+APP_RAM=512MB
+
+POSTGRES_CORES=1
+POSTGRES_RAM=512MB
+
+POSTGRES_HOST="db"
+POSTGRES_PORT=5432
+POSTGRES_DB="postgres" # default for postgres docker image
+POSTGRES_USER="postgres" # default for postgres docker image
+POSTGRES_PASSWORD="msm" # default for postgres docker image
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..912f06f
--- /dev/null
+++ b/README.md
@@ -0,0 +1,16 @@
+# MAAS Site Manager
+
+This repository contains the MAAS Site Manager MVP. It one sub-folder for every sub-project.
+
+## How to run a local development environment?
+
+### Manually
+
+Follow the instruction in `/backend/README.md` and `/frontend/README.md`.
+
+### Via docker compose
+
+* Make sure to have a recent version of [docker](https://docs.docker.com/get-docker/) installed.
+* Run `docker compose up --build` to start the backend and the database
+* Run `cd frontend` and `yarn run serve` to start the frontend
+
diff --git a/backend/.dockerignore b/backend/.dockerignore
new file mode 100644
index 0000000..d885fbd
--- /dev/null
+++ b/backend/.dockerignore
@@ -0,0 +1,2 @@
+.tox
+msm.egg-info
diff --git a/backend/Dockerfile b/backend/Dockerfile
new file mode 100644
index 0000000..4bd1ba3
--- /dev/null
+++ b/backend/Dockerfile
@@ -0,0 +1,29 @@
+FROM python:3.10
+LABEL maintainer="MAAS Commiters <maas-commiters@xxxxxxxxxxxxx>"
+
+ARG VERSION=development
+ENV VERSION ${VERSION}
+
+# ensure python stdout and stderr streams are sent straight to terminal
+# also ensure nothing is buffered and thus may never be written
+ENV PYTHONUNBUFFERED 1
+
+# RUN apt-get update -q -y && apt-get install -q -y --no-install-recommends --no-install-suggests \
+# tox
+
+RUN python -m pip install --user tox
+
+# create app directory
+RUN mkdir /app
+WORKDIR /app
+
+# Bundle app source
+COPY . /app/
+
+# Expose port
+EXPOSE 8000
+
+#ENTRYPOINT ["tail", "-f", "/dev/null"] # to keep it running for debugging
+ENTRYPOINT ["python", "-m", "tox", "-e", "run", "run"]
+# CMD "python -m tox -e run run"
+#CMD "python wait-for-postgres.py"
diff --git a/backend/README.md b/backend/README.md
index 78d1b7b..9bf524c 100644
--- a/backend/README.md
+++ b/backend/README.md
@@ -20,7 +20,7 @@ These are the step needed for setup:
## Linting and testing
- The project uses `tox` for running python-related workflows:
+The project uses `tox` for running python-related workflows:
- Code formatting: `tox -e format`
- Linting: `tox -e lint`
@@ -59,18 +59,26 @@ It requires a PostgreSQL database set up to connect to. One (more persistent)
option is to set up the required user and database in the system-wide
PostgreSQL instance (installed via `make install-dependencies`).
+Make sure to set the following environment variables when starting the app:
+
+```
+export POSTGRES_HOST="hostname"
+export POSTGRES_PORT=5432
+export POSTGRES_DB="postgres" # default for postgres docker image
+export POSTGRES_USER="postgres" # default for postgres docker image
+export POSTGRES_PASSWORD="msm"
+```
+
Another (quicker) way is to launch a PostgreSQL instance via `docker`:
```
docker run --rm -it \
-p 5432:5432 \
- -e POSTGRES_PASSWORD=pass \
+ -e POSTGRES_PASSWORD=msm \
--name postgres \
postgres:14
```
-This setup matches the current hardcoded default DSN, so the app can be run
-right away after staring the `postgres` container.
Database schema setup will happen automatically at application startup.
The application can be run via
diff --git a/backend/msm/user_api/_setup.py b/backend/msm/user_api/_setup.py
index f181be3..58b272f 100644
--- a/backend/msm/user_api/_setup.py
+++ b/backend/msm/user_api/_setup.py
@@ -1,11 +1,20 @@
+from os import environ
+
from fastapi import FastAPI
from . import _base
from .. import PACKAGE
from ..db import Database
-# XXX drop hardcoded default
-DEFAULT_DB_DSN = "postgresql+asyncpg://postgres:pass@localhost/postgres"
+POSTGRES_HOST = environ.get("POSTGRES_HOST")
+POSTGRES_PORT = environ.get("POSTGRES_PORT")
+POSTGRES_DB = environ.get("POSTGRES_DB")
+POSTGRES_USER = environ.get("POSTGRES_USER")
+POSTGRES_PASSWORD = environ.get("POSTGRES_PASSWORD")
+DEFAULT_DB_DSN = (
+ "postgresql+asyncpg://"
+ + f"{POSTGRES_USER}:{POSTGRES_PASSWORD}@{POSTGRES_HOST}/{POSTGRES_DB}"
+)
def create_app(db_dsn: str = DEFAULT_DB_DSN) -> FastAPI:
diff --git a/backend/setup.cfg b/backend/setup.cfg
index f55af69..c889873 100644
--- a/backend/setup.cfg
+++ b/backend/setup.cfg
@@ -8,6 +8,7 @@ author_email = maas-devel@xxxxxxxxxxxxxxxxxxx
[options]
packages = find:
python_requires = >= 3.10
+pass_env = POSTGRES_HOST, POSTGRES_PORT, POSTGRES_DB, POSTGRES_USER, POSTGRES_PASSWORD
install_requires =
fastapi
SQLAlchemy[postgresql_asyncpg]
@@ -75,10 +76,11 @@ commands =
{envbindir}/mypy -p msm {posargs}
[testenv:run]
+pass_env = POSTGRES_HOST, POSTGRES_PORT, POSTGRES_DB, POSTGRES_USER, POSTGRES_PASSWORD
deps =
-e .
uvicorn
commands =
- {envbindir}/uvicorn --factory msm.user_api:create_app --reload {posargs}
+ {envbindir}/uvicorn --factory msm.user_api:create_app --host 0.0.0.0 --reload {posargs}
diff --git a/docker-compose.yaml b/docker-compose.yaml
new file mode 100644
index 0000000..05d795f
--- /dev/null
+++ b/docker-compose.yaml
@@ -0,0 +1,31 @@
+version: "3.9"
+services:
+ postgres:
+ image: postgres:14
+ env_file: .env.dev
+ ports:
+ - 5432:5432
+ volumes:
+ - postgres-data:/var/lib/postgresql/data
+ healthcheck:
+ test: ["CMD-SHELL", "pg_isready -U postgres"]
+ interval: 5s
+ timeout: 5s
+ retries: 5
+
+ backend:
+ env_file: .env.dev
+ depends_on:
+ postgres:
+ condition: service_healthy
+ build:
+ context: ./backend
+ volumes:
+ - ./backend:/app
+ ports:
+ - "8000:8000"
+ links:
+ - "postgres:db"
+
+volumes:
+ postgres-data:
Follow ups