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.

112 lines
2.6 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. CONFIG=$SERVICE_CONFIGSTORE/etc/mongod.conf
  14. mkdir -p "$(dirname "$CONFIG")"
  15. ## XXXvlab: replication here is hardwired and not handled at all
  16. cat <<EOF > "$CONFIG"
  17. storage:
  18. dbPath: /var/lib/mongodb
  19. net:
  20. bindIp: 0.0.0.0
  21. replication:
  22. oplogSizeMB: 128
  23. replSetName: rs01
  24. EOF
  25. chown -v "$uid" "$CONFIG"
  26. dirs=(/var/{log,lib}/mongodb )
  27. host_dirs=()
  28. for dir in "${dirs[@]}"; do
  29. host_dirs+=("$SERVICE_DATASTORE$dir")
  30. done
  31. mkdir -p "${host_dirs[@]}"
  32. find "${host_dirs[@]}" \! -user "$uid" -print0 | while read-0 f; do
  33. chown -v "$uid" "$f" || exit 1
  34. done
  35. config_hash=$(cat "$CONFIG" | md5_compat) || exit 1
  36. init-config-add "
  37. $MASTER_BASE_SERVICE_NAME:
  38. labels:
  39. - compose.config_hash=$config_hash
  40. "
  41. ensure_db_docker_running
  42. ## ReplicaSet initialization
  43. cmd="rs.initiate({ _id: 'rs01', members: [ { _id: 0, host: '$SERVICE_NAME:27017' } ]})"
  44. debug "${WHITE}running:$NORMAL $cmd"
  45. out=$(ddb <<<"$cmd")
  46. if [[ "$out" == *"\"codeName\" : \"AlreadyInitialized\""* ]]; then
  47. info "ReplicaSet already initialized."
  48. elif [[ "$out" == *"\"ok\" : 1"* ]]; then
  49. info "ReplicaSet initialized. "
  50. else
  51. err "ReplicaSet initialisation failed:"
  52. echo "$out" >&2
  53. exit 13
  54. fi
  55. ## Enable read if db version >= 4.2
  56. if ! version=$(mongo:db:version); then
  57. err "Couldn't get database version"
  58. exit 1
  59. fi
  60. echo "Current mongo database version: '$version'." >&2
  61. if version_gt "$version" 4.1; then
  62. cmd="db.getMongo().setSecondaryOk()"
  63. debug "${WHITE}running:$NORMAL $cmd"
  64. out=$(ddb <<<"$cmd") || {
  65. err "Failed database command. Output:"
  66. echo "$out" | prefix " | " >&2
  67. exit 1
  68. }
  69. fi
  70. ## This is some sort of migrating code and should be moved to upgrade
  71. ## directory.
  72. major_version=${version%.*}
  73. cmd="db.adminCommand( { setFeatureCompatibilityVersion: \"${major_version}\" } )"
  74. debug "${WHITE}running:$NORMAL $cmd"
  75. out=$(ddb <<<"$cmd")
  76. if [[ "$out" == *"\"ok\" : 1"* ]]; then
  77. info "Feature Compatibility set to ${major_version}. "
  78. else
  79. err "Failed to set feature compatibility version failed:"
  80. echo "$out" | prefix " | " >&2
  81. exit 13
  82. fi