This commit is contained in:
Martin McKeaveney 2020-10-08 20:23:58 +01:00
parent ee73c7f418
commit 3994816c69
2 changed files with 40 additions and 40 deletions

View File

@ -22,19 +22,20 @@ async function invalidateCDN(cfDistribution, appId) {
}
exports.updateDeploymentQuota = async function(quota) {
const response = await fetch(
`${process.env.DEPLOYMENT_CREDENTIALS_URL}/deploy/success`,
{
method: "POST",
body: JSON.stringify({
apiKey: process.env.BUDIBASE_API_KEY,
quota,
}),
}
)
const DEPLOYMENT_SUCCESS_URL =
process.env.DEPLOYMENT_CREDENTIALS_URL + "deploy/success"
console.log(DEPLOYMENT_SUCCESS_URL)
const response = await fetch(DEPLOYMENT_SUCCESS_URL, {
method: "POST",
body: JSON.stringify({
apiKey: process.env.BUDIBASE_API_KEY,
quota,
}),
})
if (response.status !== 200) {
throw new Error(`Error updating deployment quota for app`)
throw new Error(`Error updating deployment quota for API Key`)
}
const json = await response.json()
@ -163,30 +164,33 @@ exports.uploadAppAssets = async function({
// Upload file attachments
const db = new PouchDB(instanceId)
const fileUploads = await db.get("_local/fileuploads")
if (fileUploads) {
for (let file of fileUploads.uploads) {
if (file.uploaded) continue
const attachmentUpload = prepareUploadForS3({
file,
s3Key: `assets/${appId}/attachments/${file.processedFileName}`,
s3,
metadata: { accountId },
})
uploads.push(attachmentUpload)
// mark file as uploaded
file.uploaded = true
}
db.put(fileUploads)
let fileUploads
try {
fileUploads = await db.get("_local/fileuploads")
} catch (err) {
fileUploads = { _id: "_local/fileuploads", uploads: [] }
}
for (let file of fileUploads.uploads) {
if (file.uploaded) continue
const attachmentUpload = prepareUploadForS3({
file,
s3Key: `assets/${appId}/attachments/${file.processedFileName}`,
s3,
metadata: { accountId },
})
uploads.push(attachmentUpload)
// mark file as uploaded
file.uploaded = true
}
db.put(fileUploads)
try {
await Promise.all(uploads)
// TODO: update dynamoDB with a synopsis of the app deployment for historical purposes
await invalidateCDN(cfDistribution, appId)
} catch (err) {
console.error("Error uploading budibase app assets to s3", err)

View File

@ -5,13 +5,10 @@ const {
verifyDeployment,
updateDeploymentQuota,
} = require("./aws")
const { getRecordParams } = require("../../db/utils")
function replicate(local, remote) {
return new Promise((resolve, reject) => {
const replication = local.sync(remote, {
retry: true,
})
const replication = local.sync(remote)
replication.on("complete", () => resolve())
replication.on("error", err => reject(err))
@ -40,11 +37,10 @@ async function replicateCouch({ instanceId, clientId, credentials }) {
async function getCurrentInstanceQuota(instanceId) {
const db = new PouchDB(instanceId)
const records = await db.allDocs(
getRecordParams("", null, {
include_docs: true,
})
)
const records = await db.allDocs({
startkey: "record:",
endkey: `record:\ufff0`,
})
const existingRecords = records.rows.length
const designDoc = await db.get("_design/database")