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.

74 lines
1.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. 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