FROM nginx:latest

RUN apt-get update \
 && DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends \
 cron \
 git \
 libaugeas0 \
 libjson-perl \
 libwww-perl \
 python3 \
 python3-requests \
 python3-yaml \
 python3-venv \
 wget \
 && python3 -m venv /opt/certbot/ \
 && /opt/certbot/bin/pip install --upgrade pip \
 && /opt/certbot/bin/pip install certbot certbot-nginx \
 && ln -s /opt/certbot/bin/certbot /usr/bin/certbot \
 && git clone https://gitlab.com/Linaro/tcwg/jenkins-scripts.git \
 && mkdir -p /public_html /public_html/for-ci-devs \
 && ln -s tcwg.html /public_html/index.html \
 && git clone https://gitlab.com/Linaro/tcwg/linaro-scripts.git \
 && /linaro-scripts/monitor/install.sh /linaro-scripts-monitor \
 && echo "*/20 * * * * git -C /jenkins-scripts pull" > /tmp/crontab.txt \
 && echo "*/20 * * * * /jenkins-scripts/generate-cimonitor-dashboard.py /jenkins-scripts/cimonitor-configs/CI-TCWG.yaml /jenkins-scripts/cimonitor-configs/CI-TCWG-BISECT.yaml /jenkins-scripts/cimonitor-configs/CI-TCWG-ARM.yaml /jenkins-scripts/cimonitor-configs/LNT.yaml /jenkins-scripts/cimonitor-configs/LNT-ARM.yaml --output /public_html > /public_html/generate-cimonitor-dashboard.trace 2>&1" >> /tmp/crontab.txt \
 && echo "*/5 * * * * git -C /linaro-scripts pull >/dev/null 2>&1 && /linaro-scripts-monitor/bin/bot-status.py /linaro-scripts-monitor/bin/linaro.json /linaro-scripts-monitor/html/index.html && chmod +r /linaro-scripts-monitor/html/index.html" >> /tmp/crontab.txt \
 && echo "0 0,12 * * * /opt/certbot/bin/python -c 'import random; import time; time.sleep(random.random() * 3600)' && certbot renew -q" >> /tmp/crontab.txt \
 && echo "0 0 1 * * /opt/certbot/bin/pip install --upgrade certbot certbot-nginx" >> /tmp/crontab.txt \
 && crontab /tmp/crontab.txt \
 && rm -rf \
 /etc/nginx/conf.d/default.conf \
 /var/lib/apt/lists/* \
 /tmp/* \
 /var/tmp/*
COPY tcwg.conf /etc/nginx/conf.d/tcwg.conf
COPY index-root.html /public_html_root/index.html
COPY index-ci.html /public_html_root/ci/index.html
# Newer versions of Docker have "COPY --chmod", but not the one on tcwg-ex42-01.
RUN chmod 644 public_html_root/index.html public_html_root/ci/index.html

CMD ["bash", "-c", "service cron start && exec nginx -g 'daemon off;'"]

# docker run \
#   --init \
#   --mount type=volume,src=letsencrypt,dst=/etc/letsencrypt \
#   -d \
#   -p 80:80 \
#   -p 443:443 \
#   -p 8080:8080 \
#   -p 9091:9091 \
#   -p 9092:9092 \
#   -p 9093:9093 \
#   --name cimonitor \
#   linaro/ci-amd64-tcwg-cimonitor-nginx
