From 052d6da4714a25fa0e3673c4d55f026e2e461810 Mon Sep 17 00:00:00 2001 From: Chris Brame Date: Thu, 11 Apr 2019 01:56:41 -0400 Subject: [PATCH] fix(backup): mongodb binaries for mongodb 4 windows #138 chore(database): added support for MongoDB Atlas (shards) --- src/backup/backup.js | 2 +- src/controllers/install.js | 7 +++++- src/database/index.js | 50 ++++++++++++++++++++++++++------------ src/settings/defaults.js | 6 ++++- 4 files changed, 46 insertions(+), 19 deletions(-) diff --git a/src/backup/backup.js b/src/backup/backup.js index 2fa9cb8ef..9328c8339 100644 --- a/src/backup/backup.js +++ b/src/backup/backup.js @@ -126,7 +126,7 @@ function runBackup (callback) { }) }) } else { - callback(new Error('MongoDump falied with code ' + code)) + callback(new Error('MongoDump failed with code ' + code)) } }) } diff --git a/src/controllers/install.js b/src/controllers/install.js index a3306c969..bf4d5883a 100644 --- a/src/controllers/install.js +++ b/src/controllers/install.js @@ -39,6 +39,9 @@ installController.mongotest = function (req, res) { var CONNECTION_URI = 'mongodb://' + data.username + ':' + dbPassword + '@' + data.host + ':' + data.port + '/' + data.database + if (data.port === '---') + CONNECTION_URI = 'mongodb+srv://' + data.username + ':' + dbPassword + '@' + data.host + '/' + data.database + var child = require('child_process').fork(path.join(__dirname, '../../src/install/mongotest'), { env: { FORK: 1, NODE_ENV: global.env, MONGOTESTURI: CONNECTION_URI } }) @@ -124,6 +127,7 @@ installController.install = function (req, res) { var dbPassword = encodeURIComponent(password) var conuri = 'mongodb://' + username + ':' + dbPassword + '@' + host + ':' + port + '/' + database + if (port === '---') conuri = 'mongodb+srv://' + username + ':' + dbPassword + '@' + host + '/' + database async.waterfall( [ @@ -334,7 +338,8 @@ installController.install = function (req, res) { port: port, username: username, password: password, - database: database + database: database, + shard: port === '---' }, tokens: { secret: chance.hash() + chance.md5(), diff --git a/src/database/index.js b/src/database/index.js index d8a9d83b3..9e50f0fb0 100644 --- a/src/database/index.js +++ b/src/database/index.js @@ -22,26 +22,40 @@ var mongoConnectionUri = { port: process.env.TD_MONGODB_PORT || nconf.get('mongo:port') || '27017', username: process.env.TD_MONGODB_USERNAME || nconf.get('mongo:username'), password: process.env.TD_MONGODB_PASSWORD || nconf.get('mongo:password'), - database: process.env.TD_MONGODB_DATABASE || nconf.get('mongo:database') + database: process.env.TD_MONGODB_DATABASE || nconf.get('mongo:database'), + shard: process.env.TD_MONGODB_SHARD || nconf.get('mongo:shard') } var CONNECTION_URI = '' -if (!mongoConnectionUri.username) +if (!mongoConnectionUri.username) { CONNECTION_URI = 'mongodb://' + mongoConnectionUri.server + ':' + mongoConnectionUri.port + '/' + mongoConnectionUri.database -else { + if (mongoConnectionUri.shard === true) + CONNECTION_URI = 'mongodb+srv://' + mongoConnectionUri.server + '/' + mongoConnectionUri.database +} else { mongoConnectionUri.password = encodeURIComponent(mongoConnectionUri.password) - CONNECTION_URI = - 'mongodb://' + - mongoConnectionUri.username + - ':' + - mongoConnectionUri.password + - '@' + - mongoConnectionUri.server + - ':' + - mongoConnectionUri.port + - '/' + - mongoConnectionUri.database + if (mongoConnectionUri.shard === true) + CONNECTION_URI = + 'mongodb+srv://' + + mongoConnectionUri.username + + ':' + + mongoConnectionUri.password + + '@' + + mongoConnectionUri.server + + '/' + + mongoConnectionUri.database + else + CONNECTION_URI = + 'mongodb://' + + mongoConnectionUri.username + + ':' + + mongoConnectionUri.password + + '@' + + mongoConnectionUri.server + + ':' + + mongoConnectionUri.port + + '/' + + mongoConnectionUri.database } if (process.env.TD_MONGODB_URI) CONNECTION_URI = process.env.TD_MONGODB_URI @@ -56,6 +70,7 @@ var options = { module.exports.init = function (callback, connectionString, opts) { if (connectionString) CONNECTION_URI = connectionString if (opts) options = opts + options.dbName = mongoConnectionUri.database if (db.connection) { return callback(null, db) @@ -71,8 +86,11 @@ module.exports.init = function (callback, connectionString, opts) { } db.connection = mongoose.connection - - return callback(null, db) + mongoose.connection.db.admin().command({ buildInfo: 1 }, function (err, info) { + if (err) winston.warn(err.message) + db.version = info.version + return callback(null, db) + }) }) .catch(function (e) { winston.error('Oh no, something went wrong with DB! - ' + e.message) diff --git a/src/settings/defaults.js b/src/settings/defaults.js index 28c07bb7f..0e3bf5950 100644 --- a/src/settings/defaults.js +++ b/src/settings/defaults.js @@ -220,8 +220,12 @@ function createDirectories (callback) { function downloadWin32MongoDBTools (callback) { var http = require('http') var os = require('os') + var semver = require('semver') + var dbVersion = require('../database').db.version || '3.6.9' + var fileVersion = semver(dbVersion).major + '.' + semver(dbVersion).minor + if (os.platform() === 'win32') { - var filename = 'mongodb-tools.3.6.9-win32x64.zip' + var filename = 'mongodb-tools.' + fileVersion + '-win32x64.zip' var savePath = path.join(__dirname, '../backup/bin/win32/') fs.ensureDirSync(savePath) if (