# Dockerfile

ARG         AWS_ACCOUNT=086679231553

FROM        ${AWS_ACCOUNT}.dkr.ecr.us-east-1.amazonaws.com/docker-parent-images:python311 AS base

WORKDIR     /var/app

USER        root

# Create and configure virtualenv
ENV         VIRTUAL_ENV /var/venv
RUN         python3.11 -m venv ${VIRTUAL_ENV}
ENV         PATH "$VIRTUAL_ENV/bin:$PATH"

RUN         pip install --no-cache-dir --upgrade pip setuptools


########################################
### Build image for test-integration ###
########################################
FROM        base AS test-integration

COPY        requirements-int.txt .
RUN         pip install --no-cache-dir -I -r requirements-int.txt

COPY        tests/__init__.py ./tests/__init__.py
COPY        tests/integration ./tests/integration
RUN         touch tests/integration/.env
COPY        scripts/test-integration.sh ./scripts/test-integration.sh

USER        worker
ENTRYPOINT  [ "scripts/test-integration.sh" ]


##################################
### Build image for grass code ###
##################################
FROM        base AS grass

# Install requirements
COPY        requirements.txt .
RUN         pip install --no-cache-dir -I -r requirements.txt


##############################
### Build image for deploy ###
##############################
FROM        grass AS deploy

USER        root

RUN         apt-get update && apt-get -y install haproxy \
            && apt-get -y clean && rm -rf /var/lib/apt/lists/*
RUN         pip install --no-cache-dir supervisor==4.3.0

RUN         mkdir -p /var/lib/haproxy
RUN         chown haproxy:haproxy /var/lib/haproxy

# Copy files to run ows-grass
COPY        grass/ ./grass
COPY        deploy/ ./deploy
COPY        application.py .

ENTRYPOINT  ["/var/app/deploy/start.sh"]
EXPOSE      8080


#################################
### Build image for unit-lint ###
#################################
FROM        grass AS unit-lint

# Install dev/test requirements
COPY        requirements-dev.txt .
RUN         pip install --no-cache-dir -I -r requirements-dev.txt

# Copy files required to run linting and unit tests
COPY        tests/__init__.py ./tests/__init__.py
COPY        tests/unit ./tests/unit
COPY        scripts/unit-lint.sh ./scripts/unit-lint.sh
COPY        ruff.toml .

# Copy files to run ows-grass
COPY        grass/ ./grass
COPY        deploy/ ./deploy
COPY        application.py .
USER        worker

ENTRYPOINT  ["scripts/unit-lint.sh"]
