Merge pull request #851 from Budibase/turn-off-invalidation

remove cf invalidation, adding validation around successful deploys only
This commit is contained in:
Martin McKeaveney 2020-11-10 19:55:43 +00:00 committed by GitHub
commit a1645db1f2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 18 additions and 99 deletions

View File

@ -44,6 +44,7 @@
}
onMount(() => {
fetchDeployments()
poll = setInterval(fetchDeployments, POLL_INTERVAL)
})
@ -55,10 +56,12 @@
<header>
<h4>Deployment History</h4>
<div class="deploy-div">
<a target="_blank" href={`https://${appId}.app.budi.live/${appId}`}>
View Your Deployed App →
</a>
<Button primary on:click={() => modal.show()}>View webhooks</Button>
{#if deployments.some(deployment => deployment.status === 'SUCCESS')}
<a target="_blank" href={`https://${appId}.app.budi.live/${appId}`}>
View Your Deployed App →
</a>
<Button primary on:click={() => modal.show()}>View webhooks</Button>
{/if}
</div>
</header>
<div class="deployment-list">

View File

@ -17,19 +17,16 @@
$: appId = $store.appId
async function deployApp() {
loading = true
const DEPLOY_URL = `/api/deploy`
try {
notifier.info("Starting Deployment..")
notifier.info(`Deployment started. Please wait.`)
const response = await api.post(DEPLOY_URL)
const json = await response.json()
if (response.status !== 200) {
throw new Error()
}
notifier.info(`Deployment started. Please wait.`)
loading = false
analytics.captureEvent("Deployed App", {
appId,
})
@ -43,7 +40,6 @@
})
analytics.captureException(err)
notifier.danger("Deployment unsuccessful. Please try again later.")
loading = false
}
}
</script>
@ -51,13 +47,7 @@
<section>
<div>
<h4>It's time to shine!</h4>
<Button secondary medium on:click={deployApp}>
Deploy App
{#if loading}
<Spacer extraSmall />
<Spinner size="10" />
{/if}
</Button>
<Button secondary medium on:click={deployApp}>Deploy App</Button>
</div>
<img
src="/_builder/assets/deploy-rocket.jpg"

View File

@ -12,7 +12,7 @@ JWT_SECRET={{cookieKey1}}
PORT=4001
# error level for koa-pino
LOG_LEVEL=error
LOG_LEVEL=info
DEPLOYMENT_CREDENTIALS_URL="https://dt4mpwwap8.execute-api.eu-west-1.amazonaws.com/prod/"
DEPLOYMENT_DB_URL="https://couchdb.budi.live:5984"

View File

@ -2,46 +2,11 @@ const fs = require("fs")
const { join } = require("../../../utilities/centralPath")
const AWS = require("aws-sdk")
const fetch = require("node-fetch")
const uuid = require("uuid")
const sanitize = require("sanitize-s3-objectkey")
const { budibaseAppsDir } = require("../../../utilities/budibaseDir")
const PouchDB = require("../../../db")
const env = require("../../../environment")
async function invalidateCDN(cfDistribution, appId) {
const cf = new AWS.CloudFront({})
const resp = await cf
.createInvalidation({
DistributionId: cfDistribution,
InvalidationBatch: {
CallerReference: `${appId}-${uuid.v4()}`,
Paths: {
Quantity: 1,
Items: [`/assets/${appId}/*`],
},
},
})
.promise()
return resp.Invalidation.Id
}
exports.isInvalidationComplete = async function(
distributionId,
invalidationId
) {
if (distributionId == null || invalidationId == null) {
return false
}
const cf = new AWS.CloudFront({})
const resp = await cf
.getInvalidation({
DistributionId: distributionId,
Id: invalidationId,
})
.promise()
return resp.Invalidation.Status === "Completed"
}
/**
* Finalises the deployment, updating the quota for the user API key
* The verification process returns the levels to update to.
@ -162,12 +127,7 @@ async function prepareUploadForS3({ s3Key, metadata, s3, file }) {
exports.prepareUploadForS3 = prepareUploadForS3
exports.uploadAppAssets = async function({
appId,
bucket,
cfDistribution,
accountId,
}) {
exports.uploadAppAssets = async function({ appId, bucket, accountId }) {
const s3 = new AWS.S3({
params: {
Bucket: bucket,
@ -224,8 +184,7 @@ exports.uploadAppAssets = async function({
db.put(fileUploads)
try {
await Promise.all(uploads)
return await invalidateCDN(cfDistribution, appId)
return await Promise.all(uploads)
} catch (err) {
console.error("Error uploading budibase app assets to s3", err)
throw err

View File

@ -4,7 +4,6 @@ const {
uploadAppAssets,
verifyDeployment,
updateDeploymentQuota,
isInvalidationComplete,
} = require("./aws")
const { DocumentTypes, SEPARATOR, UNICODE_MAX } = require("../../../db/utils")
const newid = require("../../../db/newid")
@ -20,12 +19,10 @@ const DeploymentStatus = {
}
// checks that deployments are in a good state, any pending will be updated
async function checkAllDeployments(deployments, user) {
async function checkAllDeployments(deployments) {
let updated = false
function update(deployment, status) {
deployment.status = status
delete deployment.invalidationId
delete deployment.cfDistribution
updated = true
}
@ -37,37 +34,6 @@ async function checkAllDeployments(deployments, user) {
) {
update(deployment, DeploymentStatus.FAILURE)
}
// if pending but not past failure point need to update them
else if (deployment.status === DeploymentStatus.PENDING) {
let complete = false
try {
complete = await isInvalidationComplete(
deployment.cfDistribution,
deployment.invalidationId
)
} catch (err) {
// system may have restarted, need to re-verify
if (
err !== undefined &&
err.code === "InvalidClientTokenId" &&
deployment.quota
) {
await verifyDeployment({
...user,
quota: deployment.quota,
})
complete = await isInvalidationComplete(
deployment.cfDistribution,
deployment.invalidationId
)
} else {
throw err
}
}
if (complete) {
update(deployment, DeploymentStatus.SUCCESS)
}
}
}
return { updated, deployments }
}
@ -157,7 +123,7 @@ async function deployApp({ appId, deploymentId }) {
console.log(`Uploading assets for appID ${appId} assets to s3..`)
const invalidationId = await uploadAppAssets({
await uploadAppAssets({
appId,
...verification,
})
@ -174,10 +140,9 @@ async function deployApp({ appId, deploymentId }) {
await storeLocalDeploymentHistory({
_id: deploymentId,
appId,
invalidationId,
cfDistribution: verification.cfDistribution,
quota: verification.quota,
status: DeploymentStatus.PENDING,
status: DeploymentStatus.SUCCESS,
})
} catch (err) {
await storeLocalDeploymentHistory({
@ -226,7 +191,7 @@ exports.deployApp = async function(ctx) {
status: DeploymentStatus.PENDING,
})
await deployApp({
deployApp({
...ctx.user,
deploymentId: deployment._id,
})

View File

@ -66,6 +66,8 @@ router.use(async (ctx, next) => {
}
})
router.get("/health", ctx => (ctx.status = 200))
router.use(authRoutes.routes())
router.use(authRoutes.allowedMethods())