hotfix.lua 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. local skynet = require "skynet"
  2. local codecache = require "skynet.codecache"
  3. require "skynet.manager"
  4. local logger = require "logger"
  5. local stringify = require "stringify"
  6. local cjson = require "cjson"
  7. local assetcenter
  8. local watchdog
  9. local authz = {
  10. yytx = "lee@YY-Games.520"
  11. }
  12. local whitelist = {
  13. ["192.168.1.23"] = true,
  14. ["192.168.1.127"] = true,
  15. ["14.29.136.211"] = true,
  16. ["192.168.1.41"] = true,
  17. }
  18. local updateserver
  19. local function newserver()
  20. local newnum = {}
  21. table.insert(newnum,{server=skynet.localname(".quest"),configname={"TaskConfig_proto",}})--z力量
  22. return newnum
  23. end
  24. local function hotfix_true(hotfixlist,configname)
  25. if hotfixlist then
  26. if hotfixlist == "*" then
  27. return 1
  28. end
  29. for k, v in pairs(configname) do
  30. if hotfixlist[v] then
  31. logger.trace("_______________________热更配置:%s,更新服务",v)
  32. return 1
  33. end
  34. end
  35. end
  36. return
  37. end
  38. --对需要更新配置的服务进行更新
  39. local function updateconfig(hotfixlist)
  40. updateserver = updateserver or newserver()
  41. if next(updateserver) then
  42. for k, v in pairs(updateserver) do
  43. if v.configname and hotfix_true(hotfixlist,v.configname) then
  44. skynet.send(v.server, "lua", "hotfix", hotfixlist)
  45. end
  46. end
  47. else
  48. logger.trace("_______________________热更配置无服务需要")
  49. end
  50. end
  51. -- local z_trace = require "zlf_log"
  52. -- local z_trace = z_trace.trace
  53. -- example:
  54. -- http://192.168.1.57:9001/hotfix?user=lee&pwd=lee@123&config=*
  55. -- http://192.168.1.57:9001/hotfix?user=lee&pwd=lee@123&code=*
  56. -- http://192.168.1.57:9001/hotfix?user=lee&pwd=lee@123&code=*&config=*
  57. local function hotfix(args, ipaddr, header)
  58. logger.warn("处理来自主机 %s 的hotfix请求,:%s", ipaddr,args.config)
  59. if not whitelist[ipaddr] then
  60. --return { "403 - Forbidden" }
  61. -- return cjson.encode({errno = 403, host = header.host, info = "不信任ip"})
  62. end
  63. -- 鉴权
  64. -- local user = args.user or ""
  65. -- local pwd = args.pwd or ""
  66. -- if authz[user] ~= pwd then
  67. -- -- return { "403 - Forbidden" }
  68. -- return cjson.encode({errno = 403, host = header.host, info = "账号或密码错误"})
  69. -- end
  70. logger.warn("更新配置文件:%s",args.config)
  71. -- 热更配置文件
  72. if args.config then
  73. local hotfixlist
  74. assetcenter = assetcenter or skynet.localname(".assetcenter")
  75. local error
  76. if args.config == "*" then
  77. error = skynet.call(assetcenter, "lua", "hotfix")
  78. hotfixlist = "*"
  79. elseif args.config then
  80. hotfixlist = {}
  81. local ok, entity = pcall(cjson.decode, args.config)
  82. -- z_trace.guild(" --- entity = ", entity)
  83. if not ok then
  84. return { code=400, msg="Bad request" }
  85. end
  86. for k, v in pairs(entity) do
  87. logger.warn("更新配置文件:%s",v)
  88. error = skynet.call(assetcenter, "lua", "hotfix",v)
  89. hotfixlist[v] = 1
  90. end
  91. else
  92. return cjson.encode({errno = 400, host = header.host, msg = "缺少配置表参数"})
  93. end
  94. if error then
  95. return cjson.encode({errno = 400, host = header.host, msg = "读取文件失败: ".. error})
  96. end
  97. updateconfig(hotfixlist)
  98. -- else
  99. -- return cjson.encode({errno = 400, host = header.host, info = "参数未nil : "})
  100. end
  101. -- 热更源代码
  102. if args.code then
  103. logger.warn("热更新代码")
  104. codecache.clear()
  105. end
  106. -- 通知 watchdog, 放弃 agent 池中的失效对象
  107. watchdog = watchdog or skynet.localname(".ws_watchdog")
  108. skynet.call(watchdog, "lua", "hotfix")
  109. return cjson.encode({errno = 200, host = header.host, msg = "热更新 成功"})
  110. end
  111. return hotfix