def initUpstreamRunstatus(projectName): nodeHealthStatusPath = sys.path[0] + os.sep + 'runtime' + os.sep + str( projectName) + '-node-health-status.json' nodeHealthStatus = JsonFileFunc.readFile(nodeHealthStatusPath) upstreamStatusPath = sys.path[0] + os.sep + 'runtime' + os.sep + str( projectName) + '-upstream-status.json' upstreamStatus = JsonFileFunc.readFile(upstreamStatusPath) if upstreamStatus != None: return upstreamStatus else: FormatPrint.printWarn( str(projectName) + "-upstream-status.json runtime file not exisit ,will create it ") upstreamStatus = { 'total-node-count': len(nodeHealthStatus["nodeinfo"].keys()), 'upstream-node-count': len(nodeHealthStatus["nodeinfo"].keys()) } for nodeName in nodeHealthStatus["nodeinfo"].keys(): upstreamStatus[nodeName] = { 'last-status': 'running', 'is-in-upstream': True, 'is-rebooting': False, 'rebooting-count': 0, 'last-check-time': time.strftime('%Y-%m-%d %H:%M:%S') } JsonFileFunc.createFile(upstreamStatusPath, upstreamStatus) return upstreamStatus
def readFile(fielPath): jsonData = None try: with open(fielPath, 'r') as file: jsonData = json.load(file) except Exception as e: FormatPrint.printError('read [ ' + str(fielPath) + ' ] not exists') return jsonData
def restartTomcat(tomcatStartScriptPath, tomcatKillScriptPath, tomcatTag=None): if killTomcat(tomcatKillScriptPath, tomcatTag) and startTomcat( tomcatStartScriptPath, tomcatTag): FormatPrint.printInfo("重启tomcat成功") return True else: FormatPrint.printInfo("重启tomcat失败") return False
def startHealthCheck(projectName): path = sys.path[0] + os.sep + 'shell' + os.sep + 'healthCheck.sh ' cmd = path + " start " + str(projectName) if os.system(cmd) == 0: FormatPrint.printInfo("启动健康检查服务成功") return True else: FormatPrint.printInfo("启动健康检查服务失败") return False
def restartHealthCheck(projectName): path = sys.path[0] + os.sep + 'shell' + os.sep + 'healthCheck.sh ' cmd = path + " restart " + str(projectName) if os.system(cmd) == 0: FormatPrint.printInfo(" start ngxnx-tomcat service sucess ") return True else: FormatPrint.printInfo(" start ngxnx-tomcat service fail ") return False
def restartTomcats(tomcatStartScriptPath, tomcatKillScriptPath, tomcatTags=None): FormatPrint.printInfo("重启tomcat" + str(tomcatTags)) for tomcatTag in tomcatTags: killTomcat(tomcatKillScriptPath, tomcatTag) startTomcat(tomcatStartScriptPath, tomcatTag) FormatPrint.printInfo("重启tomcat" + str(tomcatTags) + "动作完成") return True
def replaceResource(self): FormatPrint.printDebug("替换更新资源") command = self.willBeUpdateTomcatGroup_JSON[ "tomcatresourceupdatescriptpath"] + " " + str( self.projectUpdateVersion) + " " + str(self.updateTime) FormatPrint.printInfo("执行资源替换脚本:" + str(command)) if os.system(command) == 0: return True else: return False
def modifyRunStatus(self): FormatPrint.printInfo("修改运行时状态文件") ''' 项目启动完成后,发现存在问题:部分tomcat未启动,node-health-runstatus文件需要被修改,需要重新被定义 ''' if self.modifyRunTomcatGroupStatusFile( ) and self.modifyRunNginxGroupStatusFile( ) and self.modifyNodeHealthStatusFile() and self.delUpstreamStatusFile( ): return True else: return False
def projectUpdate(self): FormatPrint.printDebug("项目更新") tomcatTags = [] for tomcat in self.willBeUpdateTomcatGroup_JSON["tomcatgroupinfo"][ "tomcats"]: tomcatTags.append(tomcat["tomcattag"]) if self.replaceResource() and TomcatFunc.restartTomcats( self.tomcatStartScriptPath, self.tomcatKillScriptPath, tomcatTags) and self.checkServiceIsOk(): return True else: return False
def initRunNgixn(self): FormatPrint.printInfo("初始化nginx运行数据") try: with open( sys.path[0] + os.sep + 'runtime' + os.sep + 'nginx-group-runstatus.json', 'r') as nginx_run_status_file: self.crurentProjectRunNginxGroup_JSON = json.load( nginx_run_status_file)["projectname"][self.projectName] currentRunNginxGroup = self.crurentProjectRunNginxGroup_JSON[ "currentrun"] self.runNginxGroup_JSON = self.crurentProjectRunNginxGroup_JSON[ currentRunNginxGroup] except Exception as e: # file not exist, create it FormatPrint.printWarn(str(e)) FormatPrint.printWarn("初始化tomcat运行组数据,出现异常:nginx运行时文件不存在,将创建文件") init_nginx_groups_runstatus_JSON = {} init_nginx_group_runstatus_JSON = {} for projectname in self.conf_JSON["projectname"].keys(): FormatPrint.printInfo("当前配置项目:" + str(projectname)) new_nginx_group_runstatus_JSON = { "currentrun": "groupmaster", "groupmaster": { "lastmodify": time.strftime('%Y-%m-%d %H:%M:%S'), "modifytimes": "1" }, "groupbackup": { "lastmodify": time.strftime('%Y-%m-%d %H:%M:%S'), "modifytimes": "0" } } init_nginx_group_runstatus_JSON[ projectname] = new_nginx_group_runstatus_JSON init_nginx_groups_runstatus_JSON = { "projectname": init_nginx_group_runstatus_JSON } with open( sys.path[0] + os.sep + 'runtime' + os.sep + 'nginx-group-runstatus.json', 'w') as new_tomcat_run_status_file: new_tomcat_run_status_file.write( json.dumps(init_nginx_groups_runstatus_JSON, indent=4)) FormatPrint.printInfo("创建nginx组运行时状态文件完成") self.crurentProjectRunNginxGroup_JSON = init_nginx_groups_runstatus_JSON[ "projectname"][self.projectName] currentRunGroup = self.crurentProjectRunNginxGroup_JSON[ "currentrun"] self.runTomcatGroup_JSON = self.crurentProjectRunNginxGroup_JSON[ currentRunGroup]
def modifyRunTomcatGroupStatusFile(self): FormatPrint.printDebug("修改tomcat组运行状态") try: with open( sys.path[0] + os.sep + 'runtime' + os.sep + 'tomcat-group-runstatus.json', 'r') as tomcat_run_status_file: tomcat_run_status_JSON = json.load(tomcat_run_status_file) willBeUpdateGroupRunStatus_JSON = self.crurentProjectRunTomcatGroup_JSON[ self.willBeUpdatedGroup] new_updateGroupTomcatRunStatus_JSON = { 'failtimes': willBeUpdateGroupRunStatus_JSON["failtimes"], 'lastrollbacktime': willBeUpdateGroupRunStatus_JSON["lastrollbacktime"], 'lastmodifytime': time.strftime('%Y-%m-%d %H:%M:%S'), 'sucesstimes': int(willBeUpdateGroupRunStatus_JSON["sucesstimes"]) + 1, 'rollbacktimes': '0', 'runtimes': int(willBeUpdateGroupRunStatus_JSON["runtimes"]) + 1 } new_projectTomcatRunStatus_JSON = { "deploymentmode": self.deploymentmode, "currentrun": self.willBeUpdatedGroup, "totalruntimes": int(tomcat_run_status_JSON["projectname"][self.projectName] ["totalruntimes"]) + 1, self.willBeUpdatedGroup: new_updateGroupTomcatRunStatus_JSON, self.runGroup: self.crurentProjectRunTomcatGroup_JSON[self.runGroup] } tomcat_run_status_JSON["projectname"][ self.projectName] = new_projectTomcatRunStatus_JSON with open( sys.path[0] + os.sep + 'runtime' + os.sep + 'tomcat-group-runstatus.json', 'w') as new_tomcat_run_status_file: new_tomcat_run_status_file.write( json.dumps(tomcat_run_status_JSON, indent=4)) except Exception as e: FormatPrint.printError("修改tomcat组运行状态时,出现异常:" + str(e)) return False return True
def startNginx(cmd): FormatPrint.printInfo("启动nginx") FormatPrint.printInfo("执行命令:" + str(cmd)) if os.system(cmd) == 0: FormatPrint.printInfo("启动nginx成功") return True else: FormatPrint.printInfo("启动nginx失败") return False
def delUpstreamStatusFile(projectName): orgin_file = sys.path[0] + os.sep + 'runtime' + os.sep + str( projectName) + '-upstream-status.json' if os.path.exists(orgin_file): try: new_file = sys.path[ 0] + os.sep + 'runtime' + os.sep + time.strftime( '%Y%m%d%H%M%S') + str( projectName) + '-upstream-status.json' os.rename(orgin_file, new_file) except Exception as e: FormatPrint.printError("删除upstream-status文件出错:" + str(e)) return False return True else: FormatPrint.printInfo("upstream-status文件不存在,未初始化") return True
def reloadNginx(cmd): FormatPrint.printInfo("exec command:" + str(cmd)) if os.system(cmd) == 0: FormatPrint.printInfo("reload ngninx sucess") return True else: FormatPrint.printError("reload ngninx error ") return False
def modifyRunNginxGroupStatusFile(self): FormatPrint.printDebug("修改nginx组运行状态") try: with open( sys.path[0] + os.sep + 'runtime' + os.sep + 'nginx-group-runstatus.json', 'r') as nginx_run_status_file: nginx_run_status_JSON = json.load(nginx_run_status_file) willBeUpdateGroupRunStatus_JSON = self.crurentProjectRunNginxGroup_JSON[ self.willBeUpdatedGroup] new_updateGroupNginxRunStatus_JSON = { "lastmodify": time.strftime('%Y-%m-%d %H:%M:%S'), "modifytimes": int(willBeUpdateGroupRunStatus_JSON["modifytimes"]) + 1 } new_projectNginxRunStatus_JSON = { "deploymentmode": self.deploymentmode, "currentrun": self.willBeUpdatedGroup, self.willBeUpdatedGroup: new_updateGroupNginxRunStatus_JSON, self.runGroup: self.crurentProjectRunNginxGroup_JSON[self.runGroup] } nginx_run_status_JSON["projectname"][ self.projectName] = new_projectNginxRunStatus_JSON with open( sys.path[0] + os.sep + 'runtime' + os.sep + 'nginx-group-runstatus.json', 'w') as new_tomcat_run_status_file: new_tomcat_run_status_file.write( json.dumps(nginx_run_status_JSON, indent=4)) except Exception as e: FormatPrint.printError("修改nginx组运行状态时,出现异常:" + str(e)) return False return True
def readConfig(self): FormatPrint.printInfo("读取配置文件:readConfig") with open(sys.path[0] + os.sep + 'conf' + os.sep + 'tomcat-conf.json', 'r') as tomcat_conf: self.conf_JSON = json.load(tomcat_conf) FormatPrint.printDebug("当前配置文件信息为:" + str(self.conf_JSON)) if self.projectName in self.conf_JSON["projectname"].keys(): self.projectConf_JSON = self.conf_JSON["projectname"][ self.projectName] self.deploymentmode = self.conf_JSON["projectname"][ self.projectName]["deploymentmode"] FormatPrint.printDebug("当前操作项目的配置文件信息为:" + str(self.projectConf_JSON)) else: FormatPrint.printFalat("不存在" + str(self.projectName) + "项目配置信息,中断更新") sys.exit(-1)
def replceResource(pu): tomcat_conf = pu.projectJson.tomcatConf command = tomcat_conf['projectname'][pu.projectName][ pu.willUpdateGroup[0]]['tomcatresourceupdatescriptpath'] + " " + str( pu.updateVersion) + " " + str(pu.updateTime) FormatPrint.printInfo("执行资源替换脚本:" + str(command)) if os.system(command) == 0: FormatPrint.printInfo("replace resource sucess ") return True else: FormatPrint.printInfo("replace resource fail ") return False
def modifyNodeHealthStatus(pu): projecName = pu.projectName deploymentmode = pu.deploymentmode tomcatGroup = pu.willUpdateGroup tomcat_conf = pu.projectJson.tomcatConf tomcatTags = tomcat_conf['projectname'][projecName][tomcatGroup]['tomcatgroupinfo']['tomcats'] servicecheckurl = tomcat_conf['projectname'][projecName][tomcatGroup]['servicecheckurl'] servicecheckpar = tomcat_conf['projectname'][projecName][tomcatGroup]['servicecheckpar'] currentRunGroup = pu.willUpdateGroup if deploymentmode == 'single': FormatPrint.printError(" you must be joking , no posslible ") elif deploymentmode == 'onehalf': FormatPrint.printInfo(" onehalf mode modify node-health-status ") elif deploymentmode == 'double': FormatPrint.printInfo(" double mode modify node-health-status ") else: FormatPrint.printFalat(" you must bu configure worng deploymentmode ")
def projectUpdate(projectName, projectUpdateVersion, updateType, updateTime): pu = ProjectUpdate(projectName) pu.updateType = updateType pu.projectUpdateVersion = projectUpdateVersion pu.updateTime = updateTime pu.readConfig() # 读取配置文件信息 #(全组启动方式), (单项目启动方式), (半启动方式),默认采用全组启动方式 if pu.deploymentmode == None or pu.deploymentmode not in [ "double", "single", "onehalf" ]: FormatPrint.printFalat("配置文件deploymentmode未配置") exit(-1) if pu.checkRuntimeStateConf(): pass else: FormatPrint.printInfo("运行状态检查与当前配置文件不一致") FormatPrint.printInfo("是否进行运行时文件切换?") exit(-1) #FormatPrint.printDebug("当前tomcat运行组信息:"+str(pu.crurentProjectRunTomcatGroup_JSON)) #FormatPrint.printDebug("当前nginx运行信息:"+str(pu.crurentProjectRunNginxGroup_JSON)) #FormatPrint.printDebug("当前运行的组为:"+str(pu.runGroup)) if pu.deploymentmode == 'single': pu.willBeUpdatedGroup = 'groupmaster' elif pu.runGroup == 'groupmaster': pu.willBeUpdatedGroup = 'groupbackup' else: pu.willBeUpdatedGroup = 'groupmaster' if pu.deploymentmode == 'onehalf': for deploymentmode in ['groupmaster', 'groupbackup']: pu.deploymentmode = deploymentmode pu.updateProcess() else: pu.updateProcess()
def changeNginxConf(projectName, sucessRestartTomcatTags, type="default"): if (type == "update"): delUpstreamStatusFile(projectName) path = sys.path[0] + os.sep + 'runtime' + os.sep + str( projectName) + '-node-health-status.json' nodehealthstatus = JsonFileFunc.readFile(path) upstreamName = nodehealthstatus['upstreamName'] nginxConfPath = nodehealthstatus['nginxConfPath'] nginxreloadcmd = nodehealthstatus['nginxreloadcmd'] nginxreplacestarttag = nodehealthstatus['nginxreplacestarttag'] nginxreplaceendtag = nodehealthstatus['nginxreplaceendtag'] nginxrootstarttag = nodehealthstatus['nginxrootstarttag'] nginxrootendtag = nodehealthstatus['nginxrootendtag'] nginxrootconf = nodehealthstatus['nginxrootconf'] upstreamList = getUpstreamList(nodehealthstatus, sucessRestartTomcatTags) FormatPrint.printInfo("tomcat重启完成,修改nginx配置") ''' 1、读取配置,读取开始标识和结束标识 2、生成修改部分内容 3、循环读取配置文件,找到开始标识和结束标识 4、替换开始标识和结束标识之间的内容 ''' upstream_conf = nginxreplacestarttag + '\n' upstream_conf += '\tupstream ' + upstreamName + '\n\t{\n' for upstreamStr in upstreamList: upstream_conf += '\t\t' + upstreamStr + '\n' upstream_conf += '\t}\n' upstream_conf = upstream_conf.decode('utf-8') root_conf = nginxrootstarttag + '\n' root_conf += nginxrootconf + '\n' root_conf = root_conf.decode('utf-8') FormatPrint.printInfo("修改的nginx的upstream配置文件如下:\n" + upstream_conf.encode('utf-8')) FormatPrint.printInfo("修改的nginx的root路径配置文件如下:\n" + root_conf.encode('utf-8')) confstr = "" try: with open(nginxConfPath, 'r') as nginx_conf_temp_file: isContinue = False isRooTContinue = False for line in nginx_conf_temp_file: line = line.decode('utf-8') # 找到这个标识,修改upstream内容 if line.find(nginxreplaceendtag) != -1: isContinue = False confstr += upstream_conf if isContinue or line.find(nginxreplacestarttag) != -1: isContinue = True continue # 找到这个标识,修改root内容 if line.find(nginxrootendtag) != -1: isRooTContinue = False confstr += root_conf if isRooTContinue or line.find(nginxrootstarttag) != -1: isRooTContinue = True continue confstr += line except Exception as e: FormatPrint.printError("读取配置nginx配置文件出错,错误信息如下:\n" + str(e)) return False try: with open(nginxConfPath, 'w') as nginx_conf_file: nginx_conf_file.write(confstr.encode('utf-8')) except Exception as e: FormatPrint.printError("修改配置nginx配置文件出错,错误信息如下:\n" + str(e)) return False if reloadNginx(nginxreloadcmd): return True else: return False
def healthCheckOnce(): FormatPrint.printDebug("startHealthCheckOnce") if HealthCheck.checkOnce(projectName): FormatPrint.printInfo("一次性健康检查服务成功") else: FormatPrint.printInfo("一次性健康检查服务失败")
def checkNodeService(self): FormatPrint.printInfo("check nginx-tomcat node service is " + str(self.runtimes)) self.runtimes = int(self.runtimes) + 1 socket.setdefaulttimeout(5) path = sys.path[0] + os.sep + 'runtime' + os.sep + str(self.projectName) + '-node-health-status.json' nodeHealthStatus = JsonFileFunc.readFile(path) for node_name in nodeHealthStatus['nodeinfo'].keys(): node = nodeHealthStatus['nodeinfo'][node_name] url = nodeHealthStatus['nodeinfo'][node_name]['health-check-url'] request_data = nodeHealthStatus['nodeinfo'][node_name]['health-check-data'] response = None ret_code = 502 begin_time = time.time() try: response = urllib2.urlopen(url, request_data) except Exception as e: FormatPrint.printWarn(str(self.projectName) + "-" + str(node_name)+"service node exception:"+str(e)) if hasattr(e, 'code'): ret_code = e.code finally: end_time = time.time() if response: ret_data = response.read() ret_code = response.code response.close() if isinstance(ret_data, bytes): ret_data = bytes.decode(ret_data) node['last-response-time'] = round((end_time - begin_time), 3) node['last-check-time'] = time.strftime('%Y-%m-%d %H:%M:%S') node['last-response-code'] = ret_code if ret_code == 200: FormatPrint.printInfo("节点正常:" + str(self.projectName) + "-" + str(node_name) ) node['status'] = 'running' try: # 重启失败时,status为0 node['last-response-data'] = json.loads(ret_data) if node['last-response-data']['status'] != 1: node['status'] = 'error' except: node['status'] = 'error' if node['status'] == 'running': # http返回码为200且服务返回status=1,才重置fail-count node['fail-count'] = 0 elif node['status'] == 'running' or node['status'] == 'n/a': node['status'] = 'error' if node['status'] == 'error': node['fail-count'] += 1 if node['fail-count'] >= 3: node['status'] = 'dead' elif node['status'] == 'dead': node['fail-count'] += 1 if node['status'] == 'error': FormatPrint.printWarn("node error:"+str(self.projectName)+"-" + str(node_name) + " has been detected ERROR for fail-count " + str(node['fail-count'])) if node['status'] == 'dead': FormatPrint.printError("node dead"+str(self.projectName)+ "-" + str(node_name) + " has been detected DEAD for fail-count " + str( node['fail-count'])) nodeHealthStatus['nodeinfo'][node_name] = node JsonFileFunc.createFile(path, nodeHealthStatus)
def stopHealthCheck(): FormatPrint.printDebug("stopHealthCheck") if HealthCheck.stopHealthCheck(projectName): FormatPrint.printInfo("关闭健康检查服务成功") else: FormatPrint.printInfo("关闭健康检查服务失败")
def healthCheckStatus(): FormatPrint.printDebug("healthCheckStatus") HealthCheck.healthCheckStatus(projectName)
def healthCheckAll(): FormatPrint.printDebug("healthCheckAll") if HealthCheck.checkAllTime(projectName): FormatPrint.printInfo("多次性健康检查服务成功") else: FormatPrint.printInfo("多次性健康检查服务失败")
def killTomcat(tomcatKillScriptPath, tomcatTag=None): if not tomcatKillScriptPath: FormatPrint.printError("tomcat关闭脚本路径未指定") return False if tomcatTag == None: FormatPrint.printError("未指定关闭的tomcat") return False else: FormatPrint.printInfo("当前关闭的tomcat编号为" + str(tomcatTag)) command = str(tomcatKillScriptPath) + " " + str(tomcatTag) FormatPrint.printDebug("当前执行的命令为:" + str(command)) if os.system(command) == 0: FormatPrint.printInfo("关闭tomcat" + str(tomcatTag) + "成功") return True else: FormatPrint.printInfo("关闭tomcat" + str(tomcatTag) + "失败") return False
def startTomcat(tomcatStartScriptPath, tomcatTag=None): if not tomcatStartScriptPath: FormatPrint.printError("tomcat启动脚本路径未指定") return False if tomcatTag == None: FormatPrint.printError("未指定启动的tomcat") return False else: FormatPrint.printInfo("当前启动的tomcat编号为" + str(tomcatTag)) command = str(tomcatStartScriptPath) + " " + str(tomcatTag) FormatPrint.printDebug("当前执行的命令为:" + str(command)) if os.system(command) == 0: FormatPrint.printInfo("启动tomcat" + str(tomcatTag) + "成功") return True else: FormatPrint.printInfo("启动tomcat" + str(tomcatTag) + "失败") return False
def projectUpdate(projectName, updateVersion, updateType, updateTime): pu = ProjectUpdate() pu.projectName = projectName pu.updateVersion = updateVersion pu.updateType = updateType pu.updateTime = updateTime #A、关闭健康检查服务 if HealthCheck.stopHealthCheck(projectName): pass else: FormatPrint.printFalat("stopHealthCheck is wrong") tomcatPath = sys.path[0] + os.sep + 'conf' + os.sep + 'tomcat-conf.json' pu.tomcatConf = JsonFileFunc.readFile(tomcatPath) if pu.tomcatConf is None: FormatPrint.printFalat('can not read tomcat-conf configure') if pu.projectName not in pu.tomcatConf['projectname']: FormatPrint.printFalat( str(pu.projectName) + ' not configure in the tomcat-conf.json') pu.deploymentmode = pu.tomcatConf['projectname'][projectName][ 'deploymentmode'] pu.hostInfostr = str(pu.tomcatConf['hostname']) + ":" + str( pu.tomcatConf['serverip']) ##################调用服务 if pu.deploymentmode == 'single': FormatPrint.printDebug("curent project is single deploymentmode") __projectupdate_single.process(pu) elif pu.deploymentmode == 'onehalf': FormatPrint.printDebug("curent project is onehalf deploymentmode") __projectupdate_onehalf.process(pu) elif pu.deploymentmode == 'double': FormatPrint.printDebug("curent project is double deploymentmode") __projectupdate_double.process(pu) else: FormatPrint.printFalat( str(pu.projectName) + 'project configure wrong deploymentmode ') #F、启动健康检查服务 if HealthCheck.startHealthCheck(projectName): pass else: FormatPrint.printFalat("startHealthCheck is wrong")
def checkServiceIsOk(pu): projectName = pu.projectName willUpdateGroup = pu.willUpdateGroup[0] hostinfostr = pu.hostInfostr tomcat_conf = pu.projectJson.tomcatConf maxTime = tomcat_conf['tomcatmaxrestattime'] endTime = tomcat_conf['endUpdateWaiteMaxTime'] tomcatKillScriptPath = tomcat_conf['projectname'][ pu.projectName][willUpdateGroup]['tomcatkillscriptpath'] restartTomcats = tomcat_conf['projectname'][projectName][willUpdateGroup][ 'tomcatgroupinfo']['tomcats'] healCheckUrl = tomcat_conf['projectname'][projectName][willUpdateGroup][ 'servicecheckurl'] checkData = tomcat_conf['projectname'][projectName][willUpdateGroup][ 'servicecheckpar'] socket.setdefaulttimeout(5) sucessRestartTomcattags = [] tomcatStartTime = time.time() restartTomcatTagDict = {} for tomcatInfo in restartTomcats: restartTomcatTagDict[tomcatInfo['tomcattag']] = tomcatInfo while True: intervalTime = time.time() - tomcatStartTime FormatPrint.printInfo("tomcat重启,服务检查耗时:" + str(intervalTime)) if len(restartTomcatTagDict) == 0 or ( intervalTime > float(maxTime) and len(sucessRestartTomcattags) > 0): for tomcatTag in sucessRestartTomcattags: FormatPrint.printInfo("tomcat" + str(tomcatTag) + "启动成功") # 记得关闭未启动成功的tocmat if len(restartTomcatTagDict) > 0: for failTocmatTag in restartTomcatTagDict: TomcatFunc.killTomcat(tomcatKillScriptPath, failTocmatTag) break if intervalTime > float(endTime): FormatPrint.printFalat("在最大重启时间内,未发现已经完成启动的tomcat,终止更新") return sucessRestartTomcattags deltomcatTags = [] for tomcatTag in restartTomcatTagDict.keys(): tomcat = restartTomcatTagDict[tomcatTag] checkUrl = "http://" + tomcat["serviceip"] + ":" + tomcat[ "port"] + str(healCheckUrl) try: response = urllib2.urlopen(checkUrl, checkData) ret_data = response.read() ret_code = response.code response.close() if isinstance(ret_data, bytes): ret_data = bytes.decode(ret_data) ret_data = json.loads(ret_data) if ret_data["status"] == 1 and ret_code == 200: FormatPrint.printInfo( "####################################################################" ) FormatPrint.printInfo("tomcat更新记录如下:") FormatPrint.printInfo( str(hostinfostr) + "_" + str(projectName) + "项目,tomcat" + str(tomcatTag) + "更新成功") FormatPrint.printInfo("开始更新时间:" + str(tomcatStartTime)) FormatPrint.printInfo("更新结束时间:" + time.strftime('%Y-%m-%d %H:%M:%S')) FormatPrint.printInfo( "####################################################################" ) sucessRestartTomcattags.append(tomcatTag) deltomcatTags.append(tomcatTag) except Exception as e: FormatPrint.printWarn( str(hostinfostr) + "_" + str(projectName) + "tomcat" + str(tomcatTag) + "更新异常:" + str(e)) if (len(deltomcatTags) > 0): for deltag in deltomcatTags: del restartTomcatTagDict[deltag] time.sleep(3) #清空deltomcatTags 临时变量 del deltomcatTags[:] return sucessRestartTomcattags
def restartHealthCheck(): FormatPrint.printDebug("restartHealthCheck") if HealthCheck.restartHealthCheck(projectName): FormatPrint.printInfo("重启健康检查服务成功") else: FormatPrint.printInfo("重启健康检查服务失败")