def execute_docker(self,cmd="",sid="",containers=[]): cheungssh_info={"content":"","status":False} try: server_config=CheungSSHControler.convert_id_to_ip(sid=sid) if not server_config["status"]: for cid in containers: log={"status":False,"content":server_config["content"],"cid":cid} log=json.dumps(log,encoding="utf8",ensure_ascii=False) self.REDIS.incr("log.%s.current" % tid) self.REDIS.rpush("log.docker.container.%s" %self.parameters["tid"],log) print "解析配置失败",server_config["content"] raise CheungSSHError(server_config["content"]) ssh=CheungSSH_SSH() data=ssh.login(**server_config["content"]) if not data["status"]: print "登录失败" #####一次性失败一个服务器的全部docker for cid in containers: log={"status":False,"content":data["content"],"cid":cid} log=json.dumps(log,encoding="utf8",ensure_ascii=False) self.REDIS.incr("log.%s.current" % tid) self.REDIS.rpush("log.docker.container.%s" %self.parameters["tid"],log) raise CheungSSHError(data["content"]) #####这里不用多线程了,困难很多,设计到shell的复制 for c in containers: #####循环每一个容器,一个容器就是一个命令 print "执行命里干活" _cmd=cmd % (self.task_type,c) self.message_process(**{"cmd":_cmd,"sid":sid,"tid":self.parameters["tid"],"shell":ssh,"cid":c}) #####每一个服务器执行完成了所有的任务后注销shell ssh.logout() cheungssh_info["status"]=True except Exception,e: print "报错",str(e) cheungssh_info["status"]=False cheungssh_info["content"]=str(e)
def deployment_script_init_and_execute(sid,sfile,username): cheungssh_info={"content":"","status":False} tid=str(random.randint(90000000000000000000,99999999999999999999)) try: sfile=os.path.join(cheungssh_settings.script_dir,username,os.path.basename(sfile)) dfile=os.path.join('/tmp/',tid) host=cheungssh_modul_controler.CheungSSHControler.convert_id_to_ip(sid) if not host["status"]:raise CheungSSHError(host['content']) _host_info=host['content'] sftp=CheungSSHFileTransfer() login=sftp.login(**_host_info) print 676767 if not login["status"]: print login["content"],"登录失败" raise CheungSSHError(login["content"]) cheungssh_info=sftp.upload(sfile,dfile,"") sftp.logout() if not cheungssh_info["status"]:raise CheungSSHError(cheungssh_info["content"]) cheungssh_info["dfile"]=dfile ssh=CheungSSH_SSH() login=ssh.login(**_host_info) if not login["status"]:raise CheungSSHError(login["content"]) cheungssh_info=ssh.execute(dfile,ignore=True) ssh.logout() except Exception,e: cheungssh_info["status"]=False cheungssh_info["content"]=str(e)