Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 6 additions & 8 deletions deploy/docker/entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -173,17 +173,15 @@ init_replica_set() {
fi

if [[ $isUriLocal -gt 0 ]]; then
# Check mongodb cloud Replica Set
echo "Checking Replica Set of external MongoDB"

mongo_state="$(mongosh "$APPSMITH_MONGODB_URI" --quiet --eval "rs.status().ok")"
if [[ ${mongo_state: -1} -eq 1 ]]; then
echo "Mongodb cloud Replica Set is enabled"
if appsmithctl check-replica-set; then
echo "MongoDB ReplicaSet is enabled"
else
echo -e "\033[0;31m*************************************************************************************************************\033[0m"
echo -e "\033[0;31m* MongoDB Replica Set is not enabled *\033[0m"
echo -e "\033[0;31m* Please ensure the credentials provided for MongoDB, has `readWrite` and `clusterMonitor` roles. *\033[0m"
echo -e "\033[0;31m*************************************************************************************************************\033[0m"
echo -e "\033[0;31m***************************************************************************************\033[0m"
echo -e "\033[0;31m* MongoDB Replica Set is not enabled *\033[0m"
echo -e "\033[0;31m* Please ensure the credentials provided for MongoDB, has `readWrite` role. *\033[0m"
echo -e "\033[0;31m***************************************************************************************\033[0m"
exit 1
fi
fi
Expand Down
8 changes: 5 additions & 3 deletions deploy/docker/utils/bin/check_replica_set.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
const { MongoClient } = require("mongodb");
const { preprocessMongoDBURI } = require("./utils");

async function exec() {
const client = new MongoClient(process.env.APPSMITH_MONGODB_URI, {
const client = new MongoClient(preprocessMongoDBURI(process.env.APPSMITH_MONGODB_URI), {
useNewUrlParser: true,
useUnifiedTopology: true,
});
Expand All @@ -13,7 +14,7 @@ async function exec() {
} catch (err) {
console.error("Error trying to check replicaset", err);
} finally {
client.close();
await client.close();
}

process.exit(isReplicaSetEnabled ? 0 : 1);
Expand All @@ -23,7 +24,7 @@ async function checkReplicaSet(client) {
await client.connect();
return await new Promise((resolve) => {
try {
client
const changeStream = client
.db()
.collection("user")
.watch()
Expand All @@ -36,6 +37,7 @@ async function checkReplicaSet(client) {
// setTimeout so the error event can kick-in first
setTimeout(() => {
resolve(true);
changeStream.close();
}, 1000);
} catch (err) {
console.error("Error thrown when checking replicaset", err);
Expand Down
31 changes: 30 additions & 1 deletion deploy/docker/utils/bin/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const shell = require("shelljs");
const fsPromises = require("fs/promises");
const Constants = require("./constants");
const childProcess = require("child_process");
const { ConnectionString } = require("mongodb-connection-string-url");

function showHelp() {
console.log(
Expand Down Expand Up @@ -102,6 +103,33 @@ async function getCurrentAppsmithVersion() {
return content.match(/\bexports\.VERSION\s*=\s*["']([^"]+)["']/)[1];
}

function preprocessMongoDBURI(uri /* string */) {
// Partially taken from <https://github.com/mongodb-js/mongosh/blob/8fde100d6d5ec711eb9565b85cb2e28e2da47c80/packages/arg-parser/src/uri-generator.ts#L248>
// If we don't add the `directConnection` parameter for non-SRV URIs, we'll see the problem at <https://github.com/appsmithorg/appsmith/issues/16104>.
const cs = new ConnectionString(uri);

const params = cs.searchParams;
params.set('appName', 'appsmithctl');

if (
!cs.isSRV
&& !params.has('replicaSet')
&& !params.has('directConnection')
&& !params.has('loadBalanced')
&& cs.hosts.length === 1
) {
params.set('directConnection', 'true');
}

// For localhost connections, set a lower timeout to avoid hanging for too long.
// Taken from <https://github.com/mongodb-js/mongosh/blob/8fde100d6d5ec711eb9565b85cb2e28e2da47c80/packages/arg-parser/src/uri-generator.ts#L156>.
if (!params.has('serverSelectionTimeoutMS') && cs.hosts.every(host => ['localhost', '127.0.0.1'].includes(host.split(':')[0]))) {
params.set('serverSelectionTimeoutMS', '2000');
}

return cs.toString();
}

module.exports = {
showHelp,
start,
Expand All @@ -110,5 +138,6 @@ module.exports = {
listLocalBackupFiles,
updateLastBackupErrorMailSentInMilliSec,
getLastBackupErrorMailSentInMilliSec,
getCurrentAppsmithVersion
getCurrentAppsmithVersion,
preprocessMongoDBURI,
};
Loading