bootandkill-servers 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 3020"
  36. killProcessListeningOnPort 3020 || 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 3020"
  57. killProcessListeningOnPort 3020 || 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 3020
  69. echo "[${__base}] --> Running acceptance tests"
  70. buildWait=20
  71. if [ "${TRAVIS}" = "true" ]; then
  72. buildWait=60
  73. echo "Increasing build wait because Travis has no Hexo build cache"
  74. fi
  75. echo "[${__base}] --> Sleeping ${buildWait}s, because the port may be open, but Hexo may still be injecting/building stuff"
  76. echo "[${__base}] --> and I don't know yet how to check for that"
  77. sleep ${buildWait}
  78. # @todo: Instead of this `sleep` we can wait until the required files are in public : )
  79. ${@}