lambduh-execute
Advanced tools
Comparing version 1.1.0 to 1.2.0
84
index.js
@@ -5,51 +5,49 @@ var Q = require('q'); | ||
module.exports = function(script) { | ||
return function(options) { | ||
var def = Q.defer(); | ||
module.exports = function(result, script) { | ||
var def = Q.defer(); | ||
if (!script) { | ||
//hand options through if no script was handed in | ||
def.resolve(options); | ||
} else if (script.shell) { | ||
proc.exec(script.shell, function(error, stdout, stderr) { | ||
if (error) { | ||
console.log("exec error: " + error); | ||
def.reject(options); | ||
} | ||
if (!script) { | ||
def.resolve(result); | ||
} else if (script.shell) { | ||
proc.exec(script.shell, function(error, stdout, stderr) { | ||
if (error) { | ||
console.log("exec error: " + error); | ||
//TODO: should this be rejecting the result? | ||
def.reject(result); | ||
} | ||
if (script.logOutput) { | ||
console.log("exec stdout: " + stdout); | ||
console.log("exec stderr: " + stderr); | ||
} | ||
if (script.logOutput) { | ||
console.log("exec stdout: " + stdout); | ||
console.log("exec stderr: " + stderr); | ||
} | ||
def.resolve(options); | ||
}); | ||
} else if (script.bashScript) { | ||
def.resolve(result); | ||
}); | ||
} else if (script.bashScript) { | ||
var child = proc.spawn(script.bashScript, script.bashParams); | ||
child.stdout.on('data', function(data) { | ||
if (script.logOutput) { | ||
console.log("exec stdout: " + data); | ||
} | ||
}); | ||
child.stderr.on('data', function(data) { | ||
if (script.logOutput) { | ||
console.log("exec stderr: " + data); | ||
} | ||
}); | ||
child.on('exit', function(code) { | ||
if (code != 0) { | ||
def.reject(new Error('error running bash script')); | ||
} else { | ||
def.resolve(options) | ||
} | ||
}); | ||
var child = proc.spawn(script.bashScript, script.bashParams); | ||
child.stdout.on('data', function(data) { | ||
if (script.logOutput) { | ||
console.log("exec stdout: " + data); | ||
} | ||
}); | ||
child.stderr.on('data', function(data) { | ||
if (script.logOutput) { | ||
console.log("exec stderr: " + data); | ||
} | ||
}); | ||
child.on('exit', function(code) { | ||
if (code != 0) { | ||
def.reject(new Error('error running bash script')); | ||
} else { | ||
def.resolve(result) | ||
} | ||
}); | ||
} else { | ||
//odd to do this first and last. | ||
def.resolve(options); | ||
} | ||
} else { | ||
//odd to do this first and last. | ||
def.resolve(result); | ||
} | ||
return def.promise; | ||
} | ||
return def.promise; | ||
} |
{ | ||
"name": "lambduh-execute", | ||
"version": "1.1.0", | ||
"version": "1.2.0", | ||
"description": "Execute any shell string or bash script from AWS Lambda", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
# lambduh-execute | ||
Execute any shell string or bash script from AWS Lambda | ||
# Install | ||
``` | ||
npm i --save lambduh-execute | ||
``` | ||
# Usage | ||
@@ -12,53 +18,50 @@ | ||
exports.handler = function(event, context) { | ||
var promises = []; | ||
promises.push(execute({ | ||
shell: "echo `ls /tmp/`", | ||
var result = {} | ||
execute(result, { | ||
shell: "echo `ls /tmp/`", // logs output of /tmp/ dir on your lambda machine | ||
logOutput: true | ||
}) // logs output of /tmp/ dir on your lambda machine | ||
promises.push(execute({ | ||
shell: "cp /var/task/ffmpeg /tmp/.; chmod 755 /tmp/ffmpeg", | ||
logOutput: true | ||
}) // copies an ffmpeg binary to /tmp/ and chmods permissions to run it | ||
//if you need data on the options object, wrap and return the promise | ||
promises.push(function(options) { | ||
return execute({ | ||
shell: "rm " + options.mp4Path | ||
})(options); | ||
}) // pulls in path from options object to fire dynamic script | ||
//you can also run a bashScript of your choice | ||
promises.push(function(options) { | ||
return execute({ | ||
bashScript: "/tmp/path/to/bash" | ||
})(options); | ||
}) | ||
//and hand in any parameters you'd like | ||
promises.push(function(options) { | ||
return execute({ | ||
bashScript: "/tmp/path/to/bash", | ||
bashParams: ["filey-namey"] | ||
})(options); | ||
.then(function(result) { | ||
return execute(result, { | ||
shell: "cp /var/task/ffmpeg /tmp/.; chmod 755 /tmp/ffmpeg", // copies an ffmpeg binary to /tmp/ and chmods permissions to run it | ||
logOutput: true | ||
}) | ||
}) | ||
promises.push(function(options) { | ||
.then(function(result) { | ||
return execute(result, { | ||
shell: "rm " + result.mp4Path // pulls in path from options object to fire dynamic script | ||
}); | ||
}) | ||
.then(function(result) { | ||
return execute(result, { | ||
bashScript: "/tmp/path/to/bash/script" //you can also run a bash script | ||
}); | ||
}) | ||
.then(function(result) { | ||
return execute(result, { | ||
bashScript: "/tmp/path/to/bash/script", | ||
bashParams: ["filey-namey"] //and hand in any parameters you'd like | ||
}); | ||
}) | ||
.then(function(result) { | ||
context.done() | ||
}) | ||
promises.reduce(Q.when, Q()) | ||
.fail(function(err) { | ||
console.log("derp"); | ||
console.log(err); | ||
context.done(null, err); | ||
}); | ||
.fail(function(err) { | ||
console.log("derp"); | ||
console.log(err); | ||
context.done(null, err); | ||
}); | ||
} | ||
``` | ||
This module takes a `script` object that can have two fields: a `shell` field for writing a string of unix commands to be executed, and a `logOutput` boolean for showing the stdout and stderr logs. | ||
This module takes a `script` object that can have a few options: | ||
- a `shell` field for writing a string of unix commands to be executed | ||
- a `baseScript` field with a path to the bash script to be executed | ||
- a `baseParams` array with params to be passed to the script | ||
- a `logOutput` boolean for showing the stdout and stderr logs | ||
`logOutput` defaults to false. | ||
The tests in this repo could use enforcement around `logOutput` - I'm onto bigger fish for now, will hopefully get back to it. |
@@ -12,20 +12,16 @@ var chai = require('chai'); | ||
it('should return a function', function() { | ||
expect(execute()).to.be.a('function'); | ||
it('should return a promise', function() { | ||
expect(execute().then).to.exist; | ||
}); | ||
it('should return a function that returns a promise', function() { | ||
expect(execute()().then).to.exist; | ||
}); | ||
it('should return an options object handed through it', function(done) { | ||
var options = { | ||
it('should return an result object handed through it', function(done) { | ||
var result = { | ||
key: 'val' | ||
} | ||
execute()(options).then(function(opts) { | ||
if (opts) { | ||
expect(opts).to.equal(options); | ||
execute(result).then(function(res) { | ||
if (res) { | ||
expect(res).to.equal(result); | ||
done(); | ||
} else { | ||
done(new Error('Expected options to be resolved')); | ||
done(new Error('Expected result to be resolved')); | ||
} | ||
@@ -40,5 +36,5 @@ }, function() { | ||
//Test by creating file and asserting that it exists | ||
execute({ | ||
execute(null, { | ||
shell: 'echo "new file content" >> ./test/file.txt' | ||
})().then(function(){ | ||
}).then(function(){ | ||
expect("./test/file.txt").to.be.a.file("file.txt not found") | ||
@@ -51,5 +47,5 @@ done() | ||
//Remove file and asserting that it does not exist | ||
execute({ | ||
execute(null, { | ||
shell: 'rm ./test/file.txt' | ||
})().then(function(){ | ||
}).then(function(){ | ||
expect("./test/file.txt").not.to.be.a.file("file.txt not found") | ||
@@ -66,5 +62,5 @@ done() | ||
//Test by creating file and asserting that it exists | ||
execute({ | ||
execute(null, { | ||
bashScript: './test/test-script' | ||
})().then(function(){ | ||
}).then(function(){ | ||
expect("./test/file.txt").to.be.a.file("file.txt not found") | ||
@@ -77,5 +73,5 @@ done() | ||
//Remove file and asserting that it does not exist | ||
execute({ | ||
execute(null, { | ||
shell: 'rm ./test/file.txt' | ||
})().then(function(){ | ||
}).then(function(){ | ||
expect("./test/file.txt").not.to.be.a.file("file.txt not found") | ||
@@ -90,6 +86,6 @@ done() | ||
//Test by creating file and asserting that it exists | ||
execute({ | ||
execute(null, { | ||
bashScript: './test/test-script-params', | ||
bashParams: ['./test/file.txt'] | ||
})().then(function(){ | ||
}).then(function(){ | ||
expect("./test/file.txt").to.be.a.file("file.txt not found") | ||
@@ -102,5 +98,5 @@ done() | ||
//Remove file and asserting that it does not exist | ||
execute({ | ||
execute(null, { | ||
shell: 'rm ./test/file.txt' | ||
})().then(function(){ | ||
}).then(function(){ | ||
expect("./test/file.txt").not.to.be.a.file("file.txt not found") | ||
@@ -118,5 +114,5 @@ done() | ||
it('should default logging to false', function() { | ||
execute({ | ||
execute(null, { | ||
shell: 'echo "i should not log"' | ||
})().then(function(){ | ||
}).then(function(){ | ||
done() | ||
@@ -130,6 +126,6 @@ }, function() { | ||
it('should allow toggling logging', function() { | ||
execute({ | ||
execute(null, { | ||
logOutput: true, | ||
shell: 'echo "i should log"' | ||
})().then(function(){ | ||
}).then(function(){ | ||
done() | ||
@@ -136,0 +132,0 @@ }, function() { |
67
9774
172