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.

71 lines
2.0 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. if dbs=$(mongo:db:ls); then
  28. if matching_db=$(e "$dbs" | egrep "[a-zA-Z0-9.-_]*_${DBNAME}"); then
  29. if e "$dbs" | grep "^${DBNAME}$"; then
  30. err "Error: unexpected database '$matching_db'" \
  31. "found along with database named '${DBNAME}'."
  32. exit 1
  33. fi
  34. ## Let's take for granted that only current source service is
  35. ## concerned by the database. We need to restart source service
  36. ## container only if already running.
  37. container_ids=($(get_running_containers_for_service "$MASTER_BASE_SERVICE_NAME"))
  38. for container_id in "${container_ids[@]}"; do
  39. mongo_url=$(docker:container:env_get "$container_id" MONGO_URL) || return 1
  40. if [[ "$mongo_url" == */"${matching_db}"* ]]; then
  41. info "Attempting to stop current containers of service $MASTER_BASE_SERVICE_NAME"
  42. docker stop -t 5 "$container_id"
  43. fi
  44. done
  45. mongo:db:rename "$matching_db" "${DBNAME}"
  46. ## we DO NOT want to start the stopped container again as they are not
  47. ## properly configured
  48. fi
  49. else
  50. err "Failed to query for database list"
  51. exit 14
  52. fi