Skip to content
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

Post install script environment #154

Merged
merged 3 commits into from
Sep 20, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
12 changes: 6 additions & 6 deletions lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}
Expand Down
49 changes: 49 additions & 0 deletions test/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions test/post_install.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/bash
printf "Your environment is $1"