budibase/packages/server/src/api/controllers/deploy/aws.js

119 lines
2.7 KiB
JavaScript
Raw Normal View History

const fs = require("fs")
const AWS = require("aws-sdk")
const fetch = require("node-fetch")
2020-07-07 22:29:20 +02:00
const { budibaseAppsDir } = require("../../../utilities/budibaseDir")
2020-07-07 18:47:18 +02:00
async function invalidateCDN(cfDistribution, appId) {
2020-07-06 20:43:40 +02:00
const cf = new AWS.CloudFront({})
2020-07-07 22:29:20 +02:00
return cf
.createInvalidation({
DistributionId: cfDistribution,
InvalidationBatch: {
CallerReference: appId,
Paths: {
Quantity: 1,
Items: [`/assets/${appId}/*`],
},
},
})
.promise()
2020-07-06 20:43:40 +02:00
}
2020-07-03 00:22:20 +02:00
2020-07-08 17:31:26 +02:00
exports.fetchTemporaryCredentials = async function() {
2020-07-06 20:43:40 +02:00
const response = await fetch(process.env.DEPLOYMENT_CREDENTIALS_URL, {
method: "POST",
body: JSON.stringify({
2020-07-07 22:29:20 +02:00
apiKey: process.env.BUDIBASE_API_KEY,
}),
})
if (response.status !== 200) {
2020-07-07 22:29:20 +02:00
throw new Error(
`Error fetching temporary credentials for api key: ${process.env.BUDIBASE_API_KEY}`
)
}
const json = await response.json()
return json
2020-07-07 22:29:20 +02:00
}
2020-07-03 00:22:20 +02:00
const CONTENT_TYPE_MAP = {
html: "text/html",
css: "text/css",
2020-07-07 22:29:20 +02:00
js: "application/javascript",
}
2020-07-03 00:22:20 +02:00
2020-07-06 20:43:40 +02:00
/**
* Recursively walk a directory tree and execute a callback on all files.
2020-07-07 18:47:18 +02:00
* @param {String} dirPath - Directory to traverse
* @param {Function} callback - callback to execute on files
2020-07-06 20:43:40 +02:00
*/
function walkDir(dirPath, callback) {
for (let filename of fs.readdirSync(dirPath)) {
2020-07-07 22:29:20 +02:00
const filePath = `${dirPath}/${filename}`
2020-07-06 20:43:40 +02:00
const stat = fs.lstatSync(filePath)
2020-07-07 22:29:20 +02:00
2020-07-06 20:43:40 +02:00
if (stat.isFile()) {
2020-07-07 18:47:18 +02:00
callback(filePath)
2020-07-06 20:43:40 +02:00
} else {
walkDir(filePath, callback)
}
}
}
2020-07-08 17:31:26 +02:00
exports.uploadAppAssets = async function({
appId,
credentials,
bucket,
cfDistribution,
accountId,
}) {
AWS.config.update({
2020-07-07 22:29:20 +02:00
accessKeyId: credentials.AccessKeyId,
secretAccessKey: credentials.SecretAccessKey,
2020-07-07 22:29:20 +02:00
sessionToken: credentials.SessionToken,
})
const s3 = new AWS.S3({
params: {
2020-07-07 22:29:20 +02:00
Bucket: bucket,
},
})
const appAssetsPath = `${budibaseAppsDir()}/${appId}/public`
const appPages = fs.readdirSync(appAssetsPath)
const uploads = []
for (let page of appPages) {
2020-07-07 18:47:18 +02:00
walkDir(`${appAssetsPath}/${page}`, function prepareUploadsForS3(filePath) {
const fileExtension = [...filePath.split(".")].pop()
const fileBytes = fs.readFileSync(filePath)
2020-07-06 20:43:40 +02:00
2020-07-07 22:29:20 +02:00
const upload = s3
.upload({
Key: filePath.replace(appAssetsPath, `assets/${appId}`),
Body: fileBytes,
ContentType: CONTENT_TYPE_MAP[fileExtension],
Metadata: {
accountId,
},
})
.promise()
2020-07-06 20:43:40 +02:00
uploads.push(upload)
})
}
try {
2020-07-07 18:47:18 +02:00
await Promise.all(uploads)
2020-07-07 22:29:20 +02:00
await invalidateCDN(cfDistribution, appId)
} catch (err) {
console.error("Error uploading budibase app assets to s3", err)
throw err
}
2020-07-07 22:29:20 +02:00
}