-
Notifications
You must be signed in to change notification settings - Fork 185
Partial refactor, multi region, version tag #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
c8e0107
Fixed variable declaration style
d3c2aaf
Refactor setup process
7c3b9d2
Fix style
4e4bf43
Fix style
ba6bd67
Allow version tag, multi region
c9e2feb
add asyncjs
d5351e7
Fix part of style, allow version tag
984490d
Version bump
d937f4d
Update test version
c2a1bd0
Fix quote style
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,64 +1,62 @@ | ||
| #!/usr/bin/env node | ||
|
|
||
| var | ||
| dotenv = require( "dotenv" ), | ||
| lambda = require( "../lib/main.js" ), | ||
| program = require( "commander" ); | ||
| packageJson = require( process.cwd() + "/package.json" ); | ||
|
|
||
| dotenv.load( ); | ||
|
|
||
| var | ||
| AWS_ENVIRONMENT = process.env.AWS_ENVIRONMENT || "development", | ||
| AWS_ACCESS_KEY_ID = process.env.AWS_ACCESS_KEY_ID || "missing", | ||
| AWS_SECRET_ACCESS_KEY = process.env.AWS_SECRET_ACCESS_KEY || "missing", | ||
| AWS_REGION = process.env.AWS_REGION || "us-east-1", | ||
| AWS_FUNCTION_NAME = process.env.AWS_FUNCTION_NAME || packageJson.name, | ||
| AWS_HANDLER = process.env.AWS_HANDLER || "index.handler", | ||
| AWS_MODE = "event", | ||
| AWS_ROLE = process.env.AWS_ROLE || "missing", | ||
| AWS_MEMORY_SIZE = process.env.AWS_MEMORY_SIZE || 128, | ||
| AWS_TIMEOUT = process.env.AWS_TIMEOUT || 3, | ||
| AWS_DESCRIPTION = process.env.AWS_DESCRIPTION || "", | ||
| AWS_RUNTIME = process.env.AWS_RUNTIME || "nodejs"; | ||
| var dotenv = require('dotenv'); | ||
| var lambda = require('../lib/main.js'); | ||
| var program = require('commander'); | ||
| var packageJson = require(process.cwd() + '/package.json'); | ||
|
|
||
| dotenv.load(); | ||
|
|
||
| var AWS_ENVIRONMENT = process.env.AWS_ENVIRONMENT || 'development'; | ||
| var AWS_ACCESS_KEY_ID = process.env.AWS_ACCESS_KEY_ID || 'missing'; | ||
| var AWS_SECRET_ACCESS_KEY = process.env.AWS_SECRET_ACCESS_KEY || 'missing'; | ||
| var AWS_REGION = process.env.AWS_REGION || 'us-east-1'; | ||
| var AWS_FUNCTION_NAME = process.env.AWS_FUNCTION_NAME || packageJson.name; | ||
| var AWS_HANDLER = process.env.AWS_HANDLER || 'index.handler'; | ||
| var AWS_MODE = 'event'; | ||
| var AWS_ROLE = process.env.AWS_ROLE || 'missing'; | ||
| var AWS_MEMORY_SIZE = process.env.AWS_MEMORY_SIZE || 128; | ||
| var AWS_TIMEOUT = process.env.AWS_TIMEOUT || 3; | ||
| var AWS_DESCRIPTION = process.env.AWS_DESCRIPTION || ''; | ||
| var AWS_RUNTIME = process.env.AWS_RUNTIME || 'nodejs'; | ||
| var AWS_FUNCTION_VERSION = process.env.AWS_FUNCTION_VERSION || packageJson.version; | ||
|
|
||
| program | ||
| .version( lambda.version ) | ||
| .command( "deploy" ) | ||
| .description( "Deploy your application to Amazon Lambda" ) | ||
| .option( "-e, --environment [" + AWS_ENVIRONMENT + "]", "Choose environment {development, stating, production}", AWS_ENVIRONMENT ) | ||
| .option( "-a, --accessKey [" + AWS_ACCESS_KEY_ID + "]", "AWS Access Key", AWS_ACCESS_KEY_ID ) | ||
| .option( "-s, --secretKey [" + AWS_SECRET_ACCESS_KEY + "]", "AWS Secret Key", AWS_SECRET_ACCESS_KEY ) | ||
| .option( "-r, --region [" + AWS_REGION + "]", "AWS Region", AWS_REGION ) | ||
| .option( "-n, --functionName [" + AWS_FUNCTION_NAME + "]", "Lambda FunctionName", AWS_FUNCTION_NAME ) | ||
| .option( "-h, --handler [" + AWS_HANDLER + "]", "Lambda Handler {index.handler}", AWS_HANDLER ) | ||
| .option( "-m, --mode [" + AWS_MODE + "]", "Lambda Mode", AWS_MODE ) | ||
| .option( "-o, --role [" + AWS_ROLE + "]", "Amazon role", AWS_ROLE ) | ||
| .option( "-m, --memorySize [" + AWS_MEMORY_SIZE + "]", "Lambda Memory Size", AWS_MEMORY_SIZE ) | ||
| .option( "-t, --timeout [" + AWS_TIMEOUT + "]", "Lambda Timeout", AWS_TIMEOUT ) | ||
| .option( "-d, --description [" + AWS_DESCRIPTION + "]", "Lambda Description", AWS_DESCRIPTION ) | ||
| .option( "-u, --runtime [" + AWS_RUNTIME + "]", "Lambda Runtime", AWS_RUNTIME ) | ||
| .command( 'deploy' ) | ||
| .description( 'Deploy your application to Amazon Lambda' ) | ||
| .option( '-e, --environment [' + AWS_ENVIRONMENT + ']', 'Choose environment {development, stating, production}', AWS_ENVIRONMENT ) | ||
| .option( '-a, --accessKey [' + AWS_ACCESS_KEY_ID + ']', 'AWS Access Key', AWS_ACCESS_KEY_ID ) | ||
| .option( '-s, --secretKey [' + AWS_SECRET_ACCESS_KEY + ']', 'AWS Secret Key', AWS_SECRET_ACCESS_KEY ) | ||
| .option( '-r, --region [' + AWS_REGION + ']', 'AWS Region', AWS_REGION ) | ||
| .option( '-n, --functionName [' + AWS_FUNCTION_NAME + ']', 'Lambda FunctionName', AWS_FUNCTION_NAME ) | ||
| .option( '-h, --handler [' + AWS_HANDLER + ']', 'Lambda Handler {index.handler}', AWS_HANDLER ) | ||
| .option( '-m, --mode [' + AWS_MODE + ']', 'Lambda Mode', AWS_MODE ) | ||
| .option( '-o, --role [' + AWS_ROLE + ']', 'Amazon role', AWS_ROLE ) | ||
| .option( '-m, --memorySize [' + AWS_MEMORY_SIZE + ']', 'Lambda Memory Size', AWS_MEMORY_SIZE ) | ||
| .option( '-t, --timeout [' + AWS_TIMEOUT + ']', 'Lambda Timeout', AWS_TIMEOUT ) | ||
| .option( '-d, --description [' + AWS_DESCRIPTION + ']', 'Lambda Description', AWS_DESCRIPTION ) | ||
| .option( '-u, --runtime [' + AWS_RUNTIME + ']', 'Lambda Runtime', AWS_RUNTIME ) | ||
| .option( '-v, --version [' + AWS_FUNCTION_VERSION + ']', 'Lambda Function Version', AWS_FUNCTION_VERSION ) | ||
| .action( function( prg ) { | ||
| lambda.deploy( prg ); | ||
| } ); | ||
|
|
||
| program | ||
| .version( lambda.version ) | ||
| .command( "run" ) | ||
| .description( "Run your Amazon Lambda application locally" ) | ||
| .option( "-h, --handler [" + AWS_HANDLER + "]", "Lambda Handler {index.handler}", AWS_HANDLER ) | ||
| .command( 'run' ) | ||
| .description( 'Run your Amazon Lambda application locally' ) | ||
| .option( '-h, --handler [' + AWS_HANDLER + ']', 'Lambda Handler {index.handler}', AWS_HANDLER ) | ||
| .action( function( prg ) { | ||
| lambda.run( prg ); | ||
| } ); | ||
|
|
||
| program | ||
| .version( lambda.version ) | ||
| .command( "setup" ) | ||
| .description( "Sets up the .env file." ) | ||
| .command( 'setup' ) | ||
| .description( 'Sets up the .env file.' ) | ||
| .action( function( prg ) { | ||
| lambda.setup( ); | ||
| } ) | ||
|
|
||
|
|
||
|
|
||
| program.parse( process.argv ); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,88 +1,72 @@ | ||
| "use strict"; | ||
|
|
||
| var | ||
| aws = require( "aws-sdk" ), | ||
| exec = require( "child_process" ).exec, | ||
| fs = require( "fs" ), | ||
| os = require( "os" ), | ||
| packageJson = require( "./../package.json" ), | ||
| path = require( "path" ); | ||
|
|
||
| var Lambda = function( ) { | ||
| var aws = require('aws-sdk'); | ||
| var exec = require('child_process').exec; | ||
| var fs = require('fs'); | ||
| var os = require('os'); | ||
| var packageJson = require('./../package.json'); | ||
| var path = require('path'); | ||
| var async = require('async'); | ||
|
|
||
| var Lambda = function() { | ||
| this.version = packageJson.version; | ||
|
|
||
| return this; | ||
| }; | ||
|
|
||
| Lambda.prototype._createEnvFile = function( ) { | ||
| var | ||
| envfile = process.cwd() + "/.env", | ||
| envexamplefile = __dirname + "/.env.example"; | ||
|
|
||
| if ( !fs.existsSync( envfile ) ) { | ||
| console.log( ".env file successfully created" ) | ||
| fs.writeFileSync( envfile, fs.readFileSync( envexamplefile) ); | ||
| Lambda.prototype._createSampleFile = function(file) { | ||
| var exampleFile = process.cwd() + '/' + file; | ||
| var boilerplateFile = __dirname + '/' + file + '.example'; | ||
|
|
||
| if (!fs.existsSync(exampleFile)) { | ||
| fs.writeFileSync(exampleFile, fs.readFileSync(boilerplateFile)); | ||
| console.log(exampleFile + ' file successfully created'); | ||
| } | ||
|
|
||
| }; | ||
|
|
||
| Lambda.prototype._createEventJsonFile = function( ) { | ||
| var | ||
| eventfile = process.cwd() + "/event.json", | ||
| eventexamplefile = __dirname + "/event.json.example"; | ||
|
|
||
| if ( !fs.existsSync( eventfile ) ) { | ||
| console.log( "event.json file successfully created" ) | ||
| fs.writeFileSync( eventfile, fs.readFileSync( eventexamplefile ) ); | ||
| } | ||
|
|
||
| Lambda.prototype.setup = function() { | ||
| console.log('Running setup.'); | ||
| this._createSampleFile('.env'); | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nice refactor |
||
| this._createSampleFile('event.json'); | ||
| console.log('Setup done. Edit the .env and event.json files as needed.'); | ||
| }; | ||
|
|
||
| Lambda.prototype.setup = function( ) { | ||
| console.log( "Running setup." ); | ||
| this._createEnvFile( ); | ||
| this._createEventJsonFile( ); | ||
| console.log( "Setup done. Edit the .env and event.json files as needed." ); | ||
| }; | ||
| Lambda.prototype.run = function(program) { | ||
| this._createSampleFile('event.json'); | ||
|
|
||
| Lambda.prototype.run = function( program ) { | ||
| this._createEventJsonFile( ); | ||
| var dir = program.directory; | ||
| var splitHandler = program.handler.split('.'); | ||
| var filename = splitHandler[0] + '.js'; | ||
| var handlername = splitHandler[1]; | ||
|
|
||
| var | ||
| dir = program.directory, | ||
| splitHandler = program.handler.split( "." ), | ||
| filename = splitHandler[0] + ".js", | ||
| handlername = splitHandler[1]; | ||
| var handler = require(process.cwd() + '/' + filename)[handlername]; | ||
| var event = require(process.cwd() + '/event.json'); | ||
|
|
||
| var | ||
| handler = require( process.cwd() + "/" + filename )[handlername], | ||
| event = require( process.cwd() + "/event.json" ); | ||
|
|
||
| this._runHandler( handler, event ); | ||
| } | ||
| this._runHandler(handler, event); | ||
| }; | ||
|
|
||
| Lambda.prototype._runHandler = function( handler, event ) { | ||
| Lambda.prototype._runHandler = function(handler, event) { | ||
| var context = { | ||
| done: function( ) { | ||
| done: function() { | ||
| process.exit(0); | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| handler( event, context ); | ||
| handler(event, context); | ||
| }; | ||
|
|
||
| Lambda.prototype._zipfileTmpPath = function( program ) { | ||
| var | ||
| ms_since_epoch = +new Date, | ||
| filename = program.functionName + "-" + ms_since_epoch + ".zip", | ||
| zipfile = path.join( os.tmpDir(), filename ); | ||
| Lambda.prototype._zipfileTmpPath = function(program) { | ||
| var ms_since_epoch = +new Date(); | ||
| var filename = program.functionName + '-' + ms_since_epoch + '.zip'; | ||
| var zipfile = path.join(os.tmpDir(), filename); | ||
|
|
||
| return zipfile; | ||
| }; | ||
|
|
||
| Lambda.prototype._params = function( program, buffer ) { | ||
| Lambda.prototype._params = function(program, buffer) { | ||
| var params = { | ||
| FunctionName: program.functionName + "-" + program.environment, | ||
| FunctionName: program.functionName + '-' + program.environment + (program.version ? '/' + program.version: ''), | ||
| FunctionZip: buffer, | ||
| Handler: program.handler, | ||
| Mode: program.mode, | ||
|
|
@@ -96,46 +80,52 @@ Lambda.prototype._params = function( program, buffer ) { | |
| return params; | ||
| }; | ||
|
|
||
| Lambda.prototype.deploy = function( program ) { | ||
| this._createEnvFile( ); | ||
|
|
||
| aws.config.update({ | ||
| accessKeyId: program.accessKey, | ||
| secretAccessKey: program.secretKey, | ||
| region: program.region | ||
| }); | ||
|
|
||
| var | ||
| _this = this, | ||
| lambda = new aws.Lambda( { apiVersion: "2014-11-11" } ), | ||
| zipfile = _this._zipfileTmpPath( program ); | ||
| Lambda.prototype.deploy = function(program) { | ||
| this._createSampleFile('.env'); | ||
|
|
||
| var _this = this; | ||
| var zipfile = _this._zipfileTmpPath(program); | ||
| var regions = program.regions.split(','); | ||
|
|
||
| // zip up but ignore .git, .dotfiles, .log files, and the event.json file. | ||
| console.log( "Generating zip file" ); | ||
| exec( "zip -roq " + zipfile + " * -x '.git' -x '*/\.*' -x '*/\*.log' -x 'event.json'", function( err, stdout, stderr ) { | ||
| if ( err ) { | ||
| console.log('Generating zip file'); | ||
| exec( "zip -roq " + zipfile + " * -x '.git' -x '*/\.*' -x '*/\*.log' -x 'event.json'", function(err, stdout, stderr) { | ||
| if (err) { | ||
| throw err; | ||
| } | ||
|
|
||
| console.log( "Reading zip file to memory" ); | ||
| var | ||
| buffer = fs.readFileSync( zipfile ), | ||
| params = _this._params( program, buffer ); | ||
|
|
||
| console.log( "Uploading zip file to AWS Lambda with parameters:" ); | ||
| console.log( params ); | ||
|
|
||
| lambda.uploadFunction( params, function( err, data ) { | ||
| if ( err ) { | ||
| console.log( err ); | ||
| } else { | ||
| console.log( "Zip file done uploading. Results follow:" ); | ||
| console.log( data ); | ||
| console.log('Reading zip file to memory'); | ||
| var buffer = fs.readFileSync(zipfile); | ||
| var params = _this._params(program, buffer); | ||
|
|
||
| async.each(regions, function(region, cb) { | ||
| console.log('Working in region: ' + region); | ||
| console.log('Uploading zip file to AWS Lambda with parameters:'); | ||
| console.log(params); | ||
|
|
||
| aws.config.update({ | ||
| accessKeyId: program.accessKey, | ||
| secretAccessKey: program.secretKey, | ||
| region: region | ||
| }); | ||
|
|
||
| var lambda = new aws.Lambda({ apiVersion: '2014-11-11' }); | ||
|
|
||
| lambda.uploadFunction(params, function(err, data) { | ||
| if (err) { | ||
| cb(err); | ||
| } | ||
| console.log('Zip file done uploading. Results follow:'); | ||
| console.log(data); | ||
| cb(); | ||
| }); | ||
|
|
||
| }, function(err) { | ||
| if (err) { | ||
| console.log(err); | ||
| } | ||
| } ); | ||
|
|
||
| } ); | ||
| } | ||
|
|
||
| module.exports = new Lambda( ); | ||
| }); | ||
| }); | ||
| }; | ||
|
|
||
| module.exports = new Lambda(); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nice. airbnb style.