1
0
Fork 0
mirror of synced 2024-06-03 02:55:14 +12:00

Some work towards replication of couch locally.

This commit is contained in:
mike12345567 2020-12-04 17:10:01 +00:00
parent bf82348f9f
commit ffad7bb3d0
5 changed files with 52 additions and 30 deletions

View file

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

View file

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

View file

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

View file

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

View file

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