mirror of
https://github.com/serverless/serverless.git
synced 2026-01-18 14:58:43 +00:00
135 lines
4.6 KiB
JavaScript
135 lines
4.6 KiB
JavaScript
'use strict';
|
|
|
|
const _ = require('lodash');
|
|
const BbPromise = require('bluebird');
|
|
const platform = require('@serverless/platform-sdk');
|
|
const getS3EndpointForRegion = require('../utils/getS3EndpointForRegion');
|
|
|
|
const NO_UPDATE_MESSAGE = 'No updates are to be performed.';
|
|
|
|
module.exports = {
|
|
createFallback() {
|
|
this.createLater = false;
|
|
this.serverless.cli.log('Creating Stack...');
|
|
|
|
const stackName = this.provider.naming.getStackName();
|
|
let stackTags = { STAGE: this.provider.getStage() };
|
|
const compiledTemplateFileName = 'compiled-cloudformation-template.json';
|
|
const s3Endpoint = getS3EndpointForRegion(this.provider.getRegion());
|
|
const templateUrl = `https://${s3Endpoint}/${this.bucketName}/${this.serverless.service.package.artifactDirectoryName}/${compiledTemplateFileName}`;
|
|
|
|
// Merge additional stack tags
|
|
if (typeof this.serverless.service.provider.stackTags === 'object') {
|
|
stackTags = _.extend(stackTags, this.serverless.service.provider.stackTags);
|
|
}
|
|
|
|
const params = {
|
|
StackName: stackName,
|
|
OnFailure: 'ROLLBACK',
|
|
Capabilities: [
|
|
'CAPABILITY_IAM',
|
|
'CAPABILITY_NAMED_IAM',
|
|
],
|
|
Parameters: [],
|
|
TemplateURL: templateUrl,
|
|
Tags: Object.keys(stackTags).map((key) => ({ Key: key, Value: stackTags[key] })),
|
|
};
|
|
|
|
if (this.serverless.service.provider.cfnRole) {
|
|
params.RoleARN = this.serverless.service.provider.cfnRole;
|
|
}
|
|
|
|
if (this.serverless.service.provider.notificationArns) {
|
|
params.NotificationARNs = this.serverless.service.provider.notificationArns;
|
|
}
|
|
|
|
return this.provider.request('CloudFormation',
|
|
'createStack',
|
|
params)
|
|
.then((cfData) => this.monitorStack('create', cfData)).catch((e) => {
|
|
if (this.serverless.service.deployment && this.serverless.service.deployment.deploymentId) {
|
|
const deploymentData = this.serverless.service.deployment;
|
|
deploymentData.status = 'Failed';
|
|
return platform.updateDeployment(deploymentData)
|
|
.then(() => {
|
|
throw e;
|
|
});
|
|
}
|
|
throw e;
|
|
});
|
|
},
|
|
|
|
update() {
|
|
const compiledTemplateFileName = 'compiled-cloudformation-template.json';
|
|
const s3Endpoint = getS3EndpointForRegion(this.provider.getRegion());
|
|
const templateUrl = `https://${s3Endpoint}/${this.bucketName}/${this.serverless.service.package.artifactDirectoryName}/${compiledTemplateFileName}`;
|
|
|
|
this.serverless.cli.log('Updating Stack...');
|
|
const stackName = this.provider.naming.getStackName();
|
|
let stackTags = { STAGE: this.provider.getStage() };
|
|
|
|
// Merge additional stack tags
|
|
if (typeof this.serverless.service.provider.stackTags === 'object') {
|
|
stackTags = _.extend(stackTags, this.serverless.service.provider.stackTags);
|
|
}
|
|
|
|
const params = {
|
|
StackName: stackName,
|
|
Capabilities: [
|
|
'CAPABILITY_IAM',
|
|
'CAPABILITY_NAMED_IAM',
|
|
],
|
|
Parameters: [],
|
|
TemplateURL: templateUrl,
|
|
Tags: Object.keys(stackTags).map((key) => ({ Key: key, Value: stackTags[key] })),
|
|
};
|
|
|
|
if (this.serverless.service.provider.cfnRole) {
|
|
params.RoleARN = this.serverless.service.provider.cfnRole;
|
|
}
|
|
|
|
if (this.serverless.service.provider.notificationArns) {
|
|
params.NotificationARNs = this.serverless.service.provider.notificationArns;
|
|
}
|
|
|
|
// Policy must have at least one statement, otherwise no updates would be possible at all
|
|
if (this.serverless.service.provider.stackPolicy &&
|
|
!_.isEmpty(this.serverless.service.provider.stackPolicy)) {
|
|
params.StackPolicyBody = JSON.stringify({
|
|
Statement: this.serverless.service.provider.stackPolicy,
|
|
});
|
|
}
|
|
|
|
return this.provider.request('CloudFormation',
|
|
'updateStack',
|
|
params)
|
|
.then((cfData) => this.monitorStack('update', cfData))
|
|
.catch((e) => {
|
|
if (e.message === NO_UPDATE_MESSAGE) {
|
|
return BbPromise.resolve();
|
|
}
|
|
if (this.serverless.service.deployment && this.serverless.service.deployment.deploymentId) {
|
|
const deploymentData = this.serverless.service.deployment;
|
|
deploymentData.status = 'Failed';
|
|
return platform.updateDeployment(deploymentData)
|
|
.then(() => {
|
|
throw e;
|
|
});
|
|
}
|
|
throw e;
|
|
});
|
|
},
|
|
|
|
updateStack() {
|
|
return BbPromise.bind(this)
|
|
.then(() => {
|
|
if (this.createLater) {
|
|
return BbPromise.bind(this)
|
|
.then(this.createFallback);
|
|
}
|
|
return BbPromise.bind(this)
|
|
.then(this.update);
|
|
});
|
|
},
|
|
};
|