From d424e274359d43b2734d9702cb7cacd554f87189 Mon Sep 17 00:00:00 2001 From: xuriwuyun Date: Mon, 26 Jun 2023 16:21:08 +0800 Subject: [PATCH 1/3] fix mongodb post start script --- deploy/mongodb/scripts/replicaset-post-start.tpl | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/deploy/mongodb/scripts/replicaset-post-start.tpl b/deploy/mongodb/scripts/replicaset-post-start.tpl index d4c79463a42..c7697a7d9b9 100644 --- a/deploy/mongodb/scripts/replicaset-post-start.tpl +++ b/deploy/mongodb/scripts/replicaset-post-start.tpl @@ -17,7 +17,7 @@ INDEX=$(echo $KB_POD_NAME | grep -o "\-[0-9]\+\$"); INDEX=${INDEX#-}; if [ $INDEX -ne 0 ]; then exit 0; fi -until mongosh --quiet --port $PORT --eval "print('ready')"; do sleep 1; done +until mongosh --quiet --port $PORT --eval "print('I am ready')"; do sleep 1; done RPL_SET_NAME=$(echo $KB_POD_NAME | grep -o ".*-"); RPL_SET_NAME=${RPL_SET_NAME%-}; @@ -40,8 +40,11 @@ done CONFIGSVR="" if [ ""$IS_CONFIGSVR = "true" ]; then CONFIGSVR="configsvr: true,"; fi -until is_inited=$(mongosh --quiet --port $PORT --eval "rs.status().ok" -u root --password $MONGODB_ROOT_PASSWORD || mongosh --quiet --port $PORT --eval "try { rs.status().ok } catch (e) { 0 }") ; do sleep 1; done -if [ $is_inited -ne 1 ]; then +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]})"; From 31012da5b16754d49b4ef579c2afb24b2c1c624c Mon Sep 17 00:00:00 2001 From: xuriwuyun Date: Tue, 27 Jun 2023 10:11:32 +0800 Subject: [PATCH 2/3] fix --- .../mongodb/scripts/replicaset-post-start.tpl | 29 ++++++++++++------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/deploy/mongodb/scripts/replicaset-post-start.tpl b/deploy/mongodb/scripts/replicaset-post-start.tpl index c7697a7d9b9..e75b9940b99 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 [ -n $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 & From e7c1337ae33681bb2fc57b41ebb2db5fc7a02d44 Mon Sep 17 00:00:00 2001 From: xuriwuyun Date: Tue, 27 Jun 2023 10:22:08 +0800 Subject: [PATCH 3/3] fix --- deploy/mongodb/scripts/replicaset-post-start.tpl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/deploy/mongodb/scripts/replicaset-post-start.tpl b/deploy/mongodb/scripts/replicaset-post-start.tpl index e75b9940b99..79c91638b1f 100644 --- a/deploy/mongodb/scripts/replicaset-post-start.tpl +++ b/deploy/mongodb/scripts/replicaset-post-start.tpl @@ -27,7 +27,7 @@ done echo is_inited: $is_inited -if [ -n $is_inited ]; then +if [ $is_inited ]; then exit 0 fi