def func_init(ip, gameobj, q): """ 服务初始化功能 :param ip: 目标机器IP :param gameobj: 游戏对象 :param q: Queue :return: Queue """ ssh = myssh(ip) tmp = {} if ssh: """1、判断远端是否存在保存路径""" srp = "if [ ! -d '{dest_path}' ];then (mkdir -p {dest_path});fi;".format(dest_path=gameobj.remote_initshell_path) ssh.exec_command(srp) """2、把脚本推送到远端""" pushcmd = "scp -pr -o StrictHostKeyChecking=no -i " + current_app.config["MYFLASKPROROOTKEY"] + " {src} root@{ip}:{dest};".format(ip=ip, src=gameobj.local_initshell_path, dest=gameobj.remote_initshell_path) bash(pushcmd) """3、执行shell,可能需要指定环境变量""" execute = "source /etc/profile;sh {shell}".format(shell=os.path.join(gameobj.remote_initshell_path,os.path.basename(gameobj.local_initshell_path))) stding,stdout,stderr = ssh.exec_command(execute) tmp["stdout"] = stdout.readlines() tmp["stderr"] = stderr.readlines() ssh.close() else: current_app.logger.error("shh连接错误") tmp["stderr"] = "shh连接错误" tmp["stdout"] = "" q[ip] = tmp
def func_batch(ip,scriptap,filename,q): tmp = dict() ssh = myssh(ip) if ssh: try: # 第一步:推shell pushshell = "scp -pr -o StrictHostKeyChecking=no -i " + current_app.config["MYFLASKPROROOTKEY"] + " {src} root@{ip}:{dest};".format( ip=ip, src=scriptap, dest=os.path.join("/tmp", filename)) bash(pushshell) """2、执行脚本""" execute = "source /etc/profile;sh {shell}".format(shell=os.path.join("/tmp", filename)) stding, stdout, stderr = ssh.exec_command(execute) tmp["stdout"] = stdout.readlines() tmp["stderr"] = stderr.readlines() ssh.close() except Exception as e: current_app.logger.error(e) tmp["stdout"] = "" tmp["stderr"] = str(e) else: tmp['stdout'] = "" tmp['stderr'] = "ssh 连接错误或超时. 请查看日志\n" current_app.logger.error(tmp["stderr"]) q[ip] = tmp
def func_updateprog(ip, zonename, game, version, md5, style, q): """IP,区服名称,游戏对象,版本,md5,更新类型""" ssh = myssh(ip) tmp = {} if ssh: try: """1、判断远端是否存在保存发布包 和shell 路径""" srp = "if [ ! -d '{pkg_path}' ];then (mkdir -p {pkg_path});fi;if [ ! -d '{shell_path}' ];then (mkdir -p {shell_path});fi;".format( pkg_path=game.remote_update_pkg_path, shell_path=game.remote_hot_update_shell_path if style =="hot_udpate" else game.remote_cold_update_shell_path) ssh.exec_command(srp) """2、把版本和脚本推送到指定目录""" pushversion = "scp -pr -o StrictHostKeyChecking=no -i " + current_app.config["MYFLASKPROROOTKEY"] + " {src} root@{ip}:{dest};".format( ip=ip, src=os.path.join(game.local_update_pkg_path, version), dest=os.path.join(game.remote_update_pkg_path, version)) filename = slugify(game.name, to_lower=True, separator="") + '.sh' pushshell = "scp -pr -o StrictHostKeyChecking=no -i " + current_app.config["MYFLASKPROROOTKEY"] + " {src} root@{ip}:{dest};".format( ip=ip, src=game.local_hot_update_shell_path if style =="hot_update" else game.local_cold_update_shell_path, dest=os.path.join(game.remote_hot_update_shell_path if style =="hot_update" else game.remote_cold_update_shell_path,filename)) bash(pushversion + pushshell) """3、推送后校验MD5值""" i,o,e = ssh.exec_command('md5sum {remote_ver}'.format(remote_ver=os.path.join(game.remote_update_pkg_path,version))) if o.readlines()[0].split(' ')[0].upper() != md5.upper(): raise Exception('版本md5值传输前后不一致,终止解压更新操作!\n') """4、解压发布包到指定目录""" cmd1 = "rm -rf {unzippath} && mkdir -p {unzippath} &&".format(unzippath=game.remote_unzip_path) cmd2 = "unzip -o -O CP936 -d {unzippath} {dest_version} >/dev/null 2>&1 &&".format( unzippath=game.remote_unzip_path, dest_version=os.path.join(game.remote_update_pkg_path, version)) """5、执行脚本""" execute = "source /etc/profile;sh {shell}".format( shell=os.path.join(game.remote_hot_update_shell_path if type=="hot_update" else game.remote_cold_update_shell_path,filename)) stding, stdout, stderr = ssh.exec_command(cmd1 + cmd2 + execute) tmp["stdout"] = stdout.readlines() tmp["stderr"] = stderr.readlines() ssh.close() except Exception as e: current_app.logger.error(e) tmp["stdout"] = "" tmp["stderr"] = str(e) else: current_app.logger.error("shh连接错误") tmp["stderr"] = "shh连接错误" tmp["stdout"] = "" q[ip] = tmp
def func_openservi(zoneip,zonename,game,version,q): ssh = myssh(zoneip) tmp = {} if ssh: """1、判断远端是否存在保存发布包 和shell 路径""" srp = "if [ ! -d '{pkg_path}' ];then (mkdir -p {pkg_path});fi;if [ ! -d '{shell_path}' ];then (mkdir -p {shell_path});fi;".format( pkg_path=game.remote_open_service_pkg_path,shell_path=game.remote_open_service_shell_path) ssh.exec_command(srp) """2、把版本s推送到远端""" for ver in version: pushcmd = "scp -pr -o StrictHostKeyChecking=no -i " + current_app.config["MYFLASKPROROOTKEY"] + " {src} root@{ip}:{dest};".format( ip=zoneip, src=os.path.join(game.local_open_service_pkg_path, ver), dest=os.path.join(game.remote_open_service_pkg_path, ver)) bash(pushcmd) """推送shell脚本""" # local_shell_path = os.path.join(basedir,"scripts","openshell") filename = slugify(game.name, to_lower=True, separator="") + '.sh' pushshell = "scp -pr -o StrictHostKeyChecking=no -i " + current_app.config["MYFLASKPROROOTKEY"] + " {src} root@{ip}:{dest};".format( ip=zoneip, src=game.local_open_service_shell_path, dest=os.path.join(game.remote_open_service_shell_path, filename)) bash(pushshell) """3、解压发布包到指定目录""" cmd2 = "" cmd1 = "rm -rf {unzippath} && mkdir -p {unzippath} &&".format(unzippath=game.remote_unzip_path) for ver in version: cmd2 += "unzip -o -O CP936 -d {unzippath} {dest_version} >/dev/null 2>&1 &&".format(unzippath=game.remote_unzip_path, dest_version=os.path.join(game.remote_open_service_pkg_path,ver)) """4、执行脚本""" execute = "source /etc/profile;sh {shell}".format( shell=os.path.join(game.remote_open_service_shell_path, filename)) stding, stdout, stderr = ssh.exec_command(cmd1 + cmd2 + execute) tmp["stdout"] = stdout.readlines() tmp["stderr"] = stderr.readlines() ssh.close() else: current_app.logger.error("shh连接错误") tmp["stderr"] = "shh连接错误" tmp["stdout"] = "" q[zoneip] = tmp
def func_toggle(ip,zonename,gameobj,local_file_path,remote_path,filename,q): tmp = dict() ssh = myssh(ip) if ssh: try: # 第一步:在远端创建保存shell 脚本路径 srp = "if [ ! -d '{shell_path}' ];then (mkdir -p {shell_path});fi;".format(shell_path=remote_path) stdin1, stdout1, stderr1 = ssh.exec_command(srp) # 第二步:推shell pushshell = "scp -pr -o StrictHostKeyChecking=no -i " + current_app.config["MYFLASKPROROOTKEY"] + " {src} root@{ip}:{dest};".format( ip=ip, src=local_file_path, dest=os.path.join(remote_path,filename)) bash(pushshell) """3、执行脚本""" execute = "source /etc/profile;sh {shell}".format( shell=os.path.join(remote_path,filename)) stding, stdout, stderr = ssh.exec_command(execute) tmp["stdout"] = stdout.readlines() tmp["stderr"] = stderr.readlines() ssh.close() except Exception as e: current_app.logger.error(e) tmp["stdout"] = "" tmp["stderr"] = str(e) else: tmp['stdout'] = "" tmp['stderr'] = "ssh 连接错误或超时. 请查看日志\n" current_app.logger.error(tmp["stderr"]) q[ip] = tmp