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.

83 lines
2.5 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. echo "Current mongo database version: '$version'." >&2
  17. if version_gt "$version" 4.1; then
  18. cmd="db.getMongo().setSecondaryOk()"
  19. debug "${WHITE}running:$NORMAL $cmd"
  20. out=$(ddb < <(echo "use $DBNAME";
  21. echo "$cmd")) || {
  22. err "Failed database command. Output:"
  23. echo "$out" | prefix " | " >&2
  24. exit 1
  25. }
  26. fi
  27. ## This is some sort of migrating code and should be moved to upgrade
  28. ## directory.
  29. if dbs=$(mongo:db:ls); then
  30. if matching_db=$(e "$dbs" | egrep "[a-zA-Z0-9.-_]*_${DBNAME}"); then
  31. if e "$dbs" | grep "^${DBNAME}$"; then
  32. err "Error: unexpected database '$matching_db'" \
  33. "found along with database named '${DBNAME}'."
  34. exit 1
  35. fi
  36. ## XXXvlab: there's probably an unsolved issue here when
  37. ## =ensure_db_docker_running= returned the current running
  38. ## service container, that we are about to stop in the
  39. ## following lines. I'm not digging much into that because
  40. ## this code is probably not concerning any current live
  41. ## deployment anymore: this was written for an evolution of
  42. ## the charm a long time ago, and probably should be deleted
  43. ## now.
  44. ## Let's take for granted that only current source service is
  45. ## concerned by the database. We need to restart source service
  46. ## container only if already running.
  47. container_ids=($(get_running_containers_for_service "$MASTER_BASE_SERVICE_NAME"))
  48. for container_id in "${container_ids[@]}"; do
  49. mongo_url=$(docker:container:env_get "$container_id" MONGO_URL) || exit 1
  50. if [[ "$mongo_url" == */"${matching_db}"* ]]; then
  51. info "Attempting to stop current containers of service $MASTER_BASE_SERVICE_NAME"
  52. docker stop -t 5 "$container_id" || exit 1
  53. fi
  54. done
  55. mongo:db:rename "$matching_db" "${DBNAME}"
  56. ## we DO NOT want to start the stopped container again as they are not
  57. ## properly configured
  58. fi
  59. else
  60. err "Failed to query for database list"
  61. exit 14
  62. fi