Browse Source

fix: [mongo] direct install didn't work anymore.

Signed-off-by: Valentin Lab <valentin.lab@kalysto.org>
0k/dev/master
Valentin Lab 5 years ago
parent
commit
db4deb4fc4
  1. 10
      mongo/hooks/mongo_database-relation-joined
  2. 43
      mongo/lib/common

10
mongo/hooks/mongo_database-relation-joined

@ -10,8 +10,10 @@ DBNAME=$(relation-get dbname) || {
} }
dbs=$(mongo:db:ls) || exit 1
if matching_db=$(e "$dbs" | egrep "[a-zA-Z0-9.-_]*_${DBNAME}"); then
ensure_db_docker_running
if dbs=$(mongo:db:ls); then
if matching_db=$(e "$dbs" | egrep "[a-zA-Z0-9.-_]*_${DBNAME}"); then
if e "$dbs" | grep "^${DBNAME}$"; then if e "$dbs" | grep "^${DBNAME}$"; then
err "Error: unexpected database '$matching_db'" \ err "Error: unexpected database '$matching_db'" \
"found along with database named '${DBNAME}'." "found along with database named '${DBNAME}'."
@ -33,6 +35,10 @@ if matching_db=$(e "$dbs" | egrep "[a-zA-Z0-9.-_]*_${DBNAME}"); then
## we DO NOT want to start the stopped container again as they are not ## we DO NOT want to start the stopped container again as they are not
## properly configured ## properly configured
fi
else
err "Failed to query for database list"
exit 14
fi fi
## ReplicaSet initialization ## ReplicaSet initialization

43
mongo/lib/common

@ -20,23 +20,60 @@ _set_server_db_params() {
_set_db_params() { _set_db_params() {
local docker_ip="$1" docker_network="$2" local docker_ip="$1" docker_network="$2"
## XXXvlab: include this in compose-core
if [ "${_db_params}" != "$docker_ip:$docker_network" ]; then
db_docker_opts+=("--network" "$docker_network") db_docker_opts+=("--network" "$docker_network")
db_cmd_opts+=("--host" "$docker_ip") db_cmd_opts+=("--host" "$docker_ip")
check_command="db.serverStatus().ok" check_command="db.serverStatus().ok"
_db_params="$docker_ip:$docker_network"
echo callers: "${FUNCNAME[@]}" >&2
fi
} }
ddb() { dcmd mongo --quiet "$@"; } ddb() { dcmd mongo --quiet "$@"; }
djson() {
local out err
if ! out=$(ddb); then
err "Mongo call failed"
return 128
fi
if [ -z "$out" ]; then
err "Mongo replied with empty output"
return 64
fi
if [ "$(e "$out" | jq -r .ok)" != "1" ]; then
if ! e "$out" | jq . >/dev/null 2>&1; then
err "Mongo didn't output JSON, output follows:"
e "$out" | prefix " | "
return 32
fi >&2
errmsg=$(e "$out" | jq -r '.errmsg')
code_name=$(e "$out" | jq -r '.codeName')
err "Mongo failed with error message: $errmsg (code: ${code_name})"
e "$out"
return 32
fi
e "$out"
return 0
}
mongo:db:ls() { mongo:db:ls() {
local out local out
if ! out=$(ddb < <(echo "JSON.stringify(db.adminCommand( { listDatabases: 1 } ))")); then
if out=$(djson < <(echo "JSON.stringify(db.adminCommand( { listDatabases: 1 } ))")); then
e "$out" | jq -r '.databases[] | .name'
return 0
else
if [ "$(e "$out" | jq -r '.codeName')" == "NotMasterNoSlaveOk" ]; then
## equivalent to having no databases
return 0
fi
err "Could not query list of databases." err "Could not query list of databases."
return 1 return 1
fi fi
e "$out" | jq -r '.databases[] | .name'
} }

Loading…
Cancel
Save