diff --git a/deploy/mongodb/scripts/replicaset-post-start.tpl b/deploy/mongodb/scripts/replicaset-post-start.tpl index c7697a7d9b9..79c91638b1f 100644 --- a/deploy/mongodb/scripts/replicaset-post-start.tpl +++ b/deploy/mongodb/scripts/replicaset-post-start.tpl @@ -15,10 +15,22 @@ PORT={{ $mongodb_port }} MONGODB_ROOT={{ $mongodb_root }} INDEX=$(echo $KB_POD_NAME | grep -o "\-[0-9]\+\$"); INDEX=${INDEX#-}; -if [ $INDEX -ne 0 ]; then exit 0; fi +if [ $INDEX -ne 0 ]; then + exit 0 +fi until mongosh --quiet --port $PORT --eval "print('I am ready')"; do sleep 1; done +until is_inited=$(mongosh --quiet --port $PORT --eval "rs.status().set" -u root --password $MONGODB_ROOT_PASSWORD || mongosh --quiet --port $PORT --eval "try { rs.status().set } catch (e) { '' }") ; do + sleep 1; +done + +echo is_inited: $is_inited + +if [ $is_inited ]; then + exit 0 +fi + RPL_SET_NAME=$(echo $KB_POD_NAME | grep -o ".*-"); RPL_SET_NAME=${RPL_SET_NAME%-}; @@ -40,16 +52,11 @@ done CONFIGSVR="" if [ ""$IS_CONFIGSVR = "true" ]; then CONFIGSVR="configsvr: true,"; fi -until is_inited=$(mongosh --quiet --port $PORT --eval "rs.status().set" -u root --password $MONGODB_ROOT_PASSWORD || mongosh --quiet --port $PORT --eval "try { rs.status().set } catch (e) { '' }") ; do sleep 1; done - -echo is_inited: $is_inited -if [ -z $is_inited ]; then - echo "initiate replset" - sleep 10 - set -e - mongosh --quiet --port $PORT --eval "rs.initiate({_id: \"$RPL_SET_NAME\", $CONFIGSVR members: [$MEMBERS]})"; - set +e -fi; +echo "initiate replset" +sleep 10 +set -e +mongosh --quiet --port $PORT --eval "rs.initiate({_id: \"$RPL_SET_NAME\", $CONFIGSVR members: [$MEMBERS]})"; +set +e (until mongosh --quiet --port $PORT --eval "rs.isMaster().isWritablePrimary"|grep true; do sleep 1; done; mongosh --quiet --port $PORT admin --eval "db.createUser({ user: '$MONGODB_ROOT_USER', pwd: '$MONGODB_ROOT_PASSWORD', roles: [{role: 'root', db: 'admin'}] })") /dev/null 2>&1 &