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.

118 lines
2.9 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() { dcmd mongo --quiet "$@"; }
  37. djson() {
  38. local out err
  39. if ! out=$(ddb); then
  40. err "Mongo call failed"
  41. return 128
  42. fi
  43. if [ -z "$out" ]; then
  44. err "Mongo replied with empty output"
  45. return 64
  46. fi
  47. if [ "$(e "$out" | jq -r .ok)" != "1" ]; then
  48. if ! e "$out" | jq . >/dev/null 2>&1; then
  49. err "Mongo didn't output JSON, output follows:"
  50. e "$out" | prefix " | "
  51. return 32
  52. fi >&2
  53. errmsg=$(e "$out" | jq -r '.errmsg')
  54. code_name=$(e "$out" | jq -r '.codeName')
  55. err "Mongo failed with error message: $errmsg (code: ${code_name})"
  56. e "$out"
  57. return 32
  58. fi
  59. e "$out"
  60. return 0
  61. }
  62. mongo:db:ls() {
  63. local out
  64. if out=$(djson < <(echo "JSON.stringify(db.adminCommand( { listDatabases: 1 } ))")); then
  65. e "$out" | jq -r '.databases[] | .name'
  66. return 0
  67. else
  68. if [ "$(e "$out" | jq -r '.codeName')" == "NotMasterNoSlaveOk" ]; then
  69. ## equivalent to having no databases
  70. return 0
  71. fi
  72. err "Could not query list of databases."
  73. return 1
  74. fi
  75. }
  76. mongo:db:rename() {
  77. local src="$1" dst="$2" out
  78. if ! out=$(ddb <<EOF
  79. db.copyDatabase("$src","$dst");
  80. use $src;
  81. db.dropDatabase();
  82. EOF
  83. ); then
  84. err "Could not rename database '$src' to '$dst'."
  85. err "$out"
  86. return 1
  87. fi
  88. debug "$out"
  89. }
  90. mongo:db:version() {
  91. local out
  92. if out=$(ddb < <(echo "db.version()")); then
  93. e "$out"
  94. return 0
  95. else
  96. err "Could not query version of database."
  97. return 1
  98. fi
  99. }