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.

56 lines
1.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. dbs=$(mongo:db:ls) || exit 1
  9. if matching_db=$(e "$dbs" | egrep "[a-zA-Z0-9.-_]*_${DBNAME}"); then
  10. if e "$dbs" | grep "^${DBNAME}$"; then
  11. err "Error: unexpected database '$matching_db'" \
  12. "found along with database named '${DBNAME}'."
  13. exit 1
  14. fi
  15. ## Let's take for granted that only current source service is
  16. ## concerned by the database. We need to restart source service
  17. ## container only if already running.
  18. container_ids=($(get_running_containers_for_service "$MASTER_BASE_SERVICE_NAME"))
  19. for container_id in "${container_ids[@]}"; do
  20. mongo_url=$(docker:container:env_get "$container_id" MONGO_URL) || return 1
  21. if [[ "$mongo_url" == */"${matching_db}"* ]]; then
  22. info "Attempting to stop current containers of service $MASTER_BASE_SERVICE_NAME"
  23. docker stop -t 5 "$container_id"
  24. fi
  25. done
  26. mongo:db:rename "$matching_db" "${DBNAME}"
  27. ## we DO NOT want to start the stopped container again as they are not
  28. ## properly configured
  29. fi
  30. ## ReplicaSet initialization
  31. cmd="rs.initiate({ _id: 'rs01', members: [ { _id: 0, host: '$TARGET_SERVICE_NAME:27017' } ]})"
  32. debug "${WHITE}running:$NORMAL $cmd"
  33. out=$(ddb < <(echo "use $DBNAME";
  34. echo "$cmd"))
  35. if [[ "$out" == *"\"codeName\" : \"AlreadyInitialized\""* ]]; then
  36. exit 0
  37. fi
  38. if [[ "$out" == *"\"ok\" : 1"* ]]; then
  39. exit 0
  40. fi
  41. err Replicate Set initialisation failed
  42. echo "$out" >&2