From 0f67313e616d693db5428dbd373e5ecd38dfba96 Mon Sep 17 00:00:00 2001 From: Frank Schmid Date: Fri, 24 Mar 2017 14:10:15 +0100 Subject: [PATCH] Save state --- lib/plugins/aws/common/index.js | 12 +++++++- lib/plugins/aws/common/lib/cleanupTempDir.js | 2 +- .../aws/common/lib/moveArtifactsToPackage.js | 30 +++++++++++++++++++ lib/plugins/aws/lib/naming.js | 4 +++ .../aws/package/compile/functions/index.js | 3 +- lib/plugins/aws/package/index.js | 11 ++++--- .../aws/package/lib/generateCoreTemplate.js | 4 ++- .../aws/package/lib/saveCompiledTemplate.js | 4 ++- .../aws/package/lib/saveServiceState.js | 22 ++++++++++++++ lifecycle-changes.md | 27 ++++++++--------- 10 files changed, 95 insertions(+), 24 deletions(-) create mode 100644 lib/plugins/aws/common/lib/moveArtifactsToPackage.js create mode 100644 lib/plugins/aws/package/lib/saveServiceState.js diff --git a/lib/plugins/aws/common/index.js b/lib/plugins/aws/common/index.js index 3df8e5f5f..801300513 100644 --- a/lib/plugins/aws/common/index.js +++ b/lib/plugins/aws/common/index.js @@ -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] @@ -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), }; } } diff --git a/lib/plugins/aws/common/lib/cleanupTempDir.js b/lib/plugins/aws/common/lib/cleanupTempDir.js index fc990cd86..62e674fd7 100644 --- a/lib/plugins/aws/common/lib/cleanupTempDir.js +++ b/lib/plugins/aws/common/lib/cleanupTempDir.js @@ -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'); diff --git a/lib/plugins/aws/common/lib/moveArtifactsToPackage.js b/lib/plugins/aws/common/lib/moveArtifactsToPackage.js new file mode 100644 index 000000000..ea0ddcc10 --- /dev/null +++ b/lib/plugins/aws/common/lib/moveArtifactsToPackage.js @@ -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(); + }, +}; diff --git a/lib/plugins/aws/lib/naming.js b/lib/plugins/aws/lib/naming.js index aa792f782..5fab5d4a5 100644 --- a/lib/plugins/aws/lib/naming.js +++ b/lib/plugins/aws/lib/naming.js @@ -57,6 +57,10 @@ module.exports = { return `${functionName}.zip`; }, + getServiceStateFileName() { + return 'serverless-state.json'; + }, + getCompiledTemplateFileName() { return 'cf-compiled-template.json'; }, diff --git a/lib/plugins/aws/package/compile/functions/index.js b/lib/plugins/aws/package/compile/functions/index.js index eee588597..b74668c74 100644 --- a/lib/plugins/aws/package/compile/functions/index.js +++ b/lib/plugins/aws/package/compile/functions/index.js @@ -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'); diff --git a/lib/plugins/aws/package/index.js b/lib/plugins/aws/package/index.js index fce373e06..0e761bde3 100644 --- a/lib/plugins/aws/package/index.js +++ b/lib/plugins/aws/package/index.js @@ -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')), }; } } diff --git a/lib/plugins/aws/package/lib/generateCoreTemplate.js b/lib/plugins/aws/package/lib/generateCoreTemplate.js index 13375b8c9..a7e54fd04 100644 --- a/lib/plugins/aws/package/lib/generateCoreTemplate.js +++ b/lib/plugins/aws/package/lib/generateCoreTemplate.js @@ -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); diff --git a/lib/plugins/aws/package/lib/saveCompiledTemplate.js b/lib/plugins/aws/package/lib/saveCompiledTemplate.js index d5ceb71e3..477098f93 100644 --- a/lib/plugins/aws/package/lib/saveCompiledTemplate.js +++ b/lib/plugins/aws/package/lib/saveCompiledTemplate.js @@ -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); diff --git a/lib/plugins/aws/package/lib/saveServiceState.js b/lib/plugins/aws/package/lib/saveServiceState.js new file mode 100644 index 000000000..14975792d --- /dev/null +++ b/lib/plugins/aws/package/lib/saveServiceState.js @@ -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(); + }, + +}; diff --git a/lifecycle-changes.md b/lifecycle-changes.md index a546c92d0..9296f3c7a 100644 --- a/lifecycle-changes.md +++ b/lifecycle-changes.md @@ -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