bootandkill-servers 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. #!/usr/bin/env bash
  2. # How to run:
  3. #
  4. # ./bootandkill-servers ./test-acceptance
  5. #
  6. # this will boot hexo & uppy-server, run the script that you provided as an argument,
  7. # and tear down the servers
  8. set -o pipefail
  9. set -o errexit
  10. set -o nounset
  11. # set -o xtrace
  12. # Set magic variables for current file & dir
  13. __dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
  14. __file="${__dir}/$(basename "${BASH_SOURCE[0]}")"
  15. __base="$(basename ${__file} .sh)"
  16. __root="$(cd "$(dirname "${__dir}")" && pwd)"
  17. if [ ! -f "${__root}/env.sh" ]; then
  18. cp "${__root}/env.example.sh" "${__root}/env.sh"
  19. fi
  20. if [ "${UPPYSERVER_DROPBOX_KEY:-}" = "" ] || [ "${UPPYSERVER_DROPBOX_KEY:-***}" = "***" ]; then
  21. source "${__root}/env.sh"
  22. fi
  23. if [ "${UPPYSERVER_DROPBOX_KEY:-}" = "" ] || [ "${UPPYSERVER_DROPBOX_KEY:-***}" = "***" ]; then
  24. echo "[${__base}] Env var UPPYSERVER_DROPBOX_KEY still had the example value '${UPPYSERVER_DROPBOX_KEY:-}'. "
  25. echo "[${__base}] Please save the actual secrets in '${__root}/env.sh' and try again"
  26. exit 1
  27. fi
  28. function killProcessListeningOnPort () {
  29. local port="${1}"
  30. lsof -n -i4TCP:${port} | awk '/LISTEN/ {print $2}' |xargs kill -9
  31. }
  32. function cleanup_servers () {
  33. echo "[${__base}] --> Killing any server listening on port 4000"
  34. killProcessListeningOnPort 4000 || true
  35. echo "[${__base}] --> Killing any server listening on port 8080"
  36. killProcessListeningOnPort 8080 || true
  37. kill -9 ${tailPid}
  38. }
  39. function waitForPortOpen () {
  40. local port="${1}"
  41. local limit="${2:-60}"
  42. local attempts=0
  43. echo "[${__base}] waiting on port ${port} to open... "
  44. while ! echo exit | nc localhost ${port}; do
  45. let "attempts = attempts + 1"
  46. echo "[${__base}] still waiting on port ${port} to open... (${attempts} / ${limit}) "
  47. sleep 1
  48. if [ "${attempts}" -ge "${limit}" ]; then
  49. echo "[${__base}] --> Port did not open for ${limit} seconds. Aborting. "
  50. exit 1
  51. fi
  52. done
  53. }
  54. echo "[${__base}] --> Killing any server listening on port 4000"
  55. killProcessListeningOnPort 4000 || true
  56. echo "[${__base}] --> Killing any server listening on port 8080"
  57. killProcessListeningOnPort 8080 || true
  58. echo "[${__base}] --> Start webserver and uppy-server in the background"
  59. rm -f nohup.out || true
  60. touch nohup.out
  61. nohup npm run start &
  62. tail -f nohup.out &
  63. tailPid=${!}
  64. trap cleanup_servers EXIT
  65. echo "[${__base}] --> Wait for hexo webserver to be online"
  66. waitForPortOpen 4000
  67. echo "[${__base}] --> Wait for uppy-server to be online"
  68. waitForPortOpen 8080
  69. echo "[${__base}] --> Running acceptance tests"
  70. echo "[${__base}] --> Sleeping 20s, because the port may be open, but Hexo may still be injecting/building stuff"
  71. echo "[${__base}] --> and I don't know yet how to check for that"
  72. sleep 20
  73. ${@}