From d5d43ff880698cdc704e81f6041c127c0096fb71 Mon Sep 17 00:00:00 2001 From: Julien Barreau Date: Wed, 11 Dec 2024 16:50:49 +0100 Subject: [PATCH 1/5] chore: add docker running environment for django example --- .dockerignore | 96 +++++++++++++++++++ src/_example/django/Dockerfile | 50 ++++++++++ src/_example/django/README.md | 41 ++++++-- .../django_demo/.forestadmin-schema.json | 4 +- src/_example/django/docker-compose.yaml | 36 +++++++ src/_example/django/docker-entrypoint.sh | 25 +++++ src/_example/django/pyproject.toml | 2 +- 7 files changed, 241 insertions(+), 13 deletions(-) create mode 100644 .dockerignore create mode 100644 src/_example/django/Dockerfile create mode 100644 src/_example/django/docker-compose.yaml create mode 100755 src/_example/django/docker-entrypoint.sh diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 000000000..ba63fad8a --- /dev/null +++ b/.dockerignore @@ -0,0 +1,96 @@ +# Include any files or directories that you don't want to be copied to your +# container here (e.g., local build artifacts, temporary files, etc.). +# +# For more help, visit the .dockerignore file reference guide at +# https://docs.docker.com/engine/reference/builder/#dockerignore-file + +**/.DS_Store +**/__pycache__ +**/.venv +**/.classpath +**/.dockerignore +**/.env +**/.git +**/.gitignore +**/.project +**/.settings +**/.toolstarget +**/.vs +**/.vscode +**/*.*proj.user +**/*.dbmdl +**/*.jfm +**/bin +**/charts +**/docker-compose* +**/compose* +**/Dockerfile* +**/node_modules +**/npm-debug.log +**/obj +**/secrets.dev.yaml +**/values.dev.yaml +LICENSE +README.md + +**/node_modules +**/.env +**/.venv +**/env/ +**/venv/ +**/ENV/ +**/env.bak/ +**/venv.bak/ +**/venv*/ +**/venv* +**/venv** +**/src/tmp_venv/ +**/virtualenv/ +**/skillcorner_venv/ +**/__pypackages__/ +__pycache__/ + +*.py[cod] +*$py.class +*.sql +*.so + +.Python +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +share/python-wheels/ +*.egg-info/ +.installed.cfg +*.egg +MANIFEST + +htmlcov/ +.tox/ +.nox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*.cover +*.py,cover +.hypothesis/ +.pytest_cache/ +cover/ + + +instance/ +.webassets-cache + + +src/_example/**.sql* \ No newline at end of file diff --git a/src/_example/django/Dockerfile b/src/_example/django/Dockerfile new file mode 100644 index 000000000..f37debace --- /dev/null +++ b/src/_example/django/Dockerfile @@ -0,0 +1,50 @@ +# syntax=docker/dockerfile:1 + +# Comments are provided throughout this file to help you get started. +# If you need more help, visit the Dockerfile reference guide at +# https://docs.docker.com/engine/reference/builder/ + +ARG PYTHON_VERSION=3.11 +FROM python:${PYTHON_VERSION}-slim AS base + +# Prevents Python from writing pyc files. +ENV PYTHONDONTWRITEBYTECODE=1 + +# Keeps Python from buffering stdout and stderr to avoid situations where +# the application crashes without emitting any logs due to buffering. +ENV PYTHONUNBUFFERED=1 \ + POETRY_VIRTUALENVS_CREATE=false \ + POETRY_VIRTUALENVS_IN_PROJECT=false \ + POETRY_NO_INTERACTION=1 \ + POETRY_CACHE_DIR='/var/cache/pypoetry' \ + POETRY_HOME='/opt/poetry/' + +RUN \ + # install system requirement + apt update && \ + apt install -y iputils-ping curl && \ + # cleanup + apt clean + +# Copy the source code into the container. +# we're using a volume for that, but for initialization +COPY ./ /app/ + +WORKDIR /app/src/_example/django/ + +RUN curl -sSL https://install.python-poetry.org | python3 - ;\ + export PATH="/opt/poetry/bin:$PATH" + +RUN /opt/poetry/bin/poetry install --no-cache + +# for a bind mount on running repo +VOLUME /app + +# Expose the port that the application listens on. +EXPOSE 8000 + +# set entrypoint +ENTRYPOINT ["/app/src/_example/django/docker-entrypoint.sh"] + +# Run the application. +CMD /opt/poetry/bin/poetry run poe runserver diff --git a/src/_example/django/README.md b/src/_example/django/README.md index 9be1f0384..b147390d9 100644 --- a/src/_example/django/README.md +++ b/src/_example/django/README.md @@ -3,7 +3,28 @@ ## See django version lifecycle [https://endoflife.date/django](https://endoflife.date/django) -## Requirements +## Using docker + +First you need to update the [docker compose file](./docker-compose.yaml) to add your `FOREST_ENV_SECRET` and `FOREST_AUTH_SECRET` + +Normally with docker, you just have a few command to use, in the correct folder: + +```bash +cd src/_example/django + +docker compose up -d --build # to start in background +# Example project is now running on port 8000 + +docker compose logs -f # to see the output + +docker compose down # to stop +``` + +⚠️ If you already have a [`.env`](./.env) at the root of this example project, it will be used, overriding what's define in the [docker compose file](./docker-compose.yaml). + +## Setup a dev environment on your machine + +### Requirements To manage your demo's dependencies you should install [poetry](https://python-poetry.org/docs/). @@ -11,7 +32,7 @@ To manage your demo's dependencies you should install [poetry](https://python-po curl -sSL https://install.python-poetry.org | python3 - ``` -### Already met problems +#### Already met problems - If you're having SSL issue with Python while installing poetry, visit this link: - Make sure poetry is in your $PATH, sometimes on mac there is conflict between python installed graphically and installed with brew. You can use these commands to help you to find the problems @@ -23,48 +44,48 @@ which python3.10 echo "PATH=$PATH;~/Library/Application Support/pypoetry/venv/bin" >> ~/.zshrc # this is default installation path for poetry on mac ``` -## (Optional) Auto-load .env files with poetry +### (Optional) Auto-load .env files with poetry ```bash poetry self add poetry-dotenv-plugin ``` -## create a virtual env +### create a virtual env ```bash python -m venv venv # the last 'venv' is the relative path where you want your virtual env ``` -## activate it +### activate it ```bash source venv/bin/activate # adapt venv with the path previously used ``` -## Install the dependencies +### Install the dependencies ```bash poetry install --with dev ``` -## Init your database +### Init your database ```bash poetry run poe init-db ``` -## Populate your database with fake data +### Populate your database with fake data ```bash poetry run poe populate-db ``` -## Run the server +### Run the server ```bash poetry run poe runserver ``` -## Next step +### Next step The last needed step is to onboard the project on [forestadmin](https://www.forestadmin.com/) diff --git a/src/_example/django/django_demo/.forestadmin-schema.json b/src/_example/django/django_demo/.forestadmin-schema.json index 6503219e8..9f8a1e03d 100644 --- a/src/_example/django/django_demo/.forestadmin-schema.json +++ b/src/_example/django/django_demo/.forestadmin-schema.json @@ -3542,10 +3542,10 @@ ], "meta": { "liana": "agent-python", - "liana_version": "1.17.0", + "liana_version": "1.18.5", "stack": { "engine": "python", - "engine_version": "3.12.4" + "engine_version": "3.11.11" } } } \ No newline at end of file diff --git a/src/_example/django/docker-compose.yaml b/src/_example/django/docker-compose.yaml new file mode 100644 index 000000000..36b0b3b4b --- /dev/null +++ b/src/_example/django/docker-compose.yaml @@ -0,0 +1,36 @@ +# Comments are provided throughout this file to help you get started. +# If you need more help, visit the Docker compose reference guide at +# https://docs.docker.com/compose/compose-file/ + +# Here the instructions define your application as a service called "server". +# This service is built from the Dockerfile in the current directory. +# You can add other services your application may depend on here, such as a +# database or a cache. For examples, see the Awesome Compose repository: +# https://github.com/docker/awesome-compose +services: + agent-python_django-example: + container_name: agent-python_django-example + # image: agent-python_django-example + build: + context: ../../../ + dockerfile: src/_example/django/Dockerfile + args: + PYTHON_VERSION: 3.11 # 3.11 is default if not set + environment: + # forest environment related + FOREST_ENV_SECRET: ${FOREST_ENV_SECRET} + FOREST_AUTH_SECRET: ${FOREST_AUTH_SECRET} + FOREST_IS_PRODUCTION: "False" + # forest related + FOREST_VERIFY_SSL: "False" + FOREST_SERVER_URL: 'https://api.development.forestadmin.com' + # django related + DJANGO_SECRET_KEY: "^=k+h&r(f7d+#@3f)%h2xef!zvsn2f5_^ahuo*9v7k^6gk=*ey" + DJANGO_DEBUG: True + DJANGO_ALLOWED_HOSTS: "*" + volumes: + # root of repo at /app + - ../../../:/app + ports: + - 8000:8000 + diff --git a/src/_example/django/docker-entrypoint.sh b/src/_example/django/docker-entrypoint.sh new file mode 100755 index 000000000..d96c75301 --- /dev/null +++ b/src/_example/django/docker-entrypoint.sh @@ -0,0 +1,25 @@ +#!/bin/sh +# set -x + +# api.development.forestadmin.com as host machine +if ! grep -q api.development.forestadmin.com /etc/hosts +then + FOREST_BE=$(ping -c 1 host.docker.internal | awk 'NR==1 {print $3}' | cut -d \( -f2 | cut -d \) -f1) + echo "$FOREST_BE api.development.forestadmin.com" >> /etc/hosts +fi + +# install requirements +echo "updating requirements" +/opt/poetry/bin/poetry install --no-cache -q + +# init and seed db +if [ ! -f django_demo/db.sqlite3 ] +then + echo "creating and seeding databases" + /opt/poetry/bin/poetry run poe init-db + /opt/poetry/bin/poetry run poe populate-db +fi + +# set of command +# start long running process at the end that is passed from CMD +exec "$@" diff --git a/src/_example/django/pyproject.toml b/src/_example/django/pyproject.toml index ec8bf3be5..8b7d84dce 100644 --- a/src/_example/django/pyproject.toml +++ b/src/_example/django/pyproject.toml @@ -37,4 +37,4 @@ type = "simple" [tool.poe.tasks] init-db = { shell = "cd django_demo && python manage.py migrate" } populate-db = { shell = "cd django_demo && python manage.py populate-db && python manage.py sqlalchemy_init" } -runserver = { shell = "cd django_demo && python manage.py runserver" } +runserver = { shell = "cd django_demo && python manage.py runserver 0.0.0.0:8000" } From 057eb46de6b65d7dbb977f073e940e84832d29d2 Mon Sep 17 00:00:00 2001 From: Julien Barreau Date: Wed, 11 Dec 2024 16:52:30 +0100 Subject: [PATCH 2/5] chore: udpdate readme --- src/_example/django/README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/_example/django/README.md b/src/_example/django/README.md index b147390d9..14af88201 100644 --- a/src/_example/django/README.md +++ b/src/_example/django/README.md @@ -12,12 +12,12 @@ Normally with docker, you just have a few command to use, in the correct folder: ```bash cd src/_example/django -docker compose up -d --build # to start in background -# Example project is now running on port 8000 +docker compose up -d # to start in background +# Example project is now running on port 8000 with auto reload docker compose logs -f # to see the output -docker compose down # to stop +docker compose down # to cleanup ``` ⚠️ If you already have a [`.env`](./.env) at the root of this example project, it will be used, overriding what's define in the [docker compose file](./docker-compose.yaml). From b2f57b9bb64b5b15c8f79e55d7bc5317c2b4dc4b Mon Sep 17 00:00:00 2001 From: Julien Barreau Date: Wed, 11 Dec 2024 17:06:15 +0100 Subject: [PATCH 3/5] chore: remove useless comment --- src/_example/django/docker-compose.yaml | 1 - 1 file changed, 1 deletion(-) diff --git a/src/_example/django/docker-compose.yaml b/src/_example/django/docker-compose.yaml index 36b0b3b4b..86cdd5cd5 100644 --- a/src/_example/django/docker-compose.yaml +++ b/src/_example/django/docker-compose.yaml @@ -10,7 +10,6 @@ services: agent-python_django-example: container_name: agent-python_django-example - # image: agent-python_django-example build: context: ../../../ dockerfile: src/_example/django/Dockerfile From 371820f6e59270747a859494070fffa8fd59e5b0 Mon Sep 17 00:00:00 2001 From: Julien Barreau Date: Tue, 17 Dec 2024 15:03:16 +0100 Subject: [PATCH 4/5] chore: modify environment variable management --- src/_example/django/.env.docker.sample | 7 +++++++ src/_example/django/README.md | 4 ++-- src/_example/django/docker-compose.yaml | 5 +---- 3 files changed, 10 insertions(+), 6 deletions(-) create mode 100644 src/_example/django/.env.docker.sample diff --git a/src/_example/django/.env.docker.sample b/src/_example/django/.env.docker.sample new file mode 100644 index 000000000..fe10b8c3d --- /dev/null +++ b/src/_example/django/.env.docker.sample @@ -0,0 +1,7 @@ +# forest environment settings +FOREST_ENV_SECRET= +FOREST_AUTH_SECRET= +# forest settings +FOREST_IS_PRODUCTION=False +FOREST_VERIFY_SSL=False +FOREST_SERVER_URL=https://api.development.forestadmin.com \ No newline at end of file diff --git a/src/_example/django/README.md b/src/_example/django/README.md index 14af88201..eec9bc067 100644 --- a/src/_example/django/README.md +++ b/src/_example/django/README.md @@ -5,7 +5,7 @@ ## Using docker -First you need to update the [docker compose file](./docker-compose.yaml) to add your `FOREST_ENV_SECRET` and `FOREST_AUTH_SECRET` +First you need to create a `.env` file from [.env.docker.sample](./.env.docker.sample) file to add your `FOREST_ENV_SECRET` and `FOREST_AUTH_SECRET` Normally with docker, you just have a few command to use, in the correct folder: @@ -20,7 +20,7 @@ docker compose logs -f # to see the output docker compose down # to cleanup ``` -⚠️ If you already have a [`.env`](./.env) at the root of this example project, it will be used, overriding what's define in the [docker compose file](./docker-compose.yaml). +⚠️ If you already have a [`.env`](./.env) at the root of this example project, it will be used, overriding environment variables defined in the [docker-compose.yaml](./docker-compose.yaml) file. ## Setup a dev environment on your machine diff --git a/src/_example/django/docker-compose.yaml b/src/_example/django/docker-compose.yaml index 86cdd5cd5..c676963c6 100644 --- a/src/_example/django/docker-compose.yaml +++ b/src/_example/django/docker-compose.yaml @@ -16,11 +16,8 @@ services: args: PYTHON_VERSION: 3.11 # 3.11 is default if not set environment: - # forest environment related - FOREST_ENV_SECRET: ${FOREST_ENV_SECRET} - FOREST_AUTH_SECRET: ${FOREST_AUTH_SECRET} - FOREST_IS_PRODUCTION: "False" # forest related + FOREST_IS_PRODUCTION: "False" FOREST_VERIFY_SSL: "False" FOREST_SERVER_URL: 'https://api.development.forestadmin.com' # django related From ec57604d2802d2e44ca22859fa6e812fab28b6bd Mon Sep 17 00:00:00 2001 From: Julien Barreau Date: Tue, 17 Dec 2024 15:19:17 +0100 Subject: [PATCH 5/5] chore: change .env again --- src/_example/django/.env.docker.sample | 6 +----- src/_example/django/docker-compose.yaml | 1 - 2 files changed, 1 insertion(+), 6 deletions(-) diff --git a/src/_example/django/.env.docker.sample b/src/_example/django/.env.docker.sample index fe10b8c3d..f9bd4b06a 100644 --- a/src/_example/django/.env.docker.sample +++ b/src/_example/django/.env.docker.sample @@ -1,7 +1,3 @@ # forest environment settings FOREST_ENV_SECRET= -FOREST_AUTH_SECRET= -# forest settings -FOREST_IS_PRODUCTION=False -FOREST_VERIFY_SSL=False -FOREST_SERVER_URL=https://api.development.forestadmin.com \ No newline at end of file +FOREST_AUTH_SECRET= \ No newline at end of file diff --git a/src/_example/django/docker-compose.yaml b/src/_example/django/docker-compose.yaml index c676963c6..a8c712f55 100644 --- a/src/_example/django/docker-compose.yaml +++ b/src/_example/django/docker-compose.yaml @@ -17,7 +17,6 @@ services: PYTHON_VERSION: 3.11 # 3.11 is default if not set environment: # forest related - FOREST_IS_PRODUCTION: "False" FOREST_VERIFY_SSL: "False" FOREST_SERVER_URL: 'https://api.development.forestadmin.com' # django related