svn_update.lua 2.1 KB

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