FROM node:22.14.0-bookworm-slim AS build

# Copy the webrtc-internals-exporter files
COPY webrtc-internals-exporter /tmp/webrtc-internals-exporter

WORKDIR /tmp/webrtc-internals-exporter/webpack

# Install dependencies
RUN --mount=type=cache,target=/root/.npm \
    npm install

# Build the project
RUN npm run build

FROM selenium/standalone-chromium:129.0

# Install Python-virtualenv
RUN sudo apt-get update && sudo sudo apt-get install -y python3-venv

WORKDIR /tmp

# Install Telegraf
RUN wget -q https://repos.influxdata.com/influxdata-archive_compat.key && \
    echo '393e8779c89ac8d958f81f942f9ad7fb82a25e133faddaf92e15b16e6ac9ce4c influxdata-archive_compat.key' | sha256sum -c && \
    cat influxdata-archive_compat.key | gpg --dearmor | sudo tee /etc/apt/trusted.gpg.d/influxdata-archive_compat.gpg > /dev/null && \
    echo 'deb [signed-by=/etc/apt/trusted.gpg.d/influxdata-archive_compat.gpg] https://repos.influxdata.com/debian stable main' | sudo tee /etc/apt/sources.list.d/influxdata.list && \
    sudo apt-get update && sudo apt-get install -y telegraf

# Create and activate a virtual environment
RUN python3 -m venv ./venv
ENV PATH="/tmp/venv/bin:$PATH"

# Install dependencies with venv
COPY requirements.txt ./requirements.txt
RUN ./venv/bin/pip install -r ./requirements.txt

# Copy files
COPY main.py .
COPY utils/ ./utils
COPY telegraf.conf ./telegraf.conf
COPY webrtc-internals-exporter /tmp/webrtc-internals-exporter
COPY --from=build /tmp/webrtc-internals-exporter/background.bundle.js /tmp/webrtc-internals-exporter/background.bundle.js
COPY --chown="${SEL_UID}:${SEL_GID}" monolith-entrypoint.sh /opt/bin/collector.sh

# Run the entrypoint
RUN chmod +x /opt/bin/collector.sh
ENTRYPOINT ["/opt/bin/collector.sh"]

# Healthcheck
HEALTHCHECK --interval=5s --timeout=10s --retries=5 --start-period=5s CMD curl -f http://localhost:9092/heartbeat || exit 1