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/.env.docker.sample b/src/_example/django/.env.docker.sample new file mode 100644 index 000000000..f9bd4b06a --- /dev/null +++ b/src/_example/django/.env.docker.sample @@ -0,0 +1,3 @@ +# forest environment settings +FOREST_ENV_SECRET= +FOREST_AUTH_SECRET= \ 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..eec9bc067 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 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: + +```bash +cd src/_example/django + +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 cleanup +``` + +⚠️ 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 + +### 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..a8c712f55 --- /dev/null +++ b/src/_example/django/docker-compose.yaml @@ -0,0 +1,31 @@ +# 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 + build: + context: ../../../ + dockerfile: src/_example/django/Dockerfile + args: + PYTHON_VERSION: 3.11 # 3.11 is default if not set + environment: + # 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" }