Beispiel #1
0
    def genInstCfg(self):
        '''
            配置文件需要包含的内容:
            1. 主库,备库IP;
            2. 域名;
            3. 代理名称,服区ID;
            4. 源服务器IP;
            5. 游戏服务端目录名称
        '''
        theIp, dbOneIp, dbTwoIp, agent, server_id, domain, open_dt = \
        self.kwargs[0], self.kwargs[4], self.kwargs[5], self.kwargs[1], self.kwargs[2], self.kwargs[3], self.kwargs[6]

        dbo = ClassDb()    ## 实例化数据库
        dbo.connect(host=dbHost,port=dbPort,user=dbUser,passwd=dbPass,db=dbName,charset="utf8")

        sql = "select sIP,sPlatServer from dbtx_plat_server where sPlatName='%s' and iStatus in (1,2) and replace(sPlatServer,'S','')<2000 order by replace(sPlatServer,'S','')+0 DESC limit 1;" % (agent)
        dbo.execute(sql)
        res = dbo.fetchone()
        try:
            targSid, file_source_server_ip = res['sPlatServer'], res['sIP']
        except Exception,err:
            saveLog.error("Get file_source_server_ip error occured, %s" % str(err))
Beispiel #2
0
    def genMergeCfg(self):
        '''
            配置文件需要包含的内容:
            1. 本机服区ID;
            2. 目标合服服区ID;
            3. 目标合服机外网IP;
            4. 所有参与合服服区ID(用逗号隔开)
        '''
        dbo = ClassDb()    ## 实例化数据库
        dbo.connect(host=dbHost,port=dbPort,user=dbUser,passwd=dbPass,db=dbName,charset="utf8")

        sPlatName, sSIDRange, sTargetPlat, sTargetSID, iRecId =\
        self.kwargs[0], self.kwargs[1], self.kwargs[2], self.kwargs[3], self.kwargs[4]
        sIdLists = sSIDRange.split(",")

        ## 获取合服目标IP
        dstMergeSrvFlag = sTargetPlat + "_" + sTargetSID
        tSql = "select sIP from dbtx_plat_server where sPlatName='%s' and sPlatServer='%s';" % (sTargetPlat, sTargetSID)
        dbo.execute(tSql)
        dstMergeSrvIp = dbo.fetchone()['sIP']

##        joinListDict = {}

        for srv in sIdLists:
            srcMergeSrvFlag = sPlatName + "_" + srv

            joinMergeSrvs = ','.join([sPlatName + "_" + x for x in sIdLists])
##            srcMergeSrvFlag, dstMergeSrvFlag, joinMergeSrvs = srv, sTargetSID, sSIDRange
##            thisPlatName, thisSid = srv.split("_")[0], srv.split("_")[1]

            ## 获取各个参与合服的机器IP
            iSql = "select sIP from dbtx_plat_server where sPlatName='%s' and sPlatServer='%s';" % (sPlatName, srv)
            dbo.execute(iSql)
            tSrvIp = dbo.fetchone()['sIP']

##            ## 将 参与合服的服区ID与IP生成一个字典,后续删服区数据时使用
##            if srcMergeSrvFlag != dstMergeSrvFlag:
##                joinListDict[srcMergeSrvFlag] = tSrvIp

            cfg_content = cfgDemo % (srcMergeSrvFlag, dstMergeSrvFlag, dstMergeSrvIp, joinMergeSrvs)
            cfgDir = DIRs["MERGE_CFG_DIR"] + srcMergeSrvFlag
            if not os.path.exists(cfgDir):  ## 创建配置文件目录
                os.makedirs(cfgDir)
            shutil.copy(DIRs["MERGE_SH_DIR"]+"dbtx_merge_backup.sh", cfgDir)
            shutil.copy(DIRs["MERGE_SH_DIR"]+"dbtx_merge_curl.sh", cfgDir)
            shutil.copy(DIRs["MERGE_SH_DIR"]+"dbtx_merge_hxdata.sh", cfgDir)
            shutil.copy(DIRs["MERGE_SH_DIR"]+"dbtx_merge_getdata.sh", cfgDir)
            shutil.copy(DIRs["MERGE_SH_DIR"]+"dbtx_merge_putdata.sh", cfgDir)
            shutil.copy(DIRs["MERGE_SH_DIR"]+"dbtx_merge.sql", cfgDir)
            shutil.copy(DIRs["MERGE_SH_DIR"]+"dbtx_merge_fanti.sql", cfgDir)
            shutil.copy(DIRs["MERGE_SH_DIR"]+"dbtx_merge_yuenan.sql", cfgDir)
            shutil.copy(DIRs["MERGE_SH_DIR"]+"dbtx_merge_import.sh", cfgDir)
            shutil.copy(DIRs["MERGE_SH_DIR"]+"dbtx_merge_php.sh", cfgDir)
            shutil.copy(DIRs["MERGE_SH_DIR"]+"dbtx_merge_active.sh", cfgDir)
            cfgFile = cfgDir + "/dbtx_merge_config.sh"
            ## 内容写入到配置文件
            wToCfg(cfgFile, cfg_content)
            saveLog.info("Gen merge config file success.")
