af8c55330d
* target: tests/.environment.sh * don't pass BIN_DIR around * manage db backup/restore separately * don't export CONFIG_DIR, DATA_DIR (derive path locations from CONFIG_YAML); redirect stdout, stderr to &3 by default in setup_file, teardown_file
88 lines
1.9 KiB
Bash
Executable file
88 lines
1.9 KiB
Bash
Executable file
#!/usr/bin/env bash
|
|
|
|
set -eu
|
|
script_name=$0
|
|
|
|
die() {
|
|
echo >&2 "$@"
|
|
exit 1
|
|
}
|
|
|
|
about() {
|
|
die "usage: $script_name [ start | stop ]"
|
|
}
|
|
|
|
#shellcheck disable=SC1007
|
|
THIS_DIR=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)
|
|
#shellcheck disable=SC1090
|
|
. "${THIS_DIR}/.environment.sh"
|
|
|
|
# you have not removed set -u above, have you?
|
|
|
|
[ -z "${CROWDSEC-}" ] && die "\$CROWDSEC must be defined."
|
|
[ -z "${LOG_DIR-}" ] && die "\$LOG_DIR must be defined."
|
|
[ -z "${PID_DIR-}" ] && die "\$PID_DIR must be defined."
|
|
|
|
if [ ! -f "${CROWDSEC}" ]; then
|
|
die "${CROWDSEC} is missing. Please build (with 'make bats-build') or install it."
|
|
fi
|
|
|
|
wait_for_port() {
|
|
for _ in $(seq 40); do
|
|
nc -z localhost "$1" >/dev/null 2>&1 && return
|
|
sleep .05
|
|
done
|
|
|
|
# send to &3 if open
|
|
if { true >&3; } 2>/dev/null; then
|
|
# cat "${LOG_DIR}/crowdsec.out" >&3
|
|
# cat "${LOG_DIR}/crowdsec.log" >&3
|
|
echo "Can't connect to port $1" >&3
|
|
else
|
|
# cat "${LOG_DIR}/crowdsec.out" >&2
|
|
# cat "${LOG_DIR}/crowdsec.log" >&2
|
|
echo "Can't connect to port $1" >&2
|
|
fi
|
|
|
|
return 1
|
|
}
|
|
|
|
DAEMON_PID=${PID_DIR}/crowdsec.pid
|
|
|
|
start_instance() {
|
|
OUT_FILE="${LOG_DIR}/crowdsec.out" \
|
|
DAEMON_PID="${DAEMON_PID}" \
|
|
"${TEST_DIR}/run-as-daemon" "${CROWDSEC}"
|
|
wait_for_port 6060
|
|
}
|
|
|
|
stop_instance() {
|
|
if [ -f "${DAEMON_PID}" ]; then
|
|
# terminate quickly with extreme prejudice, all the application data will be
|
|
# thrown away anyway. also terminate the child processes (notification plugin).
|
|
PGID="$(ps -o pgid= -p "$(cat "${DAEMON_PID}")" | tr -d ' ')"
|
|
# ps above should work on linux, freebsd, busybox..
|
|
if [ -n "${PGID}" ]; then
|
|
kill -- "-${PGID}"
|
|
fi
|
|
rm -f -- "${DAEMON_PID}"
|
|
fi
|
|
}
|
|
|
|
|
|
# ---------------------------
|
|
|
|
[ $# -lt 1 ] && about
|
|
|
|
case "$1" in
|
|
start)
|
|
start_instance
|
|
;;
|
|
stop)
|
|
stop_instance
|
|
;;
|
|
*)
|
|
about
|
|
;;
|
|
esac;
|
|
|