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.

88 lines
2.6 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. ## Enable read if db version >= 4.2
  12. if ! version=$(mongo:db:version); then
  13. err "Couldn't get database version"
  14. exit 1
  15. fi
  16. if version_gt "6.0" "$version"; then
  17. ## compatible ddb for mongo <= 6.0
  18. ddb() { dcmd mongo --quiet "$@"; }
  19. fi
  20. echo "Current mongo database version: '$version'." >&2
  21. if version_gt "$version" 4.1; then
  22. cmd="db.getMongo().setSecondaryOk()"
  23. debug "${WHITE}running:$NORMAL $cmd"
  24. out=$(ddb < <(echo "use $DBNAME";
  25. echo "$cmd")) || {
  26. err "Failed database command. Output:"
  27. echo "$out" | prefix " | " >&2
  28. exit 1
  29. }
  30. fi
  31. ## This is some sort of migrating code and should be moved to upgrade
  32. ## directory.
  33. if dbs=$(mongo:db:ls); then
  34. if matching_db=$(e "$dbs" | egrep "[a-zA-Z0-9.-_]*_${DBNAME}"); then
  35. if e "$dbs" | grep "^${DBNAME}$"; then
  36. err "Error: unexpected database '$matching_db'" \
  37. "found along with database named '${DBNAME}'."
  38. exit 1
  39. fi
  40. ## XXXvlab: there's probably an unsolved issue here when
  41. ## =ensure_db_docker_running= returned the current running
  42. ## service container, that we are about to stop in the
  43. ## following lines. I'm not digging much into that because
  44. ## this code is probably not concerning any current live
  45. ## deployment anymore: this was written for an evolution of
  46. ## the charm a long time ago, and probably should be deleted
  47. ## now.
  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) || exit 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" || exit 1
  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