Save state

This commit is contained in:
Frank Schmid 2017-03-24 14:10:15 +01:00 committed by Eslam A. Hefnawy
parent b0ef26b4a0
commit 0f67313e61
10 changed files with 95 additions and 24 deletions

View File

@ -9,6 +9,7 @@
const BbPromise = require('bluebird');
const validate = require('../lib/validate');
const cleanupTempDir = require('./lib/cleanupTempDir');
const moveArtifactsToPackage = require('./lib/moveArtifactsToPackage');
class AwsCommon {
constructor(serverless, options) {
@ -19,7 +20,8 @@ class AwsCommon {
Object.assign(
this,
validate,
cleanupTempDir
cleanupTempDir,
moveArtifactsToPackage
);
// Internal commands are addressed as aws:common:<lifecycleevent|command>[:lifecycleevent]
@ -40,6 +42,11 @@ class AwsCommon {
'cleanup',
],
},
moveArtifactsToPackage: {
lifecycleEvents: [
'move',
],
},
},
},
},
@ -52,6 +59,9 @@ class AwsCommon {
'aws:common:cleanupTempDir:cleanup': () => BbPromise.bind(this)
.then(this.cleanupTempDir),
'aws:common:moveArtifactsToPackage:move': () => BbPromise.bind(this)
.then(this.moveArtifactsToPackage),
};
}
}

View File

