2022-07-21 11:03:46 +00:00
|
|
|
#!/usr/bin/env bash
|
2022-03-09 13:45:36 +00:00
|
|
|
|
|
|
|
set -eu
|
|
|
|
|
|
|
|
die() {
|
|
|
|
echo >&2 "$@"
|
|
|
|
exit 1
|
|
|
|
}
|
|
|
|
|
|
|
|
about() {
|
|
|
|
die "usage: $0 [ start <port> | stop ]"
|
|
|
|
}
|
|
|
|
|
|
|
|
#shellcheck disable=SC1007
|
|
|
|
THIS_DIR=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)
|
2022-03-29 15:06:49 +00:00
|
|
|
cd "${THIS_DIR}"
|
2022-10-04 13:58:10 +00:00
|
|
|
# shellcheck disable=SC1091
|
|
|
|
. ./.environment.sh
|
2022-03-09 13:45:36 +00:00
|
|
|
|
|
|
|
# you have not removed set -u above, have you?
|
|
|
|
|
2022-10-04 13:58:10 +00:00
|
|
|
[[ -z "${LOG_DIR-}" ]] && die "\$LOG_DIR must be defined."
|
|
|
|
[[ -z "${PID_DIR-}" ]] && die "\$PID_DIR must be defined."
|
2022-03-09 13:45:36 +00:00
|
|
|
|
|
|
|
if ! command -v python3 >/dev/null 2>&2; then
|
|
|
|
die "The python3 executable is is missing. Please install it and try again."
|
|
|
|
fi
|
|
|
|
|
|
|
|
DAEMON_PID=${PID_DIR}/mock-http.pid
|
|
|
|
|
|
|
|
start_instance() {
|
2022-10-04 13:58:10 +00:00
|
|
|
[[ $# -lt 1 ]] && about
|
2022-08-04 09:25:34 +00:00
|
|
|
daemonize \
|
|
|
|
-p "${DAEMON_PID}" \
|
|
|
|
-e "${LOG_DIR}/mock-http.err" \
|
|
|
|
-o "${LOG_DIR}/mock-http.out" \
|
2022-10-04 13:58:10 +00:00
|
|
|
/usr/bin/env python3 -u "${THIS_DIR}/bin/mock-http.py" "$1"
|
|
|
|
./bin/wait-for-port "$1"
|
2022-03-18 09:13:12 +00:00
|
|
|
# echo "mock http started on port $1"
|
2022-03-09 13:45:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
stop_instance() {
|
2022-10-04 13:58:10 +00:00
|
|
|
if [[ -f "${DAEMON_PID}" ]]; then
|
2022-03-09 13:45:36 +00:00
|
|
|
# terminate with extreme prejudice, all the application data will be thrown away anyway
|
|
|
|
kill -9 "$(cat "${DAEMON_PID}")" > /dev/null 2>&1
|
|
|
|
rm -f -- "${DAEMON_PID}"
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
# ---------------------------
|
|
|
|
|
2022-10-04 13:58:10 +00:00
|
|
|
[[ $# -lt 1 ]] && about
|
2022-03-09 13:45:36 +00:00
|
|
|
|
|
|
|
case "$1" in
|
|
|
|
start)
|
|
|
|
shift
|
|
|
|
start_instance "$@"
|
|
|
|
;;
|
|
|
|
stop)
|
|
|
|
stop_instance
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
about
|
|
|
|
;;
|
|
|
|
esac;
|
|
|
|
|