Enable replicaset on local MongoDB only (#10010)

This commit is contained in:
geekup-legodevops 2022-02-09 16:02:46 +07:00 committed by GitHub
parent fab552cafa
commit 2d8e7fee0d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 54 additions and 31 deletions

View File

@ -63,7 +63,31 @@ unset_unused_variables() {
fi
}
check_initialized_db() {
check_mongodb_uri() {
echo "Check mongodb uri host"
isUriLocal=1
if [[ $APPSMITH_MONGODB_URI == *"localhost"* || $APPSMITH_MONGODB_URI == *"127.0.0.1"* ]]; then
echo "Use local MongoDB"
isUriLocal=0
fi
}
init_mongodb() {
if [[ $isUriLocal -eq 0 ]]; then
echo "Init database"
MONGO_DB_PATH="/appsmith-stacks/data/mongodb"
MONGO_LOG_PATH="$MONGO_DB_PATH/log"
MONGO_DB_KEY="$MONGO_DB_PATH/key"
mkdir -p "$MONGO_DB_PATH"
touch "$MONGO_LOG_PATH"
if [[ -f "$MONGO_DB_KEY" ]]; then
chmod-mongodb-key "$MONGO_DB_KEY"
fi
fi
}
init_replica_set() {
echo 'Check initialized database'
shouldPerformInitdb=1
for path in \
@ -73,39 +97,23 @@ check_initialized_db() {
"$MONGO_DB_PATH/storage.bson"; do
if [ -e "$path" ]; then
shouldPerformInitdb=0
return
break
fi
done
echo "Should initialize database"
}
init_mongodb() {
echo "Init database"
MONGO_DB_PATH="/appsmith-stacks/data/mongodb"
MONGO_LOG_PATH="$MONGO_DB_PATH/log"
MONGO_DB_KEY="$MONGO_DB_PATH/key"
mkdir -p "$MONGO_DB_PATH"
touch "$MONGO_LOG_PATH"
check_initialized_db
if [[ -f "$MONGO_DB_KEY" ]]; then
chmod-mongodb-key "$MONGO_DB_KEY"
fi
if [[ $shouldPerformInitdb -gt 0 ]]; then
if [[ $shouldPerformInitdb -gt 0 && $isUriLocal -eq 0 ]]; then
echo 'Init replica set local'
# Start installed MongoDB service - Dependencies Layer
mongod --fork --port 27017 --dbpath "$MONGO_DB_PATH" --logpath "$MONGO_LOG_PATH"
echo "Waiting 10s for mongodb init"
sleep 10
bash "/opt/appsmith/templates/mongo-init.js.sh" "$MONGO_INITDB_ROOT_USERNAME" "$MONGO_INITDB_ROOT_PASSWORD" >"/appsmith-stacks/configuration/mongo-init.js"
mongo "127.0.0.1/${MONGO_INITDB_DATABASE}" /appsmith-stacks/configuration/mongo-init.js
bash "/opt/appsmith/templates/mongo-init.js.sh" "appsmith" "$AUTO_GEN_MONGO_PASSWORD" > "/appsmith-stacks/configuration/mongo-init.js"
mongo "127.0.0.1/appsmith" /appsmith-stacks/configuration/mongo-init.js
echo "Seeding db done"
echo "Enable replica set"
mongod --dbpath "$MONGO_DB_PATH" --shutdown || true
echo "Fork process"
openssl rand -base64 756 >"$MONGO_DB_KEY"
openssl rand -base64 756 > "$MONGO_DB_KEY"
chmod-mongodb-key "$MONGO_DB_KEY"
mongod --fork --port 27017 --dbpath "$MONGO_DB_PATH" --logpath "$MONGO_LOG_PATH" --replSet mr1 --keyFile "$MONGO_DB_KEY" --bind_ip localhost
echo "Waiting 10s for mongodb init with replica set"
@ -113,6 +121,23 @@ init_mongodb() {
mongo "$APPSMITH_MONGODB_URI" --eval 'rs.initiate()'
mongod --dbpath "$MONGO_DB_PATH" --shutdown || true
fi
if [[ $isUriLocal -gt 0 ]]; then
# Check mongodb cloud replica set
echo "Check mongodb cloud replica set"
responseStatus=$(mongo "$APPSMITH_MONGODB_URI" --eval "rs.status()" | grep ok | xargs)
okString="ok : 1"
if [[ $responseStatus == *$okString* ]]; then
echo "Mongodb cloud replica set is enabled"
mongo "$APPSMITH_MONGODB_URI" --eval 'rs.initiate()'
else
echo -e "\033[0;31m********************************************************************\033[0m"
echo -e "\033[0;31m* Mongodb cloud replica set is not enabled *\033[0m"
echo -e "\033[0;31m********************************************************************\033[0m"
exit 1
fi
fi
}
chmod-mongodb-key() {
@ -136,7 +161,7 @@ configure_supervisord() {
cp -f "$SUPERVISORD_CONF_PATH/application_process/"*.conf /etc/supervisor/conf.d
# Disable services based on configuration
if [[ "$APPSMITH_MONGODB_URI" = "mongodb://appsmith:$MONGO_INITDB_ROOT_PASSWORD@localhost/appsmith" ]]; then
if [[ $isUriLocal -eq 0 ]]; then
cp "$SUPERVISORD_CONF_PATH/mongodb.conf" /etc/supervisor/conf.d/
fi
if [[ "$APPSMITH_REDIS_URL" = "redis://127.0.0.1:6379" ]]; then
@ -152,7 +177,9 @@ configure_supervisord() {
# Main Section
init_env_file
unset_unused_variables
check_mongodb_uri
init_mongodb
init_replica_set
mount_letsencrypt_directory
configure_supervisord

View File

@ -67,12 +67,8 @@ APPSMITH_RECAPTCHA_SITE_KEY=
APPSMITH_RECAPTCHA_SECRET_KEY=
APPSMITH_RECAPTCHA_ENABLED=
MONGO_INITDB_ROOT_USERNAME=appsmith
MONGO_INITDB_ROOT_PASSWORD=$MONGO_PASSWORD
MONGO_INITDB_DATABASE=appsmith
APPSMITH_MONGO_HOST=localhost:27017
APPSMITH_MONGODB_URI=mongodb://appsmith:$MONGO_PASSWORD@localhost/appsmith
APPSMITH_API_BASE_URL=http://localhost:8080
APPSMITH_MONGODB_URI=mongodb://appsmith:$MONGO_PASSWORD@localhost:27017/appsmith
APPSMITH_API_BASE_URL=http://localhost:8080/api/v1
APPSMITH_REDIS_URL=redis://127.0.0.1:6379