##
##        listFile = DIRs["LOG_DIR"] + "%s_joinServers.py" % getToday()
##        wToCfg(listFile, str(joinListDict))
            # 插入到 dbtx_merge_opLog 表
            stepNums = 9 if srcMergeSrvFlag == dstMergeSrvFlag else 6
            for st in range(stepNums):
                sSql = "INSERT INTO dbtx_merge_opLog (iRecId,sPlatName,sPlatServer,iStep,iStatus,sComment,tTime) VALUES \
                ('%s', '%s', '%s', %s, 0, '', '%s');" % (int(iRecId), sPlatName, srv, st, getTimeNow())
                dbo.execute(sSql)

            ## 通过机器人在远程合服IP上创建目录,存放合服脚本
            cCmd = "mkdir -p /root/hefu/%s" % getToday()
            cDesc, cRemarks, cdcTime = "mkdir today hefu_script_dir.", "mkdir today hefu_script_dir.", getTimeNow()
            mkSql = "insert into dbtx_cmd_execute (sCmd,iDependID,eType,sIP,sSSHport,iStatus,sDescription,sRemarks,dCreateTime,sUser,iTimeout) values (\
			'%s','0','remote','%s','22','1','%s','%s','%s','autoMerge','600');" % (cCmd, tSrvIp, cDesc, cRemarks, cdcTime)
            dbo.execute(mkSql)

            ## 插入语句,机器人将合服脚本scp推送到远程服务器上
            #tCmd = "%s /root/hefu/%s/%s" % (cfgDir, getToday(), srcMergeSrvFlag)
            tCmd = "%s /root/hefu/%s" % (cfgDir, getToday())
            tDependId = int(dbo.getLastId())   ## 获取上一条"创建目录"SQL的主键ID作为"SCP"SQL的依赖ID,即必须等创建目录完成才能执行SCP
            tDesc, tRemarks, dcTime = "Send %s dbtx_merge_config.sh" % srcMergeSrvFlag, "Send %s dbtx_merge_config.sh" % srcMergeSrvFlag, getTimeNow()
            dSql = "insert into dbtx_cmd_execute (sCmd,iDependID,eType,sIP,sSSHport,iStatus,sDescription,sRemarks,dCreateTime,sUser,iTimeout) values (\
			'%s','%d','scp','%s','22','1','%s','%s','%s','autoMerge','600');" % (tCmd, tDependId, tSrvIp, tDesc, tRemarks, dcTime)
            dbo.execute(dSql)

            ## 插入语句,机器人在远程服务器上执行合服脚本
            #tCmd = "%s /root/hefu/%s/%s" % (cfgDir, getToday(), srcMergeSrvFlag)
            mCmd = "/usr/bin/python /data/sh/Python/Automatic/mergeCtrStep.py %s %s %s > /dev/null 2>&1 &" % (iRecId, sPlatName, srv)
            mDependId = int(dbo.getLastId())   ## 获取上一条"推送配置文件"SQL的主键ID作为"执行合服脚本"SQL的依赖ID,即必须等推送配置文件完成才能执行合服脚本
            mDesc, mRemarks, mcTime = "Run %s mergeCtrStep.py" % srcMergeSrvFlag, "Run %s mergeCtrStep.py" % srcMergeSrvFlag, getTimeNow()
            mSql = "insert into dbtx_cmd_execute (sCmd,iDependID,eType,sIP,sSSHport,iStatus,sDescription,sRemarks,dCreateTime,sUser,iTimeout) values (\
			'%s','%d','remote','%s','22','1','%s','%s','%s','autoMerge','600');" % (mCmd, mDependId, tSrvIp, mDesc, mRemarks, mcTime)
            dbo.execute(mSql)