fork 0k-charms
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.

106 lines
2.9 KiB

  1. #!/bin/bash
  2. . lib/common
  3. set -e
  4. DBNAME=$(relation-get dbname) || {
  5. DBNAME="${BASE_SERVICE_NAME//\./_}"
  6. relation-set dbname "$DBNAME"
  7. }
  8. ## From: https://stackoverflow.com/questions/16989598
  9. function version_gt() { test "$(printf '%s\n' "$@" | sort -V | head -n 1)" != "$1"; }
  10. ensure_db_docker_running
  11. ## ReplicaSet initialization
  12. cmd="rs.initiate({ _id: 'rs01', members: [ { _id: 0, host: '$TARGET_SERVICE_NAME:27017' } ]})"
  13. debug "${WHITE}running:$NORMAL $cmd"
  14. out=$(ddb < <(echo "use $DBNAME";
  15. echo "$cmd"))
  16. if [[ "$out" == *"\"codeName\" : \"AlreadyInitialized\""* ]]; then
  17. info "ReplicaSet already initialized."
  18. elif [[ "$out" == *"\"ok\" : 1"* ]]; then
  19. info "ReplicaSet initialized. "
  20. else
  21. err "ReplicaSet initialisation failed:"
  22. echo "$out" >&2
  23. exit 13
  24. fi
  25. ## Enable read if db version >= 4.2
  26. if ! version=$(mongo:db:version); then
  27. err "Couldn't get database version"
  28. exit 1
  29. fi
  30. echo "Current mongo database version: '$version'." >&2
  31. if version_gt "$version" 4.1; then
  32. cmd="db.getMongo().setSecondaryOk()"
  33. debug "${WHITE}running:$NORMAL $cmd"
  34. out=$(ddb < <(echo "use $DBNAME";
  35. echo "$cmd")) || {
  36. err "Failed database command. Output:"
  37. echo "$out" | prefix " | " >&2
  38. exit 1
  39. }
  40. fi
  41. if dbs=$(mongo:db:ls); then
  42. if matching_db=$(e "$dbs" | egrep "[a-zA-Z0-9.-_]*_${DBNAME}"); then
  43. if e "$dbs" | grep "^${DBNAME}$"; then
  44. err "Error: unexpected database '$matching_db'" \
  45. "found along with database named '${DBNAME}'."
  46. exit 1
  47. fi
  48. ## Let's take for granted that only current source service is
  49. ## concerned by the database. We need to restart source service
  50. ## container only if already running.
  51. container_ids=($(get_running_containers_for_service "$MASTER_BASE_SERVICE_NAME"))
  52. for container_id in "${container_ids[@]}"; do
  53. mongo_url=$(docker:container:env_get "$container_id" MONGO_URL) || return 1
  54. if [[ "$mongo_url" == */"${matching_db}"* ]]; then
  55. info "Attempting to stop current containers of service $MASTER_BASE_SERVICE_NAME"
  56. docker stop -t 5 "$container_id"
  57. fi
  58. done
  59. mongo:db:rename "$matching_db" "${DBNAME}"
  60. ## we DO NOT want to start the stopped container again as they are not
  61. ## properly configured
  62. fi
  63. else
  64. err "Failed to query for database list"
  65. exit 14
  66. fi
  67. major_version=${version%.*}
  68. ## XXXvlab: why don't we do this initialisation in init ?
  69. cmd="db.adminCommand( { setFeatureCompatibilityVersion: \"${major_version}\" } )"
  70. debug "${WHITE}running:$NORMAL $cmd"
  71. out=$(ddb < <(echo "use $DBNAME";
  72. echo "$cmd"))
  73. if [[ "$out" == *"\"ok\" : 1"* ]]; then
  74. info "Feature Compatibility set to ${major_version}. "
  75. else
  76. err "Failed to set feature compatibieplicaSet initialisation failed:"
  77. echo "$out" | prefix " | " >&2
  78. exit 13
  79. fi