FROM registry.yandex.net/tools/raw-ubuntu:20.04

ENV REQUESTS_CA_BUNDLE=/etc/ssl/certs/ca-certificates.crt
ENV PYTHONSTARTUP=python_startup.py
RUN locale-gen ru_RU.UTF-8 && update-locale LANG=ru_RU.UTF-8

EXPOSE 80

ADD https://crls.yandex.net/allCAs.pem /etc/
RUN chown www-data:www-data /etc/allCAs.pem

RUN apt-get update \
    && apt-get install -y \
                        python3-pip \
                        postgresql-client \
                        libpq-dev \
                        wget

RUN pip3 install pgcli

RUN mkdir -p ~/.postgresql && \
        wget "https://crls.yandex.net/allCAs.pem" -O ~/.postgresql/root.crt && \
        chmod 0600 ~/.postgresql/root.crt

RUN apt-get update \
    && apt-get install -y postgresql-client-12

COPY alembic.ini /
COPY hypercorn_conf.py /
COPY python_startup.py /

COPY /watcher/asgi/watcher.asgi /watcher
COPY /watcher/alembic/watcher.alembic /watcher-db

COPY /entrypoints/shell.sh /usr/local/bin/shell
COPY /entrypoints/dbshell.sh /usr/local/bin/dbshell
COPY /entrypoints/bot.sh /usr/local/bin/watcher_bot
COPY /entrypoints/celery.sh /usr/local/bin/watcher_celery

RUN chmod +x /usr/local/bin/shell
RUN chmod +x /usr/local/bin/dbshell
RUN chmod +x /usr/local/bin/watcher_celery
RUN chmod +x /usr/local/bin/watcher_bot

CMD case "$MODE" in \
        "worker") \
            watcher_celery worker -C -q -A watcher.celery_app --uid www-data -c "$WATCHER_CELERY_WORKERS" \
            ;; \
        "bot") \
            watcher_bot \
            ;; \
        "beat") \
            watcher_celery beat -C -q -A watcher.celery_app \
            ;; \
        *) \
            /watcher --config=file:/hypercorn_conf.py intranet.watcher.asgi.app:app; \
    esac
