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.

46 lines
1.3 KiB

  1. #!/bin/bash
  2. ## When writing relation script, remember:
  3. ## - they should be idempotents
  4. ## - they can be launched while the dockers is already up
  5. ## - they are launched from the host
  6. ## - the target of the link is launched first, and get a chance to ``relation-set``
  7. ## - both side of the scripts get to use ``relation-get``.
  8. ## could generate this also if not set
  9. DBNAME=$(relation-get dbname)
  10. [ "$(relation-get password 2>/dev/null)" ] && exit 0
  11. . lib/common
  12. set -e
  13. USER=$(relation-get user)
  14. PASSWORD="$(gen_password)"
  15. POSTGIS=$(relation-get postgis 2>/dev/null) || true
  16. UNACCENT=$(relation-get unaccent 2>/dev/null) || true
  17. ensure_db_docker_running
  18. db_has_database "$DBNAME" || UNACCENT="$UNACCENT" POSTGIS="$POSTGIS" db_create "$DBNAME"
  19. if ! db_has_user "$USER"; then
  20. info "Creating a new user $USER."
  21. db_create_user "$USER" "$PASSWORD"
  22. else
  23. info "Updating password of user $USER."
  24. db_change_password "$USER" "$PASSWORD"
  25. fi
  26. db_grant_rights "$DBNAME" "$USER"
  27. pgpass_line="*:*:*:$USER:$PASSWORD"
  28. pgpass_file="$CONFIGSTORE/$BASE_SERVICE_NAME/root/.pgpass"
  29. if [ -e "$pgpass_file" ]; then
  30. sed -ri "/^.+:.+:.+:$USER:.*$/d" "$pgpass_file"
  31. fi
  32. mkdir -p "$(dirname "$pgpass_file")"
  33. echo "$pgpass_line" >> "$pgpass_file"
  34. chmod 600 "$pgpass_file"
  35. relation-set password "$PASSWORD"