@ -5,7 +5,7 @@ const path = require('path');
const fse = require('fs-extra');
module.exports = {
cleanup() {
cleanupTempDir() {
if (this.serverless.config.servicePath) {
const serverlessTmpDirPath = path.join(this.serverless.config.servicePath, '.serverless');

View File

@ -0,0 +1,30 @@
'use strict';
const BbPromise = require('bluebird');
const path = require('path');
const fse = require('fs-extra');
const _ = require('lodash');
module.exports = {
moveArtifactsToPackage() {
const packagePath = this.options.package ||
this.serverless.service.package.path ||
path.join(this.serverless.config.servicePath || '.', '.serverless');
// Only move the artifacts if it was requested by the user
if (this.serverless.config.servicePath && !_.endsWith(packagePath, '.serverless')) {
const serverlessTmpDirPath = path.join(this.serverless.config.servicePath, '.serverless');
if (this.serverless.utils.dirExistsSync(serverlessTmpDirPath)) {
if (this.serverless.utils.dirExistsSync(packagePath)) {
fse.removeSync(packagePath);
}
this.serverless.utils.writeFileDir(packagePath);
this.serverless.utils.copyDirContentsSync(serverlessTmpDirPath, packagePath);
fse.removeSync(serverlessTmpDirPath);
}
}
return BbPromise.resolve();
},
};

View File

@ -57,6 +57,10 @@ module.exports = {
return `${functionName}.zip`;
},
getServiceStateFileName() {
return 'serverless-state.json';
},
getCompiledTemplateFileName() {
return 'cf-compiled-template.json';
},

View File

@ -10,8 +10,7 @@ class AwsCompileFunctions {
this.serverless = serverless;
this.options = options;
this.packagePath = this.options.package ||
this.serverless.service.package.path ||
this.packagePath = this.serverless.service.package.path ||
path.join(this.serverless.config.servicePath || '.', '.serverless');
this.provider = this.serverless.getProvider('aws');

View File

@ -5,6 +5,7 @@ const path = require('path');
const mergeCustomProviderResources = require('./lib/mergeCustomProviderResources');
const generateArtifactDirectoryName = require('./lib/generateArtifactDirectoryName');
const generateCoreTemplate = require('./lib/generateCoreTemplate');
const saveServiceState = require('./lib/saveServiceState');
const saveCompiledTemplate = require('./lib/saveCompiledTemplate');
const mergeIamTemplates = require('./lib/mergeIamTemplates');
const zipService = require('./lib/zipService');
@ -27,6 +28,7 @@ class AwsPackage {
mergeIamTemplates,
generateArtifactDirectoryName,
mergeCustomProviderResources,
saveServiceState,
saveCompiledTemplate
);
@ -40,7 +42,7 @@ class AwsPackage {
finalize: {
lifecycleEvents: [
'mergeCustomProviderResources',
'saveCompiledTemplate',
'saveServiceState',
],
},
},
@ -70,7 +72,6 @@ class AwsPackage {
.then(this.generateArtifactDirectoryName),
'package:finalize': () => BbPromise.bind(this)
.then(() => this.serverless.pluginManager.spawn('aws:common:validate'))
.then(() => this.serverless.pluginManager.spawn('aws:package:finalize')),
/**
@ -81,8 +82,10 @@ class AwsPackage {
'aws:package:finalize:mergeCustomProviderResources': () => BbPromise.bind(this)
.then(this.mergeCustomProviderResources),
'aws:package:finalize:saveCompiledTemplate': () => BbPromise.bind(this)
.then(this.saveCompiledTemplate),
'aws:package:finalize:saveServiceState': () => BbPromise.bind(this)
.then(this.saveCompiledTemplate)
.then(this.saveServiceState)
.then(() => this.serverless.pluginManager.spawn('aws:common:moveArtifactsToPackage')),
};
}
}

View File

@ -53,7 +53,9 @@ module.exports = {
const coreTemplateFileName = this.provider.naming.getCoreTemplateFileName();
const coreTemplateFilePath = path.join(this.packagePath, coreTemplateFileName);
const coreTemplateFilePath = path.join(this.serverless.config.servicePath,
'.serverless',
coreTemplateFileName);
this.serverless.utils.writeFileSync(coreTemplateFilePath,
this.serverless.service.provider.compiledCloudFormationTemplate);

View File

@ -7,7 +7,9 @@ module.exports = {
saveCompiledTemplate() {
const compiledTemplateFileName = this.provider.naming.getCompiledTemplateFileName();
const compiledTemplateFilePath = path.join(this.packagePath, compiledTemplateFileName);
const compiledTemplateFilePath = path.join(this.serverless.config.servicePath,
'.serverless',
compiledTemplateFileName);
this.serverless.utils.writeFileSync(compiledTemplateFilePath,
this.serverless.service.provider.compiledCloudFormationTemplate);

View File

@ -0,0 +1,22 @@
'use strict';
const BbPromise = require('bluebird');
const path = require('path');
const _ = require('lodash');
module.exports = {
saveServiceState() {
const serviceStateFileName = this.provider.naming.getServiceStateFileName();
const serviceStateFilePath = path.join(this.serverless.config.servicePath,
'.serverless',
serviceStateFileName);
const state = _.assign({}, _.omit(this.serverless.service, ['serverless', 'package']));
this.serverless.utils.writeFileSync(serviceStateFilePath,
state);
return BbPromise.resolve();
},
};

View File

@ -28,9 +28,19 @@ as each command can spawn the well known lifecycles that are already used by plu
guarantees that everyone who depends e.g. on `deploy:deploy` hooks will be triggered,
regardless, if the user run a packaged or full deploy.
### Serverless state
### Commands / Invocations
The package command will, shortly said, store the compiled CF template and the deploy
#### Serverless deploy
The `serverless deploy` command is completely non-breaking. The complete serverless
internal state is kept along the package and deploy stages of the invocation.
Lifecycle events that have been moved to the package stage are redirected automatically
and tagged as deprecated (see below).
### Serverless package / deploy
The package command will, shortly said, store the Serverless service state and the deploy
command will reload it and continue with the loaded state. Plugins hooked into `before:deploy:deploy`
will have the same state as before the change and will continue to work as before. This also
applies to plugins that hooked `deploy:initialize`.
@ -200,25 +210,14 @@ the `deploy`command.
**package:initialize**
-> aws:package:initialize:generateCoreTemplate
-> aws:package:initialize:mergeIamTemplates
-> aws:package:initialize:generateArtifactDirectoryName
**package:createDeploymentArtifacts**
-> aws:package:createDeploymentArtifacts:validate
-> aws:common:validate:validate
-> aws:package:createDeploymentArtifacts:packageService
**package:compileFunctions**
**package:compileEvents**
**package:finalize**
-> aws:package:finalize:mergeCustomProviderResources
-> aws:package:finalize:saveCompiledTemplate
-> aws:package:finalize:saveServiceState
# Common