#FROM python:3.7.3-alpine
FROM python:3.7.3-slim-stretch

# The tool to run both "gunicorn" and "Celery" would be "supervisor". An example in:
# https://github.com/pm990320/docker-flask-celery
#
# 1) Build:
# docker build -t magicnexush2020/magic-nis-backend .
#
# 1.bis) REFRESH image into Docker Hub:
# ("docker login" first)
# docker push magicnexush2020/magic-nis-backend:<TAGNAME>
#
# Usage example:
# 2) Create (using a configuration file)
# docker create --name nis-local -p 8080:80 -e MAGIC_NIS_SERVICE_CONFIG_FILE="nis_docker_naples.conf" magicnexush2020/magic-nis-backend:latest
# 2) Create (using autogenerated configuration). NO NEED FOR STEP 3.
# docker create --name nis-local -p 8080:80 magicnexush2020/magic-nis-backend:latest
# 2) Create, run and remove on exit, using autogenerated configuration (NO NEED FOR STEPS 3, 4)
# docker run --rm -p 8085:80 magicnexush2020/magic-nis-backend:latest
#
# 3) docker cp nis_docker_naples.conf /app/nexinfosys/restful_service/nis_docker_naples.conf
#
# 4) docker start nis-local && docker logs nis-local -f
#
#
# LOCAL SERVER: (the configuration uses REDISLITE, so no need to have a REDIS instance) the image would be:
#
# docker create --name nis-local -p 8080:80
#               -v /home/rnebot/DATOS/docker/nis-local:/srv
#               -e MAGIC_NIS_SERVICE_CONFIG_FILE="nis_docker_local_sqlite.conf" magic-nis:latest
# docker cp nis_docker_local_sqlite.conf /app/backend/restful_service/nis_docker_local_sqlite.conf
#
#
# NOTE: in the example, the host directory (/home/rnebot/DATOS/docker/nis-local) must have RWX permissions
#       for all users: chmod rwx+o ...
#       If not, it may not be possible to create
#
# PRODUCTION SERVER (MAGIC server provided by UniNa -Naples-):
#
# docker create --name nis-local --network=magic-net -l magic-postgis -l magic-redis -v /srv/docker/magic/data/nis:/srv
#   -e VIRTUAL_HOST=one.nis.magic-nexus.eu -e VIRTUAL_PORT=80 -e LETSENCRYPT_HOST=one.nis.magic-nexus.eu
#   -e LETSENCRYPT_EMAIL=<email address> -e MAGIC_NIS_SERVICE_CONFIG_FILE="nis_docker_naples.conf"
#   -e MOD_WSGI_REQUEST_TIMEOUT=1500 -e MOD_WSGI_SOCKET_TIMEOUT=1500
#   magic-nis:latest
#
# docker cp nis_docker_naples.conf /app/backend/restful_service/nis_docker_naples.conf
#
#

# ALPINE
#RUN echo "http://dl-cdn.alpinelinux.org/alpine/latest-stable/main" > /etc/apk/repositories
#RUN echo "http://dl-cdn.alpinelinux.org/alpine/latest-stable/community" >> /etc/apk/repositories
##redis gfortran
#RUN apk update
#RUN apk add --no-cache --virtual=.build-dependencies \
#    # lxml
#    libxml2-dev libxslt-dev \
#    lapack \
##    openblas-dev \
#    openblas musl-dev \
##    libpng-dev \
#    git \
##    freetype freetype-dev \
##    build-base \
#    libstdc++ \
#    libffi-dev \
#    postgresql-dev


# NORMAL
RUN apt-get update && \
    apt-get -y install \
    liblapack3  \
    libblas3  \
    gcc \
    git \
    curl \
    build-essential \
    libpq-dev \
	libcurl4-openssl-dev \
	libssl-dev \
	mime-support \
	&& apt-get clean


# COMMON

RUN pip3 install --no-cache-dir --upgrade pip && \
    pip3 install --no-cache-dir git+https://github.com/Supervisor/supervisor gunicorn psycopg2==2.7.3.2 redislite==5.0.124623

WORKDIR /app

ENV MAGIC_NIS_SERVICE_CONFIG_FILE=""

# NOTE: "requirements.txt" can be generated from scratch with "pipreqs --force ."
COPY requirements.txt /app

RUN pip3 install --no-cache-dir -r requirements.txt

COPY supervisord.conf /etc/supervisord.conf

RUN mkdir -p /srv

EXPOSE 80
VOLUME /srv

# needs to be set else Celery gives an error (because docker runs commands inside container as root)
ENV C_FORCE_ROOT=1

# gunicorn --workers=1 --log-level=debug --timeout=2000 --bind 0.0.0.0:80 nexinfosys.restful_service.service_main:app
CMD ["/usr/local/bin/gunicorn", "--workers=3", "--log-level=debug", "--timeout=2000", "--bind", "0.0.0.0:80", "nexinfosys.restful_service.service_main:app"]
# Run supervisord
#CMD ["supervisord", "-c", "/etc/supervisord.conf"]

COPY nexinfosys /app/nexinfosys
RUN cythonize -a -i /app/nexinfosys/restful_service/helper_accel.pyx && \
    cythonize -a -i /app/nexinfosys/command_generators/parser_spreadsheet_utils_accel.pyx

