introducing SWARM_MODE env var

This commit is contained in:
bunkerity 2021-03-12 12:40:52 +01:00
parent 7756c2df3c
commit 816fa47cbb
6 changed files with 53 additions and 28 deletions

View File

@ -1,3 +1,5 @@
set api_uri "%API_URI%";
rewrite_by_lua_block {
local api = require "api"

View File

@ -128,3 +128,4 @@ ANTIBOT_SESSION_SECRET="${ANTIBOT_SESSION_SECRET-random}"
USE_CROWDSEC="${USE_CROWDSEC-no}"
USE_API="${USE_API-no}"
API_URI="${API_URI-random}"
SWARM_MODE="${SWARM_MODE-no}"

View File

@ -31,7 +31,7 @@ trap "trap_exit" TERM INT QUIT
# trap SIGHUP
function trap_reload() {
echo "[*] Catched reload operation"
if [ "$MULTISITE" = "yes" ] ; then
if [ "$MULTISITE" = "yes" ] && [ "$SWARM_MODE" != "yes" ] ; then
/opt/entrypoint/multisite-config.sh
fi
if [ -f /tmp/nginx.pid ] ; then
@ -50,17 +50,28 @@ trap "trap_reload" HUP
# do the configuration magic if needed
if [ ! -f "/opt/installed" ] ; then
echo "[*] Configuring bunkerized-nginx ..."
/opt/entrypoint/global-config.sh
if [ "$MULTISITE" = "yes" ] ; then
for server in $SERVER_NAME ; do
/opt/entrypoint/site-config.sh "$server"
echo "[*] Multi site - $server configuration done"
done
/opt/entrypoint/multisite-config.sh
else
/opt/entrypoint/site-config.sh
echo "[*] Single site - $SERVER_NAME configuration done"
# logs config
/opt/entrypoint/logs.sh
# only do config if we are not in swarm mode
if [ "$SWARM_MODE" = "no" ] ; then
# global config
/opt/entrypoint/global-config.sh
# multisite configs
if [ "$MULTISITE" = "yes" ] ; then
for server in $SERVER_NAME ; do
/opt/entrypoint/site-config.sh "$server"
echo "[*] Multi site - $server configuration done"
done
/opt/entrypoint/multisite-config.sh
# singlesite config
else
/opt/entrypoint/site-config.sh
echo "[*] Single site - $SERVER_NAME configuration done"
fi
fi
touch /opt/installed
else
@ -78,16 +89,23 @@ rsyslogd
# start crond
crond
# start nginx
# wait until config has been generated if we are in swarm mode
if [ "$SWARM_MODE" != "yes" ] ; then
echo "[*] Waiting until config has been generated ..."
while [ ! -f "/etc/nginx/autoconf" ] ; do
sleep 1
done
fi
if [ -f "/tmp/nginx-temp.pid" ] ; then
nginx -c /etc/nginx/nginx-temp.conf -s quit
fi
echo "[*] Running nginx ..."
su -s "/usr/sbin/nginx" nginx
if [ "$?" -eq 0 ] ; then
touch "/opt/running"
echo "[*] nginx successfully started !"
else
rm -f "/opt/running" 2> /dev/null
echo "[!] nginx failed to start"
fi
# list of log files to display
@ -114,7 +132,7 @@ fi
# display logs
tail -F $LOGS &
pid="$!"
while [ -f "/opt/running" ] ; do
while [ -f "/tmp/nginx.pid" ] ; do
wait "$pid"
done

View File

@ -7,8 +7,6 @@
. /opt/entrypoint/utils.sh
# copy stub confs
cp /opt/logs/rsyslog.conf /etc/rsyslog.conf
cp /opt/logs/logrotate.conf /etc/logrotate.conf
cp -r /opt/lua/* /usr/local/lib/lua
cp /opt/confs/global/* /etc/nginx/
@ -310,17 +308,9 @@ if [ "$USE_API" = "yes" ] ; then
replace_in_file "/etc/nginx/nginx.conf" "%USE_API%" "include /etc/nginx/api.conf;"
if [ "$API_URI" = "random" ] ; then
API_URI="/$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 32 | head -n 1)"
echo "[*] Generated API URI : $API_URI"
fi
replace_in_file "/usr/local/lib/lua/api.lua" "%API_URI%" "$API_URI"
replace_in_file "/etc/nginx/api.conf" "%API_URI%" "$API_URI"
else
replace_in_file "/etc/nginx/nginx.conf" "%USE_API%" ""
fi
# create empty logs
touch /var/log/access.log
touch /var/log/error.log
# setup logrotate
replace_in_file "/etc/logrotate.conf" "%LOGROTATE_MAXAGE%" "$LOGROTATE_MAXAGE"
replace_in_file "/etc/logrotate.conf" "%LOGROTATE_MINSIZE%" "$LOGROTATE_MINSIZE"
echo "$LOGROTATE_CRON /opt/scripts/logrotate.sh > /dev/null 2>&1" >> /etc/crontabs/root

14
entrypoint/logs.sh Normal file
View File

@ -0,0 +1,14 @@
#!/bin/bash
cp /opt/logs/rsyslog.conf /etc/rsyslog.conf
cp /opt/logs/logrotate.conf /etc/logrotate.conf
# create empty logs
touch /var/log/access.log
touch /var/log/error.log
touch /var/log/jobs.log
# setup logrotate
replace_in_file "/etc/logrotate.conf" "%LOGROTATE_MAXAGE%" "$LOGROTATE_MAXAGE"
replace_in_file "/etc/logrotate.conf" "%LOGROTATE_MINSIZE%" "$LOGROTATE_MINSIZE"
echo "$LOGROTATE_CRON /opt/scripts/logrotate.sh > /dev/null 2>&1" >> /etc/crontabs/root

View File

@ -1,5 +1,5 @@
local M = {}
local api_uri = "%API_URI%"
local api_uri = ngx.var.api_uri
local api_list = {}
api_list["^/reload$"] = function ()