2020-07-01 22:56:53 +02:00
|
|
|
const CouchDB = require("pouchdb")
|
|
|
|
const PouchDB = require("../../../db")
|
|
|
|
const {
|
|
|
|
uploadAppAssets,
|
|
|
|
} = require("./aws")
|
|
|
|
|
2020-07-03 00:22:20 +02:00
|
|
|
function replicate(local, remote) {
|
2020-07-01 22:56:53 +02:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
2020-07-07 18:47:18 +02:00
|
|
|
const replication = local.sync(remote);
|
2020-07-01 22:56:53 +02:00
|
|
|
|
|
|
|
replication.on("complete", () => resolve())
|
|
|
|
replication.on("error", err => reject(err))
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-07-03 00:22:20 +02:00
|
|
|
async function replicateCouch(instanceId, clientId) {
|
|
|
|
|
2020-07-07 18:47:18 +02:00
|
|
|
const databases = [
|
|
|
|
`client_${clientId}`,
|
|
|
|
"client_app_lookup",
|
|
|
|
instanceId
|
|
|
|
];
|
2020-07-03 00:22:20 +02:00
|
|
|
|
2020-07-07 18:47:18 +02:00
|
|
|
const replications = databases.map(local => {
|
|
|
|
const localDb = new PouchDB(local);
|
|
|
|
const remoteDb = new CouchDB(`${process.env.DEPLOYMENT_COUCH_DB_URL}/${local}`)
|
2020-07-03 00:22:20 +02:00
|
|
|
|
2020-07-07 18:47:18 +02:00
|
|
|
return replicate(localDb, remoteDb);
|
|
|
|
});
|
|
|
|
|
|
|
|
await Promise.all(replications)
|
2020-07-03 00:22:20 +02:00
|
|
|
}
|
|
|
|
|
2020-07-01 22:56:53 +02:00
|
|
|
exports.deployApp = async function(ctx) {
|
|
|
|
try {
|
|
|
|
const clientAppLookupDB = new PouchDB("client_app_lookup")
|
|
|
|
const { clientId } = await clientAppLookupDB.get(ctx.user.appId)
|
|
|
|
|
|
|
|
ctx.log.info(`Uploading assets for appID ${ctx.user.appId} assets to s3..`);
|
|
|
|
await uploadAppAssets({
|
|
|
|
clientId,
|
|
|
|
appId: ctx.user.appId
|
|
|
|
})
|
|
|
|
|
|
|
|
// replicate the DB to the couchDB cluster in prod
|
|
|
|
ctx.log.info("Replicating local PouchDB to remote..");
|
2020-07-03 00:22:20 +02:00
|
|
|
await replicateCouch(ctx.user.instanceId, clientId);
|
2020-07-01 22:56:53 +02:00
|
|
|
|
|
|
|
ctx.body = {
|
|
|
|
status: "SUCCESS",
|
|
|
|
completed: Date.now()
|
|
|
|
}
|
2020-07-07 18:47:18 +02:00
|
|
|
} catch (err) {
|
|
|
|
ctx.throw(err.status || 500, `Deployment Failed: ${err.message}`);
|
|
|
|
}
|
2020-07-01 22:56:53 +02:00
|
|
|
}
|