check that deployment is possible using lambda API
This commit is contained in:
parent
efa0902cc9
commit
a482d0a23e
|
@ -21,11 +21,21 @@ async function invalidateCDN(cfDistribution, appId) {
|
||||||
.promise()
|
.promise()
|
||||||
}
|
}
|
||||||
|
|
||||||
exports.fetchTemporaryCredentials = async function() {
|
/**
|
||||||
|
* Verifies the users API key and
|
||||||
|
* Verifies that the deployment fits within the quota of the user,
|
||||||
|
* @param {String} instanceId - instanceId being deployed
|
||||||
|
* @param {String} appId - appId being deployed
|
||||||
|
* @param {quota} quota - current quota being changed with this application
|
||||||
|
*/
|
||||||
|
exports.verifyDeployment = async function({ instanceId, appId, quota }) {
|
||||||
const response = await fetch(process.env.DEPLOYMENT_CREDENTIALS_URL, {
|
const response = await fetch(process.env.DEPLOYMENT_CREDENTIALS_URL, {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
body: JSON.stringify({
|
body: JSON.stringify({
|
||||||
apiKey: process.env.BUDIBASE_API_KEY,
|
apiKey: process.env.BUDIBASE_API_KEY,
|
||||||
|
instanceId,
|
||||||
|
appId,
|
||||||
|
quota,
|
||||||
}),
|
}),
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
|
@ -1,10 +1,17 @@
|
||||||
const CouchDB = require("pouchdb")
|
const CouchDB = require("pouchdb")
|
||||||
const PouchDB = require("../../../db")
|
const PouchDB = require("../../../db")
|
||||||
const { uploadAppAssets, fetchTemporaryCredentials } = require("./aws")
|
const {
|
||||||
|
uploadAppAssets,
|
||||||
|
verifyDeployment,
|
||||||
|
determineDeploymentAllowed,
|
||||||
|
} = require("./aws")
|
||||||
|
const { getRecordParams } = require("../../db/utils")
|
||||||
|
|
||||||
function replicate(local, remote) {
|
function replicate(local, remote) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
const replication = local.sync(remote)
|
const replication = local.sync(remote, {
|
||||||
|
retry: true,
|
||||||
|
})
|
||||||
|
|
||||||
replication.on("complete", () => resolve())
|
replication.on("complete", () => resolve())
|
||||||
replication.on("error", err => reject(err))
|
replication.on("error", err => reject(err))
|
||||||
|
@ -31,13 +38,37 @@ async function replicateCouch({ instanceId, clientId, credentials }) {
|
||||||
await Promise.all(replications)
|
await Promise.all(replications)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function getCurrentInstanceQuota(instanceId) {
|
||||||
|
const db = new PouchDB(instanceId)
|
||||||
|
const records = await db.allDocs(
|
||||||
|
getRecordParams("", null, {
|
||||||
|
include_docs: true,
|
||||||
|
})
|
||||||
|
)
|
||||||
|
const existingRecords = records.rows.length
|
||||||
|
return {
|
||||||
|
records: existingRecords,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
exports.deployApp = async function(ctx) {
|
exports.deployApp = async function(ctx) {
|
||||||
try {
|
try {
|
||||||
const clientAppLookupDB = new PouchDB("client_app_lookup")
|
const clientAppLookupDB = new PouchDB("client_app_lookup")
|
||||||
const { clientId } = await clientAppLookupDB.get(ctx.user.appId)
|
const { clientId } = await clientAppLookupDB.get(ctx.user.appId)
|
||||||
|
|
||||||
|
const instanceQuota = await getCurrentInstanceQuota(ctx.user.instanceId)
|
||||||
|
const credentials = await verifyDeployment({
|
||||||
|
instanceId: ctx.user.instanceId,
|
||||||
|
appId: ctx.user.appId,
|
||||||
|
quota: instanceQuota,
|
||||||
|
})
|
||||||
|
|
||||||
ctx.log.info(`Uploading assets for appID ${ctx.user.appId} assets to s3..`)
|
ctx.log.info(`Uploading assets for appID ${ctx.user.appId} assets to s3..`)
|
||||||
const credentials = await fetchTemporaryCredentials()
|
|
||||||
|
if (credentials.errors) {
|
||||||
|
ctx.throw(500, credentials.errors)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
await uploadAppAssets({
|
await uploadAppAssets({
|
||||||
clientId,
|
clientId,
|
||||||
|
|
Loading…
Reference in New Issue