Skip to content

Commit 79d50af

Browse files
committed
Add support for environment variables.
1 parent 61fb0e5 commit 79d50af

File tree

1 file changed

+46
-25
lines changed

1 file changed

+46
-25
lines changed

rootfs/etc/cont-init.d/99_crowdsec-openresty-bouncer.sh

+46-25
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,53 @@
22

33
set -e # Exit immediately if a command exits with a non-zero status.
44

5-
mkdir -p /config/crowdsec/templates
6-
echo "Deploy Crowdsec Openresty Bouncer.."
7-
sed -i 's|/defaults/crowdsec|/config/crowdsec|' /etc/nginx/conf.d/crowdsec_openresty.conf
5+
function set_properties() {
6+
sed -i "s,^$1=.*,$1=$2,g" "${3}"
7+
}
88

9-
if [ -f /data/crowdsec/crowdsec-openresty-bouncer.conf ]; then
10-
echo "Patch crowdsec-openresty-bouncer.conf .."
11-
sed "s/=.*//g" /config/crowdsec/crowdsec-openresty-bouncer.conf > /tmp/crowdsec.conf.raw
12-
sed "s/=.*//g" /defaults/crowdsec/crowdsec-openresty-bouncer.conf > /tmp/crowdsec-openresty-bouncer.conf.raw
13-
if grep -vf /tmp/crowdsec.conf.raw /tmp/crowdsec-openresty-bouncer.conf.raw ; then
14-
grep -vf /tmp/crowdsec.conf.raw /tmp/crowdsec-openresty-bouncer.conf.raw > /tmp/config.newvals
15-
cp /config/crowdsec/crowdsec-openresty-bouncer.conf /config/crowdsec/crowdsec-openresty-bouncer.conf.bak
16-
grep -f /tmp/config.newvals /defaults/crowdsec/crowdsec-openresty-bouncer.conf >> /config/crowdsec/crowdsec-openresty-bouncer.conf
17-
fi
9+
echo "Deploy Crowdsec Openresty Bouncer.."
10+
if [ -n "${CROWDSEC_OPENRESTY_BOUNCER}" ]; then
11+
while IFS= read -r line
12+
do
13+
if ! [[ "$line" != "^#" ]] || [[ "$line" != "^\n" ]]; then
14+
name=$(echo "$line" | cut -d "=" -f1)
15+
value=$(echo "$line" | cut -d "=" -f2)
16+
if grep -q "${name}" /defaults/crowdsec/crowdsec-openresty-bouncer.conf ; then
17+
set_properties "${name}" "${value}" "/defaults/crowdsec/crowdsec-openresty-bouncer.conf"
18+
fi
19+
fi
20+
done <<< "${CROWDSEC_OPENRESTY_BOUNCER}"
1821
else
19-
echo "Deploy new crowdsec-openresty-bouncer.conf .."
20-
cp /defaults/crowdsec/crowdsec-openresty-bouncer.conf /config/crowdsec/crowdsec-openresty-bouncer.conf
22+
mkdir -p /config/crowdsec/templates
23+
sed -i 's|/defaults/crowdsec|/config/crowdsec|' /etc/nginx/conf.d/crowdsec_openresty.conf
24+
25+
if [ -f /data/crowdsec/crowdsec-openresty-bouncer.conf ]; then
26+
echo "Patch crowdsec-openresty-bouncer.conf .."
27+
sed "s/=.*//g" /config/crowdsec/crowdsec-openresty-bouncer.conf > /tmp/crowdsec.conf.raw
28+
sed "s/=.*//g" /defaults/crowdsec/crowdsec-openresty-bouncer.conf > /tmp/crowdsec-openresty-bouncer.conf.raw
29+
if grep -vf /tmp/crowdsec.conf.raw /tmp/crowdsec-openresty-bouncer.conf.raw ; then
30+
grep -vf /tmp/crowdsec.conf.raw /tmp/crowdsec-openresty-bouncer.conf.raw > /tmp/config.newvals
31+
cp /config/crowdsec/crowdsec-openresty-bouncer.conf /config/crowdsec/crowdsec-openresty-bouncer.conf.bak
32+
grep -f /tmp/config.newvals /defaults/crowdsec/crowdsec-openresty-bouncer.conf >> /config/crowdsec/crowdsec-openresty-bouncer.conf
33+
fi
34+
else
35+
echo "Deploy new crowdsec-openresty-bouncer.conf .."
36+
cp /defaults/crowdsec/crowdsec-openresty-bouncer.conf /config/crowdsec/crowdsec-openresty-bouncer.conf
37+
fi
38+
#Make sure the config location is where we get the config from instead of /default/
39+
sed -i 's|/defaults/crowdsec|/data/crowdsec|' /data/crowdsec/crowdsec-openresty-bouncer.conf
2140
fi
22-
#Make sure the config location is where we get the config from instead of /default/
23-
sed -i 's|/defaults/crowdsec|/config/crowdsec|' /config/crowdsec/crowdsec-openresty-bouncer.conf
24-
echo "Deploy Crowdsec Templates .."
41+
42+
2543
#Make sure we only copy files that don't exist in /data/crowdsec.
26-
cd /defaults/crowdsec/templates/
27-
for file in *.html
28-
do
29-
if [ ! -e "/config/crowdsec/templates/${file}" ]
30-
then
31-
cp -r "/defaults/crowdsec/templates/${file}" "/config/crowdsec/templates/"
32-
fi
33-
done
44+
if [ -d "/data/crowdsec/templates" ]; then
45+
echo "Deploy Crowdsec Templates .."
46+
cd /defaults/crowdsec/templates/
47+
for file in *.html
48+
do
49+
if [ ! -e "/config/crowdsec/templates/${file}" ]
50+
then
51+
cp -r "/defaults/crowdsec/templates/${file}" "/config/crowdsec/templates/"
52+
fi
53+
done
54+
fi

0 commit comments

Comments
 (0)