# ------------------------------------------------------------------------------
# Base
FROM ubuntu:18.04 as base

# /etc/localtime has to be removed due to a bug
# https://bugs.launchpad.net/ubuntu/+source/tzdata/+bug/1554806

RUN export DEBIAN_FRONTEND=noninteractive \
    && apt-get update -qq \
    && apt-get install -y -qq --no-upgrade --no-install-recommends locales localepurge tzdata gnupg \
    && echo "Europe/Moscow" > /etc/timezone \
    && rm /etc/localtime \
    && dpkg-reconfigure -f noninteractive tzdata \
    && locale-gen --purge en_US.UTF-8 \
    && apt-get clean \
    && rm -rf /var/lib/apt/lists/* \
    && echo "MANDELETE\nDONTBOTHERNEWLOCALE\nSHOWFREEDSPACE\nVERBOSE\nen_US\nen_US.UTF-8" > /etc/locale.nopurge \
    && localepurge

ENV DEBIAN_FRONTEND=noninteractive \
    LANG='en_US.UTF-8' \
    LANGUAGE='en_US:en' \
    LC_ALL='en_US.UTF-8' \
    PYTHONUNBUFFERED=1 \
    PYTHONDONTWRITEBYTECODE=1 \
    APT_KEY_DONT_WARN_ON_DANGEROUS_USAGE=1 \
    PIP_DISABLE_PIP_VERSION_CHECK=true \
    PIP_INDEX_URL=https://pypi.yandex-team.ru/simple/

ADD etc/deps/sources.list /etc/apt/sources.list
ADD http://dist.yandex.ru/yandexrepo/GPG-KEY-yandex /
RUN apt-key add GPG-KEY-yandex

# System: common
COPY etc/deps/system-common.txt .
RUN apt-get update -qq \
    && cat system-common.txt | xargs apt-get install -y -qq --no-upgrade --no-install-recommends \
    && apt-get clean \
    && rm -rf /var/lib/apt/lists/* \
    && rm system-common.txt

# ------------------------------------------------------------------------------
# Build environment
FROM base as builder

# System: build requirements
COPY etc/deps/system-build.txt .
RUN apt-get update -qq \
    && cat system-build.txt | xargs apt-get install -y -qq --no-upgrade --no-install-recommends \
    && apt-get clean \
    && rm -rf /var/lib/apt/lists/* \
    && rm system-build.txt

COPY etc/deps/python.txt ./
RUN python3 -m venv /py3 \
    && /py3/bin/pip install --no-cache-dir pip==18.0 setuptools==39.2.0 wheel==0.31.1 \
    && /py3/bin/pip install --no-cache-dir --require-hashes -r python.txt

# ------------------------------------------------------------------------------
# Application
FROM base as release

RUN mkdir /procu
WORKDIR /procu

# Prepare directory for uwsgi pidfile
RUN mkdir -p /var/run/yandex/procu-api/ \
    && chown -R www-data: /var/run/yandex/procu-api/

# Allows to run `procu` from `docker run`
ENV PATH /py3/bin/:$PATH

# System: app specific
COPY etc/deps/system-app.txt .
RUN apt-get update -qq \
    && cat system-app.txt | xargs apt-get install -y -qq --no-upgrade --no-install-recommends \
    && apt-get clean \
    && rm -rf /var/lib/apt/lists/* \
    && rm system-app.txt

# Python dependencies
RUN pip install --no-cache-dir wheel \
    && pip install --no-cache-dir supervisor \
    && echo "source /py3/bin/activate" >> /root/.bashrc
COPY --from=builder /root/.bashrc /root/.bashrc
COPY --from=builder /py3/ /py3/

# Copy configurations
COPY etc/conf/ ./conf/

# Copy & install procu with tests
COPY setup.py pytest.ini ./
COPY procu/ ./procu
RUN /py3/bin/pip install -e .

COPY bin/ ./bin/
ENTRYPOINT ["/procu/bin/entrypoint.sh"]

EXPOSE 80
