FROM debian:bullseye-slim AS lava-build

ENV DEBIAN_FRONTEND noninteractive

COPY . /lava

RUN apt update && \
    cd /lava && \
    apt-get -o Debug::pkgProblemResolver=yes --no-install-recommends --yes build-dep . && \
    dpkg-buildpackage -b

FROM debian:bullseye-slim AS server

COPY --from=lava-build /lava-server*.deb /lava-common*.deb /
COPY docker/collabora/start-lava.sh /
COPY docker/collabora/scripts/lava /opt/lava

# LAVA won't like not finding some variables when it imports its configuration.
# We only set up the actual configuration when starting the container, but provide
# "blank" ones to make installation happy.
ARG INSTANCE_NAME=default
ARG DATABASE_URL=sqlite://:memory:
ARG SECRET_KEY=secret

# Our base image disallows installation of most documentation files,
# override that lava-server-doc, as the files are used to provide the
# live documentation.
COPY docker/collabora/lava-dpkg.cfg /etc/dpkg/dpkg.cfg.d/lava

RUN addgroup --system --gid 104 lavaserver && \
    adduser --system --gid 104 --uid 103 --home /var/lib/lava-server/home/ lavaserver && \
    apt update && \
    apt install -y --no-install-recommends \
        /*.deb \
        wait-for-it \
        python3-pip \
        python3-requests \
        python3-josepy \
        python3-eventlet \
        python3-cryptography && \
    rm /*deb
RUN pip3 install --no-deps mozilla-django-oidc=="2.0.0"

# Default logging configuration
RUN mkdir -p /etc/lava-server/settings.d
COPY docker/collabora/logging_env.yaml /etc/lava-server/settings.d/

# Ensure that LAVA can write the dispatcher configs
RUN mkdir -p /etc/lava-server/dispatcher-config/health-checks
RUN chown -R lavaserver: /etc/lava-server/dispatcher-config

# This is lava-server's port. Hitting it hits gunicorn and Django.
# This port is the one you'll want ProxyPass to, mainly.
EXPOSE 8000

# This port is opened by lava-publisher. It provides the web socket
# server that the worker connects to on the /ws/ path.
EXPOSE 8001

# Start the service as an unprivileged user
USER lavaserver

CMD "/start-lava.sh"
