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.

88 lines
1.8 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.mongosh
  19. $exname mydb2 < foo.mongosh
  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. if ! dbname=$(relation:get "$SERVICE_NAME":mongo-database dbname) || [ -z "$dbname" ]; then
  48. err "Couldn't retrieve information of ${DARKCYAN}mongo-database${NORMAL}'s relation."
  49. echo " You're database has maybe not yet be initialized." >&2
  50. exit 1
  51. fi
  52. fi
  53. mongosh() {
  54. local dbname="$1"
  55. (
  56. export SERVICE_NAME="$RELATION_TARGET_SERVICE"
  57. export SERVICE_DATASTORE="$DATASTORE/$SERVICE_NAME"
  58. DOCKER_BASE_IMAGE=$(service_base_docker_image "$SERVICE_NAME")
  59. export DOCKER_BASE_IMAGE
  60. target_charm_path=$(charm.get_dir $RELATION_TARGET_CHARM) || exit 1
  61. set +e
  62. . "$target_charm_path/lib/common"
  63. set -e
  64. ensure_db_docker_running
  65. ddb "$dbname"
  66. )
  67. }
  68. mongosh "$dbname"