FROM python:3.9.18-slim-bullseye

# based on https://github.com/glic3rinu/django-orchestra/blob/master/INSTALL.md
# HOW TO RUN THIS
#
# 0. rsync -avhP root@109.69.8.140:/opt/orchestra orchestra
# 1. copy this Dockerfile into a dir containing orchestra, like:
#     $ ls
#     Dockerfile  orchestra
# 2. docker build -f orchestra .
# 3. docker rm orchestra; docker run -v /Users/maymerichgubern/orchestra/orchestra/:/opt/orchestra -p 8443:443 -p 8080:80 -it --name orchestra orchestra bash
# 4. sed -i "s/ALLOWED_HOSTS = .*/ALLOWED_HOSTS = ('orchestra.pangea.org', 'localhost')/" panel/settings.py
# 5. service postgresql start; service uwsgi start; service nginx start
# 6. goto https://localhost:8443/admin/

#RUN apt-get update -y && apt-get upgrade -y

RUN apt update &&  apt-get install --no-install-recommends -y \
    postgresql \
    cron \
    nginx-full \
    sudo \
    ca-certificates \
    gettext \
    bind9utils \
    wget \
    expect \
    wkhtmltopdf \
    procps \
    net-tools \
    ssh \
    wkhtmltopdf \
    xvfb \
    zlib1g-dev \
    libcrack2-dev \
    libxml2-dev \
    libxslt1-dev \
    && rm -rf /var/lib/apt/lists/*

# reduce size (python specifics) -> src https://stackoverflow.com/questions/74616667/removing-pip-cache-after-installing-dependencies-in-docker-image
ENV PYTHONDONTWRITEBYTECODE=1
# here document in dockerfile src https://stackoverflow.com/questions/40359282/launch-a-cat-command-unix-into-dockerfile
RUN cat > /etc/pip.conf <<END
[install]
compile = no

[global]
no-cache-dir = True
END

RUN pip install --upgrade pip

# using binaries I avoid installing c compiler
#   pyuwsgi -> src https://discuss.python.org/t/how-to-install-uwsgi-package-from-pypi/25082/5
#   setuptools before installing requirements and the anyjson python package -> src https://stackoverflow.com/questions/72414481/error-in-anyjson-setup-command-use-2to3-is-invalid/74272722#74272722
RUN pip install \
    "setuptools<58.0.0" \
    psycopg2-binary \
    pyuwsgi

# Clean up to reduce image size
RUN apt clean && rm -rf /var/lib/apt/lists/*

# celery pinned because big changes on periodic_tasks related API
#   celery3.1 wont support > python3.9 (dependency with celery should be removed), celery3.1 won't support django4.0
#   orchestra initially was using celery to run async tasks, but then switched to process/threads without MQ... but the dependency with celery was never fully removed :(
# django-iban wont support > 4.0 (django-iban is deprecated, replace by django-localflavor)
# django 3 cannot work https://stackoverflow.com/questions/59261254/no-module-named-django-contrib-staticfiles-templatetags
# RUN pip3 install \
#     django==3.2.18 \
#     django-fluent-dashboard \
#     django-admin-tools \
#     django-extensions \
#     celery==3.1.23 \
#     django-celery==3.3.1 \
#     django-cors-headers \
#     Markdown \
#     djangorestframework \
#     ecdsa \
#     Pygments \
#     django-filter \
#     jsonfield \
#     python-dateutil \
#     https://github.com/glic3rinu/passlib/archive/master.zip \
#     django-iban \
#     requests \
#     phonenumbers \
#     django-countries \
#     django-localflavor \
#     amqp \
#     pytz \
#     cracklib \
#     lxml

# this is to ensure django project is created on top of this working directory
WORKDIR /opt/orchestra/

# TODO maybe from here goes to docker entrypoint?
# TODO assumes that the project already exists, and in some cases that would be interesting
COPY django-orchestra/requirements.txt .

# TODO fix this better in the repo itself
RUN pip install -r requirements.txt
RUN pip install lxml==4.9.3

COPY django-orchestra .
#RUN sed -i 's/lxml==3.3.5/lxml==4.9.3/' requirements.txt
# solves "No module named 'orchestra'"
RUN pip install -e .

RUN django-admin startproject panel --template=/opt/orchestra/orchestra/conf/project_template/

RUN adduser orchestra \
    && sudo adduser orchestra sudo \
    && su - orchestra

ENV PATH=$PATH:/opt/orchestra/django-orchestra/orchestra/bin

WORKDIR /opt/orchestra/panel
COPY docker/orchestra.migrate.exp /opt/orchestra/panel

COPY docker/orchestra.entrypoint.sh .
ENTRYPOINT sh ./orchestra.entrypoint.sh

# RUN expect -f ./orchestra.migrate.exp
# 
# #RUN sed -i "s/'HOST': '',/'HOST': '*',/" panel/settings.py
# RUN sed -i "s/^ALLOWED_HOSTS = \[\]/ALLOWED_HOSTS = \['*'\]/" panel/settings.py
# CMD ./manage.py runserver 0.0.0.0:9080
# EXPOSE 9080:9080

#RUN echo /opt/orchestra/django-orchestra/ > /usr/local/lib/python3.9/dist-packages/orchestra.pth

# TODO move this to entrypoint, with fakedata
#    && su postgres bash -c 'psql -f <(zcat /opt/orchestra/orchestra_db_20230907.sql)' \

#RUN service postgresql start \
#    && python3 manage.py setupnginx --user orchestra \
#    && su orchestra bash -c 'python3 manage.py setupcronbeat' \
#    && su orchestra bash -c 'python3 manage.py syncperiodictasks'