forked from 0k/0k-charms
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.
83 lines
2.5 KiB
83 lines
2.5 KiB
#!/bin/bash
|
|
|
|
. lib/common
|
|
|
|
set -e
|
|
|
|
DBNAME=$(relation-get dbname) || {
|
|
DBNAME="${BASE_SERVICE_NAME//\./_}"
|
|
relation-set dbname "$DBNAME"
|
|
}
|
|
|
|
|
|
## From: https://stackoverflow.com/questions/16989598
|
|
function version_gt() { test "$(printf '%s\n' "$@" | sort -V | head -n 1)" != "$1"; }
|
|
|
|
|
|
ensure_db_docker_running
|
|
|
|
|
|
## Enable read if db version >= 4.2
|
|
|
|
if ! version=$(mongo:db:version); then
|
|
err "Couldn't get database version"
|
|
exit 1
|
|
fi
|
|
|
|
echo "Current mongo database version: '$version'." >&2
|
|
|
|
if version_gt "$version" 4.1; then
|
|
cmd="db.getMongo().setSecondaryOk()"
|
|
debug "${WHITE}running:$NORMAL $cmd"
|
|
|
|
out=$(ddb < <(echo "use $DBNAME";
|
|
echo "$cmd")) || {
|
|
err "Failed database command. Output:"
|
|
echo "$out" | prefix " | " >&2
|
|
exit 1
|
|
}
|
|
|
|
fi
|
|
|
|
## This is some sort of migrating code and should be moved to upgrade
|
|
## directory.
|
|
|
|
if dbs=$(mongo:db:ls); then
|
|
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
|
|
|
|
## XXXvlab: there's probably an unsolved issue here when
|
|
## =ensure_db_docker_running= returned the current running
|
|
## service container, that we are about to stop in the
|
|
## following lines. I'm not digging much into that because
|
|
## this code is probably not concerning any current live
|
|
## deployment anymore: this was written for an evolution of
|
|
## the charm a long time ago, and probably should be deleted
|
|
## now.
|
|
|
|
## 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) || exit 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" || exit 1
|
|
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
|
|
else
|
|
err "Failed to query for database list"
|
|
exit 14
|
|
fi
|
|
|