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.

48 lines
1.4 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="$(relation-get password 2>/dev/null)" || PASSWORD="$(gen_password)"
  15. POSTGIS=$(relation-get postgis 2>/dev/null) || true
  16. UNACCENT=$(relation-get unaccent 2>/dev/null) || true
  17. if ! ensure_db_docker_running; then
  18. die "Can't ensure valid link to postgres"
  19. fi
  20. db_has_database "$DBNAME" || UNACCENT="$UNACCENT" POSTGIS="$POSTGIS" db_create "$DBNAME"
  21. if ! db_has_user "$USER"; then
  22. info "Creating a new user $USER."
  23. db_create_user "$USER" "$PASSWORD"
  24. else
  25. info "Updating password of user $USER."
  26. db_change_password "$USER" "$PASSWORD"
  27. fi
  28. db_grant_rights "$DBNAME" "$USER"
  29. pgpass_line="*:*:*:$USER:$PASSWORD"
  30. pgpass_file="$CONFIGSTORE/$BASE_SERVICE_NAME/root/.pgpass"
  31. if [ -e "$pgpass_file" ]; then
  32. sed -ri "/^.+:.+:.+:$USER:.*$/d" "$pgpass_file"
  33. fi
  34. mkdir -p "$(dirname "$pgpass_file")"
  35. echo "$pgpass_line" >> "$pgpass_file"
  36. chmod 600 "$pgpass_file"
  37. relation-set password "$PASSWORD"