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.

65 lines
1.7 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. . lib/common
  9. set -e
  10. DBNAME=$(relation-get dbname) || {
  11. DBNAME=$(service:map:get_one "dbname") || exit 1
  12. DBNAME="${DBNAME:-$BASE_SERVICE_NAME}"
  13. relation-set dbname "$DBNAME"
  14. }
  15. USER=$(relation-get user) || {
  16. USER=$(service:map:get_one "user") || exit 1
  17. USER="${USER:-$BASE_SERVICE_NAME}"
  18. relation-set user "$USER"
  19. }
  20. HOST=$(options-get host) || true
  21. if [ -z "$HOST" ]; then
  22. err "No host specified. Please specify ${WHITE}host${NORMAL}" \
  23. "in ${DARKYELLOW}$SERVICE_NAME${NORMAL}'s option."
  24. exit 1
  25. fi
  26. if [[ "$HOST" =~ :[0-9]+$ ]]; then
  27. PORT="${HOST##*:}"
  28. HOST="${HOST%:*}"
  29. fi
  30. if ! [[ "$HOST" =~ ^[0-9a-zA-Z.-]+$ ]]; then
  31. err "Invalid host specified: '$HOST'. Please provide a valid" \
  32. "network target as ${WHITE}host${NORMAL} value" \
  33. "in ${DARKYELLOW}$SERVICE_NAME${NORMAL}'s option."
  34. exit 1
  35. fi
  36. PORT=${PORT:-5432}
  37. relation-set host "$HOST"
  38. relation-set port "$PORT"
  39. set -e
  40. if ! PASSWORD="$(relation-get password 2>/dev/null)"; then
  41. err "Please provide a password"
  42. exit 1
  43. fi
  44. echo "SELECT 1;" | ddb "$DBNAME" || {
  45. err "Connection failed to $DBNAME on $HOST:$PORT with user $USER and given password"
  46. exit 1
  47. }
  48. array_read-0 extensions < <(relation-get extensions 2>/dev/null | shyaml get-values-0)
  49. if [ "${#extensions[@]}" -gt 0 ]; then
  50. db_install_extensions "$DBNAME" "${extensions[@]}" || exit 1
  51. fi