def run(self): cheungssh_info={"status":False,"content":"无消息"} try: if self.task_type == "single": #single for s in self.servers_list: server_config=CheungSSHControler.convert_id_to_ip(sid=s) ControlerCenter.upload_script_and_execute_command(**server_config["content"]) else: if len(self.servers_list)==1 and self.servers_list[0].upper()=="ALL": info=self.get_all_servers() if not info["status"]: raise CheungSSHError(info["content"]) else: self.servers_list=info["content"] pool=CheungSSHPool() for s in self.servers_list: server_config=CheungSSHControler.convert_id_to_ip(sid=s) #pool.add_task(test_func,{"args":11}) #pool.add_task(ControlerCenter.upload_script_and_execute_command,server_config["content"]) pool.add_task(self.upload_script_and_execute_command,server_config["content"]) pool.all_complete() info=self.save_assets_data() if info["status"]: print "已经存储资产信息" else: raise CheungSSHError("存储资产信息失败了:",info["content"]) #multi cheungssh_info["status"]=True except Exception,e: cheungssh_info["status"]=False cheungssh_info["content"]=str(e)
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 delete_crontab(sid,tid): cheungssh_info={"content":"","status":False} ssh=CheungSSH_SSH() conf=CheungSSHControler.convert_id_to_ip(sid) try: if not conf["status"]: raise CheungSSHError(conf["content"]) a=ssh.login(**conf["content"]) if a["status"]: info=ssh.execute("whoami") if info["status"]: user=info["content"].split("\n")[1:][:-1][0] path=os.path.join("/var/spool/cron/",user) info=ssh.execute("sed -i '{id}s/.*/#/' {path}".format(id=tid,path=path)) if not info["status"]: raise CheungSSHError(info["content"]) else: #REDIS.hdel("CHB0833279333-1",conf["content"]["alias"]) data=REDIS.hget("CHB0833279333-1",conf["content"]["alias"]) data=json.loads(data) del data[tid] data=json.dumps(data,encoding="utf8",ensure_ascii=False) REDIS.hset("CHB0833279333-1",conf["content"]["alias"],data) cheungssh_info["status"]=True else: cheungssh_info["content"]=a["content"] except Exception,e: cheungssh_info["status"]=False cheungssh_info["content"]=str(e)
def remote_file_content(super, username, id, action, file_content=""): #####username是那个用户请求 cheungssh_info = {"status": False, "content": ""} try: data = RemoteFileAdmin.get_remote_file_list(super, username) if not data["status"]: raise CheungSSHError(data["content"]) content = data["content"] try: ######检查权限 if not content[id]["owner"] == username: CheungSSHError("您无权查看该资源!") except KeyError: raise CheungSSHError("您指定的资源不存在!") path = content[id]["path"] path = re.sub(" ", "", path) #####空格问题 sid = content[id]["server"] ######sid host_info = CheungSSHControler.convert_id_to_ip(sid) if not host_info["status"]: raise CheungSSHError(host_info["content"]) host = host_info["content"] sftp = CheungSSHFileTransfer() login = sftp.login(**host) if not login["status"]: raise CheungSSHError(login["content"]) if action == "GET": cheungssh_info = sftp.get_filecontent(path) elif action == "WRITE": cheungssh_info = sftp.write_filecontent(path, file_content) else: raise CheungSSHError("CHB0000000024") except Exception, e: cheungssh_info["status"] = False cheungssh_info["content"] = str(e)
def run(self, sid): tid = str(random.randint(999999999999, 9999999999999)) cheungssh_info = {"content": "", "status": False} log_key = "log.{tid}.{sid}".format(tid=tid, sid=sid) cmd_key = "cmd.{tid}.{sid}".format(tid=tid, sid=sid) self.sid, self.tid, self.cmd_key = sid, tid, cmd_key try: conf = CheungSSHControler.convert_id_to_ip(sid) if not conf["status"]: raise CheungSSHError(conf["content"]) a = self.login(**conf["content"]) if not a["status"]: raise CheungSSHError(a["content"]) else: cheungssh_info["content"] = a["content"] a = threading.Thread(target=self.execute_command) a.start() #####登录成功 #####开始接受命令 cheungssh_info["tid"] = tid cheungssh_info["log_key"] = log_key cheungssh_info["cmd_key"] = cmd_key cheungssh_info["status"] = True except Exception, e: cheungssh_info["content"] = str(e)
def save_crontab_to_server(action="create/modify",data={}): cheungssh_info={"status":False,"content":""} crontab_format="{runtime} {cmd} #{dest}".format(runtime=data["runtime"],cmd=data["cmd"],dest=data["dest"]) now_time=time.strftime("%Y-%m-%d %H:%M:%S",time.localtime()) try: _sed_t=CheungSSHCrontabControler.get_sed_separate(crontab_format) if not _sed_t["status"]: raise CheungSSHError(_sed_t["content"]) else: char=_sed_t["content"] ssh=CheungSSH_SSH() conf=CheungSSHControler.convert_id_to_ip(data["sid"]) if not conf["status"]: raise CheungSSHError(conf["content"]) a=ssh.login(**conf["content"]) if a["status"]: username=conf["content"]["username"] path=os.path.join("/var/spool/cron/",username) if action=="modify": info=ssh.execute("sed -i '{id}s{char}.*{char}{crontab_format}{char}' {path}".format(char=char,id=data["tid"],path=path,crontab_format=crontab_format)) if not info["status"]: raise CheungSSHError(info["content"]) old_data=REDIS.hget("CHB0833279333-1",data["alias"]) if old_data is None:old_data={} else:old_data=json.loads(old_data) data_line={"time":data["runtime"],"dest":data["dest"],"cmd":data["cmd"],"sid":data["sid"],"alias":data["alias"],"collect_time":now_time} line_count=data["tid"] else: info=ssh.execute(""" echo '{crontab_format}' >>{path} """.format(crontab_format=crontab_format,path=path)) if not info["status"]: raise CheungSSHError(info["content"]) info=ssh.execute(""" cat {path}|wc -l """.format(path=path)) if not info["status"]: raise CheungSSHError(info["content"]) line_count=info["content"].split("\r\n")[1] old_data=REDIS.hget("CHB0833279333-1",data["alias"]) if old_data is None:old_data={} else:old_data=json.loads(old_data) data_line={"time":data["runtime"],"dest":data["dest"],"cmd":data["cmd"],"sid":data["sid"],"alias":data["alias"],"collect_time":now_time} old_data[line_count]=data_line _old_data=json.dumps(old_data,encoding="utf8",ensure_ascii=False) REDIS.hset("CHB0833279333-1",data["alias"],_old_data) cheungssh_info={"status":True,"content":old_data} else: cheungssh_info["content"]=a["content"] old_data={} except Exception,e: cheungssh_info={"content":str(e),"status":False}
def save_crontab_to_server(action="create/modify",data={}): #####action 为create/modify,data是一个dict格式 #####创建计划任务 #####data数据格式:{"sid":"","runtime":"","cmd":"","dest":""} ,如果是modify,应该有tid字段 cheungssh_info={"status":False,"content":""} crontab_format="{runtime} {cmd} #{dest}".format(runtime=data["runtime"],cmd=data["cmd"],dest=data["dest"]) now_time=time.strftime("%Y-%m-%d %H:%M:%S",time.localtime()) try: #####判断sed的分隔符 _sed_t=CheungSSHCrontabControler.get_sed_separate(crontab_format)#####获取判断字符是否符合规格 if not _sed_t["status"]: raise CheungSSHError(_sed_t["content"]) else: char=_sed_t["content"] ssh=CheungSSH_SSH() conf=CheungSSHControler.convert_id_to_ip(data["sid"]) if not conf["status"]: raise CheungSSHError(conf["content"]) a=ssh.login(**conf["content"]) if a["status"]: username=conf["content"]["username"] path=os.path.join("/var/spool/cron/",username) if action=="modify": #####修改计划任务列表 info=ssh.execute("sed -i '{id}s{char}.*{char}{crontab_format}{char}' {path}".format(char=char,id=data["tid"],path=path,crontab_format=crontab_format))######替换新的内容 if not info["status"]: raise CheungSSHError(info["content"]) old_data=REDIS.hget("CHB0833279333-1",data["alias"]) if old_data is None:old_data={} else:old_data=json.loads(old_data) data_line={"time":data["runtime"],"dest":data["dest"],"cmd":data["cmd"],"sid":data["sid"],"alias":data["alias"],"collect_time":now_time} line_count=data["tid"] else: #####在文件末尾追加 info=ssh.execute(""" echo '{crontab_format}' >>{path} """.format(crontab_format=crontab_format,path=path)) if not info["status"]: raise CheungSSHError(info["content"]) info=ssh.execute(""" cat {path}|wc -l """.format(path=path))#####统计最后一行的tid if not info["status"]: raise CheungSSHError(info["content"]) line_count=info["content"].split("\r\n")[1]#####自己递增行号 #####修改redis记录 old_data=REDIS.hget("CHB0833279333-1",data["alias"]) if old_data is None:old_data={} else:old_data=json.loads(old_data) data_line={"time":data["runtime"],"dest":data["dest"],"cmd":data["cmd"],"sid":data["sid"],"alias":data["alias"],"collect_time":now_time} #####统一的代码部分 old_data[line_count]=data_line#####把新创建的计划任务行,集合起来 _old_data=json.dumps(old_data,encoding="utf8",ensure_ascii=False) REDIS.hset("CHB0833279333-1",data["alias"],_old_data) cheungssh_info={"status":True,"content":old_data}#####返回原始数据给上层django,最后dumps到前端 else: cheungssh_info["content"]=a["content"] old_data={}#####登录失败后的默认值 except Exception,e: cheungssh_info={"content":str(e),"status":False}
def __init__(self): self.now_time=time.strftime("%Y-%m-%d",time.localtime(time.time())) self.cond=threading.Condition() self.servers=[] self.REDIS=REDIS self.controler=CheungSSHControler() self.get_docker_image_command="docker images" self.get_docker_container_command="docker ps -a" self.image_count=0 #####默认的镜像数量为0 self.container_count=0 self.all_image=[] self.all_container=[]
def init_server_conf(self, sid): #####sid是服务器ID cheungssh_info = {"status": False, "content": ""} try: self.sid = sid conf = CheungSSHControler.convert_id_to_ip(sid) if not conf["status"]: raise CheungSSHError(conf["content"]) self.conf = conf["content"] self.alias = self.conf["alias"] cheungssh_info["status"] = True except Exception, e: cheungssh_info["content"] = "获取服务器配置错误 %s" % str(e) cheungssh_info["status"] = False
def get_logfile_date(logfile, realname, _type): # 获取整个日志文件的天日,格式为:日/月/年 cheungssh_info = {"content": "", "status": False} # 存放所有日期 date = [] try: #####判断是否是远程日志,如果是,则需要提前下载 ##### try: os.makedirs(os.path.dirname(realname)) except: pass if _type == "remote": tfile = realname sid = msgpack.unpackb( REDIS.hget("CHB-0383740494845", tfile.split("/")[-1]))["sid"] #####获取服务器sid conf = CheungSSHControler.convert_id_to_ip(sid) if not conf["status"]: raise IOError(conf["content"]) conf = conf["content"] ssh = CheungSSHFileTransfer() sftp = ssh.login(**conf) if not sftp["status"]: raise IOError(sftp["content"]) t = ssh.download(remote_file=logfile, local_file=realname, tid="") shutil.move( os.path.join(cheungssh_settings.download_dir, realname.split("/")[-1]), realname) if not t["status"]: raise IOError(t["content"]) else: tfile = logfile f = open(tfile) for line in f: try: _date = re.search( "\[([0-9]{1,2}/[A-Z][a-z]{2}/20[0-9]{2}):[0-9]{2}:[0-9]{2}:[0-9]{2}", line).group(1) # 判断是否重复过 if not _date in date: date.append(_date) except: pass data = { "date": date, "type": CheungSSHAnalyLog().logtype, "realname": realname } cheungssh_info = {"content": data, "status": True} except Exception, e: cheungssh_info = {"content": str(e), "status": False}
def login(sid="", rid=""): cheungssh_info = {"content": "", "status": False} try: config = CheungSSHControler.convert_id_to_ip(sid) if not config["status"]: raise CheungSSHError(config["content"]) _config = config["content"] ssh = CheungSSH_SSH() connect = ssh.login(**_config) if connect["status"]: CheungSSHBatchCommand.shell_list.append(ssh) cheungssh_info = {"content": "已就绪", "status": True} else: cheungssh_info = { "content": connect["content"], "status": False } except Exception, e: cheungssh_info = {"content": str(e), "status": False}
def login(sid="", rid=""): #####返回的数据格式{sid:{"content":"...","status":True/False}} cheungssh_info = {"content": "", "status": False} try: config = CheungSSHControler.convert_id_to_ip(sid) if not config["status"]: raise CheungSSHError(config["content"]) _config = config["content"] ssh = CheungSSH_SSH() connect = ssh.login(**_config) if connect["status"]: CheungSSHBatchCommand.shell_list.append( ssh) #####没有的就不存,等要用的时候,找不到直接忽略不执行execute即可 cheungssh_info = {"content": "已就绪", "status": True} else: cheungssh_info = { "content": connect["content"], "status": False } except Exception, e: cheungssh_info = {"content": str(e), "status": False}
def run(self, parameter={}): cheungssh_info = {"status": True, "content": ""} try: task_type = parameter["task_type"] tid = parameter["tid"] multi_thread = parameter["multi_thread"] if not type(multi_thread) == type(False): raise CheungSSHError("CHB0000000010") if task_type == "cmd": cmd = parameter["cmd"] servers = parameter["servers"] if not type(servers) == type([]): raise CheungSSHError("CHB0000000011") total = "total.%s" % tid current = "current.%s" % tid self.REDIS.set(total, len(servers)) self.REDIS.set(current, 0) #CheungSSHConnector.progress[total]=len(servers) #CheungSSHConnector.progress[current]=0 if multi_thread: pool = CheungSSHPool() for s in servers: controler = CheungSSHControler() param = {"cmd": cmd, "sid": s, "tid": tid} pool.add_task(controler.command_controler, param) else: for s in servers: controler = CheungSSHControler() controler.command_controler(cmd=cmd, sid=s, tid=tid) elif task_type == "file": pass else: raise CheungSSHError("CHB0000000009") except Exception, e: cheungssh_info["content"] = str(e) cheungssh_info["status"] = False