diff --git a/mongo/actions/relations/mongo-database/mongosh b/mongo/actions/relations/mongo-database/mongosh new file mode 100755 index 0000000..03b47a6 --- /dev/null +++ b/mongo/actions/relations/mongo-database/mongosh @@ -0,0 +1,96 @@ +#!/bin/bash +## compose: no-hooks + +if [ -z "$SERVICE_DATASTORE" ]; then + echo "This script is meant to be run through 'compose' to work properly." >&2 + exit 1 +fi + +version=0.1 +usage="$exname [-h|--help] DBNAME" +help=" +USAGE: + +$usage + +DESCRIPTION: + +Read stdin and content to related $CHARM_NAME service in the database +DBNAME. If DBNAME is not provided, it'll take the default database +from the ${DARKCYAN}mongo-database${NORMAL} relation of current +service. + +EXAMPLES: + + $exname < foo.sql + $exname mydb2 < foo.sql + +" + + +dbname= +output= +while [ "$1" ]; do + case "$1" in + "--help"|"-h") + print_help >&2 + exit 0 + ;; + "--force"|"-f") + force=yes + ;; + --*|-*) + err "Unexpected optional argument '$1'" + print_usage >&2 + exit 1 + ;; + *) + [ -z "$dbname" ] && { dbname=$1 ; shift ; continue ; } + err "Unexpected positional argument '$1'" + print_usage >&2 + exit 1 + ;; + esac + shift +done + + +if [ -z "$dbname" ]; then + + ## + ## Fetch default dbname in relation to mongo-database + ## + + ## XXXvlab: can't get real config here + if ! read-0 ts _ _ < <(get_service_relation "$SERVICE_NAME" "mongo-database"); then + err "Couldn't find relation ${DARKCYAN}mongo-database${NORMAL}." + exit 1 + fi + + relation_dir=$(get_relation_data_dir "$SERVICE_NAME" "$ts" "mongo-database") || { + err "Failed to find relation dir" + exit 1 + } + mongo_config=$(cat "$relation_dir"/data) || exit 2 + + dbname="$(e "$mongo_config" | shyaml get-value dbname)" || { + err "Couldn't retrieve information of ${DARKCYAN}mongo-database${NORMAL}'s relation." + exit 1 + } + +fi + +set -e + +containers="$(get_running_containers_for_service "$RELATION_TARGET_SERVICE")" + +if [ -z "$containers" ]; then + err "No containers running for service $DARKYELLOW$service$NORMAL." + exit 1 +fi + + +## XXXvlab: taking first container is probably not a good idea +container_id="$(echo "$containers" | head -n 1)" +docker exec -i -u 0 \ + "${container_id}" mongo --quiet "$dbname"