FROM nginx:1.27.1-bookworm

# Install libraries and nginx
RUN apt-get update -y \
    && apt-get install -y python3 python3-pip python3-venv cron libjson-c5 libpcre3 \
    && apt-get clean

# Create directories and copy configuration files
RUN mkdir -p /opt/bin/kameleoon /etc/kameleoon /etc/logs

# Copy configuration files
COPY nginx_conf_generator.py /opt/bin/kameleoon/
COPY configuration.json /etc/kameleoon
COPY ngx_kameleoon_module.so /etc/nginx/modules
COPY kameleoon-server.conf /etc/nginx/conf.d
COPY nginx.conf /etc/nginx

# Create virtual environment and install Python libraries
COPY requirements.txt /tmp
RUN python3 -m venv /opt/venv \
    && /opt/venv/bin/pip install --upgrade pip \
    && /opt/venv/bin/pip install -r /tmp/requirements.txt

# Add cron job
COPY kameleoon-cron /etc/cron.d
RUN chmod 0644 /etc/cron.d/kameleoon-cron
# Create the log file to be able to run tail
RUN touch /var/log/cron.log
# Apply cron job
RUN crontab /etc/cron.d/kameleoon-cron

# Entry point
ENTRYPOINT /opt/venv/bin/python /opt/bin/kameleoon/nginx_conf_generator.py && cron && nginx -g 'daemon off;'
