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.

109 lines
2.7 KiB

  1. #!/bin/bash
  2. ## Init is run on host
  3. ## For now it is run every time the script is launched, but
  4. ## it should be launched only once after build.
  5. ## Accessible variables are:
  6. ## - SERVICE_NAME Name of current service
  7. ## - DOCKER_BASE_IMAGE Base image from which this service might be built if any
  8. ## - SERVICE_DATASTORE Location on host of the DATASTORE of this service
  9. ## - SERVICE_CONFIGSTORE Location on host of the CONFIGSTORE of this service
  10. . lib/common
  11. set -e
  12. uid=$(docker_get_uid "$SERVICE_NAME" "mongodb")
  13. ## Check for old bogus version, and stop and remove container
  14. ## if it is running.
  15. containers=($(get_running_containers_for_service "$SERVICE_NAME"))
  16. if [[ "${#containers[@]}" -gt 1 ]]; then
  17. err "More than 1 container running for service $DARKYELLOW$SERVICE_NAME$NORMAL."
  18. echo " Please contact administrator to fix this issue." >&2
  19. return 1
  20. fi
  21. if [[ "${#containers[@]}" == 1 ]]; then
  22. ## We need to check that this running container
  23. ## has same uid as the new image. If not, we need
  24. ## to stop and remove it.
  25. container_id=${containers[0]}
  26. container_uid=$(docker exec "$container_id" id -u mongodb)
  27. if [[ "$container_uid" != "$uid" ]]; then
  28. info "Container $container_id is running with different uid than new image."
  29. info "Stopping container $container_id."
  30. docker stop "$container_id" &&
  31. docker rm "$container_id"
  32. fi
  33. fi
  34. ##
  35. ## Normal init
  36. ##
  37. CONFIG=$SERVICE_CONFIGSTORE/etc/mongod.conf
  38. mkdir -p "$(dirname "$CONFIG")"
  39. ## XXXvlab: replication here is hardwired and not handled at all
  40. cat <<EOF > "$CONFIG"
  41. storage:
  42. dbPath: /var/lib/mongodb
  43. net:
  44. bindIp: 0.0.0.0
  45. replication:
  46. oplogSizeMB: 128
  47. replSetName: rs01
  48. EOF
  49. chown -v "$uid" "$CONFIG"
  50. dirs=(/var/{log,lib}/mongodb )
  51. host_dirs=()
  52. for dir in "${dirs[@]}"; do
  53. host_dirs+=("$SERVICE_DATASTORE$dir")
  54. done
  55. mkdir -p "${host_dirs[@]}"
  56. find "${host_dirs[@]}" \! -user "$uid" -print0 | while read-0 f; do
  57. chown -v "$uid" "$f" || exit 1
  58. done
  59. config_hash=$(cat "$CONFIG" | md5_compat) || exit 1
  60. init-config-add "
  61. $MASTER_BASE_SERVICE_NAME:
  62. labels:
  63. - compose.config_hash=$config_hash
  64. "
  65. ensure_db_docker_running
  66. ## ReplicaSet initialization
  67. cmd="rs.initiate({ _id: 'rs01', members: [ { _id: 0, host: '$SERVICE_NAME:27017' } ]})"
  68. debug "${WHITE}running:$NORMAL $cmd"
  69. out=$(ddb 2>&1 <<<"$cmd") || true
  70. if [[ "$out" == *"\"codeName\" : \"AlreadyInitialized\""* ]] ||
  71. [[ "$out" == *"MongoServerError: already initialized"* ]]; then
  72. info "ReplicaSet already initialized."
  73. elif [[ "$out" == *"\"ok\" : 1"* ]]; then
  74. info "ReplicaSet initialized. "
  75. else
  76. err "ReplicaSet initialisation failed:"
  77. echo "$out" >&2
  78. exit 13
  79. fi