From fa6701aace2ef4bbebc4deac01529070b0df8427 Mon Sep 17 00:00:00 2001 From: Valentin Lab Date: Tue, 17 Dec 2024 10:43:12 +0100 Subject: [PATCH] new: [mongo] add support for 6.0.* version --- mongo/actions/upgrade | 48 +++++++++++++++++++-------- mongo/hooks/init | 5 +-- mongo/lib/common | 6 +++- mongo/upgrade/build/6.0.19/Dockerfile | 3 +- mongo/upgrade/build/7.0.15/Dockerfile | 3 +- 5 files changed, 46 insertions(+), 19 deletions(-) diff --git a/mongo/actions/upgrade b/mongo/actions/upgrade index 5c10ae9..1b370c2 100755 --- a/mongo/actions/upgrade +++ b/mongo/actions/upgrade @@ -79,31 +79,45 @@ mongo:image:version() { } mongo:container:version() { - local container="$1" - if ! out=$(docker exec -i "$container" mongo --version); then + local container="$1" version + if ! out=$(docker exec "$container" mongod --version); then err "Couldn't infer container's '$container' mongod's version." - exit 1 + return 1 fi out=${out%%$'\n'*} out=${out%%$'\r'*} - image_version=${out#db version v} - echo "$image_version" + version=${out#db version v} + echo "$version" } mongo:container:mongo () { local container="$1" + docker exec -i "$container" mongo 2>&1 } mongo:container:fix_compat() { - local container="$1" image_version="$2" - out=$(echo "db.adminCommand( { setFeatureCompatibilityVersion: \"${image_version%.*}\" } )" | - mongo:container:mongo "$container" 2>&1) - if [[ "$out" == *"\"ok\" : 1"* ]]; then + local container="$1" image_version="$2" version + + if ! version=$(mongo:container:version "$container"); then + err "Couldn't get container's mongod version" + return 1 + fi + + + ## No need of confirmation for version < 7.0.0 + if version_gt 7.0.0 "$version"; then + mongo_cmd="db.adminCommand( { setFeatureCompatibilityVersion: \"${image_version%.*}\" } )" + else + mongo_cmd="db.adminCommand( { setFeatureCompatibilityVersion: \"${image_version%.*}\", confirm: true } )" + fi + + out=$(echo "$mongo_cmd" | mongo:container:mongo "$container" 2>&1) + if [[ "$out" == *"\"ok\" : 1"* ]] || [[ "$out" == *"ok: 1"* ]]; then debug "Feature Compatibility set to ${WHITE}${image_version%.*}${NORMAL}" else err "Failed to set feature compatibility version to ${WHITE}${image_version%.*}${NORMAL}:" - echo "$out" | prefix " | " >&2 + printf "%s" "$out" | prefix " | " >&2 return 1 fi } @@ -202,13 +216,18 @@ if [ -n "$containers" ]; then #err "Running container(s) for $DARKYELLOW$SERVICE_NAME$NORMAL are still running:" for container in $containers; do debug "Stopping container $container" - docker stop "$container" >/dev/null || { + docker:container:stop "$container" >/dev/null || { err "Failed to stop container '$container'." exit 1 } - docker rm "$container" > /dev/null || { - err "Couldn't delete container '$container'." - } + docker rm "$container" > /dev/null + + ## Check that the container is actually removed + if [ -n "$(docker ps -q -f name="$container")" ]; then + err "Failed to remove container '$container'." + exit 1 + fi + container_stopped+=("$container") done fi @@ -339,6 +358,7 @@ if [ -z "$unexpected_stop" -a -z "$expected_stop" ]; then docker stop "$migration_container" fi + if [ -n "$failed" ]; then echo "${WHITE}Failing migration logs:${NORMAL}" >&2 cat "$MIGRATION_TMPDIR/migration.log" | prefix " ${GRAY}|${NORMAL} " >&2 diff --git a/mongo/hooks/init b/mongo/hooks/init index e140d0b..00081e1 100755 --- a/mongo/hooks/init +++ b/mongo/hooks/init @@ -93,9 +93,10 @@ ensure_db_docker_running cmd="rs.initiate({ _id: 'rs01', members: [ { _id: 0, host: '$SERVICE_NAME:27017' } ]})" debug "${WHITE}running:$NORMAL $cmd" -out=$(ddb <<<"$cmd") +out=$(ddb 2>&1 <<<"$cmd") || true -if [[ "$out" == *"\"codeName\" : \"AlreadyInitialized\""* ]]; then +if [[ "$out" == *"\"codeName\" : \"AlreadyInitialized\""* ]] || + [[ "$out" == *"MongoServerError: already initialized"* ]]; then info "ReplicaSet already initialized." elif [[ "$out" == *"\"ok\" : 1"* ]]; then info "ReplicaSet initialized. " diff --git a/mongo/lib/common b/mongo/lib/common index 8d3754f..68766e2 100644 --- a/mongo/lib/common +++ b/mongo/lib/common @@ -46,7 +46,11 @@ _set_db_params() { } -ddb() { dcmd mongo --quiet "$@"; } +ddb() { + local eval_code + eval_code="$(cat - )" + dcmd mongo --quiet "$@" --eval "$eval_code"