From 22c4af6b90d1f3cb211bfc7ca2e00c8ad4b85e73 Mon Sep 17 00:00:00 2001 From: ac360 Date: Mon, 14 Mar 2016 19:01:29 -0700 Subject: [PATCH] SerializerFileSystem: Rename to Serializer --- lib/Component.js | 182 ------------------------------------ lib/Endpoint.js | 6 +- lib/Event.js | 4 +- lib/Function.js | 6 +- lib/Project.js | 4 +- lib/Region.js | 4 +- lib/Resources.js | 4 +- lib/RuntimePython27.js | 79 ++++++++++++---- lib/SerializerFileSystem.js | 5 +- lib/Stage.js | 4 +- lib/Templates.js | 4 +- lib/Variables.js | 4 +- 12 files changed, 82 insertions(+), 224 deletions(-) delete mode 100644 lib/Component.js diff --git a/lib/Component.js b/lib/Component.js deleted file mode 100644 index 2692c0442..000000000 --- a/lib/Component.js +++ /dev/null @@ -1,182 +0,0 @@ -'use strict'; - -const SError = require('./Error'), - SerializerFileSystem = require('./SerializerFileSystem'), - BbPromise = require('bluebird'), - path = require('path'), - _ = require('lodash'), - fs = require('fs'); - -let supportedRuntimes = { - "nodejs": require('./RuntimeNode'), - "python2.7": require('./RuntimePython27') -}; - -let SUtils; - -class Component extends SerializerFileSystem { - - /** - * Constructor - */ - - constructor(S, data, filePath) { - - super(S); - - SUtils = S.utils; - - this._S = S; - this._class = 'Component'; - this._config = config || {}; - - // Default Properties - this.name = data.name || 'component' + SUtils.generateShortId(6); - this.setRuntime(data.runtime || 'nodejs'); - this.custom = {}; - - this.templates = new this._S.classes.Templates(this._S, this); //TODO: add filepath - this._filePath = filePath || this.getProject().getRootPath(this.name); - - if (data) this.fromObject(data); - } - - static getSupportedRuntimes() { - return supportedRuntimes; - } - - updateConfig(config) { - if (config) this._config = _.merge(this._config, config); - } - - load() { - return this.deserialize(this); - } - - save(options) { - return this.serialize(this, options); - } - - toObject() { - return SUtils.exportObject(_.cloneDeep(this)); - } - - toObjectPopulated(options) { - - options = options || {}; - - // Validate: Check Stage & Region - if (!options.stage || !options.region) throw new SError('Both "stage" and "region" params are required'); - - // Validate: Check project path is set - if (!this._S.hasProject()) throw new SError('Function could not be populated because no project path has been set on Serverless instance'); - - let obj = this.toObject(); - - // Populate Sub-Assets Separately - let functions; - if (this.functions) { - functions = _.mapValues(this.functions, (f) => f.toObjectPopulated(options)); - delete obj.functions; - } - - // Merge templates - let templates = _.merge(this.getProject().getTemplates().toObject(), - this.getTemplates().toObject()); - - // Populate - let populated = SUtils.populate(this.getProject(), templates, obj, options.stage, options.region); - - if (functions) populated.functions = functions; - - return populated; - } - - fromObject(data) { - - // Flush data - this.functions = {}; - - if(data.runtime) { - this.setRuntime(data.runtime); - } - if (data.functions) { - let temp = {}; - for (let f of Object.keys(data.functions)) { - if (this.functions[f]) { - temp[f] = this.functions[f].fromObject(data.functions[f]); - } else { - temp[f] = new this._S.classes.Function(this._S, this, data.functions[f]); - } - } - delete data.functions; - this.functions = temp; - } - if (data.templates) { - this.templates.fromObject(data.templates); - delete data.templates; - } - - _.assign(this, data); - return this; - } - - setTemplates(templates) { - this.templates = templates; - } - - getTemplates() { - return this.templates; - } - - getName(){ - return this.name; - } - - getRuntime() { - return this._runtime; - } - - setRuntime( runtimeName ) { - let runtime = supportedRuntimes[ runtimeName ]; - - if (runtime) { - this.runtime = runtimeName; - this._runtime = new runtime( this._S ); - } else { - throw new SError( `Runtime ${runtimeName} is not supported!` ); - } - } - - getProject() { - return this._project; - } - - getAllFunctions() { - return _.values( this.functions ); - } - - getAllEndpoints() { - return _.flatten( _.map( this.getAllFunctions(), f => f.getAllEndpoints() ) ); - } - - setFunction( func ){ - this.functions[ func.name ] = func; - } - - getFilePath() { - return this._filePath; - } - - getRootPath() { - let args = _.toArray( arguments ); - args.unshift(path.dirname(this.getFilePath())); - return path.join.apply( path, args ); - } - - static validateName(name) { - return /^[a-zA-Z\d]+$/.test(name); - } -} - -module.exports = Component; \ No newline at end of file diff --git a/lib/Endpoint.js b/lib/Endpoint.js index 8a61eb9af..caf06c1fa 100644 --- a/lib/Endpoint.js +++ b/lib/Endpoint.js @@ -1,7 +1,7 @@ 'use strict'; -const SError = require('./Error'), - SerializerFileSystem = require('./SerializerFileSystem'), +const SError = require('./Error'), + Serializer = require('./Serializer'), BbPromise = require('bluebird'), path = require('path'), fs = require('fs'), @@ -9,7 +9,7 @@ const SError = require('./Error'), let SUtils; -class Endpoint extends SerializerFileSystem { +class Endpoint extends Serializer { constructor(S, func, data) { diff --git a/lib/Event.js b/lib/Event.js index 883431709..d459ad2ca 100644 --- a/lib/Event.js +++ b/lib/Event.js @@ -1,7 +1,7 @@ 'use strict'; const SError = require('./Error'), - SerializerFileSystem = require('./SerializerFileSystem'), + Serializer = require('./Serializer'), BbPromise = require('bluebird'), path = require('path'), fs = require('fs'), @@ -9,7 +9,7 @@ const SError = require('./Error'), let SUtils; -class Event extends SerializerFileSystem { +class Event extends Serializer { constructor(S, func, data) { diff --git a/lib/Function.js b/lib/Function.js index d5b49ddc0..1aa80ed82 100644 --- a/lib/Function.js +++ b/lib/Function.js @@ -1,7 +1,7 @@ 'use strict'; -const SError = require('./Error'), - SerializerFileSystem = require('./SerializerFileSystem'), +const SError = require('./Error'), + Serializer = require('./Serializer'), BbPromise = require('bluebird'), async = require('async'), path = require('path'), @@ -10,7 +10,7 @@ const SError = require('./Error'), let SUtils; -class Function extends SerializerFileSystem { +class Function extends Serializer { constructor(S, data, filePath) { diff --git a/lib/Project.js b/lib/Project.js index 6f414e5c3..4589bf0f6 100644 --- a/lib/Project.js +++ b/lib/Project.js @@ -1,7 +1,7 @@ 'use strict'; const SError = require('./Error'), - SerializerFileSystem = require('./SerializerFileSystem'), + Serializer = require('./Serializer'), BbPromise = require('bluebird'), path = require('path'), _ = require('lodash'), @@ -10,7 +10,7 @@ const SError = require('./Error'), let SUtils; -class Project extends SerializerFileSystem { +class Project extends Serializer { constructor(S, data) { diff --git a/lib/Region.js b/lib/Region.js index 5af45c5ab..16855e2c0 100644 --- a/lib/Region.js +++ b/lib/Region.js @@ -1,13 +1,13 @@ 'use strict'; const SError = require('./Error'), - SerializerFileSystem = require('./SerializerFileSystem'), + Serializer = require('./Serializer'), BbPromise = require('bluebird'), _ = require('lodash'); let SUtils; -class Region extends SerializerFileSystem { +class Region extends Serializer { constructor(S, stage, data) { super(S); diff --git a/lib/Resources.js b/lib/Resources.js index 0b5d7004a..bf97bfc8b 100644 --- a/lib/Resources.js +++ b/lib/Resources.js @@ -1,14 +1,14 @@ 'use strict'; const SError = require('./Error'), - SerializerFileSystem = require('./SerializerFileSystem'), + Serializer = require('./Serializer'), fs = require('fs'), _ = require('lodash'), BbPromise = require('bluebird'); let SUtils; -class Resources extends SerializerFileSystem { +class Resources extends Serializer { constructor(S, data, filePath) { diff --git a/lib/RuntimePython27.js b/lib/RuntimePython27.js index 7b500cf6d..6de11b313 100644 --- a/lib/RuntimePython27.js +++ b/lib/RuntimePython27.js @@ -1,6 +1,6 @@ 'use strict'; -const SError = require('./Error'), +const SError = require('./Error'), SCli = require('./utils/cli'), RuntimeBase = require('./RuntimeBase'), BbPromise = require('bluebird'), @@ -14,13 +14,14 @@ const SError = require('./Error'), /** * Pip install using prefix strategy (not virtualenv), requires a modern `pip` version */ + function pipPrefixInstall(requirements, dir) { if (exec(`pip install -t "${dir}" -r "${requirements}"`, { silent: false }).code !== 0) { throw new SError(`Error executing pip install on ${dir}`, SError.errorCodes.UNKNOWN); } process.chdir(process.cwd()); -}; +} let SUtils; @@ -31,15 +32,9 @@ class ServerlessRuntimePython27 extends RuntimeBase { SUtils = S.utils; } - installDepedencies( dir ) { - SCli.log("Installing default python dependencies with pip..."); - SCli.log(`-----------------`); - pipPrefixInstall( - this.S.getProject().getRootPath( dir, 'requirements.txt'), - this.S.getProject().getRootPath( dir, 'vendored') - ); - SCli.log(`-----------------`); - } + /** + * Scaffold + */ scaffold(func) { const handlerPath = path.join(this.S.getServerlessPath(), 'templates', 'python2.7', 'handler.py'); @@ -51,14 +46,18 @@ class ServerlessRuntimePython27 extends RuntimeBase { ])); } + /** + * Run + */ + run(func) { return SUtils .readFile(func.getRootPath('event.json')) .then((functionEvent) => { const handlerArr = func.handler.split('/').pop().split('.'), - functionFile = func.getRootPath(handlerArr[0] + '.py'), - functionHandler = handlerArr[1], - result = {}; + functionFile = func.getRootPath(handlerArr[0] + '.py'), + functionHandler = handlerArr[1], + result = {}; const childArgs = [ '--event', JSON.stringify(functionEvent), @@ -96,18 +95,46 @@ class ServerlessRuntimePython27 extends RuntimeBase { }); } + /** + * Build + * - Build the function in this runtime + */ + + build(func, stage, region) { + + // Validate + if (!func._class || func._class !== 'Function') return BbPromise.reject(new SError('A function instance is required')); + + let pathDist; + + return this.createDistDir(func.name) + .then(function(distDir) { pathDist = distDir }) + .then(() => this.copyFunction(func, pathDist, stage, region)) + .then(() => this._addEnvVarsInline(func, pathDist, stage, region)) + .then(() => this.generatePaths(func, pathDist)); + } + + /** + * Get Handler + */ + getHandler(func) { return path.join(path.dirname(func.handler), "_serverless_handler.handler").replace(/\\/g, '/'); } - _afterCopyDir(func, pathDist, stage, region) { + /** + * Add ENV Vars In-line + * - Adds a new handler that loads in ENV vars before running the main handler + */ + + _addEnvVarsInline(func, pathDist, stage, region) { return this.getEnvVars(func, stage, region) .then(envVars => { - const handlerArr = func.handler.split('.'), - handlerDir = path.dirname(func.handler), - handlerFile = handlerArr[0].split('/').pop(), - handlerMethod = handlerArr[1]; + const handlerArr = func.handler.split('.'), + handlerDir = path.dirname(func.handler), + handlerFile = handlerArr[0].split('/').pop(), + handlerMethod = handlerArr[1]; let loader = ['import os']; loader = loader.concat(_.map(envVars, (value, key) => `os.environ['${key}'] = str('${value}')`)); @@ -117,6 +144,20 @@ class ServerlessRuntimePython27 extends RuntimeBase { }); } + /** + * Install NPM Dependencies + */ + + installDependencies(dir ) { + SCli.log("Installing default python dependencies with pip..."); + SCli.log(`-----------------`); + pipPrefixInstall( + this.S.getProject().getRootPath( dir, 'requirements.txt'), + this.S.getProject().getRootPath( dir, 'vendored') + ); + SCli.log(`-----------------`); + } + } module.exports = ServerlessRuntimePython27; diff --git a/lib/SerializerFileSystem.js b/lib/SerializerFileSystem.js index 0ca982afa..2ac66fc9e 100644 --- a/lib/SerializerFileSystem.js +++ b/lib/SerializerFileSystem.js @@ -10,11 +10,10 @@ const SError = require('./Error'), let SUtils; -class SerializerFileSystem { +class Serializer { constructor(S) { this._S = S; - this._projectPath = S.config.projectPath; this._class = this.constructor.name; SUtils = S.utils; } @@ -476,7 +475,7 @@ class SerializerFileSystem { } } -module.exports = SerializerFileSystem; +module.exports = Serializer; /** * Globber diff --git a/lib/Stage.js b/lib/Stage.js index b98fb0b6b..99eca561e 100644 --- a/lib/Stage.js +++ b/lib/Stage.js @@ -1,13 +1,13 @@ 'use strict'; const SError = require('./Error'), - SerializerFileSystem = require('./SerializerFileSystem'), + Serializer = require('./Serializer'), BbPromise = require('bluebird'), _ = require('lodash'); let SUtils; -class Stage extends SerializerFileSystem { +class Stage extends Serializer { constructor(S, project, data) { diff --git a/lib/Templates.js b/lib/Templates.js index a61b84530..20d3e8e6c 100644 --- a/lib/Templates.js +++ b/lib/Templates.js @@ -1,7 +1,7 @@ 'use strict'; const SError = require('./Error'), - SerializerFileSystem = require('./SerializerFileSystem'), + Serializer = require('./Serializer'), fs = require('fs'), path = require('path'), _ = require('lodash'), @@ -9,7 +9,7 @@ const SError = require('./Error'), let SUtils; -class Templates extends SerializerFileSystem { +class Templates extends Serializer { constructor(S, data, filePath) { super(S); diff --git a/lib/Variables.js b/lib/Variables.js index fc97a6436..bcbab5c18 100644 --- a/lib/Variables.js +++ b/lib/Variables.js @@ -1,14 +1,14 @@ 'use strict'; const SError = require('./Error'), - SerializerFileSystem = require('./SerializerFileSystem'), + Serializer = require('./Serializer'), fs = require('fs'), _ = require('lodash'), BbPromise = require('bluebird'); let SUtils; -class Variables extends SerializerFileSystem { +class Variables extends Serializer { constructor(S, data, filePath) { super(S);