You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

122 lines
3.0 KiB

  1. # -*- mode: shell-script -*-
  2. include pretty
  3. export DB_NAME="$SERVICE_NAME" ## general type of database (ie: postgres/mysql...)
  4. export DB_DATADIR=/var/lib/mongodb
  5. export DATA_DIR=$SERVICE_DATASTORE$DB_DATADIR
  6. is_db_locked() {
  7. local host_db_volume
  8. if [ "${HOST_DATASTORE+x}" ]; then
  9. host_db_volume="$HOST_DATASTORE/${SERVICE_NAME}$DB_DATADIR"
  10. else
  11. host_db_volume="$DATASTORE/${SERVICE_NAME}$DB_DATADIR"
  12. fi
  13. is_volume_used "$host_db_volume"
  14. }
  15. _set_server_db_params() {
  16. local host_config_volume
  17. if [ "${HOST_DATASTORE+x}" ]; then
  18. host_config_volume="$HOST_CONFIGSTORE/${SERVICE_NAME}"
  19. else
  20. host_config_volume="$CONFIGSTORE/${SERVICE_NAME}"
  21. fi
  22. server_docker_opts+=("-v" "${host_config_volume}/etc/mongod.conf:/etc/mongod.conf"
  23. "--add-host" "${SERVICE_NAME}:127.0.0.1")
  24. }
  25. _set_db_params() {
  26. local docker_ip="$1" docker_network="$2"
  27. ## XXXvlab: include this in compose-core
  28. if [ "${_db_params}" != "$docker_ip:$docker_network" ]; then
  29. db_docker_opts+=("--network" "$docker_network")
  30. db_cmd_opts+=("--host" "$docker_ip")
  31. check_command="db.serverStatus().ok"
  32. _db_params="$docker_ip:$docker_network"
  33. echo callers: "${FUNCNAME[@]}" >&2
  34. fi
  35. }
  36. ddb() {
  37. local eval_code
  38. eval_code="$(cat - )"
  39. dcmd mongo --quiet "$@" --eval "$eval_code" </dev/null
  40. }
  41. djson() {
  42. local out err
  43. if ! out=$(ddb); then
  44. err "Mongo call failed"
  45. return 128
  46. fi
  47. if [ -z "$out" ]; then
  48. err "Mongo replied with empty output"
  49. return 64
  50. fi
  51. if [ "$(e "$out" | jq -r .ok)" != "1" ]; then
  52. if ! e "$out" | jq . >/dev/null 2>&1; then
  53. err "Mongo didn't output JSON, output follows:"
  54. e "$out" | prefix " | "
  55. return 32
  56. fi >&2
  57. errmsg=$(e "$out" | jq -r '.errmsg')
  58. code_name=$(e "$out" | jq -r '.codeName')
  59. err "Mongo failed with error message: $errmsg (code: ${code_name})"
  60. e "$out"
  61. return 32
  62. fi
  63. e "$out"
  64. return 0
  65. }
  66. mongo:db:ls() {
  67. local out
  68. if out=$(djson < <(echo "JSON.stringify(db.adminCommand( { listDatabases: 1 } ))")); then
  69. e "$out" | jq -r '.databases[] | .name'
  70. return 0
  71. else
  72. if [ "$(e "$out" | jq -r '.codeName')" == "NotMasterNoSlaveOk" ]; then
  73. ## equivalent to having no databases
  74. return 0
  75. fi
  76. err "Could not query list of databases."
  77. return 1
  78. fi
  79. }
  80. mongo:db:rename() {
  81. local src="$1" dst="$2" out
  82. if ! out=$(ddb <<EOF
  83. db.copyDatabase("$src","$dst");
  84. use $src;
  85. db.dropDatabase();
  86. EOF
  87. ); then
  88. err "Could not rename database '$src' to '$dst'."
  89. err "$out"
  90. return 1
  91. fi
  92. debug "$out"
  93. }
  94. mongo:db:version() {
  95. local out
  96. if out=$(ddb < <(echo "db.version()")); then
  97. e "$out"
  98. return 0
  99. else
  100. err "Could not query version of database."
  101. return 1
  102. fi
  103. }