FROM python:3.6-stretch
ENV PYTHONUNBUFFERED 1

LABEL maintainer="Video Core Services <video-coreservices@twitch.tv>"

ARG BUILD_DATE=now
ARG VCS_REF
ARG ALERTA_SERVER_VERSION
ARG ALERTA_UI_VERSION
ARG PIP_INDEX_URL

LABEL org.label-schema.build-date=$BUILD_DATE \
      org.label-schema.url="https://alerta.io" \
      org.label-schema.vcs-url="https://git-aws.internal.justin.tv/video-monitoring/docker-alerta" \
      org.label-schema.vcs-ref=$VCS_REF \
      org.label-schema.version=$ALERTA_SERVER_VERSION \
      org.label-schema.schema-version="1.0.0-rc.1"

# install necessary upstream packages
RUN apt-get update && \
    apt-get install -y --no-install-recommends \
    gettext-base \
    libffi-dev \
    libldap2-dev \
    libpq-dev \
    libsasl2-dev \
    libssl-dev \
    mongodb-clients \
    nginx-light \
    postgresql-client \
    python3-dev \
    supervisor \
    wget && \
    apt-get -y clean && \
    rm -rf /var/lib/apt/lists/*

# Make sure we're using artifactory
RUN export PIP_INDEX_URL=$PIP_INDEX_URL


# Install alerta pip requirements
COPY alerta-requirements.txt /app/alerta-requirements.txt
RUN pip install --no-cache-dir virtualenv && \
    virtualenv --python=python3 /venv && \
    /venv/bin/pip install -r /app/alerta-requirements.txt
ENV PATH $PATH:/venv/bin

# Install alerta and its plugins.
RUN /venv/bin/pip install alerta
RUN /venv/bin/pip install alerta-server==$ALERTA_SERVER_VERSION
COPY plugin-requirements.txt /app/plugin-requirements.txt
RUN /venv/bin/pip install -r /app/plugin-requirements.txt


# NOTE: we have not forked the web UI so we still have to install the tarball from Github.
ADD https://github.com/alerta/alerta-webui/releases/download/v${ALERTA_UI_VERSION}/alerta-webui.tar.gz /tmp/webui.tar.gz
RUN tar zxvf /tmp/webui.tar.gz -C /tmp && \
    mv /tmp/dist /web
COPY conf/config.json.template /web/config.json.template

# Copy scaffolding configs
COPY conf/wsgi.py /app/wsgi.py
COPY conf/uwsgi.ini /app/uwsgi.ini
COPY conf/nginx.conf /app/nginx.conf

RUN chgrp -R 0 /app /venv /web && \
    chmod -R g=u /app /venv /web && \
    useradd -u 1001 -g 0 alerta

RUN mkdir -p /home/alerta && chown alerta /home/alerta
USER 1001

# Set alerta env vars
ENV ALERTA_SVR_CONF_FILE /app/alertad.conf
ENV ALERTA_CONF_FILE /app/alerta.conf
ENV ALERTA_WEB_CONF_FILE /web/config.json
ENV BASE_URL /api
ENV HEARTBEAT_SEVERITY major

# Set alerta conf
COPY conf/alertad.conf /app/alertad.conf

# docker stuffs
COPY scripts/docker-entrypoint.sh /usr/local/bin/
COPY conf/supervisord.conf /app/supervisord.conf


ENTRYPOINT ["/usr/local/bin/docker-entrypoint.sh"]

EXPOSE 8080
CMD ["supervisord", "-c", "/app/supervisord.conf"]
