diff --git a/packages/server/src/api/controllers/deploy/awsDeploy.js b/packages/server/src/api/controllers/deploy/awsDeploy.js index 433913644a..26b3ea2b21 100644 --- a/packages/server/src/api/controllers/deploy/awsDeploy.js +++ b/packages/server/src/api/controllers/deploy/awsDeploy.js @@ -1,7 +1,9 @@ const AWS = require("aws-sdk") const fetch = require("node-fetch") const env = require("../../../environment") -const { deployToObjectStore } = require("./utils") +const { deployToObjectStore, performReplication } = require("./utils") +const CouchDB = require("pouchdb") +const PouchDB = require("../../../db") /** * Verifies the users API key and @@ -83,3 +85,17 @@ exports.deploy = async function(deployment) { }) await deployToObjectStore(appId, s3Client, metadata) } + +exports.replicateDb = async function(deployment) { + const appId = deployment.getAppId() + const { session } = deployment.getVerification() + const localDb = new PouchDB(appId) + const remoteDb = new CouchDB(`${env.DEPLOYMENT_DB_URL}/${appId}`, { + fetch: function(url, opts) { + opts.headers.set("Cookie", `${session};`) + return CouchDB.fetch(url, opts) + }, + }) + + return performReplication(localDb, remoteDb) +} diff --git a/packages/server/src/api/controllers/deploy/index.js b/packages/server/src/api/controllers/deploy/index.js index a37fad62a0..b0cc8f1133 100644 --- a/packages/server/src/api/controllers/deploy/index.js +++ b/packages/server/src/api/controllers/deploy/index.js @@ -1,11 +1,10 @@ -const CouchDB = require("pouchdb") const PouchDB = require("../../../db") const env = require("../../../environment") const deployment = env.SELF_HOSTED ? require("./selfDeploy") : require("./awsDeploy") -const { deploy, preDeployment, postDeployment } = deployment +const { deploy, preDeployment, postDeployment, replicateDb } = deployment const Deployment = require("./Deployment") // the max time we can wait for an invalidation to complete before considering it failed @@ -34,29 +33,6 @@ async function checkAllDeployments(deployments) { return { updated, deployments } } -function replicate(local, remote) { - return new Promise((resolve, reject) => { - const replication = local.sync(remote) - - replication.on("complete", () => resolve()) - replication.on("error", err => reject(err)) - }) -} - -async function replicateCouch(deployment) { - const appId = deployment.getAppId() - const { session } = deployment.getVerification() - const localDb = new PouchDB(appId) - const remoteDb = new CouchDB(`${env.DEPLOYMENT_DB_URL}/${appId}`, { - fetch: function(url, opts) { - opts.headers.set("Cookie", `${session};`) - return CouchDB.fetch(url, opts) - }, - }) - - return replicate(localDb, remoteDb) -} - async function storeLocalDeploymentHistory(deployment) { const appId = deployment.getAppId() const deploymentJSON = deployment.getJSON() @@ -96,9 +72,9 @@ async function deployApp(deployment) { await deploy(deployment) - // replicate the DB to the couchDB cluster in prod - console.log("Replicating local PouchDB to remote..") - await replicateCouch(deployment) + // replicate the DB to the main couchDB cluster + console.log("Replicating local PouchDB to CouchDB..") + await replicateDb(deployment) await postDeployment(deployment) diff --git a/packages/server/src/api/controllers/deploy/selfDeploy.js b/packages/server/src/api/controllers/deploy/selfDeploy.js index a18736a5f4..0b26af4e36 100644 --- a/packages/server/src/api/controllers/deploy/selfDeploy.js +++ b/packages/server/src/api/controllers/deploy/selfDeploy.js @@ -1,6 +1,8 @@ const env = require("../../../environment") const AWS = require("aws-sdk") -const { deployToObjectStore } = require("./utils") +const { deployToObjectStore, performReplication } = require("./utils") +const CouchDB = require("pouchdb") +const PouchDB = require("../../../db") const APP_BUCKET = "app-assets" @@ -25,7 +27,25 @@ exports.deploy = async function(deployment) { Bucket: APP_BUCKET, }, }) + // checking the bucket exists + try { + await objClient.headBucket({ Bucket: APP_BUCKET }).promise() + } catch (err) { + // bucket doesn't exist create it + if (err.statusCode === 404) { + await objClient.createBucket({ Bucket: APP_BUCKET }).promise() + } else { + throw err + } + } // no metadata, aws has account ID in metadata const metadata = {} await deployToObjectStore(appId, objClient, metadata) } + +exports.replicateDb = async function(deployment) { + const appId = deployment.getAppId() + const localDb = new PouchDB(appId) + const remoteDb = new CouchDB(`${env.COUCH_DB_URL}/${appId}`) + return performReplication(localDb, remoteDb) +} diff --git a/packages/server/src/api/controllers/deploy/utils.js b/packages/server/src/api/controllers/deploy/utils.js index f0a4b44e1d..4ee1f3a3f2 100644 --- a/packages/server/src/api/controllers/deploy/utils.js +++ b/packages/server/src/api/controllers/deploy/utils.js @@ -91,3 +91,12 @@ exports.deployToObjectStore = async function(appId, objectClient, metadata) { throw err } } + +exports.performReplication = (local, remote) => { + return new Promise((resolve, reject) => { + const replication = local.sync(remote) + + replication.on("complete", () => resolve()) + replication.on("error", err => reject(err)) + }) +} diff --git a/packages/server/src/environment.js b/packages/server/src/environment.js index 578645ec66..0c67f8c747 100644 --- a/packages/server/src/environment.js +++ b/packages/server/src/environment.js @@ -36,6 +36,7 @@ module.exports = { ENABLE_ANALYTICS: process.env.ENABLE_ANALYTICS, DEPLOYMENT_DB_URL: process.env.DEPLOYMENT_DB_URL, LOCAL_TEMPLATES: process.env.LOCAL_TEMPLATES, + // self hosting features MINIO_ACCESS_KEY: process.env.MINIO_ACCESS_KEY, MINIO_SECRET_KEY: process.env.MINIO_SECRET_KEY, _set(key, value) {