instance-mock-http 1.4 KB

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