123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109 |
- 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 md5 = require "md5"
- local assetcenter
- local watchdog
- local authz = {acc = "yytx", pwd = "lee@YY-Games.520"}
- -- Generate request data
- local content = {acc = authz.acc, pwd = authz.pwd, sign = false}
- content.sign = string.sub(md5.sumhexa(content.acc .. content.pwd), 9, 24)
- local whitelist = {
- ["47.99.157.83"] = true,
- }
- local updateserver
- local function newserver()
- local newnum = {}
- table.insert(newnum,{server=skynet.localname(".manage"),configname={"cdkTime_proto",}})-- 集群管理服务
- 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
- --示例1: http://192.168.1.102:9002/reload?code=xxx&ntype=1
- --示例2: http://192.168.1.102:9002/reload?code=xxx&ntype=2&vjson=["award_proto","item_proto"]
- local function reload(args, ipaddr, header)
- logger.warn("处理来自主机 %s 的hotfix热更请求,:%s", ipaddr, args.vjson)
- if not whitelist[ipaddr] then
- -- return { "403 - Forbidden" }
- -- return cjson.encode({errno = 403, msg = "不信任ip"})
- end
- -- 验证gm账号
- local code = string.sub(args.code, 1, 16)
- if code ~= content.sign then
- return cjson.encode({state = 403, msg = "账号或密码错误"})
- end
- local ntype = tonumber(args.ntype)
- -- 热更配置文件
- if ntype == 2 then
- logger.warn("更新配置文件:%s", args.vjson)
- local hotfixlist
- assetcenter = assetcenter or skynet.localname(".assetcenter")
- local error
- if args.vjson == "*" then
- error = skynet.call(assetcenter, "lua", "hotfix")
- hotfixlist = "*"
- elseif args.vjson then
- hotfixlist = {}
- local entity=cjson.decode(args.vjson)
- 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({state = 400, msg = "缺少配置表参数"})
- end
- if error then
- return cjson.encode({state = 400,msg = "读取文件失败:" .. error})
- end
- updateconfig(hotfixlist)
- -- 热更源代码
- elseif ntype == 1 then
- codecache.clear()
- else
- return cjson.encode({state =400, msg = "错误的ntype"})
- end
- -- 通知 watchdog, 放弃 agent 池中的失效对象
- -- watchdog = watchdog or skynet.localname(".watchdog")
- -- skynet.call(watchdog, "lua", "hotfix")
- logger.warn("hotfix (%s, %s)", args.code, ipaddr)
- return cjson.encode({state = 0, msg = "热更新 成功"})
- end
- return reload
|