def __execute_game_config_py__(service, basepy, gamefile, sfile, ofile, extrapath=None): compilepath = service['paths']['bin'] shtemplate = ''' #!/bin/bash cd %s pwd export CONFIGURE_SERVICE_FILE=%s export CONFIGURE_OUTPUTS_FILE=%s export PYTHONPATH=%s:${PYTHONPATH} export PYPY=%s ${PYPY} %s exit $? ''' pypath = '%s:%s:%s' % (compilepath, basepy, myfiles.get_parent_dir(gamefile)) if extrapath: pypath = '%s:%s' % (pypath, ':'.join(extrapath)) content = shtemplate % (compilepath, sfile, ofile, pypath, myhelper.get_env('PYPY_COMPILE', service['pypy']), gamefile) shfile = compilepath + '/_temp_.sh' myfiles.write_file('', shfile, content) status, output = commands.getstatusoutput('sh ' + shfile) if status != 0: mylog.error('游戏配置文件读取、运行失败:', gamefile) mylog.error(status, output) return 0 commands.getstatusoutput('rm -fr ' + shfile) 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 __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 __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 __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_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