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 |
|
9 | set -o pipefail
|
10 | set -o errexit
|
11 | set -o nounset
|
12 | set -o xtrace
|
13 |
|
14 | # Set magic variables for current file & dir
|
15 | __dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
16 | __file="${__dir}/$(basename "${BASH_SOURCE[0]}")"
|
17 | __base="$(basename ${__file} .sh)"
|
18 | __root="$(cd "$(dirname "${__dir}")" && pwd)"
|
19 |
|
20 | if [ ! -f "${__root}/env.sh" ]; then
|
21 | cp "${__root}/env.example.sh" "${__root}/env.sh"
|
22 | fi
|
23 | if [ "${UPPYSERVER_DROPBOX_KEY:-}" = "" ] || [ "${UPPYSERVER_DROPBOX_KEY:-***}" = "***" ]; then
|
24 | source "${__root}/env.sh"
|
25 | fi
|
26 | if [ "${UPPYSERVER_DROPBOX_KEY:-}" = "" ] || [ "${UPPYSERVER_DROPBOX_KEY:-***}" = "***" ]; then
|
27 | echo "[${__base}] Env var UPPYSERVER_DROPBOX_KEY still had the example value '${UPPYSERVER_DROPBOX_KEY:-}'. "
|
28 | echo "[${__base}] Please save the actual secrets in '${__root}/env.sh' and try again"
|
29 | exit 1
|
30 | fi
|
31 |
|
32 | function killProcessListeningOnPort () {
|
33 | local port="${1}"
|
34 | lsof -n -i4TCP:${port} | awk '/LISTEN/ {print $2}' |xargs --no-run-if-empty kill -9 || true
|
35 | }
|
36 |
|
37 | function cleanup_servers () {
|
38 | echo "[${__base}] --> Killing any server listening on port 4000"
|
39 | killProcessListeningOnPort 4000 || true
|
40 |
|
41 | echo "[${__base}] --> Killing any server listening on port 3020"
|
42 | killProcessListeningOnPort 3020 || true
|
43 |
|
44 | kill -9 ${tailPid}
|
45 | }
|
46 |
|
47 | function waitForPortOpen () {
|
48 | local port="${1}"
|
49 | local limit="${2:-100}"
|
50 | local attempts=0
|
51 | echo "[${__base}] waiting on port ${port} to open... "
|
52 | while ! echo exit | nc localhost ${port}; do
|
53 | let "attempts = attempts + 1"
|
54 | echo "[${__base}] still waiting on port ${port} to open... (${attempts} / ${limit}) "
|
55 | sleep 1
|
56 | if [ "${attempts}" -ge "${limit}" ]; then
|
57 | echo "[${__base}] --> Port did not open for ${limit} seconds. Aborting. "
|
58 | exit 1
|
59 | fi
|
60 | done
|
61 | }
|
62 |
|
63 | echo "[${__base}] --> Killing any server listening on port 4000"
|
64 | killProcessListeningOnPort 4000 || true
|
65 | echo "[${__base}] --> Killing any server listening on port 3020"
|
66 | killProcessListeningOnPort 3020 || true
|
67 |
|
68 | echo "[${__base}] --> Start webserver and uppy-server in the background"
|
69 | rm -f nohup.out || true
|
70 | touch nohup.out
|
71 | nohup npm run test:serve &
|
72 | tail -f nohup.out &
|
73 | tailPid=${!}
|
74 |
|
75 | trap cleanup_servers EXIT
|
76 |
|
77 |
|
78 |
|
79 | echo "[${__base}] --> Wait for hexo webserver to be online"
|
80 | waitForPortOpen 4000
|
81 |
|
82 | echo "[${__base}] --> Wait for uppy-server to be online"
|
83 | waitForPortOpen 3020
|
84 |
|
85 | echo "[${__base}] --> Running acceptance tests"
|
86 |
|
87 |
|
88 | buildWait=20
|
89 | if [ "${TRAVIS:-}" = "true" ]; then
|
90 | buildWait=60
|
91 | echo "Increasing build wait because Travis has no Hexo build cache"
|
92 | fi
|
93 |
|
94 | echo "[${__base}] --> Sleeping ${buildWait}s, because the port may be open, but Hexo may still be injecting/building stuff"
|
95 | echo "[${__base}] --> and I don't know yet how to check for that"
|
96 | sleep ${buildWait}
|
97 |
|
98 | # @todo: Instead of this `sleep` we can wait until the required files are in public : )
|
99 |
|
100 | ${@}
|