summarylogtreecommitdiffstats
path: root/cs-nginx-bouncer.install
blob: ee86f04705e6c12632917faff7e4a2b03c100968 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
NGINX_LUA_MODULE_PATCH="$(cat <<-EOF
	###########################################################
	# @@@@@@@@@@@@@@ Added by cs-nginx-bouncer @@@@@@@@@@@@@@ #
	load_module /usr/lib/nginx/modules/ndk_http_module.so;
	load_module /usr/lib/nginx/modules/ngx_http_lua_module.so;
	###########################################################
EOF
)"

pre_install() {
	mkdir -p /etc/nginx/modules
}

post_install() {
	API_KEY=`cscli bouncers add crowdsec-nginx-bouncer -o raw`
	export API_KEY
	IFS=: read HOST PORT <<< `sudo cscli config show --key "Config.API.Server.ListenURI"`
	echo "Bouncer registered to the CrowdSec Local API."

	CROWDSEC_LAPI_URL="http://${HOST:-'127.0.0.1'}:${PORT:-'8080'}"
	export CROWDSEC_LAPI_URL

	TMP=$(mktemp -p /tmp/)
	install -m600 /etc/crowdsec/bouncers/crowdsec-nginx-bouncer.conf "$TMP"
	envsubst < "$TMP" | tee /etc/crowdsec/bouncers/crowdsec-nginx-bouncer.conf >/dev/null
	
	echo installing lua-resty-http and lua-cjson using luarocks
	luarocks install lua-resty-http
	luarocks install lua-cjson
}

post_upgrade() {
	if (( $(vercmp "$2" "1.0.5-6") < 0 )); then
		TMP="$(mktemp)"
		grep -Ev "^${NGINX_LUA_MODULE_PATCH}" /etc/nginx/nginx.conf > "$TMP"
		mv "$TMP" /etc/nginx/nginx.conf

		echo -e "\n**NEW: Please add the following line to your nginx conf file:"
		echo -e "==> include /etc/nginx/modules/*.conf; <==\n"
	fi
}

post_remove() {
	echo 'removing lua-resty-http and lua-cjson' using luarocks
	luarocks remove lua-resty-http
	luarocks remove lua-cjson

	if ! cscli bouncers remove crowdsec-nginx-bouncer --error; then
		echo -e "\nDon't forget to uninstall the plugin!"
		cscli bouncers list
	fi
}