svn_update.lua 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. local skynet = require "skynet"
  2. require "skynet.manager"
  3. local logger = require "logger"
  4. local stringify = require "stringify"
  5. local cjson = require "cjson"
  6. local md5 = require "md5"
  7. local authz = {acc = "yytx", pwd = "lee@YY-Games.520"}
  8. -- Generate request data
  9. local content = {acc = authz.acc, pwd = authz.pwd, sign = false}
  10. content.sign = string.sub(md5.sumhexa(content.acc .. content.pwd), 9, 24)
  11. local whitelist = {
  12. ["192.168.1.23"] = true,
  13. ["192.168.1.127"] = true,
  14. ["192.168.1.50"] = true,
  15. ["192.168.1.41"] = true,
  16. ["14.29.136.211"] = true,
  17. ["222.212.88.4"] = true,
  18. }
  19. local function update(path)
  20. local f = io.popen(string.format("sudo svn up %s", path))
  21. local text = f:read("*all")
  22. f:close()
  23. return text
  24. end
  25. local function create_html_page(text)
  26. return string.format(
  27. "<!DOCTYPE html><html><head><meta charset='UTF-8'></head> <body>%s</body> </html>",
  28. string.gsub(text, "[\n]", "<br/>"))
  29. end
  30. local function response(header, content)
  31. header = header or {}
  32. if header['pokemon-version'] then
  33. return { content }
  34. else
  35. return create_html_page(content)
  36. end
  37. end
  38. -- example:
  39. -- http://192.168.1.57:9001/svn_update?user=lee&pwd=lee@123&type=1 -- 配置
  40. -- http://192.168.1.57:9001/svn_update?user=lee&pwd=lee@123&type=2 -- 代码
  41. local function svn_update(args, ipaddr, header)
  42. logger.warn("处理来自主机 %s 的SVN更新请求", ipaddr)
  43. if not whitelist[ipaddr] then
  44. --return { code=403, msg="Forbidden" }
  45. --return cjson.encode({state = 403, msg= "不信任ip"})
  46. end
  47. -- 验证gm账号
  48. local code = string.sub(args.code, 1, 16)
  49. if code ~= content.sign then
  50. return cjson.encode({state = 403, msg = "账号或密码错误"})
  51. end
  52. logger.trace(" === args = %s", stringify(args))
  53. -- 先通过 svn 更新配置文件和代码
  54. local descripe = ""
  55. if tonumber(args.type) == 1 then
  56. descripe = update(skynet.getenv "asset")
  57. end
  58. if tonumber(args.type) == 2 then
  59. descripe = descripe .. update(skynet.getenv "src")
  60. end
  61. logger.warn("svn_update (%s, %s)\n%s", user, ipaddr, descripe)
  62. return cjson.encode({state =0, msg = "SVN更新 成功", content = response(header, descripe)})
  63. -- return response(header, content)
  64. end
  65. return svn_update