2021-09-01 05:32:08 +00:00
|
|
|
// Init function export mongodb
|
2021-11-26 10:10:16 +00:00
|
|
|
const shell = require('shelljs');
|
|
|
|
|
const readlineSync = require('readline-sync');
|
|
|
|
|
const process = require('process');
|
2021-09-14 13:31:06 +00:00
|
|
|
const Constants = require('./constants')
|
2021-09-01 05:32:08 +00:00
|
|
|
|
|
|
|
|
function import_database() {
|
2021-11-26 10:10:16 +00:00
|
|
|
console.log('import_database ....')
|
|
|
|
|
const cmd = `mongorestore --uri='${process.env.APPSMITH_MONGODB_URI}' --drop --archive='${Constants.RESTORE_PATH}/${Constants.DUMP_FILE_NAME}' --gzip`
|
|
|
|
|
shell.exec(cmd)
|
|
|
|
|
console.log('import_database done')
|
2021-09-01 05:32:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function stop_application() {
|
2021-11-26 10:10:16 +00:00
|
|
|
shell.exec('/usr/bin/supervisorctl stop backend rts')
|
2021-09-01 05:32:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function start_application() {
|
2021-11-26 10:10:16 +00:00
|
|
|
shell.exec('/usr/bin/supervisorctl start backend rts')
|
2021-09-01 05:32:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Main application workflow
|
2021-11-26 10:10:16 +00:00
|
|
|
const main = () => {
|
|
|
|
|
let errorCode = 0
|
|
|
|
|
try {
|
2021-09-01 05:32:08 +00:00
|
|
|
|
2021-11-26 10:10:16 +00:00
|
|
|
check_supervisord_status_cmd = '/usr/bin/supervisorctl'
|
|
|
|
|
shell.exec(check_supervisord_status_cmd, function (code) {
|
|
|
|
|
if (code > 0) {
|
|
|
|
|
shell.echo('application is not running, starting supervisord')
|
|
|
|
|
shell.exec('/usr/bin/supervisord')
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
shell.echo('stop backend & rts application before import database')
|
|
|
|
|
stop_application()
|
|
|
|
|
const shellCmdResult = shell.exec(`mongo ${process.env.APPSMITH_MONGODB_URI} --quiet --eval "db.getCollectionNames().length"`)
|
|
|
|
|
const collectionsLen = parseInt(shellCmdResult.stdout.toString().trimEnd())
|
|
|
|
|
if (collectionsLen > 0) {
|
|
|
|
|
shell.echo()
|
|
|
|
|
shell.echo('**************************** WARNING ****************************')
|
|
|
|
|
shell.echo(`Your target database is not empty, it has data in ${collectionsLen} collections.`)
|
|
|
|
|
const input = readlineSync.question('Importing this DB will erase this data. Are you sure you want to proceed?[Yes/No] ')
|
|
|
|
|
const answer = input && input.toLocaleUpperCase()
|
|
|
|
|
if (answer === 'Y' || answer === 'YES') {
|
|
|
|
|
import_database()
|
|
|
|
|
return
|
|
|
|
|
} else if (answer === 'N' || answer === 'NO') {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
shell.echo(`Your input is invalid. Please try to run import command again.`)
|
|
|
|
|
return
|
|
|
|
|
} else {
|
|
|
|
|
import_database()
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
} catch (err) {
|
|
|
|
|
shell.echo(err)
|
|
|
|
|
errorCode = 1
|
|
|
|
|
} finally {
|
|
|
|
|
shell.echo('start backend & rts application after import database')
|
|
|
|
|
start_application()
|
|
|
|
|
process.exit(errorCode)
|
|
|
|
|
}
|
2021-09-01 05:32:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
module.exports = {
|
2021-11-26 10:10:16 +00:00
|
|
|
runImportDatabase: main
|
2021-09-14 13:31:06 +00:00
|
|
|
};
|