github.com/crowdsecurity/crowdsec@v1.6.1/test/instance-mock-http (about) 1 #!/usr/bin/env bash 2 3 set -eu 4 5 die() { 6 echo >&2 "$@" 7 exit 1 8 } 9 10 about() { 11 die "usage: $0 [ start <port> | stop ]" 12 } 13 14 #shellcheck disable=SC1007 15 THIS_DIR=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd) 16 cd "${THIS_DIR}" 17 # shellcheck disable=SC1091 18 . ./.environment.sh 19 20 # you have not removed set -u above, have you? 21 22 [[ -z "${LOG_DIR-}" ]] && die "\$LOG_DIR must be defined." 23 [[ -z "${PID_DIR-}" ]] && die "\$PID_DIR must be defined." 24 25 if ! command -v python3 >/dev/null 2>&2; then 26 die "The python3 executable is is missing. Please install it and try again." 27 fi 28 29 DAEMON_PID=${PID_DIR}/mock-http.pid 30 31 start_instance() { 32 [[ $# -lt 1 ]] && about 33 daemonize \ 34 -p "${DAEMON_PID}" \ 35 -e "${LOG_DIR}/mock-http.err" \ 36 -o "${LOG_DIR}/mock-http.out" \ 37 /usr/bin/env python3 -u "${THIS_DIR}/bin/mock-http.py" "$1" 38 ./bin/wait-for-port "$1" 39 # echo "mock http started on port $1" 40 } 41 42 stop_instance() { 43 if [[ -f "${DAEMON_PID}" ]]; then 44 # terminate with extreme prejudice, all the application data will be thrown away anyway 45 kill -9 "$(cat "${DAEMON_PID}")" > /dev/null 2>&1 46 rm -f -- "${DAEMON_PID}" 47 fi 48 } 49 50 51 # --------------------------- 52 53 [[ $# -lt 1 ]] && about 54 55 case "$1" in 56 start) 57 shift 58 start_instance "$@" 59 ;; 60 stop) 61 stop_instance 62 ;; 63 *) 64 about 65 ;; 66 esac; 67