run_database_emulator.sh 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. #!/bin/bash
  2. # Copyright 2020 Google LLC
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. # USAGE: run_database_emulator.sh { run | start | stop }
  16. #
  17. # Downloads and runs the Firebase Realtime Database emulator
  18. set -euo pipefail
  19. VERSION='4.7.2'
  20. FILENAME="firebase-database-emulator-v${VERSION}.jar"
  21. URL="https://storage.googleapis.com/firebase-preview-drop/emulator/${FILENAME}"
  22. cache_dir="${HOME}/.cache/firebase/emulators"
  23. jar="${cache_dir}/${FILENAME}"
  24. cd $(git rev-parse --show-toplevel)
  25. pid_file="firebase-database-emulator.pid"
  26. log_file="firebase-database-emulator.log"
  27. function help() {
  28. cat 1>&2 <<EOF
  29. run_database_emulator.sh {run|start|stop}
  30. EOF
  31. }
  32. # Downloads the emulator jar if it doesn't already exist
  33. function ensure_exists() {
  34. if [[ ! -f "$jar" ]]; then
  35. echo "Downloading Firebase Realtime Database emulator" 1>&2
  36. mkdir -p "${cache_dir}"
  37. curl -s -o "${jar}" "${URL}"
  38. fi
  39. }
  40. # Runs the emulator synchronously
  41. function run() {
  42. exec java -jar "$jar" "$@"
  43. }
  44. # Verifies the emulator isn't already running at the PID in the pid_file
  45. function check_not_running() {
  46. if [[ -f "${pid_file}" ]]; then
  47. pid=$(cat "${pid_file}")
  48. if kill -0 "${pid}" >& /dev/null; then
  49. echo "Firebase Realtime Database emulator already running as PID ${pid}" 1>&2
  50. return 1
  51. fi
  52. echo "Removing stale PID file" 1>&2
  53. rm "${pid_file}"
  54. fi
  55. }
  56. # Starts the emulator in the background
  57. function start() {
  58. check_not_running
  59. run "$@" >& "${log_file}" &
  60. pid="$!"
  61. echo "$pid" > "${pid_file}"
  62. echo "Firebase Realtime Database emulator running as PID ${pid}" 1>&2
  63. }
  64. # Stops the emulator if it's running
  65. function stop() {
  66. if [[ -f "${pid_file}" ]]; then
  67. pid=$(cat "${pid_file}")
  68. kill "${pid}" || true
  69. rm "${pid_file}"
  70. fi
  71. }
  72. command=run
  73. if [[ $# -gt 0 ]]; then
  74. command="$1"
  75. shift
  76. fi
  77. case "${command}" in
  78. run)
  79. ensure_exists
  80. run "$@"
  81. ;;
  82. start)
  83. ensure_exists
  84. start
  85. ;;
  86. stop)
  87. stop
  88. ;;
  89. download)
  90. ensure_exists
  91. ;;
  92. *)
  93. help
  94. exit 1
  95. ;;
  96. esac