Some work towards replication of couch locally.
This commit is contained in:
parent
e80fb466e6
commit
2291a5acdb
|
@ -1,7 +1,9 @@
|
||||||
const AWS = require("aws-sdk")
|
const AWS = require("aws-sdk")
|
||||||
const fetch = require("node-fetch")
|
const fetch = require("node-fetch")
|
||||||
const env = require("../../../environment")
|
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
|
* Verifies the users API key and
|
||||||
|
@ -83,3 +85,17 @@ exports.deploy = async function(deployment) {
|
||||||
})
|
})
|
||||||
await deployToObjectStore(appId, s3Client, metadata)
|
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)
|
||||||
|
}
|
||||||
|
|
|
@ -1,11 +1,10 @@
|
||||||
const CouchDB = require("pouchdb")
|
|
||||||
const PouchDB = require("../../../db")
|
const PouchDB = require("../../../db")
|
||||||
|
|
||||||
const env = require("../../../environment")
|
const env = require("../../../environment")
|
||||||
const deployment = env.SELF_HOSTED
|
const deployment = env.SELF_HOSTED
|
||||||
? require("./selfDeploy")
|
? require("./selfDeploy")
|
||||||
: require("./awsDeploy")
|
: require("./awsDeploy")
|
||||||
const { deploy, preDeployment, postDeployment } = deployment
|
const { deploy, preDeployment, postDeployment, replicateDb } = deployment
|
||||||
const Deployment = require("./Deployment")
|
const Deployment = require("./Deployment")
|
||||||
|
|
||||||
// the max time we can wait for an invalidation to complete before considering it failed
|
// 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 }
|
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) {
|
async function storeLocalDeploymentHistory(deployment) {
|
||||||
const appId = deployment.getAppId()
|
const appId = deployment.getAppId()
|
||||||
const deploymentJSON = deployment.getJSON()
|
const deploymentJSON = deployment.getJSON()
|
||||||
|
@ -96,9 +72,9 @@ async function deployApp(deployment) {
|
||||||
|
|
||||||
await deploy(deployment)
|
await deploy(deployment)
|
||||||
|
|
||||||
// replicate the DB to the couchDB cluster in prod
|
// replicate the DB to the main couchDB cluster
|
||||||
console.log("Replicating local PouchDB to remote..")
|
console.log("Replicating local PouchDB to CouchDB..")
|
||||||
await replicateCouch(deployment)
|
await replicateDb(deployment)
|
||||||
|
|
||||||
await postDeployment(deployment)
|
await postDeployment(deployment)
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
const env = require("../../../environment")
|
const env = require("../../../environment")
|
||||||
const AWS = require("aws-sdk")
|
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"
|
const APP_BUCKET = "app-assets"
|
||||||
|
|
||||||
|
@ -25,7 +27,25 @@ exports.deploy = async function(deployment) {
|
||||||
Bucket: APP_BUCKET,
|
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
|
// no metadata, aws has account ID in metadata
|
||||||
const metadata = {}
|
const metadata = {}
|
||||||
await deployToObjectStore(appId, objClient, 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)
|
||||||
|
}
|
||||||
|
|
|
@ -91,3 +91,12 @@ exports.deployToObjectStore = async function(appId, objectClient, metadata) {
|
||||||
throw err
|
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))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
|
@ -36,6 +36,7 @@ module.exports = {
|
||||||
ENABLE_ANALYTICS: process.env.ENABLE_ANALYTICS,
|
ENABLE_ANALYTICS: process.env.ENABLE_ANALYTICS,
|
||||||
DEPLOYMENT_DB_URL: process.env.DEPLOYMENT_DB_URL,
|
DEPLOYMENT_DB_URL: process.env.DEPLOYMENT_DB_URL,
|
||||||
LOCAL_TEMPLATES: process.env.LOCAL_TEMPLATES,
|
LOCAL_TEMPLATES: process.env.LOCAL_TEMPLATES,
|
||||||
|
// self hosting features
|
||||||
MINIO_ACCESS_KEY: process.env.MINIO_ACCESS_KEY,
|
MINIO_ACCESS_KEY: process.env.MINIO_ACCESS_KEY,
|
||||||
MINIO_SECRET_KEY: process.env.MINIO_SECRET_KEY,
|
MINIO_SECRET_KEY: process.env.MINIO_SECRET_KEY,
|
||||||
_set(key, value) {
|
_set(key, value) {
|
||||||
|
|
Loading…
Reference in New Issue