##### base stage #####

# Massage python image into the shape that we’ll need for most later
# stages.

FROM 483193324480.dkr.ecr.us-east-1.amazonaws.com/python:3.9-slim AS base

# Allow Jenkins to override with internal mirror
ARG PYPI_INDEX_URL=https://pypi.org/simple
ENV PYPI_INDEX_URL=${PYPI_INDEX_URL}

# We need to recreate the /usr/share/man/man{1..8} directories first
# because they were clobbered by a parent image
# Update apt-get cache
RUN seq 1 8 | xargs -I{} mkdir -p /usr/share/man/man{} \
    && sed -i 's|http://deb.debian.org/debian|http://archive.debian.org/debian|g' /etc/apt/sources.list \
    && sed -i 's|http://security.debian.org/debian-security|http://archive.debian.org/debian-security|g' /etc/apt/sources.list \
    && sed -i '/buster-updates/d' /etc/apt/sources.list \
    && printf 'Acquire::Check-Valid-Until "false";\nAcquire::AllowInsecureRepositories "true";\n' > /etc/apt/apt.conf.d/99buster-eol \
    && apt-get update --fix-missing \
    && pip install --upgrade --no-cache-dir --index-url ${PYPI_INDEX_URL} \
    pip

WORKDIR /atlas-auth-proxy


##### virtualenv stage #####

# Generate a Python virtual env to be used in latter stages. Latter
# stages are expected to copy /opt/venv, possibly adding to it. Latter
# stages are _not_ expected to need OS build deps.

FROM base AS virtualenv

# Install OS build deps
RUN DEBIAN_FRONTEND=noninteractive \
    apt-get install -y --no-install-recommends \
    build-essential \
    # Create virtualenv to house Python run-time dependencies
    && python -m venv /opt/venv

# Make sure we use the virtualenv
ENV PATH="/opt/venv/bin:$PATH"

COPY requirements/ /opt/requirements/

# Pip is assumed to be out-of-date
# Install only PyPI packages needed for deployment (which are assumed
# to also be needed for testing)
RUN pip install --upgrade --no-cache-dir --index-url ${PYPI_INDEX_URL} \
    pip setuptools wheel \
    && pip install --no-cache-dir --index-url ${PYPI_INDEX_URL} \
    -r /opt/requirements/base.txt


##### ready stage #####

# Latter stages are expected to start from this stage. This stage is
# expected to include everything necessary to execute the app directly
# from the command line _without_ any of the expected deployment
# technologies.

FROM base AS ready

# Virtualenv generated in virtualenv stage
COPY --from=virtualenv /opt/venv /opt/venv

# Make sure we use the virtualenv
ENV PATH="/opt/venv/bin:$PATH"


##### deploy-base stage #####

# This stage is expected to include all necessary binaries and
# configuration for a deployable image, _except_ the source tree,
# which is added in the terminal deploy stage to make better use of
# Docker’s layer cache.

FROM ready as deploy-base

ARG VERSION
ARG REVISION
ARG BUILDTIME

LABEL com.datadoghq.tags.service=atlas-auth-proxy
LABEL com.datadoghq.tags.version=${VERSION}

ENV DD_SERVICE=atlas-auth-proxy
ENV DD_VERSION=${VERSION}

# Configure uWSGI
COPY config/uwsgi.ini /etc/uwsgi/

# App will listen on this port
EXPOSE 8000

# run uwsgi
CMD ["/opt/venv/bin/ddtrace-run", "/opt/venv/bin/uwsgi", "--ini", "/etc/uwsgi/uwsgi.ini"]


##### test stage #####

# Configure and run unit tests, and set up everything necessary in
# this image for running multi-container test suites. It is _not_
# expected that latter stages will start from this stage. This image
# assumes an available Postgres database to use, and will attempt to
# migrate that database. *Never configure this image to use a live
# database, as it _will be wiped_.* Test suites depending on
# additional containers should instead be configured to use this stage
# in Docker Compose.

FROM ready AS test

COPY requirements/ /opt/requirements/

# Install only PyPI packages needed for testing in existing venv
RUN pip install --no-cache-dir --index-url $PYPI_INDEX_URL \
    -r /opt/requirements/dev.txt

COPY auth_proxy auth_proxy

# Copy in test directories
COPY tests tests
COPY .coveragerc ./
COPY pyproject.toml ./

# Run tests that don’t depend on other containers or services
CMD ["pytest"]


##### deploy stage #####

# This is expected to be the terminal stage, and the stage that
# provides a deployment image.

FROM deploy-base AS deploy

# Copy in source
COPY auth_proxy auth_proxy
