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.

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