def __mini_webroot__(params): wwwroot = params['service']['paths']['webroot'] jarpath = params['__pyscript_path__'] mylog.log('压缩WEBROOT :', wwwroot) basename = os.path.basename(wwwroot) htmlfiles = [] for parent, _, filenames in os.walk(wwwroot): if parent.find('.svn') < 0 and parent.find('configure') < 0: for filename in filenames: if filename.find('.svn') < 0: fnames = filename.split('.') ext = fnames[-1] # if ext == 'html' : # 某些情况下, 编码错乱, 不进行压缩处理了 # htmlfiles.append(('html', parent, filename)) if ext == 'js' or ext == 'css': if not 'min' in fnames: htmlfiles.append(('java', parent, filename)) count = len(htmlfiles) maxlen = 0 for x in xrange(count): mtype, parent, filename = htmlfiles[x] maxlen = max(maxlen, len(parent + '/' + htmlfiles[x][2])) maxlen += 10 for x in xrange(count): htmlfile = htmlfiles[x] mtype, parent, filename = htmlfile fullpath = parent + '/' + filename logoutpath = fullpath while len(logoutpath) < maxlen: logoutpath = logoutpath + ' ' sys.stdout.write('\r') sys.stdout.write('min web file [%d/%d] %s' % (x + 1, count, logoutpath)) if mtype == 'html': content = __execute_html__(fullpath) else: buffilepath = fullpath.replace('/' + basename + '/', '/.' + basename + '/') bufcontent = myfiles.read_file(buffilepath) newcontent = myfiles.read_file(fullpath) if bufcontent == newcontent: content = myfiles.read_file(buffilepath + '.mindata_') else: fnames = filename.split('.') content = __execute_java__(jarpath, fnames[-1], fullpath) myfiles.make_dirs(os.path.dirname(buffilepath)) myfiles.write_file('', buffilepath, newcontent) myfiles.write_file('', buffilepath + '.mindata_', content) myfiles.write_file('', fullpath, content) sys.stdout.write('\rmin web file %d done.%s\n' % (count, ' ' * maxlen))
def __action_start_clean_status__(params): mylog.log('清除启动状态标志') status, outputs = exec_index_script_on_first_server(params, -3, 'cleanall') if status == 0 : datas = get_status_json_datas(outputs) if datas['result'] == 0 : mylog.log('清除启动状态标志成功') return 0 mylog.error('清除启动状态标志失败 !!') mylog.error(outputs) return 1
def action_make_encrypt_pyc(actparams): ''' 将pyc进行途游方式的加密处理,加密后必须使用途游的pypy-enc进行运行 ''' params = myhelper.action_common_init(actparams) if not params: return 0 service = params['service'] # 加密处理 if service['encodepyc']: mylog.log('加密运行处理') compilepath = service['paths']['bin'] cmd = 'find %s -name "*.py" | xargs rm -fr' % (compilepath) status, output = commands.getstatusoutput(cmd) if status != 0: mylog.log('ERRROR !!', '加密模式,游戏工程py文件删除失败:', compilepath) mylog.log(status, output) return 0 pycfiles = myfiles.find_py_files(compilepath, '.pyc', False) for pycfile in pycfiles: __tuyoo_enc_pyc_file__('%s/%s' % (compilepath, pycfile)) mylog.log('加密运行处理完成') return 1
def action_load_service(params): ''' 装载并检测服务启动配置文件 ''' # 取得当前文件的路径,添加到PYTHONPATH当中 servicefile = myfiles.normpath(params['servicefile']) mylog.log('装载配置文件 :', servicefile) service = myfiles.read_json_file(servicefile, True) if not __check_basic_info__(params, service, servicefile): return 0 if not __check_projects_paths__(params, service): return 0 if not __check_configure_for_servers__(params, service): return 0 if not __check_configure_default_value__(params, service): return 0 if not __check_configure_for_process__(service): return 0 if not __check_service_url__(service, 'http.game'): return 0 if not __check_service_url__(service, 'http.download'): return 0 if not __check_service_url__(service, 'http.sdk'): return 0 encodepyc = service.get('encodepyc') if not encodepyc in (0, 1): return mylog.error('配置文件 : encodepyc 是否使用加密pyc错误,0或1') # pypy = service.get('pypy') # if not isinstance(pypy, str) : # return mylog.error('配置文件 : pypy命令执行值错误') # if pypy[0] != '/' : # pypy = '/bin/' + pypy # if not myfiles.file_exists(pypy) : # return mylog.error('配置文件 : pypy可执行命令丢失 ' + pypy) params['service'] = service __print_service_content__(params) return 1
def action_stop(actparams): ''' 服务整体全部停止服务 ''' mylog.log(mylog.wrap_color_cyan('开始停止服务')) params = myhelper.action_common_init(actparams) if not params: return 0 haserror = mutil_thread_server_action(params, thread_action_stop) if haserror: mylog.error('停止服务失败 !!') return 0 mylog.log('停止服务完成') return 1
def action_clean_all(actparams): ''' 清空运行期的所有目录log、bireport ''' mylog.log(mylog.wrap_color_cyan('清除所有机器的编译、日志内容')) params = myhelper.action_common_init(actparams) if not params : return 0 haserror = mutil_thread_server_action(params, thread_action_clean_all) if haserror : mylog.error('清除所有机器的编译、日志内容失败 !!') return 0 mylog.log(mylog.wrap_color_cyan('清除所有机器的编译、日志内容完成')) return 1
def execute_hot_cmd(actparams, act_name, hotcmd_params): mylog.log('开始' + act_name) params = myhelper.action_common_init(actparams) if not params: return 0 service = params['service'] script = service['_script_hotcmd_'] status, outputs = exec_index_script_on_first_server( params, script, hotcmd_params) if status != 0: mylog.error(act_name + '失败') mylog.error(outputs) return 0 datas = get_status_json_datas(outputs) if datas.get('result', -1) != 0: mylog.log(act_name + '失败') mylog.error(outputs) return 0 iserror = 0 process = datas.get('process') if isinstance(process, dict): for prockey, presult in process.items(): result = presult.get('result', {}) ok = result.get('ok', 0) if ok != 1: iserror = 1 mylog.error(prockey, result, presult.get('error', {})) else: mylog.log(prockey, result) if iserror: mylog.log(act_name + '失败') return 0 mylog.log(act_name + '成功') return 1
def action_hotfix(actparams): ''' 推送hotfix目录下的所有文件到各个服务器,并在各个服务器执行给出的文件 ''' mylog.log('准备HOTFIX') params = myhelper.action_common_init(actparams) if not params: return 0 service = params['service'] # 创建所有的路径 hotfixdir = service['paths']['hotfix'] if not myfiles.make_path(hotfixdir): return 0 # 拷贝源文件到bin的install路径 cpfpaths = [] cptpaths = [] for proj in service['projects']: projpath = proj['path'] cpfpaths.append('%s/hotfix' % (projpath)) cptpaths.append(hotfixdir) if not myfiles.copy_files_all(cpfpaths, cptpaths): return 0 hotfixpy = actparams['hotfix'] hotfixpy = hotfixdir + '/' + hotfixpy if not myfiles.file_exists(hotfixpy): mylog.error('没有找到HOTFIX文件', hotfixpy) return 0 if not push_dirs_to_all_server(params, '推送HOTFIX代码', DIR_HOTFIX): return 0 return execute_hot_cmd(actparams, 'HOTFIX', 'exec_hotfix_py ' + hotfixpy)
def __write_redis_config_script__(params, service, configuredata): redisfile = 'configure.json' mylog.log('保存游戏配置 :', redisfile) myfiles.write_file(service['paths']['script'], redisfile, configuredata) service['_configure_json_file_'] = redisfile _sh_template_ = ''' #!/bin/bash export MAKE_TIME='%s' export PATH_SCRIPT='%s' export PATH_LOG='%s' sh ${PATH_SCRIPT}/shscript/_hotcmd_.sh "$@" exit $? ''' content = _sh_template_ % (params['_make_time_'], service['paths']['script'], service['paths']['log']) outfile = 'hotcmd.sh' mylog.log('生成游戏热更新脚本 :', outfile) myfiles.write_file(service['paths']['script'], outfile, content) service['_script_hotcmd_'] = outfile return 1
def action_backup(actparams): ''' 备份当前的所有内容、工程文件、编译输出文件,测试模式不进行任何备份 ''' mylog.log('备份全部代码') params = myhelper.action_common_init(actparams, True) if not params : mylog.log('找不到上次的编译结果,无法备份') return 1 backfile = tar_gzip_folders(params, PACK_TYPE_TAR_GZIP, DIR_ALL) if not backfile : mylog.error('备份失败') return 0 mylog.log('备份成功') return 1
def action_start(actparams): ''' 服务整体全部重新启动 ''' mylog.log('准备启动服务') params = myhelper.action_common_init(actparams) if not params : return 0 if __action_start_clean_status__(params) : return 0 mylog.log(mylog.wrap_color_cyan('开始启动服务')) haserror = mutil_thread_server_action(params, __thread_action_start__) if haserror : mylog.error('启动服务失败 !!') return 0 mylog.log('启动服务完成') return 1
def push_dirs_to_all_server(params, actname, push_dirs): service = params['service'] servers = service['servers'] if len(servers) == 1: if servers[0].get('localhost', 0) == 1: mylog.log(actname, ':', '本机运行,无需推送') return 1 pushfile = tar_gzip_folders(params, PACK_TYPE_TGZ, push_dirs) if pushfile == None: return 0 params['pushfile'] = pushfile mylog.log(mylog.wrap_color_cyan(actname)) haserror = mutil_thread_server_action(params, __thread_action_push__) if haserror: mylog.error(actname, ':', '失败 !!') return 0 mylog.log(actname + '完成') return 1
def __make_output_script__(params, service, server, process, servershnames, serverprocid): def _sort_process_(p1, p2): vs = {'game':1, 'account':2, 'entity' : 3, 'heart': 4, 'quick': 5, 'conn': 6, 'http': 7, 'robot' : 8, } t1 = vs[p1['type']] t2 = vs[p2['type']] if t1 == t2 : return 0 if t1 > t2 : return 1 return -1 process.sort(_sort_process_) sshhost = server['sshhost'] psidbase = service['prockey'] mylog.log('生成服务器脚本 :', sshhost) basepath = service['paths']['script'] basepath = basepath + '/' + sshhost myfiles.make_path(basepath, True) shnames = [] myprocids = [] cpucount = server.get('cpucount', 0) if cpucount > 1 : cpucount = cpucount - 1 # cpu0始终不进行绑定,留给系统进行处理信息 binpath = service['paths']['bin'] mainpy = 'tyframework/service.py' if not myfiles.file_exists(binpath + '/' + mainpy) : mainpy = 'tyframework/server.py' if myfiles.file_exists(binpath + '/' + mainpy + 'c') : mainpy += 'c' for x in xrange(len(process)) : proce = process[x] ptype = proce['type'][0:4] if cpucount > 0 : taskset = ' taskset -c %d ' % ((x % cpucount) + 1) else: taskset = '' basefilename = ptype + '-' + service['name'] + '-' + str(service['id']) + '-' + '%05d' % (proce['id']) prockey = psidbase + basefilename proce['key'] = prockey serverprocid.append(prockey) myprocids.append(prockey) shname = basefilename + '.sh' shnames.append(shname) shscript = ''' #!/bin/bash # SERVICE FILE=%s export MAKE_TIME='%s' export PYPY='%s' export PATH_SCRIPT='%s' export PATH_LOG='%s' export PATH_WEBROOT='%s' export PYTHONPATH='%s' export PROCKEY='%s' export PROCCLASS='%s' export PROCLOG='%s' export HOOKPROCESS='%s' export TASKSET='%s' sh ${PATH_SCRIPT}/shscript/_start_.sh ''' % ( service['_servicefile_'], params['_make_time_'], service['pypy'], service['paths']['script'], service['paths']['log'], service['paths']['webroot'], service['paths']['bin'], prockey , mainpy, basefilename + '.log', service['hook'], taskset ) myfiles.write_file(basepath, shname, shscript) servershnames.append(shname) # 生成获取状态的脚本 allname = service['name'] + '-all.sh' shcontent = ''' #!/bin/bash # SERVICE FILE=%s export MAKE_TIME='%s' export PYPY='%s' export PATH_SCRIPT='%s' export PATH_LOG='%s' export PROCKEYS='%s' sh ${PATH_SCRIPT}/shscript/_status_.sh "$@" ''' % ( service['_servicefile_'], params['_make_time_'], service['pypy'], service['paths']['script'], service['paths']['log'], json.dumps(serverprocid) ) shname = 'status-' + allname myfiles.write_file(basepath, shname, shcontent) servershnames.append(shname) # 生成停止全部的脚本 allname = service['name'] + '-all.sh' shcontent = ''' #!/bin/bash # SERVICE FILE=%s export MAKE_TIME='%s' export PYPY='%s' export PATH_SCRIPT='%s' export PATH_LOG='%s' export PROCKEY='%s' sh ${PATH_SCRIPT}/shscript/_stop_.sh ''' % ( service['_servicefile_'], params['_make_time_'], service['pypy'], service['paths']['script'], service['paths']['log'], psidbase ) shname = 'kill-' + allname myfiles.write_file(basepath, shname, shcontent) servershnames.append(shname) # 生成启动全部的脚本 lines_remote = [] lines_local = [] for shname in shnames : lines_remote.append('nohup ${SHELL_FOLDER}/%s >>/tmp/_start_$$.log 2>&1 &' % (shname)) lines_local.append('${SHELL_FOLDER}/%s' % (shname)) shcontent = ''' #!/bin/bash # SERVICE FILE=%s export MAKE_TIME='%s' export PYPY='%s' export SHELL_FOLDER=$(cd `dirname ${0}`; pwd) export PATH_SCRIPT='%s' export PATH_LOG='%s' export PROCKEY='%s' export ALLNOHUP=${1} if [ "${ALLNOHUP}" = "allnohup" ] then export ALL_PROCKEYS='%s' %s sh ${PATH_SCRIPT}/shscript/_check_while1_.sh else %s fi ''' % ( service['_servicefile_'], params['_make_time_'], service['pypy'], service['paths']['script'], service['paths']['log'], psidbase, '|'.join(myprocids), '\n'.join(lines_remote), '\n'.join(lines_local) ) shname = 'start-' + allname myfiles.write_file(basepath, shname, shcontent) servershnames.append(shname) return 1
def action_make_compile_pyc(actparams): ''' 预编译所有的py文件到pyc,以便发现语法错误 ''' params = myhelper.action_common_init(actparams) if not params: return 0 service = params['service'] # 编译文件 mylog.log('开始预编译PY文件') compilepath = service['paths']['bin'] makesosh = myfiles.find_py_files(compilepath, 'makeso.sh') for msh in makesosh: cmd = 'sh %s/%s' % (compilepath, msh) mylog.log('编译SO文件 :', msh) status, output = commands.getstatusoutput(cmd) if status != 0: mylog.log('ERRROR !!', '工程so文件编译失败:', compilepath) mylog.log(output) return 0 mylog.log('编译PY文件 :', compilepath) pyfiles = myfiles.find_py_files(compilepath, '.py', True) content = '\n'.join(pyfiles) # 生成编译文件 cfilepath = '%s/c.py' % (compilepath) myfiles.write_file('', cfilepath, content) pypy = myhelper.get_env('PYPY_COMPILE', service['pypy']) cmd = 'export PYTHONPATH=%s; %s -tt %s' % (compilepath, pypy, cfilepath) status, output = commands.getstatusoutput(cmd) if status != 0: mylog.log('ERRROR !!', '工程py文件编译失败:', compilepath) mylog.log(output) return 0 commands.getstatusoutput('rm -fr ' + cfilepath) mylog.log('预编译PY文件成功!') return 1
def main(): params = parse_cmd_lines() needenv = False try: import redis except: needenv = True print 'ERROR !! python的运行环境缺少redis模块' try: import paramiko except: needenv = True print 'ERROR !! python的运行环境缺少paramiko模块' if needenv: print '请以root用户执行以下命令安装缺少模块:' print '' print 'sh ' + params['__pyscript_path__'] + '/shscript/install-env.sh' print '' return global tasks autobackup = params['autobackup'] if 'servicefile' in params: add_tasks('action_load_service') add_tasks('action_make_begin') add_tasks('action_clean_bin') add_tasks('action_make_copy_source') add_tasks('action_make_compile_pyc') add_tasks('action_make_configure') add_tasks('action_make_script') add_tasks('action_make_webroot') add_tasks('action_make_encrypt_pyc') add_tasks('action_make_end') action = params.get('action', '') if action == 'prepare': add_tasks('action_push_bin') add_tasks('action_stop') add_tasks('action_configure_install') if action == 'start': add_tasks('action_push_bin') add_tasks('action_stop') add_tasks('action_configure_install') add_tasks('action_start') if action == 'stop': add_tasks('action_stop') if action == 'clean': add_tasks('action_clean_log') add_tasks('action_clean_bin') if action == 'clean_all': add_tasks('action_clean_all') if len(tasks) == 0: return useage() ct = time.time() from _main_helper_ import mylog try: roopath = os.path.dirname(params['__source_path__']) mylog.open_log_file(roopath) mylog.log('操作根目录 :', roopath) mylog.log( '======================================================================' ) mylog.log('任务列表 :', tasks) mylog.log( '======================================================================' ) for x in xrange(len(tasks)): task = tasks[x] mylog.log( mylog.wrap_color_cyan('执行任务 [%d/%d] : %s' % (x + 1, len(tasks), task))) tfun = None taskstr = 'from _%s_ import %s as tfun' % (task, task) exec taskstr if tfun(params) != 1: return 1 mylog.log('') mylog.log('完成. 使用时间 %0.2f 秒' % (time.time() - ct)) mylog.log( '========================== done ============================================' ) return 0 finally: try: mylog.close_log_file() except: pass
def action_make_configure(actparams, pypath=None): ''' 读取本地当前的配置文件内容,生成配置JSON文件,生成配置装载的脚本 ''' params = myhelper.action_common_init(actparams) if not params: return 0 service = params['service'] # 每次执行配置文件时,使用__service__.json和__outputs__.json进行数据交换 compilepath = service['paths']['bin'] sfile = compilepath + '/__service__.json' myfiles.write_file('', sfile, json.dumps(service)) ofile = compilepath + '/__outputs__.json' myfiles.write_file('', ofile, json.dumps({})) i = 1 mcount = len(service['projects']) * 2 for proj in service['projects']: pyfile = proj['configure_py'] mylog.log('装载游戏配置 [%d/%d] :' % (i, mcount), pyfile) i += 1 basepy = myfiles.get_parent_dir(service['projects'][0]['configure_py']) if not __execute_game_config_py__(service, basepy, pyfile, sfile, ofile, pypath): return 0 pyfile = proj['configure_json'] mylog.log('装载游戏配置 [%d/%d] :' % (i, mcount), pyfile) i += 1 basepy = myfiles.get_parent_dir( service['projects'][0]['configure_json']) if not __execute_game_config_py__(service, basepy, pyfile, sfile, ofile, pypath): return 0 patchpy = myfiles.get_parent_dir(compilepath) + '/patch_config.py' if myfiles.file_exists(patchpy): cmd = 'pypy ' + patchpy + ' ' + ofile mylog.log('执行游戏配置文件补丁:', cmd) status, output = commands.getstatusoutput(cmd) for l in output.split('\n'): mylog.log(l) if status != 0: mylog.error('游戏配置文件补丁失败:', patchpy) mylog.error(status, output) return 0 outputs = myfiles.read_json_file(ofile) cmdlist = [] configuredatas = outputs.get('configuredatas', {}) for k, v in configuredatas.items(): cmdlist.append(['set', k, v]) if not __write_redis_config_script__(params, service, cmdlist): return 0 commands.getstatusoutput('rm -fr ' + sfile) commands.getstatusoutput('rm -fr ' + ofile) return 1
def tar_gzip_folders(params, pack_type, tar_dirs): service = params['service'] root_path = myfiles.get_parent_dir(params['__source_path__']) tar_paths = [] tar_prefix = 0 if tar_dirs & DIR_MAKE_BIN: tar_prefix |= DIR_MAKE_BIN tar_paths.append(service['paths']['bin']) if tar_dirs & DIR_MAKE_SCRIPT: tar_prefix |= DIR_MAKE_SCRIPT tar_paths.append(service['paths']['script']) if tar_dirs & DIR_MAKE_WEBROOT: tar_prefix |= DIR_MAKE_WEBROOT tar_paths.append(service['paths']['webroot']) if tar_dirs & DIR_SOURCE: tar_prefix |= DIR_SOURCE projs = service['projects'] for proj in projs: tar_paths.append(proj['path']) tar_paths.append(myfiles.get_parent_dir(params['__pyscript_path__'])) if tar_dirs & DIR_HOTFIX: tar_prefix |= DIR_MAKE_WEBROOT tar_paths.append(service['paths']['hotfix']) if len(tar_paths) == 0: raise Exception('没有指定要打包的目录!') if PACK_TYPE_TAR == pack_type or PACK_TYPE_TAR_GZIP == pack_type: ext_name = '.tar' elif PACK_TYPE_TGZ == pack_type: ext_name = '.tgz' else: raise Exception('打包类型错误') tartime = datetime.datetime.now().strftime('%Y%m%d-%H%M%S') tgzfile = str(tar_prefix) + '-' + tartime + ext_name backpath = service['paths']['backup'] myfiles.make_dirs(backpath) tgzfile = backpath + '/' + tgzfile if os.path.isfile(tgzfile) or os.path.isfile(tgzfile + '.gz'): mylog.log('打包成功 :', tgzfile) return tgzfile allpaths = [] for x in xrange(len(tar_paths)): p = tar_paths[x] p = p.replace(root_path, '.') if p[0] == '/': mylog.log('打包失败,原路径与当前路径不一致', tar_paths[x], root_path) return None allpaths.append(p) tarpath = ' '.join(allpaths) if PACK_TYPE_TAR == pack_type or PACK_TYPE_TAR_GZIP == pack_type: cmd = 'tar cf %s %s' % (tgzfile, tarpath) elif PACK_TYPE_TGZ == pack_type: cmd = 'tar cfz %s %s' % (tgzfile, tarpath) cmd = 'cd %s; %s' % (root_path, cmd) commands.getstatusoutput('rm -fr %s' % (tgzfile)) status, output = commands.getstatusoutput(cmd) if status != 0: mylog.error('打包失败!!', output) return None if not os.path.isfile(tgzfile): mylog.error('打包失败!! 找不到文件 :', tgzfile) return None if PACK_TYPE_TAR_GZIP == pack_type: commands.getstatusoutput('rm -fr %s.gz' % (tgzfile)) cmd = 'nohup gzip %s >/dev/null 2>&1 &' % (tgzfile) os.popen(cmd) tgzfile += '.gz' mylog.log('打包成功 :', tgzfile) return tgzfile
def __print_service_content__(params): service = params['service'] mylog.log( '================================================================================' ) mylog.log('Service Configer :', params['servicefile']) mylog.log('Python Exec :', service['pypy']) mylog.log( '--------------------------------------------------------------------------------' ) mylog.log('Run Mode :', service['mode']) mylog.log('Simulation :', service['simulation']) mylog.log('Game Name :', service['name']) mylog.log('Bin Path :', service['paths']['bin']) mylog.log('Log Path :', service['paths']['log']) mylog.log('BiReport Path :', service['paths']['bireport']) mylog.log('WebRoot Path :', service['paths']['webroot']) mylog.log('Script Path :', service['paths']['script']) mylog.log('Http Game :', service['http.game']) mylog.log('Http Download :', service['http.download']) mylog.log('Http SDK :', service['http.sdk']) mylog.log( '--------------------------------------------------------------------------------' ) bireports = service.get('bireport.server', None) if bireports: for k, v in bireports.items(): mylog.log('BiReport : ', k, '->', v) else: mylog.log('BiReport : Use Log File') mylog.log( '--------------------------------------------------------------------------------' ) projects = service.get('projects') for x in xrange(len(projects)): proj = projects[x] mylog.log('Project Path : %s' % (proj['path'])) mylog.log('Project Configure PY : %s' % (proj['configure_py'])) mylog.log('Project Configure JSON : %s' % (proj['configure_json'])) mylog.log( '--------------------------------------------------------------------------------' ) mkeys = service['mysql'].keys() mkeys.sort() for dbalias in mkeys: mysql = service['mysql'][dbalias] slen = '%s@%s:%d/%s' % (mysql['user'], mysql['host'], mysql['port'], mysql['dbname']) mylog.log('MySql For %-10s :' % (dbalias), slen) mylog.log( '--------------------------------------------------------------------------------' ) mkeys = service['redis'].keys() mkeys.sort() for dbalias in mkeys: rd = service['redis'][dbalias] if isinstance(rd, dict): slen = '%s:%d:%d' % (rd['host'], rd['port'], rd['dbid']) mylog.log('Redis For %-14s :' % (dbalias), slen) mylog.log( '--------------------------------------------------------------------------------' ) for dbalias in mkeys: rdatas = service['redis'][dbalias] if isinstance(rdatas, list): for x in xrange(len(rdatas)): rd = rdatas[x] slen = '%s:%d:%d %d' % (rd['host'], rd['port'], rd['dbid'], rd['useridmod']) mylog.log( 'Redis For %-14s %d/%d :' % (dbalias, x, len(rdatas)), slen) mylog.log( '--------------------------------------------------------------------------------' ) mylog.log( 'Servers N/C : PC : INTERNET : INTRANT : SSH : ID ' ) servers = service['servers'] for x in xrange(len(servers)): slen = '%02d : %15s : %15s : %15s : %s' % ( servers[x]['proccount'], servers[x]['internet'], servers[x]['intrant'], servers[x]['sshhost'], servers[x]['id']) mylog.log('Servers %02d/%02d :' % (x, len(servers)), slen) mylog.log( '--------------------------------------------------------------------------------' ) mylog.log( 'Process N/C : UDP : TCP : HTTP : ID : TYPE : SERVER' ) process = service['process'] for x in xrange(len(process)): proc = process[x] udp = 0 if 'udp' in proc: udp = proc['udp'] tcp = 0 if 'tcp' in proc: tcp = proc['tcp'] if 'ssl' in proc: tcp = proc['ssl'] http = 0 if 'http' in proc: http = proc['http'] slen = '%5d : %5d : %5d : %5d : %8s : %s' % ( udp, tcp, http, proc['id'], proc['type'], proc['server']) mylog.log('Process %03d/%03d :' % (x, len(process)), slen) mylog.log( '================================================================================' )
def __check_configure_default_value__(params, service): srv0 = service['servers'][0] internet = srv0['internet'] intrant = srv0.get('intrant', internet) redis_host = service.get('redis.host', intrant) redis_port = int(service.get('redis.port', 6379)) redis_dbid = int(service.get('redis.dbid', 0)) service['configuer.redis.host'] = redis_host service['configuer.redis.port'] = redis_port service['configuer.redis.dbid'] = redis_dbid if not 'corporation' in service: service['corporation'] = 'tuyoo' corporation = service['corporation'] corporationdefault = None mylog.log('当前定义的公司为 :', corporation) try: make_default_setting = None exec('from _corporation_.%s import make_default_setting' % (corporation)) corporationdefault = make_default_setting(service) except: return mylog.error('配置文件 : servers中公司的定义不在控制范围内 ' + corporation) from _corporation_.tester import make_tester_default_setting testerdefault = make_tester_default_setting(service) mode = service['mode'] if mode in (1, 2) and not 'redis.fixhead' in service: service['redis.fixhead'] = service['name'] + '.' if 'redis.fixhead' in service: if service['redis.fixhead'][-1] != '.': service['redis.fixhead'] = service['redis.fixhead'] + '.' if not 'encodepyc' in service: service['encodepyc'] = 0 if len(str(service.get('pypy', ''))) <= 0: if not service['encodepyc']: service['pypy'] = 'pypy' else: service['pypy'] = 'pypy-enc' if not 'mysql' in service: if mode in (1, 2): service['mysql'] = corporationdefault['mysql'] else: service['mysql'] = testerdefault['mysql'] if not 'redis' in service: if mode in (1, 2): service['redis'] = corporationdefault['redis'] else: service['redis'] = testerdefault['redis'] root_path = params['__source_path__'] service['paths'] = { 'bin': myfiles.normpath(root_path + '/../bin'), 'log': myfiles.normpath(root_path + '/../logs'), 'bireport': myfiles.normpath(root_path + '/../bireport'), 'script': myfiles.normpath(root_path + '/../script'), 'webroot': myfiles.normpath(root_path + '/../webroot'), 'backup': myfiles.normpath(root_path + '/../backup'), 'hotfix': myfiles.normpath(root_path + '/../hotfix'), } if service.get('sharelog', 0): service['paths']['log'] = '/home/share/logs_' + str(service['id']) service['paths']['bireport'] = '/home/share/bireport_' + str( service['id']) if not 'process' in service: service['process'] = testerdefault['process'] httpport = service.get('http.port', redis_port + 2) firsthttpproc = None httpcount = 0 process = service['process'] for proc in process: if proc['type'] == 'http': if proc.get('http', 0) <= 0: proc['http'] = httpport httpport += 1 httpcount += 1 if firsthttpproc == None: firsthttpproc = proc connport = service.get('tcp.port', httpport) for proc in process: if proc['type'] == 'conn': if proc['id'] == 10: # 机器人接入,机器人服务修改后,需要去掉此处特殊处理 proc['ssl'] = connport else: proc['tcp'] = connport connport += 1 udpport = service.get('udp.port', connport) if udpport % 2 != 0: # UDP端口,偶数为接收,奇数为发送 udpport += 1 for proc in process: proc['udp'] = udpport udpport += 2 if not 'config' in service: service['config'] = 'game/default.py' if not firsthttpproc: return mylog.error('配置文件 : 必须定义至少一个HTTP进程') http_srv = None for srv in service['servers']: if srv['id'] == firsthttpproc['server']: http_srv = srv break http_internet = http_srv['internet'] http_port = firsthttpproc['http'] if not 'http.game' in service: if mode == 1: service['http.game'] = 'http://%s' % (http_internet) else: if httpcount == 1: service['http.game'] = 'http://%s:%d' % (http_internet, http_port) else: service['http.game'] = 'http://%s' % (http_internet) if not 'http.download' in service: if mode == 1: service['http.download'] = 'http://%s' % (http_internet) else: if httpcount == 1: service['http.download'] = 'http://%s:%d' % (http_internet, http_port) else: service['http.download'] = 'http://%s' % (http_internet) if not 'http.sdk' in service: if mode in (1, 2): service['http.sdk'] = corporationdefault['http.sdk'] else: service['http.sdk'] = service['http.game'] rconf = service['redis']['config'] service['prockey'] = '%s:%d:%d:' % (rconf['host'], rconf['port'], rconf['dbid']) hook_process = service.get('hook', service['mode'] in (1, 2)) if hook_process: hook_process = 'hook' else: hook_process = 'nohook' service['hook'] = hook_process if not 'bicollect.server' in service: if mode in (1, 2): service['bicollect.server'] = corporationdefault[ 'bicollect.server'] else: service['bicollect.server'] = testerdefault['bicollect.server'] return 1
def __check_configure_for_servers__(params, service): ids = {} internets = {} intrants = {} if not 'servers' in service: return mylog.error('配置文件 : 必须定义servers') servers = service['servers'] if not isinstance(servers, list) or len(servers) <= 0: return mylog.error('配置文件 : servers定义为空或非数组') for x in xrange(len(servers)): server = servers[x] if 'internet' not in server: return mylog.error('配置文件 : servers[', x, ']的对外定义internet丢失') if server['internet'] not in internets: internets[server['internet']] = 1 else: return mylog.error('配置文件 : servers[', x, ']的internet [', server['internet'], '] 重复') if 'intrant' not in server: server['intrant'] = server['internet'] if server['intrant'] not in intrants: intrants[server['intrant']] = 1 else: return mylog.error('配置文件 : servers[', x, ']的intrant [', server['intrant'], '] 重复') if 'id' not in server: return mylog.error('配置文件 : servers[', x, ']的id丢失') if server['id'] not in ids: ids[server['id']] = 1 else: return mylog.error('配置文件 : servers[', x, ']的id [', server['id'], '] 重复') server['user'] = myhelper.whoami() if 'pwd' not in server: server['pwd'] = '' if not 'port' in server: server['port'] = 22 if not 'cpucount' in server: server['cpucount'] = 0 if not isinstance(server['port'], int) or server['port'] < 0: return mylog.error('配置文件 : servers[', x, ']的SSH登录端口不正确') if not isinstance(server['cpucount'], int) or server['cpucount'] < 0: return mylog.error('配置文件 : servers[', x, ']的CUP数量定义不正确') if not isinstance(server['user'], (str, unicode)): return mylog.error('配置文件 : servers[', x, ']的登陆用户名定义不正确') if not isinstance(server['pwd'], (str, unicode)): return mylog.error('配置文件 : servers[', x, ']的登陆密码定义不正确') if not isinstance(server['id'], (str, unicode)): return mylog.error('配置文件 : servers[', x, ']的服务器ID定义不正确') if not isinstance(server['intrant'], (str, unicode)): return mylog.error('配置文件 : servers[', x, ']的内网地址定义不正确') if not isinstance(server['internet'], (str, unicode)): return mylog.error('配置文件 : servers[', x, ']的对外地址定义不正确') islocalhost0 = myhelper.is_local_ip(server['internet']) islocalhost1 = myhelper.is_local_ip(server['intrant']) if islocalhost0 or islocalhost1: server['sshhost'] = server['intrant'] server['localhost'] = 1 else: if myhelper.check_ssh_port(server['intrant'], server['port']): server['sshhost'] = server['intrant'] else: if myhelper.check_ssh_port(server['internet'], server['port']): server['sshhost'] = server['internet'] else: return mylog.error('配置文件 : servers[', x, ']SSH端口检测失败') mylog.log('检查服务器SSH : %02d/%02d %-16s %s' % (x, len(servers), server['id'], server['sshhost'])) return 1