##### 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}
ENV PYPI_EXTRA_INDEX_URL=https://artifacts.apollo.stream/repository/pypi-all/simple

# 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} \
    --extra-index-url ${PYPI_EXTRA_INDEX_URL} pip \
    && mkdir atlas-um

WORKDIR atlas-um

##### 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
# and create virtualenv to house Python run-time dependencies
RUN DEBIAN_FRONTEND=noninteractive \
    apt-get install -y --no-install-recommends \
    build-essential \
    libpcre3-dev \
    && python -m venv /opt/venv

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

# Pip requirements generated in requirements stage
COPY /requirements/ /opt/requirements

# Pip is assumed to be out-of-date
# and install wheel into venv so it will be available when building
RUN pip install --upgrade --no-cache-dir --index-url ${PYPI_INDEX_URL} \
    --extra-index-url ${PYPI_EXTRA_INDEX_URL} \
    pip wheel \
    && pip install --no-cache-dir --index-url ${PYPI_INDEX_URL} \
    --extra-index-url ${PYPI_EXTRA_INDEX_URL} \
    -r /opt/requirements/base.txt

##### Frontend stage #####

# Converting all raw frontend assets to their final state

FROM 483193324480.dkr.ecr.us-east-1.amazonaws.com/node:16.13.2-alpine3.15 AS frontend

RUN npm install -g gulp-cli

COPY assets /assets

RUN cd /assets/ && npm i \
    && cd node_modules/fomantic-ui \
    && npx gulp install \
    && npx gulp build

##### 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

# Install OS run deps
RUN DEBIAN_FRONTEND=noninteractive \
    apt-get install -y --no-install-recommends \
    libpcre3

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

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

# Set up env vars needed at app run-time
ENV AUTH0_CLIENT_ID AUTH0_CLIENT_SECRET AUTH0_DOMAIN


##### 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

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

ENV DD_SERVICE=atlas-um
ENV DD_VERSION=${VERSION}
ENV DD_CALL_BASIC_CONFIG=0

# Configure uWSGI
COPY 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

# Pip dev requirements generated in requirements stage
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} \
    --extra-index-url ${PYPI_EXTRA_INDEX_URL} \
    -r /opt/requirements/dev.txt

# Add test CA cert
COPY test-config test-config
RUN python ./test-config/add_test_ca_cert.py ./test-config/mockserver-ca-cert.pem

# Copy in source and database migrations
COPY atlas_um atlas_um
COPY --from=frontend /atlas_um/static/semantic atlas_um/static/semantic

# Copy in test directories
COPY features features
COPY tests tests
COPY .coveragerc ./

##### 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 atlas_um atlas_um
COPY --from=frontend /atlas_um/static/semantic atlas_um/static/semantic
RUN cd atlas_um && flask digest compile
