Browse Source

fix: dev: [rocketchat,mongo] do not use ``$PROJECT_NAME`` in mongo database url.

Indeed, when changing project name, ``rocketchat`` would create a new
database and would not be able to use previous database.

``mongo`` side is here to ensure proper update of existing installation
that would use previous naming scheme.

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

27
mongo/hooks/mongo_database-relation-joined

@ -10,6 +10,33 @@ DBNAME=$(relation-get dbname) || {
}
dbs=$(mongo:db:ls) || exit 1
if matching_db=$(e "$dbs" | egrep "[a-zA-Z0-9.-_]*_${DBNAME}"); then
if e "$dbs" | grep "^${DBNAME}$"; then
err "Error: unexpected database '$matching_db'" \
"found along with database named '${DBNAME}'."
exit 1
fi
## Let's take for granted that only current source service is
## concerned by the database. We need to restart source service
## container only if already running.
container_ids=($(get_running_containers_for_service "$MASTER_BASE_SERVICE_NAME"))
for container_id in "${container_ids[@]}"; do
mongo_url=$(docker:container:env_get "$container_id" MONGO_URL) || return 1
if [[ "$mongo_url" == */"${matching_db}"* ]]; then
info "Attempting to stop current containers of service $MASTER_BASE_SERVICE_NAME"
docker stop -t 5 "$container_id"
fi
done
mongo:db:rename "$matching_db" "${DBNAME}"
## we DO NOT want to start the stopped container again as they are not
## properly configured
fi
## ReplicaSet initialization
cmd="rs.initiate({ _id: 'rs01', members: [ { _id: 0, host: '$TARGET_SERVICE_NAME:27017' } ]})"
debug "${WHITE}running:$NORMAL $cmd"

28
mongo/lib/common

@ -27,4 +27,30 @@ _set_db_params() {
check_command="db.serverStatus().ok"
}
ddb () { dcmd mongo "$@"; }
ddb() { dcmd mongo --quiet "$@"; }
mongo:db:ls() {
local out
if ! out=$(ddb < <(echo "JSON.stringify(db.adminCommand( { listDatabases: 1 } ))")); then
err "Could not query list of databases."
return 1
fi
e "$out" | jq -r '.databases[] | .name'
}
mongo:db:rename() {
local src="$1" dst="$2" out
if ! out=$(ddb <<EOF
db.copyDatabase("$src","$dst");
use $src;
db.dropDatabase();
EOF
); then
err "Could not rename database '$src' to '$dst'."
err "$out"
return 1
fi
debug "$out"
}

2
rocketchat/hooks/mongo_database-relation-joined

@ -5,7 +5,7 @@ set -e
DBNAME="$(relation-get dbname)" || exit 1
MONGO_URL=mongodb://${TARGET_SERVICE_NAME}:27017/${PROJECT_NAME}_${DBNAME}?replicaSet=rs01
MONGO_URL=mongodb://${TARGET_SERVICE_NAME}:27017/${DBNAME}?replicaSet=rs01
MONGO_OPLOG_URL=mongodb://${TARGET_SERVICE_NAME}:27017/local?replicaSet=rs01
config-add "\

Loading…
Cancel
Save