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.

96 lines
2.1 KiB

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