56 lines
1.9 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. # Please note that postgres detect on its own if its datadir needs to be populated
  11. . lib/common
  12. set -e
  13. if ! [ -f "$LOCAL_DB_PASSFILE" ]; then
  14. POSTGRES_ROOT_PASSWORD="$(gen_password)"
  15. ## required by image of postgres for first run
  16. server_docker_opts=("${server_docker_opts[@]}" -e POSTGRES_HOST_AUTH_METHOD=trust)
  17. ddb < <(echo "ALTER USER postgres WITH ENCRYPTED password '$POSTGRES_ROOT_PASSWORD'")
  18. cat <<EOF > "$LOCAL_DB_PASSFILE"
  19. *:*:*:postgres:$POSTGRES_ROOT_PASSWORD
  20. EOF
  21. chmod 600 "$LOCAL_DB_PASSFILE"
  22. info "New root password for postgres."
  23. ## resetting ``server_docker_opts``, and forcing stop to remove the
  24. ## POSTGRES_HOST_AUTH_METHOD arg.
  25. docker stop "$container_id"
  26. docker rm "$container_id"
  27. server_docker_opts=()
  28. _set_server_db_params
  29. fi
  30. if ! egrep "^host all all (0.0.0.0/0|all) md5\$" "$PG_HBA" >/dev/null 2>&1; then
  31. if egrep "^host all all (0.0.0.0/0|all) trust\$" "$PG_HBA" >/dev/null 2>&1; then
  32. sed -ri 's%^host all all (0\.0\.0\.0/0|all) trust$%host all all \1 md5%g' \
  33. "$PG_HBA"
  34. if is_db_locked; then
  35. ensure_db_docker_running
  36. docker restart "$container_id"
  37. info "Restarted container $container_id"
  38. fi
  39. info "Accepting connection from outside."
  40. else
  41. die "Can't ensure connection from outside. Please update the charm init script."
  42. fi
  43. fi