# Use an official Python runtime as a parent image
FROM registry.yandex.net/ubuntu:xenial
MAINTAINER Aleksei Petrov <petrovalex@yandex-team.ru>

# environment variables
ENV LOGDIR /var/log/jafar
ENV WORKDIR /jafar
ENV PIP_DEFAULT_TIMEOUT 120

# restrict parallel BLAS
ENV OPENBLAS_NUM_THREADS 1
ENV MKL_NUM_THREADS 1

# Set the working directory to /jafar
WORKDIR $WORKDIR

# folders and permissions
RUN mkdir -p $LOGDIR
RUN mkdir -p /var/lib/jafar/
RUN find $LOGDIR -type d -exec chmod 775 {} \;
RUN find $LOGDIR -type f -exec chmod 664 {} \;

# dependencies
# debian packages
COPY src/deb_requirements.txt deb_requirements.txt
RUN apt-get update && apt-get install -y --allow-change-held-packages --allow-downgrades $(cat deb_requirements.txt)

RUN apt-get install python-pip -y --allow-change-held-packages --allow-downgrades && pip install --upgrade pip==20.0.2
RUN pip install setuptools

# python packages
COPY src/requirements.txt requirements.txt
RUN pip install -r requirements.txt
RUN pip install supervisor

# configs
COPY deploy/supervisord-api.conf /etc/supervisord.conf
COPY deploy/gunicorn /etc/gunicorn
COPY deploy/haproxy /etc/haproxy

# Copy the src directory contents into the container at /jafar
COPY src .

# other init stuff
RUN python setup.py build_ext --inplace

# cleanup
RUN apt-get purge make g++ -y --allow-change-held-packages --allow-downgrades && apt-get autoremove -y --allow-change-held-packages --allow-downgrades

# run tests
RUN YENV_TYPE=testing python setup.py test 2>&1

# start gunicorn
CMD ["/usr/local/bin/supervisord", "-c", "/etc/supervisord.conf"]
