From 99e96ce5d2ef17ad71c99ebc2eedff780e1ea3c5 Mon Sep 17 00:00:00 2001 From: Ivan Toth Date: Thu, 15 Sep 2016 12:47:24 +0200 Subject: [PATCH 1/3] passing environment to post install script --- lib/main.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/main.js b/lib/main.js index 288e5bcd..15fd094f 100644 --- a/lib/main.js +++ b/lib/main.js @@ -184,18 +184,18 @@ Lambda.prototype._npmInstall = function (program, codeDirectory, callback) { }); }; -Lambda.prototype._postInstallScript = function (codeDirectory, callback) { +Lambda.prototype._postInstallScript = function (program, codeDirectory, callback) { var script_filename = 'post_install.sh'; - var cmd = './' + script_filename; + var cmd = './' + script_filename + ' ' + program.environment; var filePath = [codeDirectory, script_filename].join('/'); fs.exists(filePath, function (exists) { if (exists) { - console.log('=> Running post install script '+script_filename); - exec(cmd, { cwd: codeDirectory,maxBuffer: 50 * 1024 * 1024 }, function (error, stdout, stderr) { + console.log('=> Running post install script ' + script_filename); + exec(cmd, { cwd: codeDirectory, maxBuffer: 50 * 1024 * 1024 }, function (error, stdout, stderr) { if (error) { - callback(error + " stdout: " + stdout + "stderr" + stderr); + callback(error + " stdout: " + stdout + " stderr: " + stderr); } else { console.log("\t\t" + stdout); callback(null); @@ -403,7 +403,7 @@ Lambda.prototype._buildAndArchive = function (program, archive_callback) { return archive_callback(err); } - _this._postInstallScript(codeDirectory, function (err) { + _this._postInstallScript(program, codeDirectory, function (err) { if (err) { return archive_callback(err); } From e34c31754ff274463d42a5d9ec8942f38c8a7d1c Mon Sep 17 00:00:00 2001 From: Ivan Toth Date: Thu, 15 Sep 2016 14:12:41 +0200 Subject: [PATCH 2/3] Readme update --- README.md | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 72784607..21c78dba 100644 --- a/README.md +++ b/README.md @@ -143,7 +143,19 @@ a callback function. These will still work for now for backward compatibility, v0.10.36 is still supported, and can be targeted by changing the `AWS_RUNTIME` value to `nodejs` in the `.env` file. ## Post install script -When running `node-lambda deploy` if you need to do some action after `npm install --production` and before deploying to AWS Lambda (i.e. replace some modules with precompiled ones or download some libraries) you can create `post_install.sh` script. If the file exists the script will be executed (and output shown after execution) if not it is skipped. Make sure that the script is executable. +When running `node-lambda deploy` if you need to do some action after `npm install --production` and before deploying to AWS Lambda (e.g. replace some modules with precompiled ones or download some libraries, replace some config file depending on environment) you can create `post_install.sh` script. If the file exists the script will be executed (and output shown after execution) if not it is skipped. Environment string is passed to script as first parameter so you can use it if needed. Make sure that the script is executable. + +Example `post_install.sh`: +``` +printf "\n\n###### Post install script ###### \n" +ENV="production"; +if [ ! -z $1 ] + then + ENV=$1; +fi +cp -v "config_$ENV.js" "config.js" \ +&& printf "###### DONE! ###### \n\n" +``` ## Prebuilt packages The `--prebuiltDirectory` flag is useful for working with Webpack for example. It skips `npm install --production` and `post_install.sh` and simply packages the specified directory. From 9a65463b69f8dac6a494e85d6a24049b57ea437b Mon Sep 17 00:00:00 2001 From: Ivan Toth Date: Tue, 20 Sep 2016 15:29:30 +0200 Subject: [PATCH 3/3] post install script tests --- test/main.js | 49 ++++++++++++++++++++++++++++++++++++++++++++ test/post_install.sh | 2 ++ 2 files changed, 51 insertions(+) create mode 100755 test/post_install.sh diff --git a/test/main.js b/test/main.js index 1e471097..396cf3fa 100644 --- a/test/main.js +++ b/test/main.js @@ -196,6 +196,55 @@ describe('node-lambda', function () { }); }); + describe('_postInstallScript', function () { + var hook; + /** + * Capture console output + */ + function captureStream(stream){ + var oldWrite = stream.write; + var buf = ''; + stream.write = function(chunk, encoding, callback){ + buf += chunk.toString(); // chunk is a String or Buffer + oldWrite.apply(stream, arguments); + } + + return { + unhook: function unhook(){ + stream.write = oldWrite; + }, + captured: function(){ + return buf; + } + }; + } + beforeEach(function(){ + hook = captureStream(process.stdout); + }); + afterEach(function(){ + hook.unhook(); + }); + + + it('should not throw any errors if no script', function (done) { + lambda._postInstallScript(program, codeDirectory, function (err) { + assert.equal(err, null); + done(); + }); + }); + + it('running script gives expected output', function (done) { + fs.writeFileSync(codeDirectory + '/post_install.sh', fs.readFileSync('test/post_install.sh')); + fs.chmodSync(codeDirectory + '/post_install.sh', '755'); + lambda._postInstallScript(program, codeDirectory, function (err) { + assert.equal(err, null); + assert.equal("=> Running post install script post_install.sh\n\t\tYour environment is "+program.environment+"\n", hook.captured()); + fs.unlinkSync(codeDirectory + '/post_install.sh'); + done(); + }); + }); + }); + describe('_zip', function () { beforeEach(function (done) { this.timeout(30000); // give it time to build the node modules diff --git a/test/post_install.sh b/test/post_install.sh new file mode 100755 index 00000000..3005785b --- /dev/null +++ b/test/post_install.sh @@ -0,0 +1,2 @@ +#!/bin/bash +printf "Your environment is $1"