FROM 475275892927.dkr.ecr.us-east-1.amazonaws.com/python-builder:3.7-aws2-0.1.0 as builder
ARG PACKAGE
#Add this in case you use inheritance in makefiles
#COPY ./Makefile.inc /Makefile.inc
COPY . /${PACKAGE}
WORKDIR /${PACKAGE}
USER ${ROOT_USER}
RUN chown -R ${APP_USER}:${APP_GROUP} /${PACKAGE}
USER ${APP_USER}
RUN make build && \
    make upload && \
    ./venv/bin/pip3 wheel -r requirements/base.pip --wheel-dir ./dist

FROM 475275892927.dkr.ecr.us-east-1.amazonaws.com/python:3.7-aws2-2.0.0
ARG PACKAGE
ARG APP

ARG REVISION
ARG BUILDTIME
LABEL REVISION=${REVISION}
LABEL BUILDTIME=${BUILDTIME}

COPY --from=builder /${PACKAGE}/pip.conf /app/pip.conf
COPY --from=builder /${PACKAGE}/.pypirc /app/.pypirc
COPY --from=builder /${PACKAGE}/dist/ /app/dist
USER ${ROOT_USER}
RUN chown -R ${APP_USER}:${APP_GROUP} /app
USER ${APP_USER}
WORKDIR /app

ENV PACKAGE=${PACKAGE}

RUN python3 -m venv venv && \
    source ./venv/bin/activate && \
        cp pip.conf ./venv/ && \
        pip3 install -U pip && \
        pip3 install ${PACKAGE} --no-index -f /app/dist && \
        rm -rf /app/dist && \
        echo $' \
        set -euxo pipefail \n \
        env \n \
        exec ./venv/bin/backfill $@ \n' >> /app/entrypoint.sh

ENTRYPOINT ["/bin/sh", "/app/entrypoint.sh"]
