Browse Source

Some work towards replication of couch locally.

pull/4023/head
mike12345567 6 years ago
parent
commit
ffad7bb3d0
  1. 18
      packages/server/src/api/controllers/deploy/awsDeploy.js
  2. 32
      packages/server/src/api/controllers/deploy/index.js
  3. 22
      packages/server/src/api/controllers/deploy/selfDeploy.js
  4. 9
      packages/server/src/api/controllers/deploy/utils.js
  5. 1
      packages/server/src/environment.js

18
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)
}

32
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)

22
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)
}

9
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))
})
}

1
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) {

Loading…
Cancel
Save