|
| 1 | +# lua-resty-exec |
| 2 | + |
| 3 | +A small Lua module for executing processes. It's primarily |
| 4 | +intended to be used with OpenResty, but will work in regular Lua applications |
| 5 | +as well. When used with OpenResty, it's completely non-blocking (otherwise it |
| 6 | +falls back to using LuaSocket and does block). |
| 7 | + |
| 8 | +It's similar to (and inspired by) |
| 9 | +[lua-resty-shell](https://github.com/juce/lua-resty-shell), the primary |
| 10 | +difference being this module uses sockexec, which doesn't spawn a shell - |
| 11 | +instead you provide an array of argument strings, which means you don't need |
| 12 | +to worry about shell escaping/quoting/parsing rules. |
| 13 | + |
| 14 | +This requires your web server to have an active instance of |
| 15 | +[sockexec](https://github.com/jprjr/sockexec) running. |
| 16 | + |
| 17 | +## Usage |
| 18 | + |
| 19 | +```lua |
| 20 | +local exec = require'resty.exec' |
| 21 | +local prog = exec.new('/tmp/exec.sock') |
| 22 | +``` |
| 23 | + |
| 24 | +Creates a new `prog` object, using `/tmp/exec.sock` for its connection to |
| 25 | +sockexec. |
| 26 | + |
| 27 | +From there, you can use `prog` in a couple of different ways: |
| 28 | + |
| 29 | +### ez-mode |
| 30 | + |
| 31 | +```lua |
| 32 | +local res, err = prog('uname') |
| 33 | + |
| 34 | +-- res = { stdout = "Linux\n", stderr = nil, exitcode = 0, termsig = nil } |
| 35 | +-- err = nil |
| 36 | + |
| 37 | +ngx.print(res.stdout) |
| 38 | +``` |
| 39 | + |
| 40 | +This will run `uname`, with no data on stdin. |
| 41 | + |
| 42 | +Returns a table of output/error codes, with `err` set to any errors |
| 43 | +encountered. |
| 44 | + |
| 45 | +### Setup argv beforehand |
| 46 | + |
| 47 | +```lua |
| 48 | +prog.argv = { 'uname', '-a' } |
| 49 | +local res, err = prog() |
| 50 | + |
| 51 | +-- res = { stdout = "Linux localhost 3.10.18 #1 SMP Tue Aug 2 21:08:34 PDT 2016 x86_64 GNU/Linux\n", stderr = nil, exitcode = 0, termsig = nil } |
| 52 | +-- err = nil |
| 53 | + |
| 54 | +ngx.print(res.stdout) |
| 55 | +``` |
| 56 | + |
| 57 | +### Setup stdin beforehand |
| 58 | + |
| 59 | +```lua |
| 60 | +prog.stdin = 'this is neat!' |
| 61 | +local res, err = prog('cat') |
| 62 | + |
| 63 | +-- res = { stdout = "this is neat!", stderr = nil, exitcode = 0, termsig = nil } |
| 64 | +-- err = nil |
| 65 | + |
| 66 | +ngx.print(res.stdout) |
| 67 | +``` |
| 68 | + |
| 69 | +### Call with explicit argv and stdin |
| 70 | + |
| 71 | +```lua |
| 72 | +local res, err = prog( { argv = 'cat', stdin = 'fun!' } ) |
| 73 | + |
| 74 | +-- res = { stdout = "fun!", stderr = nil, exitcode = 0, termsig = nil } |
| 75 | +-- err = nil |
| 76 | + |
| 77 | +ngx.print(res.stdout) |
| 78 | +``` |
| 79 | + |
| 80 | +Note: here `argv` is a string, which is fine if your program doesn't need any |
| 81 | +arguments. |
| 82 | + |
| 83 | +### Setup stdout/stderr callbacks |
| 84 | + |
| 85 | +If you set `prog.stdout` or `prog.stderr` to a function, it will be called for |
| 86 | +each chunk of stdout/stderr data received. |
| 87 | + |
| 88 | +Please note that there's no guarantees of stdout/stderr being a complete |
| 89 | +string, or anything particularly sensible for that matter! |
| 90 | + |
| 91 | +```lua |
| 92 | +-- set bufsize to some smaller value, the default is 4096 |
| 93 | +-- this allows data to stream in smaller chunks |
| 94 | +prog.bufsize = 10 |
| 95 | + |
| 96 | +prog.stdout = function(data) |
| 97 | + ngx.print(data) |
| 98 | + ngx.flush(true) |
| 99 | +end |
| 100 | + |
| 101 | +local res, err = prog('some-program') |
| 102 | + |
| 103 | +``` |
| 104 | + |
| 105 | +### But I actually want a shell! |
| 106 | + |
| 107 | +Not a problem! You can just do something like: |
| 108 | + |
| 109 | +```lua |
| 110 | +local res, err = prog('bash','-c','echo $PATH') |
| 111 | +``` |
| 112 | + |
| 113 | +Or if you want to run an entire script: |
| 114 | + |
| 115 | +```lua |
| 116 | +prog.stdin = script_data |
| 117 | +local res, err = prog('bash') |
| 118 | +``` |
| 119 | + |
| 120 | + |
| 121 | +## Some example nginx configs |
| 122 | + |
| 123 | +Assuming you're running sockexec at `/tmp/exec.sock` |
| 124 | + |
| 125 | +``` |
| 126 | +$ sockexec /tmp/exec.sock |
| 127 | +``` |
| 128 | + |
| 129 | +Then in your nginx config: |
| 130 | + |
| 131 | +```nginx |
| 132 | +location /uname-1 { |
| 133 | + content_by_lua_block { |
| 134 | + local prog = require'resty.exec'.new('/tmp/exec.sock') |
| 135 | + local data,err = prog('uname') |
| 136 | + if(err) then |
| 137 | + ngx.say(err) |
| 138 | + else |
| 139 | + ngx.say(data.stdout) |
| 140 | + end |
| 141 | + } |
| 142 | +} |
| 143 | +location /uname-2 { |
| 144 | + content_by_lua_block { |
| 145 | + local prog = require'resty.exec'.new('/tmp/exec.sock') |
| 146 | + prog.argv = { 'uname', '-a' } |
| 147 | + local data,err = prog() |
| 148 | + if(err) then |
| 149 | + ngx.say(err) |
| 150 | + else |
| 151 | + ngx.say(data.stdout) |
| 152 | + end |
| 153 | + } |
| 154 | +} |
| 155 | +location /cat-1 { |
| 156 | + content_by_lua_block { |
| 157 | + local prog = require'resty.exec'.new('/tmp/exec.sock') |
| 158 | + prog.stdin = 'this is neat!' |
| 159 | + local data,err = prog('cat') |
| 160 | + if(err) then |
| 161 | + ngx.say(err) |
| 162 | + else |
| 163 | + ngx.say(data.stdout) |
| 164 | + end |
| 165 | + } |
| 166 | +} |
| 167 | +location /cat-2 { |
| 168 | + content_by_lua_block { |
| 169 | + local prog = require'resty.exec'.new('/tmp/exec.sock') |
| 170 | + local data,err = prog({argv = 'cat', stdin = 'awesome'}) |
| 171 | + if(err) then |
| 172 | + ngx.say(err) |
| 173 | + else |
| 174 | + ngx.say(data.stdout) |
| 175 | + end |
| 176 | + } |
| 177 | +} |
| 178 | +location /slow-print { |
| 179 | + content_by_lua_block { |
| 180 | + local prog = require'resty.exec'.new('/tmp/exec.sock') |
| 181 | + prog.bufsize = 10 |
| 182 | + prog.stdout = function(v) |
| 183 | + ngx.print(v) |
| 184 | + ngx.flush(true) |
| 185 | + end |
| 186 | + prog('/usr/local/bin/slow-print') |
| 187 | + } |
| 188 | + # look in `/misc` of this repo for `slow-print` |
| 189 | +} |
| 190 | +location /shell { |
| 191 | + content_by_lua_block { |
| 192 | + local prog = require'resty.exec'.new('/tmp/exec.sock') |
| 193 | + local data, err = prog('bash','-c','echo $PATH') |
| 194 | + if(err) then |
| 195 | + ngx.say(err) |
| 196 | + else |
| 197 | + ngx.say(data.stdout) |
| 198 | + end |
| 199 | + } |
| 200 | +} |
| 201 | +
|
| 202 | +``` |
| 203 | + |
| 204 | +## License |
| 205 | + |
| 206 | +MIT license (see `LICENSE`) |
0 commit comments