def recoverByProject(failurIptemp, recoverIptemp): global recoverIpSsh, failurIpSsh, recoverIpSshAstd, mysqldir, failurIp, recoverIp, failurIpSshAstd, allservers, wips, ismobile, dianxinIp, liantongIp, header, backstage_interface_url, curdir, recovermysqldir, dnsgame dnsgame = gameOption("dns_game", default=state.game) failurIp = failurIptemp recoverIp = recoverIptemp curdir = os.path.abspath(os.path.dirname(__file__)) project = state.game recoverIpSsh = ssh.ssh(recoverIp, user="******") failurIpSsh = ssh.ssh(failurIp, user="******") recoverIpSshAstd = ssh.ssh(recoverIp) failurIpSshAstd = ssh.ssh(failurIp) mysqldir = cmd( failurIpSsh, "grep '^innodb_data_home_dir' /etc/my.cnf | cut -d '=' -f 2|xargs echo" ).strip() recovermysqldir = cmd( recoverIpSsh, "grep '^innodb_data_home_dir' /etc/my.cnf | cut -d '=' -f 2|xargs echo" ).strip() stop_service() donwloadMysql() downloadNginx() download(state.game) logCopy() change_configfile() start_service() startGame() backstageDB = gameOption("backstage_db") headTag = gameOption("backstage_tag") ismobile = gameOption("is_mobile", type="bool") if ismobile: partnersType = 2 else: partnersType = 1 backstageIp = gameOption("backstage") header = {"host": gameOption("backstage_header")} backstage_interface_url = gameOption("backstage_interface_url") allservers = serverlist.getRecoverServerList(backstageDB, headTag, partnersType, backstageIp, failurIp) wips = getip.getServerWip(recoverIp) if len(wips) == 0: print "ERROR: 获取外网ip失败!" else: dianxinIp = wips[0] if len(wips) == 2: liantongIp = wips[1] else: liantongIp = wips[0] backstageChange() dnsChange() faileDirs = cmd(failurIpSshAstd, "find /app/ -maxdepth 1 -type d").split("\n") recoverDirs = cmd(recoverIpSshAstd, "find /app/ -maxdepth 1 -type d").split("\n") d1 = set(faileDirs).difference(set(recoverDirs)) if len(d1) > 0: print "故障机器目录如下,请确认是否已完全迁移!" print d1
def addwhiteip(iplist, yx): if not check.checkIpList(iplist): raise Exception("iplist 格式不正确:%s" % iplist) backstageDB = gameOption("backstage_db") headTag = gameOption("backstage_tag") ismobile = gameOption("is_mobile", type='bool') lyWhiteIpKey = gameOption("lyWhiteIpKey") if ismobile: partnersType = 2 else: partnersType = 1 backstageIp = gameOption("backstage") mixservers = serverlist.serverRange(backstageDB, headTag, partnersType, "mix", backstageIp, serverlist="%s_.*" % yx) resultStatus = True if len(mixservers) == 0: raise Exception("没有获取到游戏列表,请确认!") state.servers = mixservers state.ignoreErrorHost = True ccthread.run(updateProperties, iplist, yx, lyWhiteIpKey) proxy_pay1_ip = gameOption("proxy_pay1_ip", default="") proxy_pay2_ip = gameOption("proxy_pay2_ip", default="") proxy_pay1 = gameOption("proxy_pay1", default="") proxy_pay2 = gameOption("proxy_pay2", default="") if proxy_pay1.strip() != "": proxy_pay1_ssh = ssh.ssh(proxy_pay1_ip) updateProxy(proxy_pay1_ssh, proxy_pay1_ip, proxy_pay1, iplist, yx, lyWhiteIpKey) if proxy_pay2.strip() != "": proxy_pay2_ssh = ssh.ssh(proxy_pay2_ip) updateProxy(proxy_pay2_ssh, proxy_pay2_ip, proxy_pay2, iplist, yx, lyWhiteIpKey) if len(state.errorResult) > 0: resultStatus = False print "\n", "*" * 50 for i in state.errorResult: print "[%s] %s" % (i, state.errorResult[i]) print "\n", "*" * 50 print "更新服务器数:%d" % len(mixservers) print "更新失败数:%d" % len(state.errorResult) print "连接失败服务器数量:%d" % len(state.errorHost) try: import template templateObj = template.template(state.game, state.language, mixservers[0][0], ip=mixservers[0][1]) templateObj.updateServerProperties() print "更新模板成功!" except Exception, e1: resultStatus = False print "ERROR:模板更新失败!msg:%s" % str(e1)
def recoverByMysql(failurIptemp,recoverDate,recoverfile): global recoverIpSsh,failurIpSsh,failurIpSshAstd,mysqldir,failurIp,curdir,recovermysqldir,backup_date,filename,recoverdate,nowdate,allservers,database_list,mysql_recover_dir,backstage_interface_url,header,ismobile,dnsgame dnsgame = gameOption("dns_game",state.game) backstage_interface_url = gameOption("backstage_interface_url") header = gameOption("backstage_header") backstageIp = gameOption("backstage") backstageIpSsh = ssh.ssh(backstageIp,user="******") backstageDb = gameOption("backstage_db") headTag = gameOption("backstage_tag") ismobile = gameOption("is_mobile",type="bool") if ismobile: partnersType = 2 else: partnersType = 1 failurIp = failurIptemp curdir = os.path.abspath(os.path.dirname(__file__)) filename = recoverfile f = open(filename).readlines() database_list = [] for i in f: if i.strip() == "": continue i_split = i.split("@") database_list.append(i_split[0].strip()) project = state.game backup_date = recoverDate mysql_recover_dir = "/app/opbak/mysqlrecover" recoverdate = datetime.datetime.strptime(backup_date,"%Y-%m-%d").strftime("%Y%m%d") nowdate = datetime.datetime.now().strftime("%Y-%m-%d") failurIpSsh = ssh.ssh(failurIp,user="******") failurIpSshAstd = ssh.ssh(failurIp) mysqldir = cmd(failurIpSsh,"grep '^innodb_data_home_dir' /etc/my.cnf | cut -d '=' -f 2|xargs echo").strip() #recovermysqldir = cmd(recoverIpSsh,"grep '^innodb_data_home_dir' /etc/my.cnf | cut -d '=' -f 2|xargs echo").strip() allservers = serverlist.getRecoverServerList(backstageDb,headTag,partnersType,backstageIp,failurIp) wip = getip.getServerWip(failurIp) if len(wip) >0 : olddianxinip = wip[0] else: raise Exception("获取外网ip失败!") #游戏还原 createBinlog() threads = [] for line in f: s = line.split("@") servername = s[0].strip() ip = s[1].strip() t = threading.Thread(target=recoverByGame,args=(servername,ip,)) threads.append(t) t.start() for i in threads: i.join()
def recoverByGame(servername,ip): stopGame(servername) if ip.strip() == failurIp.strip(): localRecover(servername) startService(servername,failurIpSshAstd) else: recoverSsh = ssh.ssh(ip,user="******") recoverSshAstd = ssh.ssh(ip) recover(servername,recoverSsh,recoverSshAstd,ip) backstageChange(servername,ip) dnsChange(servername,ip) copyNginx(servername,ip,recoverSsh) #selectPort(servername,ip,recoverSshAstd) startService(servername,recoverSshAstd)
def __init__(self,game,language,servername,ip=None,port=22,mainServername=None): #logging.basicConfig(level=logging.INFO, # format='%(asctime)s %(levelname)s %(message)s', # datefmt='%Y-%m-%d %H:%M:%S', # ) self.game = game self.language = language self.servername = servername self.ip = ip self.rootDir = mainOption("rootdir").replace("${game}",game) self.commonPath = "newserver/%s/common"%language self.templatePath = "newserver/%s/template"%language self.propertiesPath = "newserver/%s/properties"%language self.wwwPath = "newserver/%s/www"%language self.yx,self.quhao = getYxNum(servername) if not check.nullCheck(mainServername): self.mainServer = game + "_" + mainServername else: self.mainServer = None if not check.checkServer(str(servername)): print "请输入正确的游戏服名称" #logging.error("请输入正确的游戏服名称") sys.exit(1) #if not check.checkIp(str(ip)): if ip == None: backstage_db = gameOption("backstage_db") backstage_tag = gameOption("backstage_tag") is_mobile = gameOption("is_mobile",type="bool") if is_mobile: partnersType = 2 else: partnersType = 1 backstage_ip = gameOption("backstage") print backstage_db,backstage_tag,partnersType,"mix",backstage_ip,servername serverList = serverlist.serverRange(backstage_db,backstage_tag,partnersType,"mix",backstage_ip,serverlist=servername) if len(serverList) != 1: print "ERROR: 获取服务器ip失败或者不唯一",serverList sys.exit(1) else: self.ip = serverList[0][1] self.ssh = ssh.ssh(self.ip,port=port) self.www_dir_type = gameOption("www_dir_type") www_ip = gameOption("www_ip") www_ssh_ip = gameOption("www_ssh_ip") www_port = gameOption("www_port",default="80") www_ssh_port = gameOption("www_ssh_port",default="22") self.www_root = gameOption("www_root") self.template_tar_dir = gameOption("template_tar_dir").replace(" ","").split(",") self.template_exclude_dir = gameOption("template_exclude_dir").replace(" ","").split(",") self.www_ssh = ssh.ssh(www_ssh_ip,port=int(www_ssh_port))
def recoverByProject(failurIptemp,recoverIptemp): global recoverIpSsh,failurIpSsh,recoverIpSshAstd,mysqldir,failurIp,recoverIp,failurIpSshAstd,allservers,wips,ismobile,dianxinIp,liantongIp,header,backstage_interface_url,curdir,recovermysqldir,dnsgame dnsgame = gameOption("dns_game",default=state.game) failurIp = failurIptemp recoverIp = recoverIptemp curdir = os.path.abspath(os.path.dirname(__file__)) project = state.game recoverIpSsh = ssh.ssh(recoverIp,user="******") failurIpSsh = ssh.ssh(failurIp,user="******") recoverIpSshAstd = ssh.ssh(recoverIp) failurIpSshAstd = ssh.ssh(failurIp) mysqldir = cmd(failurIpSsh,"grep '^innodb_data_home_dir' /etc/my.cnf | cut -d '=' -f 2|xargs echo").strip() recovermysqldir = cmd(recoverIpSsh,"grep '^innodb_data_home_dir' /etc/my.cnf | cut -d '=' -f 2|xargs echo").strip() stop_service() donwloadMysql() downloadNginx() download(state.game) logCopy() change_configfile() start_service() startGame() backstageDB = gameOption("backstage_db") headTag = gameOption("backstage_tag") ismobile = gameOption("is_mobile",type="bool") if ismobile: partnersType = 2 else: partnersType = 1 backstageIp =gameOption("backstage") header = {"host":gameOption("backstage_header")} backstage_interface_url = gameOption("backstage_interface_url") allservers = serverlist.getRecoverServerList(backstageDB,headTag,partnersType,backstageIp,failurIp) wips = getip.getServerWip(recoverIp) if len(wips) == 0: print "ERROR: 获取外网ip失败!" else: dianxinIp = wips[0] if len(wips) == 2: liantongIp = wips[1] else: liantongIp = wips[0] backstageChange() dnsChange() faileDirs = cmd(failurIpSshAstd,"find /app/ -maxdepth 1 -type d").split("\n") recoverDirs = cmd(recoverIpSshAstd,"find /app/ -maxdepth 1 -type d").split("\n") d1 = set(faileDirs).difference(set(recoverDirs)) if len(d1) > 0: print "故障机器目录如下,请确认是否已完全迁移!" print d1
def dnsJiexi(self): try: if self.add_dns: dnsSsh = ssh.ssh("10.6.196.65") status,stdout,stderr = dnsSsh.cmd("/app/opbin/dns/dnsapi -g %s -a up -d %s -l 1 -i %s"%(self.dns_game,self.server_url_tag,self.dianxinIp)) logging.info("/app/opbin/dns/dnsapi -g %s -a up -d %s -l 1 -i %s"%(self.dns_game,self.server_url_tag,self.dianxinIp)) logging.info(stdout) if stdout.find("Record modify success") >= 0: logging.info("电信域名解析修改成功") else: logging.info("电信域名解析修改失败") if self.xianluType == 2: status,stdout,stderr = dnsSsh.cmd("/app/opbin/dns/dnsapi -g %s -a up -d %s -l 2 -i %s"%(self.dns_game,self.server_url_tag,self.liantongIp)) logging.info("/app/opbin/dns/dnsapi -g %s -a up -d %s -l 2 -i %s"%(self.dns_game,self.server_url_tag,self.liantongIp)) logging.info(stdout) if stdout.find("Record modify success") >= 0: logging.info("联通域名解析修改成功") else: logging.info("联通域名解析修改失败") if self.lianyun_add_dns: if self.add_dns: logging.info("服务器恢复不用通知联运cname解析,我们自己已解析!") else: logging.info("服务器恢复需要通知联运重新做A记录%s A %s" %(self.server_url,self.dianxinIp)) if self.xianluType == 2: '''如果是CNAME记录,则电信跟联通只需要解析一条,否则需要解析每一条''' if not self.add_dns: logging.info("联通域名解析","%s A %s"%(self.server_url,self.liantongIp)) except Exception,e1: #self.statusCheck(False,"%s\n解析域名失败"%(str(e1))) logging.info("%s\n解析域名失败"%(str(e1)))
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(',') for whiteIp in ipList: content = ''' if ( $remote_addr = %s ) { set $gototest 1; }'''%whiteIp wwwIpSshAstd.exitcmd("sed -i '/\<%s\>/,/}/d' %s"%(whiteIp.strip(),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 dnsJiexi(self): try: if self.add_dns: dnsSsh = ssh.ssh("10.6.196.65") logging.info("/app/opbin/dns/dnsapi -g %s -a add -d %s -l 1 -i %s"%(self.dns_game,self.server_url_tag,self.dianxinIp)) status,stdout,stderr = dnsSsh.cmd("/app/opbin/dns/dnsapi -g %s -a add -d %s -l 1 -i %s"%(self.dns_game,self.server_url_tag,self.dianxinIp)) logging.info(stdout) if stdout.find("Record add success") >= 0: self.html.add("电信域名解析","成功",color="green") else: self.html.add("电信域名解析","失败!" + stdout.strip(),color="red") self.htmlStatus = False logging.info(str(stderr)) if self.xianluType == 2: logging.info("/app/opbin/dns/dnsapi -g %s -a add -d %s -l 2 -i %s"%(self.dns_game,self.server_url_tag,self.liantongIp)) status,stdout,stderr = dnsSsh.cmd("/app/opbin/dns/dnsapi -g %s -a add -d %s -l 2 -i %s"%(self.dns_game,self.server_url_tag,self.liantongIp)) logging.info(stdout) if stdout.find("Record add success") >= 0: self.html.add("联通域名解析","成功",color="green") else: self.html.add("联通域名解析","失败!"+stdout.strip(),color="red") self.htmlStatus = False logging.info(str(stderr)) if self.lianyun_add_dns: if self.add_dns: self.html.add("电信域名解析","%s CNAME %s"%(self.server_url,self.ast_full_url)) else: self.html.add("电信域名解析","%s A %s"%(self.server_url,self.dianxinIp)) if self.xianluType == 2: '''如果是CNAME记录,则电信跟联通只需要解析一条,否则需要解析每一条''' if not self.add_dns: self.html.add("联通域名解析","%s A %s"%(self.server_url,self.liantongIp)) except Exception,e1: self.statusCheck(False,"%s\n解析域名失败"%(str(e1)))
def resRsyncResult(): try: print "等待30秒钟同步资源目录..." sys.stdout.flush() time.sleep(30) rsync_module = gameOption("rsync_module", default="").strip() rsync_root = gameOption("rsync_root", default="").strip() rsync_backup_ip = gameOption("rsync_backup_ip", default="").strip() rootSshObj = ssh.ssh(ip, port=port, user="******") if rsync_module != "" and rsync_root != "" and rsync_backup_ip != "": for i in range(6): out = execute( fabriccmd, '''cd %s && rsync -art -R --dry-run --delete --out-format="%%n" ./ %s::%s --password-file=/etc/rsyncd.secret''' % (rsync_root, rsync_backup_ip, rsync_module), hosts=[ip])[ip] if out == None: break elif out.strip() != "": print "资源暂未完全同步到备用下载点,等待60s后重新检查..." sys.stdout.flush() time.sleep(60) else: print "资源同步完毕!" break else: print "WARNNING: 资源有未同步的情况,30s之后将会更新version.lua,或者手动终止任务!!!!!!!" sys.stdout.flush() time.sleep(30) except Exception, e10: print "WARNNING:检查资源同步进度失败!err:%s" % str(e10)
def deploy(ip, username, password, source, destination): f = open("Deploy.log", "a+") f.write(f"NewPRDeploy.py") try: print(f"Connecting to {ip}") f.write(f"Connecting to {ip}") connection = ssh(ip, username, password)#connects to remote desktop connection.openShell()#makes connection to remote connection.sendShell(f"rm -rf ~/Desktop/firmware")#removes all files and directories currently in folder print(f"Moving files from {source} to {destination}") connection.sendShell(f"mv {source} {destination}")#moves all contents from source directory to destination directory time.sleep(1)#gives time for command to execute tempfolder = source[:-1]#removes last character from source variable, removes the * so the directory can be deleted connection.sendShell(f"rm -rf {tempfolder}")#removes temp folder from Desktop time.sleep(1) connection.closeConnection()#closes Connection between host and remote desktop print(f"************{ip} PASS********************\n") f.write(f"************{ip} PASS********************\n")#Writes a pass on logs except: print(f"Could not deploy, was not able to connect to {ip}") f.write(f"************{ip} FAIL********************\n")#writes to log f.write(f"IPSW Files were not Deployed to {ip}")#writes to log f.close()
def tar_file(self,filelist,plat,server,host,destar,sshport): if len(filelist) == 0: fstring="所有文件" cmd = '''"cd /data/server/%s_%s/ && tar czf %s --exclude=sql/backup/* --exclude=sql/recover/* sql script gamectl user_default.beam ebin config/game.crontab "'''%(plat,server,destar) else: fstring="" for f in filelist: if f == "config": fstring=fstring+"config/common.conf " else: fstring=fstring+"ebin/*/%s.beam "%f print "\033[1;33m所需打包的文件:\033[0m",fstring cmd = '''"cd /data/server/%s_%s/ && tar czf %s --exclude=sql/backup/* --exclude=sql/recover/* %s"'''%(plat,server,destar,fstring) tarfile="/data/server/%s_%s/%s"%(plat,server,destar) local_cmd = '''scp -C -r -P %s root@%s:%s /tmp/'''%(sshport,host,tarfile) try: SSH=ssh(host,port=sshport) #print '''\033[1;32m"%s_%s"\033[0m,%s打包...CMD:\033[1;33m%s\033[0m'''%(plat,server,host,cmd) stdout,stderr,returncode=SSH.run(cmd,drop_socket=False) if returncode != 0: message = '''\033[1;31m在"%s_%s"主机%s上打包"%s"失败,失败信息:{%s,%s}\033[0m'''%(plat,server,host,fstring,stdout,stderr) raise Exception(message) print '''\033[1;32m在"%s_%s"主机%s上打包"%s"成功...\033[0m'''%(plat,server,host,fstring) #下载到本地 print '''\033[1;32m开始下载%s文件在/tmp 下\033[0m'''%(tarfile) ret = subprocess.Popen(local_cmd,stdout=subprocess.PIPE,stderr=subprocess.PIPE,shell=True) stdout,stderr = ret.communicate() if ret.returncode != 0: print "\033[1;31m从%s '%s_%s'下载 %s 失败.失败信息:%s,%s\033[0m"%(host,plat,server,tarfile,stdout,stderr) exit() except Exception,err: message = '''"tar_file 打包函数执行失败...信息{%s}"'''%err raise Exception(message)
def resRsyncResult(): try: print "等待30秒钟同步资源目录..." sys.stdout.flush() time.sleep(30) rsync_module = gameOption("rsync_module",default="").strip() rsync_root = gameOption("rsync_root",default="").strip() rsync_backup_ip = gameOption("rsync_backup_ip",default="").strip() rootSshObj = ssh.ssh(ip,port=port,user="******") if rsync_module != "" and rsync_root != "" and rsync_backup_ip != "" : for i in range(6): out = execute(fabriccmd,'''cd %s && rsync -art -R --dry-run --delete --out-format="%%n" ./ %s::%s --password-file=/etc/rsyncd.secret'''%(rsync_root,rsync_backup_ip,rsync_module), hosts=[ip])[ip] if out == None: break elif out.strip() != "": print "资源暂未完全同步到备用下载点,等待60s后重新检查..." sys.stdout.flush() time.sleep(60) else: print "资源同步完毕!" break else: print "WARNNING: 资源有未同步的情况,30s之后将会更新version.lua,或者手动终止任务!!!!!!!" sys.stdout.flush() time.sleep(30) except Exception,e10: print "WARNNING:检查资源同步进度失败!err:%s"%str(e10)
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 dnsJiexi(self): try: if self.add_dns: dnsSsh = ssh.ssh("10.6.196.65") status,stdout,stderr = dnsSsh.cmd("/app/opbin/dns/dnsapi -g %s -a add -d %s -l 1 -i %s"%(self.game,self.server_url_tag,self.dianxinIp)) logging.info("/app/opbin/dns/dnsapi -g %s -a add -d %s -l 1 -i %s"%(self.game,self.server_url_tag,self.dianxinIp)) logging.info(stdout) if stdout.find("Record add success") >= 0: self.html.add("电信域名解析","成功",color="green") else: self.html.add("电信域名解析","失败",color="red") if self.xianluType == 2: status,stdout,stderr = dnsSsh.cmd("/app/opbin/dns/dnsapi -g %s -a add -d %s -l 2 -i %s"%(self.game,self.server_url_tag,self.liantongIp)) if stdout.find("Record add success") >= 0: self.html.add("联通域名解析","成功",color="green") else: self.html.add("联通域名解析","失败",color="red") if self.lianyun_add_dns: if self.add_dns: self.html.add("电信域名解析","%s CNAME %s"%(self.server_url,self.ast_full_url)) else: self.html.add("电信域名解析","%s A %s"%(self.server_url,self.dianxinIp)) if self.xianluType == 2: '''如果是CNAME记录,则电信跟联通只需要解析一条,否则需要解析每一条''' if not self.add_dns: self.html.add("联通域名解析","%s A %s"%(self.server_url,self.liantongIp)) except Exception,e1: self.statusCheck(False,"%s\n解析域名失败"%(str(e1)))
def tar_down_php(self, host, sshport, downpath="/tmp"): PHP_SRC_CODE_DIR = "/data/web/mwygz/admin" CODE_TAR = "php.tar.gz" TMP_FROM = "/data/web/mwygz/admin/php.tar.gz" #从服务器上打包 cmd = '''"cd %s && /bin/tar zcf %s --exclude=protected/config/config.php --exclude=protected/template_c/* --exclude=protected/config/servers/* public protected log"''' % ( PHP_SRC_CODE_DIR, CODE_TAR) SSH = ssh(host=host, port=sshport) stdout, stderr, returncode = SSH.run(cmd, drop_socket=False) if returncode != 0: message = '\033[1;31m"fail","%s:%s"cmd:"%s" 失败...\033[0m' % ( host, port, cmd) raise Exception(message) print '''\033[1;32m"success","%s:%s" 路径:"%s"打包文件成功...\033[0m''' % ( host, sshport, TMP_FROM) #下载 down_cmd = "scp -C -r %s -P %s root@%s:%s %s" % ( self.option, sshport, host, TMP_FROM, downpath) ret = subprocess.Popen(down_cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) stdout, stderr = ret.communicate() if ret.returncode != 0: message = '\033[1;31m"fail","%s:%s"下载"%s"到本地"%s"失败,信息: %s|%s \033[0m' % ( host, sshport, TMP_FROM, downpath) raise Exception(message) print '''\033[1;32m"success","%s:%s","%s"下载"%s"成功..\033[0m''' % ( host, sshport, TMP_FROM, downpath)
def send_file(self,host,sshport,file,desfile): SSH=ssh(host=host,port=sshport) stdout,stderr,KB,code=SSH.sendfile(source=file,dest=desfile,drop_socket=False) if code != 0: message = '''\033[1;31m"fail",传送 %s 到 %s:%s 失败..信息:%s,%s\033[0m'''%(file,host,desfile,stdout,stderr) raise Exception(message) print '''\033[1;32m"success",传送 %s 到 %s:%s 成功\033[0m'''%(file,host,desfile)
def reload_server(self,plat,server,host,filelist,sshport): rlist=filelist fstring="" SSH=ssh(host,port=sshport) config=False mfiles=False if "config" in filelist: config = True else: mfiles = True if config: reload_conf = '''"cd %s/%s_%s/ &&./gamectl reload -r config"'''%(self.config_dir,plat,server) stdout,stderr,returncode = SSH.run(reload_conf,drop_socket=False) if returncode != 0: message = '''\033[1;31m"%s_%s reload config "失败..IP:%s 信息:{%s}\033[0m'''%(plat,server,host,stderr.strip("\n")) raise Exception(message) print '''\033[1;32m"success",%s_%s reload conf \033[0m'''%(plat,server) if mfiles: for i in filelist: fstring=fstring+" "+i reload_file = '''"cd %s/%s_%s/ &&./gamectl reload -r code '%s'"'''%(self.config_dir,plat,server,fstring) stdout1,stderr1,returncode = SSH.run(reload_file,drop_socket=False) if returncode != 0: message = '''\033[1;31m"fail","%s_%s" CMD: %s \033[0m 错误信息:"%s,%s"'''%(plat,server,reload_file,stdout1.strip("\n"),stderr1.strip("\n")) print message else: print '''\033[1;32m"success",%s_%s reload files:%s\033[0m'''%(plat,server,fstring)
def addwhiteip(iplist,yx): if not check.checkIpList(iplist): raise Exception("iplist 格式不正确:%s"%iplist) backstageDB = gameOption("backstage_db") headTag = gameOption("backstage_tag") ismobile = gameOption("is_mobile",type='bool') lyWhiteIpKey = gameOption("lyWhiteIpKey") if ismobile: partnersType = 2 else: partnersType = 1 backstageIp = gameOption("backstage") mixservers = serverlist.serverRange(backstageDB,headTag,partnersType,"mix",backstageIp,serverlist="%s_.*"%yx) resultStatus = True if len(mixservers) == 0: raise Exception("没有获取到游戏列表,请确认!") state.servers = mixservers state.ignoreErrorHost = True ccthread.run(updateProperties,iplist,yx,lyWhiteIpKey) proxy_pay1_ip = gameOption("proxy_pay1_ip",default="") proxy_pay2_ip = gameOption("proxy_pay2_ip",default="") proxy_pay1 = gameOption("proxy_pay1",default="") proxy_pay2 = gameOption("proxy_pay2",default="") if proxy_pay1.strip() != "": proxy_pay1_ssh = ssh.ssh(proxy_pay1_ip) updateProxy(proxy_pay1_ssh,proxy_pay1_ip,proxy_pay1,iplist,yx,lyWhiteIpKey) if proxy_pay2.strip() != "": proxy_pay2_ssh = ssh.ssh(proxy_pay2_ip) updateProxy(proxy_pay2_ssh,proxy_pay2_ip,proxy_pay2,iplist,yx,lyWhiteIpKey) if len(state.errorResult) > 0: resultStatus = False print "\n","*" * 50 for i in state.errorResult: print "[%s] %s"%(i,state.errorResult[i]) print "\n","*" * 50 print "更新服务器数:%d"%len(mixservers) print "更新失败数:%d"%len(state.errorResult) print "连接失败服务器数量:%d"%len(state.errorHost) try: import template templateObj = template.template(state.game,state.language,mixservers[0][0],ip=mixservers[0][1]) templateObj.updateServerProperties() print "更新模板成功!" except Exception,e1: resultStatus = False print "ERROR:模板更新失败!msg:%s"%str(e1)
def execute(cmd): print cmd dnsip = mainOption("dnsip") dnsSsh = ssh.ssh(dnsip) try: return dnsSsh.cmd(cmd) except Exception, e: return 1, "执行失败", str(e)
def execute(cmd): print cmd dnsip = mainOption("dnsip") dnsSsh = ssh.ssh(dnsip) try: return dnsSsh.cmd(cmd) except Exception,e: return 1,"执行失败",str(e)
def __init__(self, db, rowid, username='******', password='******'): self.rowid = rowid self.db = db self.createFromRowID() self.username = username self.ip = self.db.getHostIP(self.host) self.password = password self.ssh = ssh(self.username, self.ip, self.password)
def removeCommon(self, host, address, username='******', password='******'): s = ssh(username, address, password) s.remove_folder('common') s.close()
def init(self): curdir = os.path.dirname(os.path.abspath(__file__)) self.curdir = curdir logdir = curdir +"/../logs" if not os.path.exists(logdir): os.makedirs(logdir) game = self.game language = self.language nowstring = datetime.datetime.now().strftime("%Y%m%d_%H%M%S") logging.basicConfig(level=logging.INFO, format='%(asctime)s %(levelname)s %(message)s', datefmt='%Y-%m-%d %H:%M:%S', #filename='%s/addPartner_%s_%s_%s.log'%(os.path.abspath(logdir),game,newyx,nowstring), #filename='%s/addPartner_%s_%s.log'%(os.path.abspath(logdir),game,newyx), #filemode='a' ) logging.info("开始新增联运...") print "详细日志为:%s/addPartner_%s_%s.log"%(os.path.abspath(logdir),game,self.newyx) sys.stdout.flush() self.html = mailhtml.mailhtml("%s_%s_addPartner.html"%(game,self.newyx),"%s_%s新联运添加"%(game,self.newyx)) self.email_address = gameOption("email_address") self.mail_title = "[%s][%s]新联运添加"%(game,self.newyx) self.htmlStatus = True try: self.www_ssh_ip = gameOption("www_ssh_ip") logging.info("%s建立连接..."%self.www_ssh_ip) self.ssh = ssh.ssh(self.www_ssh_ip) logging.info("%s建立ssh连接完毕!"%self.www_ssh_ip) self.clientrootdir = mainOption("clientrootdir").replace("${game}",game) self.rootdir = mainOption("rootdir").replace("${game}",game) #后台相关信息 self.backstage = gameOption("backstage") self.backstage_db = gameOption("backstage_db") self.backstage_tag = gameOption("backstage_tag",default="") self.backstage_interface_url = gameOption("backstage_interface_url") self.backstage_header = gameOption("backstage_header") self.have_backstage_interface = gameOption("have_backstage_interface",type="bool") #是否为海外 self.is_oversea = gameOption("is_oversea",type="bool") #是否为手游 if gameOption("is_mobile",type="bool"): self.is_mobile = "mobile" self.partnersType = 2 else: self.is_mobile = "web" self.partnersType = 1 #新联运添加脚本 self.addPartner_script = gameOption("addPartner_script") #后台游戏域名,后续会替换server_url self.web_url = gameOption("web_url") logging.info("变量初始化完毕") except Exception,e1: self.statusCheck(False,str(e1))
def reset(cleartime, starttime, server): if check.nullCheck(cleartime): raise Exception("清档时间不能为空") if check.nullCheck(starttime): raise Exception("开服时间不能为空") if check.nullCheck(server): raise Exception("游戏服不能为空") if not check.checkDatetime(cleartime): raise Exception("清档时间格式不正确") if not check.checkDatetime(starttime): raise Exception("开服时间格式不正确") backstageIp = gameOption("backstage") backstageDb = gameOption("backstage_db") head_tag = gameOption("backstage_tag") ismobile = gameOption("is_mobile", type="bool") if ismobile: partnersType = 2 else: partnersType = 1 serverinfo = serverlist.serverInfo(backstageDb, head_tag, partnersType, "main", backstageIp, server) if not serverinfo["result"]: raise Exception("获取游戏服在后台的信息失败!msg:%s" % serverinfo["msg"]) ip = serverinfo["ip"] if not check.checkIp(ip): raise Exception("获取游戏服ip失败!ip:%s" % ip) global sshobj sshobj = ssh.ssh(ip) players = cmd( "pandora %s_%s -e 'select count(1) from player' | grep -v count" % (state.game, server)).strip() if players != "": if int(players) > 0: print "WARNNING: 当前角色数为%s" % players servertime = cmd("date +'%Y-%m-%d %H:%M:%S'").strip() print "servertime:", servertime servertime_datetime = datetime.datetime.strptime(servertime, "%Y-%m-%d %H:%M:%S") cleartime_datetime = datetime.datetime.strptime(cleartime, '%Y-%m-%d %H:%M:%S') if servertime_datetime + datetime.timedelta( minutes=1) > cleartime_datetime: raise Exception("服务器时间大于了清档时间,清档失败!服务器时间为:%s,清档时间提交为:%s" % (servertime, cleartime)) clearshell = gameOption("clear_script") print clearshell cmd("mkdir -p /app/opbin/%s/allinone/shell/" % state.game) sshobj.put("%s/../shell/%s" % (os.path.abspath(os.path.dirname(__file__)), clearshell), remote_path="/app/opbin/%s/allinone/shell/" % state.game) tmpfile = "/tmp/crontab_%s" % datetime.datetime.now().strftime( "%Y%m%d_%H%M%S") try: cmd("crontab -l > %s" % tmpfile) except Exception, e1: if str(e1).find("no crontab for astd") < 0: raise e1
def get_pid_query(self, plat, server, host, sshport): cmd_query = '''pgrep -f %s_%s/config||echo "0"''' % (plat, server) try: SSH = ssh(host=host, port=sshport) stdout, stderr, returncode = SSH.run(cmd_query, drop_socket=False) if returncode != 0: raise Exception(stderr) output = stdout.strip("\n") #print output except Exception, err: raise Exception(err)
def download_mysql(failurIp,database,backup_date): curdir = os.path.abspath(os.path.dirname(__file__)) s = ssh.ssh(failurIp,user='******') s.put("%s/../shell/copy_database.sh"%curdir,remote_path="/app") database_list = [] os.popen("pandora -e 'use acegi_gcmob;select concat(server_flag,'_',replace(name,'S','')) from server where n_ip = %s and server.status&1=1 and istest = 0 and server.mixflag = 1;' > /tmp/get_database.txt"%failurIp).read() database_file = file('/tmp/get_database.txt') for line in database_file.readlines(): datbase_list.append(line)
def get_pid_query(self,plat,server,host,sshport): cmd_query='''pgrep -f %s_%s/config||echo "0"'''%(plat,server) try: SSH = ssh(host=host,port=sshport) stdout,stderr,returncode = SSH.run(cmd_query,drop_socket=False) if returncode != 0:raise Exception(stderr) output = stdout.strip("\n") #print output except Exception,err: raise Exception(err)
def get_plat_version(self,host,sshport,plat,server="s1"): get_version='''grep "code" /data/server/%s_%s/log/version.txt'''%(plat,server) try: SSH = ssh(host=host,port=sshport) stdout,stderr,returncode = SSH.run(get_version,drop_socket=False) if returncode != 0:raise Exception(stderr) output = stdout.strip("\n") #print output except Exception,err: raise Exception(err)
def router_login(login_details, command): router_class = ssh(login_details) try: router_command = router_class.run_command(command) #router_command = router_class.run_commands(command) for line in get_router_items(router_command): print(line) except: pass
def __init__(self, cond, name,hostname,username,RootPasswd,password,port=22): super(CBM1, self).__init__() self.cond = cond self.name = name self.hostname=hostname self.username=username self.password=password self.port=port self.RootPasswd=RootPasswd self.ssh_connect=ssh(self.hostname,self.username,self.password,self.port)
def hotswap(type, keyword, backendVersion=None): global succList, errorDict succList = [] errorDict = {} game = state.game language = state.language www_ip = gameOption("www_ip") www_ssh_ip = gameOption("www_ssh_ip") www_port = gameOption("www_port", default="80") www_header = gameOption("www_header") www_root = gameOption("www_root") www_ssh = ssh.ssh(www_ssh_ip) www_hotswap_dir = "%s/%s/hotswap" % (www_root, game) www_ssh.cmd("mkdir -p " + www_hotswap_dir) if type != "update" and type != "remote": print "ERROR: 动更类型必须为update或者remote!" sys.exit(1) www_ssh.put("/app/online/%s/hotswap/%s/hotswap.zip" % (game, language), remote_path=www_hotswap_dir) ##动更包上传资源目录后删除ftp上的动更包 os.popen("rm -f /app/online/%s/hotswap/%s/hotswap.zip" % (game, language)) serverlist = getserverlist() state.servers = serverlist #state.servers = [["feiliu_10010","10.6.197.215"]] state.ignoreErrorHost = True ccthread.run(execute, type, keyword, www_ip, www_port, www_header) resultStatus = True if len(state.errorHost) > 0: print "-" * 40 + "连接失败主机" + "-" * 30 print state.errorHost resultStatus = False if len(errorDict) > 0: print "-" * 40 + "命令执行失败服务器" + "-" * 24 for i in errorDict: print "[%s] Error:%s" % (i, errorDict[i]) print "-" * 40 + "更新失败服务器汇总为" + "-" * 22 print getSpecialServerIp(state.servers, errorDict.keys()) resultStatus = False print "-" * 40 + "汇总" + "-" * 38 print "更新服务器总数: %d" % len(state.servers) print "更新失败服务器数: %d" % len(errorDict) print "更新成功服务器数: %d" % len(succList) sys.stdout.flush() if not check.nullCheck(backendVersion): print "*" * 40 + "开始上传后端..." status, out = commands.getstatusoutput( "sh /app/opbin/rundeck/online.backend -t '%s' -g '%s'" % (backendVersion, game)) print out if status != 0: print "ERROR: 后端%s上传失败!" % backendVersion resultStatus = False if not resultStatus: sys.exit(1)
def send_file(self, host, sshport, file, desfile): SSH = ssh(host=host, port=sshport) stdout, stderr, KB, code = SSH.sendfile(source=file, dest=desfile, drop_socket=False) if code != 0: message = '''\033[1;31m"fail",传送 %s 到 %s:%s 失败..信息:%s,%s\033[0m''' % ( file, host, desfile, stdout, stderr) raise Exception(message) print '''\033[1;32m"success",传送 %s 到 %s:%s 成功\033[0m''' % (file, host, desfile)
def decompression_file(self,plat,server,host,desfile,sshport): cmd = '''"cd /data/server/%s_%s/ && tar xzf %s"'''%(plat,server,desfile) try: SSH=ssh(host,port=sshport) stdout,stderr,returncode=SSH.run(cmd,drop_socket=False) if returncode != 0: print '''\033[1;31m"%s_%s"在%s上解压%s失败\033[0m,失败信息:{%s,%s}'''%(plat,server,host,desfile,stdout,stderr) exit() print '''\033[1;32m"success","%s_%s"解压%s成功...\033[0m'''%(plat,server,desfile) except Exception,err: message = '''"decompression_file 解压函数执行失败...信息{%s}"'''%err raise Exception(message)
def _login(self, host=None, user=None, passwd=None): if not any([user, passwd]): user, passwd = self.user, self.passwd sshclient = ssh() try: sshclient.login(host, user, passwd) self.save_session[host] = sshclient if not sshclient.login_status: self.fail_num += 1 except: exit(1) self.fail_exit()
def get_plat_version(self, host, sshport, plat, server="s1"): get_version = '''grep "code" /data/server/%s_%s/log/version.txt''' % ( plat, server) try: SSH = ssh(host=host, port=sshport) stdout, stderr, returncode = SSH.run(get_version, drop_socket=False) if returncode != 0: raise Exception(stderr) output = stdout.strip("\n") #print output except Exception, err: raise Exception(err)
def sshConn(ip, servers): check = False for i in range(10): try: servers[ip] = ssh.ssh(ip) check = True break except: time.sleep(1) print "connect server %s try again ..." % ip if not check: print "connect server %s failed!" % ip
def init(self): self.clientrootdir = mainOption("clientrootdir").replace("${game}",self.game) curdir = os.path.dirname(os.path.abspath(__file__)) self.curdir = curdir self.kfgz_script = 'gcld_kfgz_match.py' self.kfgz_config = 'gcld_kfgz.conf' self.backstage_ip = gameOption("backstage") self.ssh = ssh.ssh(self.backstage_ip,port=22,closegw=True) logging.basicConfig(level=logging.INFO, format='%(asctime)s %(levelname)s %(message)s', datefmt='%Y-%m-%d %H:%M:%S', )
def sshConn(ip,servers): check = False for i in range(10): try: servers[ip] = ssh.ssh(ip) check = True break except: time.sleep(1) print "connect server %s try again ..."%ip if not check: print "connect server %s failed!"%ip
def run_cmd(self,plat,server,host,sshport,scmd,progress=False): shell_com='''"cd %s/%s_%s/ && %s"'''%(self.config_dir,plat,server,scmd) #print "开始 %s_%s "%(plat,server) try: SSH=ssh(host,port=sshport,progress=progress) stdout,stderr,returncode=SSH.run(shell_com,drop_socket=False) if returncode != 0: print '''\033[1;31m"%s_%s"在%s上执行cmd:%s失败\033[0m,失败信息:%s,%s'''%(plat,server,host,scmd,stderr,stdout) exit() print '''\033[1;32m"success","%s_%s"上执行 %s 成功...\033[0m.结果:%s'''%(plat,server,scmd,stdout.strip("\n")) except Exception,err: message = '''"shell_cmd 执行命令函数执行失败...信息{%s}"'''%err raise Exception(message)
def send_php_tar(self,host,sshport,file="/tmp/php.tar.gz",desfile="php.tar.gz"): PHP_SRC_CODE_DIR="/data/web/mwygz/admin" despath="%s/%s"%(PHP_SRC_CODE_DIR,desfile) #上传php.tar.gz self.send_file(host,sshport,file,PHP_SRC_CODE_DIR) #解压 tar_cmd = '''"cd %s && tar xzf %s && chown -R apache:apache ."'''%(PHP_SRC_CODE_DIR,desfile) SSH=ssh(host=host,port=sshport) stdout,stderr,returncode=SSH.run(tar_cmd,drop_socket=False) message = "\033[1;31m'fail','%s'执行'%s'出错,信息:%s|%s\033[0m"%(host,tar_cmd,stdout,stderr) if returncode != 0:raise Exception(message) print '\033[1;32m"success"在"%s"上解压"%s"成功..\033[0m'%(host,despath)
def getSsh(ip,port=22,user="******"): sshobj = None if not state.connectionCaches.has_key(ip): if ip not in state.errorHost: try: sshobj = ssh.ssh(ip) state.connectionCaches[ip] = sshobj #print "*************",ip,"不存在" except Exception,e: if ip not in state.errorHost: state.errorHost.append(ip) if not state.ignoreErrorHost: raise Exception("[%s] 连接失败!ERR:%s"%(ip,str(e)))
def hotswap(type,keyword,backendVersion=None): global succList,errorDict succList = [] errorDict = {} game = state.game language = state.language www_ip = gameOption("www_ip") www_ssh_ip = gameOption("www_ssh_ip") www_port = gameOption("www_port",default="80") www_header = gameOption("www_header") www_root = gameOption("www_root") www_ssh = ssh.ssh(www_ssh_ip) www_hotswap_dir = "%s/%s/hotswap"%(www_root,game) www_ssh.cmd("mkdir -p " + www_hotswap_dir) if type != "update" and type != "remote": print "ERROR: 动更类型必须为update或者remote!" sys.exit(1) www_ssh.put("/app/online/%s/hotswap/%s/hotswap.zip"%(game,language),remote_path=www_hotswap_dir) ##动更包上传资源目录后删除ftp上的动更包 os.popen("rm -f /app/online/%s/hotswap/%s/hotswap.zip"%(game,language)) serverlist = getserverlist() state.servers = serverlist #state.servers = [["feiliu_10010","10.6.197.215"]] state.ignoreErrorHost = True ccthread.run(execute,type,keyword,www_ip,www_port,www_header) resultStatus = True if len(state.errorHost) > 0: print "-"*40 + "连接失败主机" + "-"*30 print state.errorHost resultStatus = False if len(errorDict) > 0: print "-"*40 + "命令执行失败服务器" + "-"*24 for i in errorDict: print "[%s] Error:%s"%(i,errorDict[i]) print "-"*40 + "更新失败服务器汇总为" + "-"*22 print getSpecialServerIp(state.servers,errorDict.keys()) resultStatus = False print "-"*40 + "汇总" + "-"*38 print "更新服务器总数: %d"%len(state.servers) print "更新失败服务器数: %d" %len(errorDict) print "更新成功服务器数: %d" %len(succList) sys.stdout.flush() if not check.nullCheck(backendVersion): print "*" * 40 + "开始上传后端..." status,out = commands.getstatusoutput("sh /app/opbin/rundeck/online.backend -t '%s' -g '%s'"%(backendVersion,game)) print out if status != 0: print "ERROR: 后端%s上传失败!"%backendVersion resultStatus = False if not resultStatus: sys.exit(1)
def saveClusterInfo(cluster): if not os.path.isdir('cluster'): os.mkdir('cluster') try: f = open('./cluster/cluster.yaml', 'w') f.write(yaml.safe_dump(cluster, default_flow_style=False)) f.close() os.mkdir except IOError: print "Input/output error" sys.exit(1) for node in cluster['nodes']: if not os.path.isdir('./cluster/%s'%node['fqdn']): os.mkdir('./cluster/%s'%node['fqdn']) try: f = open('./cluster/%s/hardware.out'%node['fqdn'], 'w') f.write('dmidecode -t system\n') #f.write(str(ssh.ssh(node['fqdn'], 'dmidecode -t system'))) tmp = ssh.ssh(node['fqdn'], 'dmidecode -t system') for i in tmp: f.write(' '.join(i.split())) f.write('\n') f.write('dmidecode -t bios\n') #f.write(str(ssh.ssh(node['fqdn'], 'dmidecode -t bios'))) tmp = ssh.ssh(node['fqdn'], 'dmidecode -t bios') for i in tmp: f.write(' '.join(i.split())) f.write('\n') f.write('lspci\n') #f.write(str(ssh.ssh(node['fqdn'], 'lscpi'))) tmp = ssh.ssh(node['fqdn'], 'lspci') for i in tmp: f.write(' '.join(i.split())) f.write('\n') except IOError: print "Input/output error" sys.exit(1)
def createBinlog(): backstageIp = gameOption("backstage") backstageIpSsh = ssh.ssh(backstageIp,user="******") backstageDb = gameOption("backstage_db") OUT = cmd(backstageIpSsh,"pandora %s -e 'select concat(server_flag,\"_\",replace(name,\"S\",\"\")) from server where n_ip = \"%s\" and server.status&1=1 and istest = 0 and server.mixflag = 1;'|grep -v 'concat(server_flag,'"%(backstageDb,failurIp)) backstage_serverlist = OUT.split("\n") backstage_serverlist.remove('') print "backstage_serverlist:",backstage_serverlist diff = set(backstage_serverlist).difference(set(database_list)) if len(diff) > 0: raise Exception("还原数据库包含后台不存在游戏服:%s"%str(diff)) serverlist = " ".join(database_list) failurIpSsh.put("%s/../shell/copy_database.sh"%curdir,remote_path="/app") cmd(failurIpSsh,"sh /app/copy_database.sh '%s' '%s' '%s' '%s'"%(serverlist,state.game,backup_date,mysql_recover_dir))
def init(self): self.clientrootdir = mainOption("clientrootdir").replace( "${game}", self.game) curdir = os.path.dirname(os.path.abspath(__file__)) self.curdir = curdir self.kfgz_script = 'gcld_kfgz_match.py' self.kfgz_config = 'gcld_kfgz.conf' self.backstage_ip = gameOption("backstage") self.ssh = ssh.ssh(self.backstage_ip, port=22, closegw=True) logging.basicConfig( level=logging.INFO, format='%(asctime)s %(levelname)s %(message)s', datefmt='%Y-%m-%d %H:%M:%S', )
def decompression_file(self, plat, server, host, desfile, sshport): cmd = '''"cd /data/server/%s_%s/ && tar xzf %s"''' % (plat, server, desfile) try: SSH = ssh(host, port=sshport) stdout, stderr, returncode = SSH.run(cmd, drop_socket=False) if returncode != 0: print '''\033[1;31m"%s_%s"在%s上解压%s失败\033[0m,失败信息:{%s,%s}''' % ( plat, server, host, desfile, stdout, stderr) exit() print '''\033[1;32m"success","%s_%s"解压%s成功...\033[0m''' % ( plat, server, desfile) except Exception, err: message = '''"decompression_file 解压函数执行失败...信息{%s}"''' % err raise Exception(message)
def run_cmd(self, plat, server, host, sshport, scmd, progress=False): shell_com = '''"cd %s/%s_%s/ && %s"''' % (self.config_dir, plat, server, scmd) #print "开始 %s_%s "%(plat,server) try: SSH = ssh(host, port=sshport, progress=progress) stdout, stderr, returncode = SSH.run(shell_com, drop_socket=False) if returncode != 0: print '''\033[1;31m"%s_%s"在%s上执行cmd:%s失败\033[0m,失败信息:%s,%s''' % ( plat, server, host, scmd, stderr, stdout) exit() print '''\033[1;32m"success","%s_%s"上执行 %s 成功...\033[0m.结果:%s''' % ( plat, server, scmd, stdout.strip("\n")) except Exception, err: message = '''"shell_cmd 执行命令函数执行失败...信息{%s}"''' % err raise Exception(message)
def reset(cleartime,starttime,server): if check.nullCheck(cleartime): raise Exception("清档时间不能为空") if check.nullCheck(starttime): raise Exception("开服时间不能为空") if check.nullCheck(server): raise Exception("游戏服不能为空") if not check.checkDatetime(cleartime): raise Exception("清档时间格式不正确") if not check.checkDatetime(starttime): raise Exception("开服时间格式不正确") backstageIp = gameOption("backstage") backstageDb = gameOption("backstage_db") head_tag = gameOption("backstage_tag") ismobile = gameOption("is_mobile",type="bool") if ismobile: partnersType = 2 else: partnersType = 1 serverinfo = serverlist.serverInfo(backstageDb,head_tag,partnersType,"main",backstageIp,server) if not serverinfo["result"]: raise Exception("获取游戏服在后台的信息失败!msg:%s"%serverinfo["msg"]) ip = serverinfo["ip"] if not check.checkIp(ip): raise Exception("获取游戏服ip失败!ip:%s"%ip) global sshobj sshobj = ssh.ssh(ip) players = cmd("pandora %s_%s -e 'select count(1) from player' | grep -v count"%(state.game,server)).strip() if players != "": if int(players) > 0: print "WARNNING: 当前角色数为%s"%players servertime = cmd("date +'%Y-%m-%d %H:%M:%S'").strip() print "servertime:",servertime servertime_datetime = datetime.datetime.strptime(servertime,"%Y-%m-%d %H:%M:%S") cleartime_datetime = datetime.datetime.strptime(cleartime,'%Y-%m-%d %H:%M:%S') if servertime_datetime + datetime.timedelta(minutes=1) > cleartime_datetime : raise Exception("服务器时间大于了清档时间,清档失败!服务器时间为:%s,清档时间提交为:%s"%(servertime,cleartime)) clearshell = gameOption("clear_script") print clearshell cmd("mkdir -p /app/opbin/%s/allinone/shell/"%state.game) sshobj.put("%s/../shell/%s"%(os.path.abspath(os.path.dirname(__file__)),clearshell),remote_path="/app/opbin/%s/allinone/shell/"%state.game) tmpfile = "/tmp/crontab_%s"%datetime.datetime.now().strftime("%Y%m%d_%H%M%S") try: cmd("crontab -l > %s"%tmpfile) except Exception,e1: if str(e1).find("no crontab for astd") < 0: raise e1
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 send_php_tar(self, host, sshport, file="/tmp/php.tar.gz", desfile="php.tar.gz"): PHP_SRC_CODE_DIR = "/data/web/mwygz/admin" despath = "%s/%s" % (PHP_SRC_CODE_DIR, desfile) #上传php.tar.gz self.send_file(host, sshport, file, PHP_SRC_CODE_DIR) #解压 tar_cmd = '''"cd %s && tar xzf %s && chown -R apache:apache ."''' % ( PHP_SRC_CODE_DIR, desfile) SSH = ssh(host=host, port=sshport) stdout, stderr, returncode = SSH.run(tar_cmd, drop_socket=False) message = "\033[1;31m'fail','%s'执行'%s'出错,信息:%s|%s\033[0m" % ( host, tar_cmd, stdout, stderr) if returncode != 0: raise Exception(message) print '\033[1;32m"success"在"%s"上解压"%s"成功..\033[0m' % (host, despath)
def tar_file(self, filelist, plat, server, host, destar, sshport): if len(filelist) == 0: fstring = "所有文件" cmd = '''"cd /data/server/%s_%s/ && tar czf %s --exclude=sql/backup/* --exclude=sql/recover/* sql script gamectl user_default.beam ebin config/game.crontab "''' % ( plat, server, destar) else: fstring = "" for f in filelist: if f == "config": fstring = fstring + "config/common.conf " else: fstring = fstring + "ebin/*/%s.beam " % f print "\033[1;33m所需打包的文件:\033[0m", fstring cmd = '''"cd /data/server/%s_%s/ && tar czf %s --exclude=sql/backup/* --exclude=sql/recover/* %s"''' % ( plat, server, destar, fstring) tarfile = "/data/server/%s_%s/%s" % (plat, server, destar) local_cmd = '''scp -C -r -P %s root@%s:%s /tmp/''' % (sshport, host, tarfile) try: SSH = ssh(host, port=sshport) #print '''\033[1;32m"%s_%s"\033[0m,%s打包...CMD:\033[1;33m%s\033[0m'''%(plat,server,host,cmd) stdout, stderr, returncode = SSH.run(cmd, drop_socket=False) if returncode != 0: message = '''\033[1;31m在"%s_%s"主机%s上打包"%s"失败,失败信息:{%s,%s}\033[0m''' % ( plat, server, host, fstring, stdout, stderr) raise Exception(message) print '''\033[1;32m在"%s_%s"主机%s上打包"%s"成功...\033[0m''' % ( plat, server, host, fstring) #下载到本地 print '''\033[1;32m开始下载%s文件在/tmp 下\033[0m''' % (tarfile) ret = subprocess.Popen(local_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True) stdout, stderr = ret.communicate() if ret.returncode != 0: print "\033[1;31m从%s '%s_%s'下载 %s 失败.失败信息:%s,%s\033[0m" % ( host, plat, server, tarfile, stdout, stderr) exit() except Exception, err: message = '''"tar_file 打包函数执行失败...信息{%s}"''' % err raise Exception(message)