# This Dockerifle builds a simple Askbot installation
#
# It makes use of environment variables:
# 1. DATABASE_URL See https://github.com/kennethreitz/dj-database-url for details
# 2. SECRET_KEY for making hashes within Django.
# 3. ADMIN_PASSWORD used for creating a user named "admin"
# 4. NO_CRON set this to "yes" to disable the embedded cron job.
#
# Make sure to *+always* start the container with the same SECRET_KEY.
#
# Start with something like
#
# docker run -e 'DATABASE_URL=sqlite:////askbot_site/askbot.db' -e "SECRET_KEY=$(openssl rand 14 | base64)" -e ADMIN_PASSWORD=admin -p 8080:80 askbot/askbot:latest
#
# User uploads are stored in **/askbot_site/askbot/upfiles** . I'd recommend to make it a kubernetes volume.

FROM tiangolo/uwsgi-nginx:python3.6-alpine3.9

ARG SITE=askbot-site
ARG ASKBOT=.
ENV PYTHONUNBUFFERED 1
ENV ASKBOT_SITE /${SITE}

ENV UWSGI_INI /${SITE}/askbot_app/uwsgi.ini
# Not recognized by uwsgi-nginx, yet.
# The file doesn't exist either!
#ENV PRE_START_PATH /${SITE}/prestart.sh

# TODO: changing this requires another cache backend
ENV NGINX_WORKER_PROCESSES 4
ENV UWSGI_PROCESSES 4
ENV UWSGI_CHEAPER 0

ADD askbot_requirements.txt /

#RUN apt-get update && apt-get -y install cron git \
RUN apk add --update --no-cache git py3-cffi \
	gcc g++ git make unzip mkinitfs kmod mtools squashfs-tools py3-cffi \
	libffi-dev linux-headers musl-dev libc-dev openssl-dev \
	python3-dev zlib-dev libxml2-dev libxslt-dev jpeg-dev \
        postgresql-dev zlib jpeg libxml2 libxslt postgresql-libs \
        hiredis \
    && python -m pip install --upgrade pip \
    && pip install -r /askbot_requirements.txt \
    && pip install psycopg2 \
    && pip install redis django-redis-cache

RUN pip install requests-oauthlib

ADD $ASKBOT /src
RUN cd /src/ && python setup.py install \
    && askbot-setup -n /${SITE} --logfile-name=stdout --no-secret-key --create-project container-uwsgi \
        -e 1 -d postgres -u postgres -p askbotPW --db-host=postgres --db-port=5432 \
        --cache-engine redis --cache-node redis:6379 --cache-db 1

RUN true \
    && cp /${SITE}/askbot_app/prestart.sh /app \
    && /usr/bin/crontab /${SITE}/askbot_app/crontab \
    && cd /${SITE} && SECRET_KEY=whatever DJANGO_SETTINGS_MODULE=askbot_app.settings ASKBOT_CACHE=locmem python manage.py collectstatic --noinput

ADD https://github.com/ufoscout/docker-compose-wait/releases/download/2.5.0/wait /wait
RUN chmod +x /wait

WORKDIR /${SITE}
