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.

110 lines
2.6 KiB

  1. #!/bin/bash
  2. ## Load action gets a first argument a FILE/DIRECTORY/URL holding the necessary files.
  3. ##
  4. ##
  5. if [ -z "$SERVICE_DATASTORE" ]; then
  6. echo "This script is meant to be run through 'compose' to work properly." >&2
  7. exit 1
  8. fi
  9. version=0.1
  10. usage="$exname [-h|--help] DBNAME"
  11. help="
  12. USAGE:
  13. $usage
  14. DESCRIPTION:
  15. Read stdin and content to related postgres service in the database
  16. DBNAME. If DBNAME is not provided, it'll take the default database
  17. from the ${DARKCYAN}postgres-database${NORMAL} relation of current
  18. service.
  19. EXAMPLES:
  20. $exname < foo.sql
  21. $exname mydb2 < foo.sql
  22. "
  23. dbname=
  24. output=
  25. while [ "$1" ]; do
  26. case "$1" in
  27. "--help"|"-h")
  28. print_help >&2
  29. exit 0
  30. ;;
  31. "--force"|"-f")
  32. force=yes
  33. ;;
  34. --*|-*)
  35. err "Unexpected optional argument '$1'"
  36. print_usage >&2
  37. exit 1
  38. ;;
  39. *)
  40. [ -z "$dbname" ] && { dbname=$1 ; shift ; continue ; }
  41. err "Unexpected positional argument '$1'"
  42. print_usage >&2
  43. exit 1
  44. ;;
  45. esac
  46. shift
  47. done
  48. if [ -z "$dbname" ]; then
  49. ##
  50. ## Fetch default dbname in relation to postgres-database
  51. ##
  52. ## XXXvlab: can't get real config here
  53. if ! read-0 ts _ _ < <(get_service_relation "$SERVICE_NAME" "postgres-database"); then
  54. err "Couldn't find relation ${DARKCYAN}postgres-database${NORMAL}."
  55. exit 1
  56. fi
  57. relation_dir=$(get_relation_data_dir "$SERVICE_NAME" "$ts" "postgres-database") || {
  58. err "Failed to find relation dir"
  59. exit 1
  60. }
  61. postgres_config=$(cat "$relation_dir"/data) || exit 2
  62. dbname="$(e "$postgres_config" | shyaml get-value dbname)" || {
  63. err "Couldn't retrieve information of ${DARKCYAN}postgres-database${NORMAL}'s relation."
  64. exit 1
  65. }
  66. dbuser="$(e "$postgres_config" | shyaml get-value user)" || {
  67. err "Couldn't retrieve information of ${DARKCYAN}postgres-database${NORMAL}'s relation."
  68. exit 1
  69. }
  70. password="$(e "$postgres_config" | shyaml get-value password)" || {
  71. err "Couldn't retrieve information of ${DARKCYAN}postgres-database${NORMAL}'s relation."
  72. exit 1
  73. }
  74. fi
  75. set -e
  76. containers="$(get_running_containers_for_service "$RELATION_TARGET_SERVICE")"
  77. if [ -z "$containers" ]; then
  78. err "No containers running for service $DARKYELLOW$service$NORMAL."
  79. exit 1
  80. fi
  81. ## XXXvlab: taking first container is probably not a good idea
  82. container_id="$(echo "$containers" | head -n 1)"
  83. docker exec -i -u 0 \
  84. -e PGUSER="$dbuser" \
  85. -e PGPASSWORD="$password" \
  86. "${container_id}" psql -qAt "$dbname"