123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123 |
- local skynet = require "skynet"
- local codecache = require "skynet.codecache"
- require "skynet.manager"
- local logger = require "logger"
- local stringify = require "stringify"
- local cjson = require "cjson"
- local assetcenter
- local watchdog
- local authz = {
- yytx = "lee@YY-Games.520"
- }
- local whitelist = {
- ["192.168.1.23"] = true,
- ["192.168.1.127"] = true,
- ["14.29.136.211"] = true,
- ["192.168.1.41"] = true,
- }
- local updateserver
- local function newserver()
- local newnum = {}
- table.insert(newnum,{server=skynet.localname(".quest"),configname={"TaskConfig_proto",}})--z力量
- return newnum
- end
- local function hotfix_true(hotfixlist,configname)
- if hotfixlist then
- if hotfixlist == "*" then
- return 1
- end
- for k, v in pairs(configname) do
- if hotfixlist[v] then
- logger.trace("_______________________热更配置:%s,更新服务",v)
- return 1
- end
- end
- end
- return
- end
- --对需要更新配置的服务进行更新
- local function updateconfig(hotfixlist)
- updateserver = updateserver or newserver()
- if next(updateserver) then
- for k, v in pairs(updateserver) do
- if v.configname and hotfix_true(hotfixlist,v.configname) then
- skynet.send(v.server, "lua", "hotfix", hotfixlist)
- end
- end
- else
- logger.trace("_______________________热更配置无服务需要")
- end
- end
- -- local z_trace = require "zlf_log"
- -- local z_trace = z_trace.trace
- -- example:
- -- http://192.168.1.57:9001/hotfix?user=lee&pwd=lee@123&config=*
- -- http://192.168.1.57:9001/hotfix?user=lee&pwd=lee@123&code=*
- -- http://192.168.1.57:9001/hotfix?user=lee&pwd=lee@123&code=*&config=*
- local function hotfix(args, ipaddr, header)
- logger.warn("处理来自主机 %s 的hotfix请求,:%s", ipaddr,args.config)
- if not whitelist[ipaddr] then
- --return { "403 - Forbidden" }
- -- return cjson.encode({errno = 403, host = header.host, info = "不信任ip"})
- end
- -- 鉴权
- -- local user = args.user or ""
- -- local pwd = args.pwd or ""
- -- if authz[user] ~= pwd then
- -- -- return { "403 - Forbidden" }
- -- return cjson.encode({errno = 403, host = header.host, info = "账号或密码错误"})
- -- end
- logger.warn("更新配置文件:%s",args.config)
- -- 热更配置文件
- if args.config then
- local hotfixlist
- assetcenter = assetcenter or skynet.localname(".assetcenter")
- local error
- if args.config == "*" then
- error = skynet.call(assetcenter, "lua", "hotfix")
- hotfixlist = "*"
- elseif args.config then
- hotfixlist = {}
- local ok, entity = pcall(cjson.decode, args.config)
- -- z_trace.guild(" --- entity = ", entity)
- if not ok then
- return { code=400, msg="Bad request" }
- end
- for k, v in pairs(entity) do
- logger.warn("更新配置文件:%s",v)
- error = skynet.call(assetcenter, "lua", "hotfix",v)
- hotfixlist[v] = 1
- end
- else
- return cjson.encode({errno = 400, host = header.host, msg = "缺少配置表参数"})
- end
- if error then
- return cjson.encode({errno = 400, host = header.host, msg = "读取文件失败: ".. error})
- end
- updateconfig(hotfixlist)
- -- else
- -- return cjson.encode({errno = 400, host = header.host, info = "参数未nil : "})
- end
- -- 热更源代码
- if args.code then
- logger.warn("热更新代码")
- codecache.clear()
- end
- -- 通知 watchdog, 放弃 agent 池中的失效对象
- watchdog = watchdog or skynet.localname(".ws_watchdog")
- skynet.call(watchdog, "lua", "hotfix")
- return cjson.encode({errno = 200, host = header.host, msg = "热更新 成功"})
- end
- return hotfix
|