2020-12-04 06:45:20 +13:00
|
|
|
const AWS = require("aws-sdk")
|
2020-07-02 08:56:53 +12:00
|
|
|
const fetch = require("node-fetch")
|
2020-10-29 09:35:06 +13:00
|
|
|
const env = require("../../../environment")
|
2020-12-17 08:50:02 +13:00
|
|
|
const {
|
|
|
|
deployToObjectStore,
|
|
|
|
performReplication,
|
|
|
|
fetchCredentials,
|
|
|
|
} = require("./utils")
|
2020-10-08 22:56:32 +13:00
|
|
|
|
2020-10-08 08:37:55 +13:00
|
|
|
/**
|
|
|
|
* Verifies the users API key and
|
2020-11-06 00:44:18 +13:00
|
|
|
* Verifies that the deployment fits within the quota of the user
|
|
|
|
* Links to the "check-api-key" lambda.
|
2020-12-02 02:39:34 +13:00
|
|
|
* @param {object} deployment - information about the active deployment, including the appId and quota.
|
2020-10-08 08:37:55 +13:00
|
|
|
*/
|
2020-12-02 02:39:34 +13:00
|
|
|
exports.preDeployment = async function(deployment) {
|
2020-12-17 08:50:02 +13:00
|
|
|
const json = await fetchCredentials(env.DEPLOYMENT_CREDENTIALS_URL, {
|
|
|
|
apiKey: env.BUDIBASE_API_KEY,
|
|
|
|
appId: deployment.getAppId(),
|
|
|
|
quota: deployment.getQuota(),
|
2020-07-02 08:56:53 +12:00
|
|
|
})
|
|
|
|
|
2020-10-21 03:06:34 +13:00
|
|
|
// set credentials here, means any time we're verified we're ready to go
|
|
|
|
if (json.credentials) {
|
2020-12-04 06:45:20 +13:00
|
|
|
AWS.config.update({
|
2020-10-21 03:06:34 +13:00
|
|
|
accessKeyId: json.credentials.AccessKeyId,
|
|
|
|
secretAccessKey: json.credentials.SecretAccessKey,
|
|
|
|
sessionToken: json.credentials.SessionToken,
|
|
|
|
})
|
|
|
|
}
|
2020-07-02 08:56:53 +12:00
|
|
|
|
|
|
|
return json
|
2020-07-08 08:29:20 +12:00
|
|
|
}
|
2020-07-02 08:56:53 +12:00
|
|
|
|
2020-07-07 06:43:40 +12:00
|
|
|
/**
|
2020-12-02 02:39:34 +13:00
|
|
|
* Finalises the deployment, updating the quota for the user API key
|
|
|
|
* The verification process returns the levels to update to.
|
|
|
|
* Calls the "deployment-success" lambda.
|
|
|
|
* @param {object} deployment information about the active deployment, including the quota info.
|
|
|
|
* @returns {Promise<object>} The usage has been updated against the user API key.
|
2020-07-07 06:43:40 +12:00
|
|
|
*/
|
2020-12-02 02:39:34 +13:00
|
|
|
exports.postDeployment = async function(deployment) {
|
|
|
|
const DEPLOYMENT_SUCCESS_URL =
|
|
|
|
env.DEPLOYMENT_CREDENTIALS_URL + "deploy/success"
|
2020-09-16 03:22:13 +12:00
|
|
|
|
2020-12-02 02:39:34 +13:00
|
|
|
const response = await fetch(DEPLOYMENT_SUCCESS_URL, {
|
|
|
|
method: "POST",
|
|
|
|
body: JSON.stringify({
|
|
|
|
apiKey: env.BUDIBASE_API_KEY,
|
|
|
|
quota: deployment.getQuota(),
|
|
|
|
}),
|
|
|
|
headers: {
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
Accept: "application/json",
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
if (response.status !== 200) {
|
|
|
|
throw new Error(`Error updating deployment quota for API Key`)
|
2020-09-24 03:15:09 +12:00
|
|
|
}
|
2020-09-16 23:18:47 +12:00
|
|
|
|
2020-12-02 02:39:34 +13:00
|
|
|
return await response.json()
|
|
|
|
}
|
2020-09-24 03:15:09 +12:00
|
|
|
|
2020-12-02 02:39:34 +13:00
|
|
|
exports.deploy = async function(deployment) {
|
|
|
|
const appId = deployment.getAppId()
|
|
|
|
const { bucket, accountId } = deployment.getVerification()
|
2020-12-04 06:45:20 +13:00
|
|
|
const metadata = { accountId }
|
2021-03-23 07:06:10 +13:00
|
|
|
await deployToObjectStore(appId, bucket, metadata)
|
2020-07-08 08:29:20 +12:00
|
|
|
}
|
2020-12-05 06:10:01 +13:00
|
|
|
|
|
|
|
exports.replicateDb = async function(deployment) {
|
|
|
|
const appId = deployment.getAppId()
|
2020-12-17 08:50:02 +13:00
|
|
|
const verification = deployment.getVerification()
|
|
|
|
return performReplication(
|
|
|
|
appId,
|
|
|
|
verification.couchDbSession,
|
|
|
|
env.DEPLOYMENT_DB_URL
|
|
|
|
)
|
2020-12-05 06:10:01 +13:00
|
|
|
}
|