def run(options): global curdate,curtime,sqldir sqldir = options.sqldir.strip() state.ignoreErrorHost = True backstageDB = arg.gameOption("backstage_db") headTag = arg.gameOption("backstage_tag") is_mobile = arg.gameOption("is_mobile",type="bool") if is_mobile: partnersType = 2 else: partnersType = 1 backstageIp = arg.gameOption("backstage") #serverlistList = serverlist.serverRange(backstageDB,headTag,partnersType,"main",backstageIp,serverlist='.*') serverlistList = arg.getserverlist(ServerList=".*") #serverlistList = [["feiliu_88888",'10.6.196.30'],['feiliu_88889','10.6.196.30']] curdate = datetime.datetime.now().strftime("%Y%m%d") curtime = datetime.datetime.now().strftime("%Y%m%d%H%M%S") ftpdir = "/app/online/%s/sql/%s/%s/"%(state.game,state.language,sqldir) if not os.path.isdir(ftpdir): print "%s不存在!"%ftpdir sys.exit(1) os.chdir(ftpdir) result=os.system("dos2unix md5.txt && chown virtual_user.virtual_user md5.txt && md5sum -c md5.txt") if result !=0: print "md5校验失败,请确认!" sys.exit(1) if os.path.exists( sqldir + ".zip"): status = os.system("unzip *.zip") if status != 0: print "解压%s.zip失败!"%sqldir sys.exit(1) else: if os.path.exists(sqldir): os.chdir(sqldir) newserverlist = [] for file in os.listdir("."): if file.endswith(".sql"): servername = os.path.splitext(file)[0] for item in serverlistList: if state.game + "_" + item[0] == servername: newserverlist.append(item) break else: print "%s不存在该服务器列表"%servername state.errorResult[servername] = "不存在该服务器" continue #sys.exit(1) state.servers = newserverlist ccthread.run(executeSql) if len(state.errorHost) > 0: print "*" * 40,"连接失败ip如下:" print state.errorHost.keys() if len(state.errorResult) > 0: print "*"*40,"执行失败游戏服如下:" print arg.getSpecialServerIp(state.servers,state.errorResult.keys()) print "执行游戏服数:",len(state.servers) print "执行失败数 :",len(state.errorResult) print "连接失败ip数:",len(state.errorHost) if len(state.errorResult) > 0 or len(state.errorHost) > 0: sys.exit(1)
def mobileWwwTestEnvironmentDel(ip): wwwIp = arg.gameOption("mobile_www_ip") wwwBackupIp = arg.gameOption("mobile_www_backup_ip", default="") wwwPort = arg.gameOption("mobile_www_port", default=22) if wwwBackupIp != "": addWhiteIp(wwwIp, wwwPort, ip) addWhiteIp(wwwBackupIp, wwwPort, ip) else: addWhiteIp(wwwIp, wwwPort, ip)
def mobileWwwTestEnvironmentDel(ip): wwwIp = arg.gameOption("mobile_www_ip") wwwBackupIp = arg.gameOption("mobile_www_backup_ip",default="") wwwPort = arg.gameOption("mobile_www_port", default=22) if wwwBackupIp != "": addWhiteIp(wwwIp,wwwPort,ip) addWhiteIp(wwwBackupIp,wwwPort,ip) else: addWhiteIp(wwwIp,wwwPort,ip)
def download_from_resource(game, file): remote_dir, filename = os.path.split(file) mk_remote_dir(REMOTE_DIR) with cd(remote_dir): server_name = gameOption('www_header') for each_file in [filename, 'md5.txt']: run('''{} --header="Host:{}" http://{}/{}/{}/{}/{}'''.format( WGET, server_name, gameOption('www_ip'), game, RELEASE_TYPE, TIMESTAMP, each_file)) run('dos2unix md5.txt && md5sum -c md5.txt')
def getServerWip(interIp): get_outip_from_zichan = arg.gameOption("get_outip_from_zichan",type="bool",default=False) iplist = [] if not get_outip_from_zichan: sshobj = ssh.ssh(interIp.strip()) cmd = "/sbin/ifconfig -a | grep 'inet addr'|cut -d':' -f2|cut -d' ' -f1" status,r,stderr = sshobj.cmd(cmd) if status == 0: for ip in r.split("\n"): if re.search(r"^(10\.|192\.168|172\.1[6-9]\.|172\.2[0-9]\.|172\.3[01]\.|169\.254\.|127\.)",ip) or ip.strip() == "": continue else: iplist.append(ip.strip()) if len(iplist) == 0: #status,r1,stderr = sshobj.cmd("curl -s ip.cn | awk '{print $2}' | sed 's/[^0-9\\.]*\\(.*\\)/\\1/g'") status,r1,stderr = sshobj.cmd("curl -s ipip.net | awk '{print $2}' | sed 's/[^0-9\\.]*\\(.*\\)/\\1/g'") if check.checkIp(r1) : iplist.append(r1.strip()) else: print "[%s] out:%serr:%s"%(cmd,out,err) else: hsot = arg.mainOption("zichan_host") user = arg.mainOption("zichan_user") pwd = arg.mainOption("zichan_pwd") port = arg.mainOption("zichan_port",type="int") db = arg.mainOption("zichan_db") my = mysql.mysql(hsot,user,pwd,db,port) mylist = my.query("select asset_outip.ip,isp from asset_inip join asset_outip on asset_outip.did = asset_inip.did and asset_inip.ip = '%s'"%interIp) for i in mylist: if i[1].strip() == "电信": iplist.insert(0,i[0].strip()) else: iplist.append(i[0].strip()) pass return iplist
def get_test_server_info(): """ 读取项目配置文件里面的参数 Get all game info. It will return a dict like: { 'astd_17wan_1' : '10.6.120.23', 'astd_37wan_98': '10.4.5.5', . . . 'astd_37wan_8' : '10.4.5.15' } """ _server_info1 = eval(gameOption('server_list', default='{}')) #_server_info2 = eval(gameOption('pay_proxy', default='{}')) _server_info = _server_info1.copy() #_server_info.update(_server_info2) server_info = { '{}_{}'.format(GAME, each): _server_info[each] for each in _server_info } return server_info
def test_server_info(): _server_info = eval(gameOption('server_list')) server_info = { '{}_{}'.format(GAME, each): _server_info[each] for each in _server_info } return server_info
def upload_to_resource_server(game, file): dir, filename = os.path.split(file) resource_dir = '/app/www/{}/{}/{}'.format(game, RELEASE_TYPE, TIMESTAMP) resource_ip = gameOption('www_ssh_ip') execute(mk_remote_dir, resource_dir, hosts=[resource_ip]) with lcd(dir), settings(host_string=resource_ip): put(filename, resource_dir) put('md5.txt', resource_dir)
def test_pay_proxy_info(): _server_info = {} for each_part in ['pay_proxy', 'voice']: tmp_value = eval(gameOption(each_part, default='{}')) _server_info.update(tmp_value) server_info = {'{}_{}'.format(GAME, each): _server_info[each] for each in _server_info} return server_info
def download_from_resource(game, file): remote_dir, filename = os.path.split(file) mk_remote_dir(REMOTE_DIR) with cd(remote_dir): server_name = gameOption('www_header') for each_file in [filename, 'md5.txt']: run('''{} --header="Host:{}" http://{}/{}/{}/{}/{}'''.format(WGET, server_name, gameOption('www_ip'), game, RELEASE_TYPE, TIMESTAMP, each_file)) run('dos2unix md5.txt && md5sum -c md5.txt')
def test_pay_proxy_info(): _server_info = {} for each_part in ['pay_proxy', 'voice']: tmp_value = eval(gameOption(each_part, default='{}')) _server_info.update(tmp_value) server_info = { '{}_{}'.format(GAME, each): _server_info[each] for each in _server_info } return server_info
def addWhiteIp(wwwIp, wwwPort, ipList): file = "/app/nginx/conf/whitelist/%s_%s_whitelist.conf" % (state.game, state.language) basename = os.path.basename(file) bakdir = "/app/opbak/nginxconf/" curtime = datetime.datetime.now().strftime('%Y%m%d_%H%M%S') backfile = "%s_%s" % (basename, curtime) if ipList == '' or not ipList: print "需要添加的白名单ip为空,请确认!" sys.exit(1) wwwIpSshAstd = ssh.ssh(wwwIp, wwwPort) #wwwIpSshRoot = ssh.ssh(wwwIp,wwwPort,user="******") wwwIpSshAstd.exitcmd("mkdir -p %s" % bakdir) wwwIpSshAstd.exitcmd("cp %s %s/%s" % (file, bakdir, backfile)) ipList = ipList.split(',') wwwForward = arg.gameOption("mobile_www_forward", type="bool", default=False) if wwwForward: remoteTag = "http_x_forwarded_for" else: remoteTag = "remote_addr" for whiteIp in ipList: content = ''' if ( $%s = %s ) { set $gototest 1; }''' % (remoteTag, whiteIp) wwwIpSshAstd.exitcmd("echo '%s' >> %s" % (content, file)) wwwIpSshAstd.exitcmd("sed -i '/^$'/d %s" % file) #out = wwwIpSshRoot.exitcmd("/app/nginx/sbin/nginx -t 2>&1") out = wwwIpSshAstd.exitcmd("sudo /app/nginx/sbin/nginx -t 2>&1") if not re.search( "nginx: the configuration file (/app/nginx/|/usr/local/nginx/)conf/nginx.conf syntax is ok", out ) or not re.search( "nginx: configuration file (/app/nginx/|/usr/local/nginx/)conf/nginx.conf test is successful", out): print "nginx语法错误" print out wwwIpSshAstd.exitcmd("cp %s/%s %s" % (bakdir, backfile, file)) else: #wwwIpSshRoot.exitcmd("/app/nginx/sbin/nginx -s reload") wwwIpSshAstd.exitcmd("sudo /app/nginx/sbin/nginx -s reload") print "添加白名单完成" result = wwwIpSshAstd.exitcmd( "grep 'if' %s | awk -F '[=)]' '{print $2}'" % file) print "已有白名单ip如下:\n%s" % result
def get_test_server_info(game): """ Get all game info. It will get a dict like: { 'astd_17wan_1' : '10.6.120.23', 'astd_37wan_98': '10.4.5.5', . . . 'astd_37wan_8' : '10.4.5.15' } """ _server_info = {} for each_part in ['server_list', 'pay_proxy', 'voice']: tmp_value = eval(gameOption(each_part, default='{}')) _server_info.update(tmp_value) server_info = {'{}_{}'.format(game, each): _server_info[each] for each in _server_info} return server_info
def getServerWip(interIp): get_outip_from_zichan = arg.gameOption("get_outip_from_zichan", type="bool", default=False) iplist = [] if not get_outip_from_zichan: sshobj = ssh.ssh(interIp.strip()) cmd = "/sbin/ifconfig -a | grep 'inet addr'|cut -d':' -f2|cut -d' ' -f1" status, r, stderr = sshobj.cmd(cmd) if status == 0: for ip in r.split("\n"): if re.search( r"^(10\.|192\.168|172\.1[6-9]\.|172\.2[0-9]\.|172\.3[01]\.|169\.254\.|127\.)", ip) or ip.strip() == "": continue else: iplist.append(ip.strip()) if len(iplist) == 0: #status,r1,stderr = sshobj.cmd("curl -s ip.cn | awk '{print $2}' | sed 's/[^0-9\\.]*\\(.*\\)/\\1/g'") status, r1, stderr = sshobj.cmd( "curl -s ipip.net | awk '{print $2}' | sed 's/[^0-9\\.]*\\(.*\\)/\\1/g'" ) if check.checkIp(r1): iplist.append(r1.strip()) else: print "[%s] out:%serr:%s" % (cmd, out, err) else: hsot = arg.mainOption("zichan_host") user = arg.mainOption("zichan_user") pwd = arg.mainOption("zichan_pwd") port = arg.mainOption("zichan_port", type="int") db = arg.mainOption("zichan_db") my = mysql.mysql(hsot, user, pwd, db, port) mylist = my.query( "select asset_outip.ip,isp from asset_inip join asset_outip on asset_outip.did = asset_inip.did and asset_inip.ip = '%s'" % interIp) for i in mylist: if i[1].strip() == "电信": iplist.insert(0, i[0].strip()) else: iplist.append(i[0].strip()) pass return iplist
def get_test_server_info(game): """ Get all game info. It will get a dict like: { 'astd_17wan_1' : '10.6.120.23', 'astd_37wan_98': '10.4.5.5', . . . 'astd_37wan_8' : '10.4.5.15' } """ _server_info = {} for each_part in ['server_list', 'pay_proxy', 'voice']: tmp_value = eval(gameOption(each_part, default='{}')) _server_info.update(tmp_value) server_info = { '{}_{}'.format(game, each): _server_info[each] for each in _server_info } return server_info
def get_test_server_info(): """ 读取项目配置文件里面的参数 Get all game info. It will return a dict like: { 'astd_17wan_1' : '10.6.120.23', 'astd_37wan_98': '10.4.5.5', . . . 'astd_37wan_8' : '10.4.5.15' } """ _server_info1 = eval(gameOption('server_list', default='{}')) #_server_info2 = eval(gameOption('pay_proxy', default='{}')) _server_info = _server_info1.copy() #_server_info.update(_server_info2) server_info = {'{}_{}'.format(GAME, each): _server_info[each] for each in _server_info} return server_info
def addWhiteIp(wwwIp,wwwPort,ipList): file = "/app/nginx/conf/whitelist/%s_%s_whitelist.conf"%(state.game,state.language) basename = os.path.basename(file) bakdir = "/app/opbak/nginxconf/" curtime=datetime.datetime.now().strftime('%Y%m%d_%H%M%S') backfile="%s_%s"%(basename,curtime) if ipList == '' or not ipList: print "需要添加的白名单ip为空,请确认!" sys.exit(1) wwwIpSshAstd = ssh.ssh(wwwIp,wwwPort) #wwwIpSshRoot = ssh.ssh(wwwIp,wwwPort,user="******") wwwIpSshAstd.exitcmd("mkdir -p %s"%bakdir) wwwIpSshAstd.exitcmd("cp %s %s/%s"%(file,bakdir,backfile)) ipList = ipList.split(',') wwwForward = arg.gameOption("mobile_www_forward",type="bool",default=False) if wwwForward: remoteTag = "http_x_forwarded_for" else: remoteTag = "remote_addr" for whiteIp in ipList: content = ''' if ( $%s = %s ) { set $gototest 1; }'''%(remoteTag,whiteIp) wwwIpSshAstd.exitcmd("echo '%s' >> %s"%(content,file)) wwwIpSshAstd.exitcmd("sed -i '/^$'/d %s"%file) #out = wwwIpSshRoot.exitcmd("/app/nginx/sbin/nginx -t 2>&1") out = wwwIpSshAstd.exitcmd("sudo /app/nginx/sbin/nginx -t 2>&1") if not re.search("nginx: the configuration file (/app/nginx/|/usr/local/nginx/)conf/nginx.conf syntax is ok",out) or not re.search("nginx: configuration file (/app/nginx/|/usr/local/nginx/)conf/nginx.conf test is successful",out): print "nginx语法错误" print out wwwIpSshAstd.exitcmd("cp %s/%s %s"%(bakdir,backfile,file)) else: #wwwIpSshRoot.exitcmd("/app/nginx/sbin/nginx -s reload") wwwIpSshAstd.exitcmd("sudo /app/nginx/sbin/nginx -s reload") print "添加白名单完成" result=wwwIpSshAstd.exitcmd("grep 'if' %s | awk -F '[=)]' '{print $2}'"%file) print "已有白名单ip如下:\n%s"%result
def getssh(ip): import arg closegw = arg.gameOption("backstage_close_gw",type="bool",default=False) sshObj = ssh.ssh(ip,closegw=closegw) return sshObj
def test_server_info(): _server_info = eval(gameOption('server_list')) server_info = {'{}_{}'.format(GAME, each): _server_info[each] for each in _server_info} return server_info