openresty 使用lua-resty-shell 執行shell 腳本


lua-resty-shell 是一個很不錯的項目,讓我們可以無阻塞的執行shell命令,之間的通信
是通過socket (一般是unix socket)

環境准備

  • docker-compose 文件
version: "3"
services:
  app:
   build: ./
   ports:
   - "8080:80"
   volumes:
   - "./app/:/opt/app/"
   - "./nginx.conf:/usr/local/openresty/nginx/conf/nginx.conf"
  • dockerfile
FROM openresty/openresty:alpine-fat
LABEL author="1141591465@qq.com"
WORKDIR /sockproc
COPY ./sockproc/ /sockproc/
RUN make sockproc
COPY entrypoint.sh /entrypoint.sh
COPY sockproc.sh /sockproc.sh
COPY shell.lua /usr/local/openresty/lualib/resty/shell.lua
ENTRYPOINT [ "/entrypoint.sh" ]
  • dockerfile 說明
    dockerfile 同時進行了socket 服務的構建以及shell lua 封裝的copy,entrypoint 進行服務的啟動
entrypoint.sh:
#!/bin/sh
sh /sockproc.sh
exec /usr/local/openresty/bin/openresty -g "daemon off;"
sockproc.sh:
#!/bin/sh
/sockproc/sockproc /tmp/shell.sock

openresty 集成測試

  • lua 調用代碼
app/app.lua

local shell = require("resty.shell")
local args = {
   socket = "unix:/tmp/shell.sock",
}
function call()
    local status, out, err = shell.execute("cat /proc/sys/kernel/random/uuid", args)
    ngx.say(out)
end
return call
  • openresty content_by_Lua 階段調用lua shell 封裝
+ user root;
http { + lua_package_path '/opt/app/?.lua;;'; + location /test { + content_by_lua_block { + require("app")() + }
  • 測試效果

參考資料

https://github.com/juce/lua-resty-shell
https://github.com/rongfengliang/lua-resty-shell-docker-running


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM