diff --git a/Dockerfile b/Dockerfile index c1c7ad8..facbae7 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,12 +1,24 @@ FROM haproxy:1.9-alpine -RUN apk add --no-cache bash python3 py-yaml +WORKDIR /root -COPY entrypoint.* / -COPY conf.d /etc/haproxy/conf.d -COPY assets/errors-custom/* /etc/haproxy/errors-custom/ +RUN apk add --no-cache bash python3 py-yaml supervisor docker + +COPY requirements.txt /root RUN pip3 install --upgrade pip \ && pip install -r requirements.txt -ENTRYPOINT [ "/entrypoint.sh" ] -CMD ["haproxy", "-f", "/etc/haproxy/haproxy.cfg"] \ No newline at end of file + +COPY haproxy.cfg /etc/haproxy/haproxy.cfg +COPY conf.d /etc/haproxy/conf.d +COPY assets/errors-custom/* /etc/haproxy/errors-custom/ +COPY assets/supervisord.conf /etc/supervisord.conf +COPY assets/crontab /etc/crontabs/root + +COPY scripts/exit-event-listener.py /exit-event-listener.py +COPY scripts/haproxy.sh /haproxy.sh +COPY scripts/haproxy-reload.sh /haproxy-reload.sh + + +#CMD ["haproxy", "-f", "/etc/haproxy/haproxy.cfg"] +CMD ["/usr/bin/supervisord", "-n", "-c", "/etc/supervisord.conf" ] \ No newline at end of file diff --git a/README.md b/README.md index a4f42b6..687cb78 100644 --- a/README.md +++ b/README.md @@ -24,6 +24,7 @@ docker run \ -e HAPROXY_PASSWORD=password \ -e HAPROXY_STATS_PORT=1936 \ -e REFRESH_STATE=1 \ + -v /var/run/docker.sock:/var/run/docker.sock \ -d byjg/easy-haproxy ``` diff --git a/assets/crontab b/assets/crontab new file mode 100644 index 0000000..7b75766 --- /dev/null +++ b/assets/crontab @@ -0,0 +1,9 @@ +# do daily/weekly/monthly maintenance +# min hour day month weekday command +*/15 * * * * run-parts /etc/periodic/15min +0 * * * * run-parts /etc/periodic/hourly +0 2 * * * run-parts /etc/periodic/daily +0 3 * * 6 run-parts /etc/periodic/weekly +0 5 1 * * run-parts /etc/periodic/monthly +* * * * * /haproxy-reload.sh + diff --git a/assets/supervisord.conf b/assets/supervisord.conf new file mode 100644 index 0000000..5ad57e6 --- /dev/null +++ b/assets/supervisord.conf @@ -0,0 +1,45 @@ +[unix_http_server] +file=/dev/shm/supervisor.sock ; (the path to the socket file) + +[supervisord] +logfile=/tmp/supervisord.log ; (main log file;default $CWD/supervisord.log) +logfile_maxbytes=50MB ; (max main logfile bytes b4 rotation;default 50MB) +logfile_backups=10 ; (num of main logfile rotation backups;default 10) +loglevel=info ; (log level;default info; others: debug,warn,trace) +pidfile=/tmp/supervisord.pid ; (supervisord pidfile;default supervisord.pid) +nodaemon=false ; (start in foreground if true;default false) +minfds=1024 ; (min. avail startup file descriptors;default 1024) +minprocs=200 ; (min. avail process descriptors;default 200) +user=root ; + +; the below section must remain in the config file for RPC +; (supervisorctl/web interface) to work, additional interfaces may be +; added by defining them in separate rpcinterface: sections +[rpcinterface:supervisor] +supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface + +[supervisorctl] +serverurl=unix:///dev/shm/supervisor.sock ; use a unix:// URL for a unix socket + +[program:haproxy] +command = /haproxy.sh +autostart=true +autorestart=false +priority=5 +stdout_logfile=/dev/stdout +stdout_logfile_maxbytes=0 +stderr_logfile=/dev/stderr +stderr_logfile_maxbytes=0 + +[program:crond] +command=/usr/sbin/crond -f +autostart=true +autorestart=false +stdout_logfile=/dev/stdout +stdout_logfile_maxbytes=0 +stderr_logfile=/dev/stderr +stderr_logfile_maxbytes=0 + +[eventlistener:exit_on_any_fatal] +command=/exit-event-listener.py +events=PROCESS_STATE_FATAL,PROCESS_STATE_EXITED,PROCESS_STATE_STOPPED diff --git a/scripts/exit-event-listener.py b/scripts/exit-event-listener.py new file mode 100755 index 0000000..9b1826f --- /dev/null +++ b/scripts/exit-event-listener.py @@ -0,0 +1,18 @@ +#!/usr/bin/env python + +import os +import signal + +from supervisor import childutils + +def main(): + while True: + headers, payload = childutils.listener.wait() + childutils.listener.ok() + events = ['PROCESS_STATE_FATAL', 'PROCESS_STATE_EXITED', 'PROCESS_STATE_STOPPED'] + if not (headers['eventname'] in events): + continue + os.kill(os.getppid(), signal.SIGTERM) + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/scripts/haproxy-reload.sh b/scripts/haproxy-reload.sh new file mode 100755 index 0000000..402f4c6 --- /dev/null +++ b/scripts/haproxy-reload.sh @@ -0,0 +1,5 @@ +#!/usr/bin/env bash + +echo "Reloading..." + +/usr/local/sbin/haproxy -W -f /etc/haproxy/haproxy.cfg -p /run/haproxy.pid -x /var/run/haproxy.sock -sf $(cat /run/haproxy.pid) & \ No newline at end of file diff --git a/scripts/haproxy.sh b/scripts/haproxy.sh new file mode 100755 index 0000000..f6a691f --- /dev/null +++ b/scripts/haproxy.sh @@ -0,0 +1,7 @@ +#!/usr/bin/env bash + +/usr/local/sbin/haproxy -W -f /etc/haproxy/haproxy.cfg -p /run/haproxy.pid -S /var/run/haproxy.sock + +while true; do + sleep 60 +done