diff --git a/bin/deploy_to_balena.sh b/bin/deploy_to_balena.sh index ee25f990d..191726881 100755 --- a/bin/deploy_to_balena.sh +++ b/bin/deploy_to_balena.sh @@ -88,21 +88,6 @@ function prepare_balena_file() { envsubst > balena-deploy/docker-compose.yml } -function initialize_python_environment() { - ./bin/install_poetry.sh - - # Add `pyenv` to the load path. - export PYENV_ROOT="$HOME/.pyenv" - [[ -d $PYENV_ROOT/bin ]] && \ - export PATH="$PYENV_ROOT/bin:$PATH" - eval "$(pyenv init -)" - - # Add `poetry to the load path. - export PATH="$HOME/.local/bin:$PATH" - - poetry install --only=docker-image-builder -} - if ! balena whoami; then echo "Please login to Balena with `balena login` command, then run this script again." exit 0 @@ -117,11 +102,9 @@ if [[ -z "${DEV_MODE+x}" ]]; then else echo "Running in dev mode..." - initialize_python_environment - poetry run python tools/image_builder \ - --dockerfiles-only \ - --disable-cache-mounts \ - --build-target=${BOARD} + ENVIRONMENT="production" \ + BUILD_TARGET="$BOARD" \ + bin/generate_dev_mode_dockerfiles.sh cat docker-compose.balena.dev.yml.tmpl | \ envsubst > docker-compose.yml diff --git a/bin/generate_dev_mode_dockerfiles.sh b/bin/generate_dev_mode_dockerfiles.sh new file mode 100755 index 000000000..289b7f8da --- /dev/null +++ b/bin/generate_dev_mode_dockerfiles.sh @@ -0,0 +1,26 @@ +#!/bin/bash + +set -euo pipefail + +BUILDER_DOCKERFILE='docker/Dockerfile.dev' +BUILDER_IMAGE_NAME='anthias-dockerfile-image-builder' +BUILDER_CONTAINER_NAME="${BUILDER_IMAGE_NAME}-instance" +BUILD_TARGET="${BUILD_TARGET:-x86}" +ENVIRONMENT="${ENVIRONMENT:-development}" + +docker build \ + --pull \ + -f "$BUILDER_DOCKERFILE" \ + -t "$BUILDER_IMAGE_NAME" . + +docker rm -f "$BUILDER_CONTAINER_NAME" || true +docker run \ + --rm \ + --name="$BUILDER_CONTAINER_NAME" \ + -v "$(pwd):/app" \ + "$BUILDER_IMAGE_NAME" \ + poetry run python -m tools.image_builder \ + --environment="$ENVIRONMENT" \ + --dockerfiles-only \ + --disable-cache-mounts \ + --build-target="$BUILD_TARGET" diff --git a/bin/install_poetry.sh b/bin/install_poetry.sh deleted file mode 100755 index f5a9323a1..000000000 --- a/bin/install_poetry.sh +++ /dev/null @@ -1,48 +0,0 @@ -#!/bin/bash - -set -euo pipefail - -export DEBIAN_FRONTEND=noninteractive - -function install_apt_packages() { - sudo apt-get install -y \ - apt-utils \ - curl \ - dialog \ - git \ - libssl-dev -} - -function install_python() { - sudo -E apt-get install -y \ - python3 \ - python3-pip \ - python-is-python3 -} - -function install_pyenv { - if [ ! -d "$HOME/.pyenv" ]; then - curl https://pyenv.run | bash - fi - - export PYENV_ROOT="$HOME/.pyenv" - [[ -d $PYENV_ROOT/bin ]] && \ - export PATH="$PYENV_ROOT/bin:$PATH" - eval "$(pyenv init -)" -} - -function install_python3_11 { - pyenv install --skip-existing 3.11 - pyenv global 3.11 -} - -function install_poetry() { - curl -sSL https://install.python-poetry.org | python3 - -} - -sudo apt-get update -y -install_apt_packages -install_python -install_pyenv -install_python3_11 -install_poetry diff --git a/bin/start_development_server.sh b/bin/start_development_server.sh index 383c33abd..126f7eb2b 100755 --- a/bin/start_development_server.sh +++ b/bin/start_development_server.sh @@ -6,29 +6,7 @@ COMPOSE_ARGS=( '-f' 'docker-compose.dev.yml' ) -function initialize_python_environment() { - ./bin/install_poetry.sh +bin/generate_dev_mode_dockerfiles.sh - # Add `pyenv` to the load path. - export PYENV_ROOT="$HOME/.pyenv" - [[ -d $PYENV_ROOT/bin ]] && \ - export PATH="$PYENV_ROOT/bin:$PATH" - eval "$(pyenv init -)" - - # Add `poetry to the load path. - export PATH="$HOME/.local/bin:$PATH" - - poetry install --only=docker-image-builder -} - -function generate_dockerfiles() { - poetry run python tools/image_builder \ - --environment=development \ - --dockerfiles-only \ - --disable-cache-mounts -} - -initialize_python_environment -generate_dockerfiles docker compose "${COMPOSE_ARGS[@]}" down docker compose "${COMPOSE_ARGS[@]}" up -d --build diff --git a/docker/Dockerfile.dev b/docker/Dockerfile.dev new file mode 100644 index 000000000..2a3c12c66 --- /dev/null +++ b/docker/Dockerfile.dev @@ -0,0 +1,12 @@ +FROM python:3.11-bookworm + +RUN apt-get update -y && \ + curl -sSL https://install.python-poetry.org | python3 - && \ + ln -s /root/.local/bin/poetry /usr/local/bin/poetry + +WORKDIR /app + +COPY pyproject.toml poetry.lock /app/ + +RUN poetry install --only=docker-image-builder +RUN git config --global --add safe.directory /app diff --git a/docs/developer-documentation.md b/docs/developer-documentation.md index 12e2a2786..8c312450a 100644 --- a/docs/developer-documentation.md +++ b/docs/developer-documentation.md @@ -21,7 +21,8 @@ These components and their dependencies are mostly installed and handled with An To simplify development of the server module of Anthias, we've created a Docker container. This is intended to run on your local machine with the Anthias repository mounted as a volume. > [!IMPORTANT] -> Anthias is using Docker's [buildx](https://docs.docker.com/engine/reference/commandline/buildx/) for the image builds. This is used both for cross compilation as well as for local caching. You might need to run `docker buildx create --use` first. +> * Make sure that you have [installed Docker](https://docs.docker.com/engine/install/) on your machine before proceeding. +> * Anthias is using Docker's [buildx](https://docs.docker.com/engine/reference/commandline/buildx/) for the image builds. This is used both for cross compilation as well as for local caching. You might need to run `docker buildx create --use` first. Assuming you're in the source code repository, simply run: @@ -40,7 +41,15 @@ $ ./bin/start_development_server.sh ✔ Container anthias-anthias-nginx-1 Started 0.5s ``` -Running the command above will start the development server and you should be able to +> [!NOTE] +> Running the script will install Python 3.11, [pyenv](https://github.com/pyenv/pyenv), +> and [Poetry](https://python-poetry.org/) inside a Docker container on your machine. +> This is to ensure that the development environment is consistent across different +> machines. +> +> The script currently supports Debian-based systems and macOS. + +unning the command above will start the development server and you should be able to access the web interface at `http://localhost:8000`. To stop the development server, run the following: