Browse Source

new: [mongo] make ``mongosh`` use ``ensure_db_docker_running`` instead of failing if no containers are up

boris/docuseal
Valentin Lab 1 year ago
parent
commit
433f58631b
  1. 54
      mongo/actions/relations/mongo-database/mongosh

54
mongo/actions/relations/mongo-database/mongosh

@ -22,12 +22,11 @@ service.
EXAMPLES:
$exname < foo.sql
$exname mydb2 < foo.sql
$exname < foo.mongosh
$exname mydb2 < foo.mongosh
"
dbname=
output=
while [ "$1" ]; do
@ -56,41 +55,34 @@ 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}."
if ! dbname=$(relation:get "$SERVICE_NAME":mongo-database dbname) || [ -z "$dbname" ]; then
err "Couldn't retrieve information of ${DARKCYAN}mongo-database${NORMAL}'s relation."
echo " You're database has maybe not yet be initialized." >&2
exit 1
fi
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
}
mongosh() {
local dbname="$1"
(
fi
export SERVICE_NAME="$RELATION_TARGET_SERVICE"
export SERVICE_DATASTORE="$DATASTORE/$SERVICE_NAME"
DOCKER_BASE_IMAGE=$(service_base_docker_image "$SERVICE_NAME")
export DOCKER_BASE_IMAGE
set -e
target_charm_path=$(charm.get_dir $RELATION_TARGET_CHARM) || exit 1
containers="$(get_running_containers_for_service "$RELATION_TARGET_SERVICE")"
set +e
if [ -z "$containers" ]; then
err "No containers running for service $DARKYELLOW$service$NORMAL."
exit 1
fi
. "$target_charm_path/lib/common"
set -e
ensure_db_docker_running
ddb "$dbname"
)
}
## 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"
mongosh "$dbname"
Loading…
Cancel
Save