Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
console.log(`Packaging ${CONFIGS.compFullname} application...`)
// unzip source zip file
let zipPath
if (!inputs.code.src) {
// add default template
const downloadPath = `/tmp/${generateId()}`
const filename = 'template'
console.log(`Installing Default ${CONFIGS.compFullname} App...`)
try {
await download(CONFIGS.templateUrl, downloadPath, {
filename: `${filename}.zip`
})
} catch (e) {
throw new TypeError(`DOWNLOAD_TEMPLATE`, 'Download default template failed.')
}
zipPath = `${downloadPath}/${filename}.zip`
} else {
zipPath = inputs.code.src
}
return zipPath
}
async remove() {
console.log(`Removing ${CONFIGS.compFullname} App...`)
const { state } = this
const { regionList = [] } = state
const credentials = this.getCredentials()
const removeHandlers = []
for (let i = 0; i < regionList.length; i++) {
const curRegion = regionList[i]
const curState = state[curRegion]
const scf = new Scf(credentials, curRegion)
const apigw = new Apigw(credentials, curRegion)
const handler = async () => {
await scf.remove({
functionName: curState.functionName,
namespace: curState.namespace
})
// if disable apigw, no need to remove
if (state.apigwDisabled !== true) {
await apigw.remove({
created: curState.created,
environment: curState.environment,
serviceId: curState.serviceId,
apiList: curState.apiList,
customDomains: curState.customDomains
})
}
}
async remove() {
console.log(`Removing ${CONFIGS.compFullname} App...`)
const { state } = this
const { regionList = [] } = state
const credentials = this.getCredentials()
const removeHandlers = []
for (let i = 0; i < regionList.length; i++) {
const curRegion = regionList[i]
const curState = state[curRegion]
const scf = new Scf(credentials, curRegion)
const apigw = new Apigw(credentials, curRegion)
const handler = async () => {
await scf.remove({
functionName: curState.functionName,
namespace: curState.namespace
})
// if disable apigw, no need to remove
if (state.apigwDisabled !== true) {
await apigw.remove({
created: curState.created,
environment: curState.environment,
serviceId: curState.serviceId,
apiList: curState.apiList,
customDomains: curState.customDomains
})
}
const uploadCodeToCos = async (instance, appId, credentials, inputs, region) => {
const bucketName = inputs.code.bucket || `sls-cloudfunction-${region}-code`
const objectName = inputs.code.object || `${inputs.name}-${Math.floor(Date.now() / 1000)}.zip`
// if set bucket and object not pack code
if (!inputs.code.bucket || !inputs.code.object) {
const zipPath = await getCodeZipPath(instance, inputs)
console.log(`Code zip path ${zipPath}`)
// save the zip path to state for lambda to use it
instance.state.zipPath = zipPath
const cos = new Cos(credentials, region)
if (!inputs.code.bucket) {
// create default bucket
await cos.deploy({
bucket: bucketName + '-' + appId,
force: true,
lifecycle: [
{
status: 'Enabled',
id: 'deleteObject',
filter: '',
expiration: { days: '10' },
abortIncompleteMultipartUpload: { daysAfterInitiation: '10' }
}
]
})
const apigwDeployer = async () => {
const apigw = new Apigw(credentials, curRegion)
const oldState = this.state[curRegion] || {}
const apigwInputs = {
...inputs,
oldState: {
apiList: oldState.apiList || [],
customDomains: oldState.customDomains || []
}
}
// different region deployment has different service id
apigwInputs.serviceId = getServiceId(this, curRegion)
const apigwOutput = await apigw.deploy(deepClone(apigwInputs))
outputs[curRegion] = {
serviceId: apigwOutput.serviceId,
subDomain: apigwOutput.subDomain,
environment: apigwOutput.environment,
async deployFunction(credentials, inputs, regionList) {
if (!inputs.role) {
try {
const camClient = new Cam(credentials)
const roleExist = await camClient.CheckSCFExcuteRole()
if (roleExist) {
inputs.role = 'QCS_SCFExcuteRole'
}
} catch (e) {
// no op
}
}
const outputs = {}
const appId = this.getAppId()
const funcDeployer = async (curRegion) => {
const code = await uploadCodeToCos(this, appId, credentials, inputs, curRegion)
const scf = new Scf(credentials, curRegion)
const tempInputs = {
getCredentials() {
const { tmpSecrets } = this.credentials.tencent
if (!tmpSecrets || !tmpSecrets.TmpSecretId) {
throw new TypeError(
'CREDENTIAL',
'Cannot get secretId/Key, your account could be sub-account and does not have the access to use SLS_QcsRole, please make sure the role exists first, then visit https://cloud.tencent.com/document/product/1154/43006, follow the instructions to bind the role to your account.'
)
}
return {
SecretId: tmpSecrets.TmpSecretId,
SecretKey: tmpSecrets.TmpSecretKey,
Token: tmpSecrets.Token
}
}
const validateTraffic = (num) => {
if (getType(num) !== 'Number') {
throw new TypeError(
`PARAMETER_${CONFIGS.compName.toUpperCase()}_TRAFFIC`,
'traffic must be a number'
)
}
if (num < 0 || num > 1) {
throw new TypeError(
`PARAMETER_${CONFIGS.compName.toUpperCase()}_TRAFFIC`,
'traffic must be a number between 0 and 1'
)
}
return true
}
const validateTraffic = (num) => {
if (getType(num) !== 'Number') {
throw new TypeError(
`PARAMETER_${CONFIGS.compName.toUpperCase()}_TRAFFIC`,
'traffic must be a number'
)
}
if (num < 0 || num > 1) {
throw new TypeError(
`PARAMETER_${CONFIGS.compName.toUpperCase()}_TRAFFIC`,
'traffic must be a number between 0 and 1'
)
}
return true
}
const funcDeployer = async (curRegion) => {
const code = await uploadCodeToCos(this, appId, credentials, inputs, curRegion)
const scf = new Scf(credentials, curRegion)
const tempInputs = {
...inputs,
code
}
const scfOutput = await scf.deploy(deepClone(tempInputs))
outputs[curRegion] = {
functionName: scfOutput.FunctionName,
runtime: scfOutput.Runtime,
namespace: scfOutput.Namespace
}
this.state[curRegion] = {
...(this.state[curRegion] ? this.state[curRegion] : {}),
...outputs[curRegion]
}