Beispiel #1
0
	def script_init(sid,sfile,username):
		
		sfile=sfile.encode("utf-8")
		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)
			if not login["status"]:raise CheungSSHError(login["content"])
			
			_tmp_data={"status":True,"content":"","progress":"0","tid":tid}
			sftp.write_progress(_tmp_data)
			t=threading.Thread(target=sftp.upload,args=(sfile,dfile,tid))
			t.start()
			cheungssh_info["status"]=True
			cheungssh_info["tid"]=tid
			cheungssh_info["dfile"]=dfile
		except Exception,e:
			cheungssh_info["status"]=False
			cheungssh_info["content"]=str(e)
Beispiel #2
0
	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)
Beispiel #3
0
	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)
Beispiel #4
0
	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 create_task_conf(data,username):
		cheungssh_info={"content":"","status":False}
		try:
			try:
				data=json.loads(data)
			except:
				raise CheungSSHError("CHB0000000025-0")
			if not type({}) == type(data):raise CheungSSHError("CHB0000000025-1")
			
			if data.has_key("tid"):
				taskid=data["tid"]
			else:
				taskid=str(random.randint(9000000000000000,9999999999999999))
			data["taskid"]=taskid
			data["owner"]=username
			data["status"]="新建"
			data["time"]=time.strftime("%Y-%m-%d %H:%M:%S",time.localtime())
			data["model"]="灰度"
			i=0
			for _ in data["servers"]:
				#steps=server["steps"]
				ii=0
				for _ in data["servers"][i]["steps"]:
					stepid=str(random.randint(9000000000000000,9999999999999999))
					#step=data["servers"][i]["steps"][ii],这个步骤就是得到的step的dict
					data["servers"][i]["steps"][ii]["stepid"]=stepid
					ii+=1
				i+=1
			_data=json.dumps(data,encoding="utf8",ensure_ascii=False)
			cheungssh_info["status"]=True
			REDIS.hset("CSSH-R00000000002",taskid,_data)
		except Exception,e:
			cheungssh_info["status"]=False
			cheungssh_info["content"]=str(e)
Beispiel #6
0
 def upload_script_and_execute_command(self, **parameters):
     cheungssh_info = {"status": False, "content": ""}
     #if not parameters["username"]  == "root":
     #	raise CheungSSHError("非管理员账号,不能采集目标服务器资产")
     try:
         ###########上传资产查询文件
         ssh = CheungSSH_SSH()
         login = ssh.login(**parameters)
         if not login["status"]:
             raise CheungSSHError("登录错误: %s" % login["content"])
         try:
             sid = parameters["id"]
             alias = parameters["alias"]
             #########逐个获取每一个资产项目,下面是有自定义资产添加的功能的代码
             info = {}  ##########用于存储一个机器的全部资产收集数据
             #tmp_assets_conf=copy.deepcopy(assets_conf) ########复制一个,用来使用,必须深拷贝
             #custom_assets_class_list=REDIS.get("custom.assets.class.list")
             #if not custom_assets_class_list is None:
             #	custom_assets_class_list=msgpack.unpackb(custom_assets_class_list)#####如果没有自定义资产,那么下面的json代码就要报错
             #else:
             #	custom_assets_class_list={}
             #tmp_assets_conf=dict(tmp_assets_conf,**custom_assets_class_list)
             #for asset in tmp_assets_conf.keys():
             _assets_conf = copy.deepcopy(self.assets_conf)
             for asset in _assets_conf.keys():
                 ######不能用中文
                 #####print tmp_assets_conf[asset]["command"]
                 if _assets_conf[asset].has_key("asset_type"):
                     if _assets_conf[asset]["asset_type"] == "static":
                         #######静态资产,不执行命令
                         continue
                 if asset == "time":
                     _assets_conf[asset][
                         "value"] = self.time  ########在原来的assets_conf上加了value字段
                     continue
                 cmd = _assets_conf[asset]["command"][ssh.kws['os_type']]
                 data = ssh.execute(cmd=cmd,
                                    sid=parameters["id"],
                                    tid=0,
                                    ignore=True)  ######执行资产列表的命令
                 if data["status"] is True:
                     #######判断是否执行成功
                     try:
                         result = "\n".join(
                             data["content"].split('\r\n')[1:-1])
                         #print data['content'].split('OKD'),8888888888888888888888888888888888888888888
                     except Exception, e:
                         result = "获取数据失败 %s" % str(e)
                 else:
                     result = "获取数据失败 %s" % data["content"]
                 _assets_conf[asset][
                     "value"] = result  ########在原来的assets_conf上加了value字段
             self.assets_data[sid] = {
                 "sid": sid,
                 "alias": alias,
                 "data": _assets_conf
             }
             print "已经取得资产信息."
         except ValueError:
             raise CheungSSHError("获取资产数据失败!")
	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)
Beispiel #8
0
	def modify_crontab_list(self,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["time"],cmd=data["cmd"],dest=data["description"])
		try:
			#####判断sed的分隔符
			_sed_t=Crontab.get_sed_separate(crontab_format)#####获取判断字符是否符合规格
			if not _sed_t["status"]:
				raise CheungSSHError(_sed_t["content"])
			else:
				char=_sed_t["content"]
			ssh=CheungSSH_SSH()
			configuration = ServersInventory().get_server(sid=data["sid"])
			if not configuration["status"]:raise IOError(configuration["content"])
			a=ssh.login(**configuration["content"])
			if a["status"]:
				username=configuration["content"]["username"]
				path=os.path.join("/var/spool/cron/",username)
				if data["action"]=="modify":
					#####修改计划任务列表
					info=ssh.execute("sed -i '{id}s{char}.*{char}{crontab_format}{char}' {path}".format(char=char,id=data["line_id"],path=path,crontab_format=crontab_format))######替换新的内容
					if not info["status"]:
						raise CheungSSHError(info["content"])
				else:
					#####在文件末尾追加
					info=ssh.execute(""" echo  '{crontab_format}' >>{path} """.format(crontab_format=crontab_format,path=path))
					if not info["status"]:
						raise CheungSSHError(info["content"])
				cheungssh_info["status"] = True
			else:
				cheungssh_info["content"]=a["content"]
		except Exception,e:
			cheungssh_info={"content":str(e),"status":False}
 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)
Beispiel #10
0
	def sudo_login(self):
		cheungssh_info={"status":False,"content":""}
		try:
			if self.username=="root":raise CheungSSHError("root不能sudo")
			self.shell.send('sudo su - root\n')
			buff=''
			_buff=""
			while True:
				buff+=self.shell.recv(1024)
				if re.search('(\[sudo\] password for {username})|(\[sudo\] {username}.*((assw(or)?d)|密码))'.format(username=self.username),buff.split('\n')[-1]):
					self.shell.send('%s\n' %self.sudo_password) 
					while True:
						_buff+=self.shell.recv(1024)
						if re.search('Sorry, try again',_buff):
							raise CheungSSHError("sudo密码错误")
						elif re.search('%s.*sudoers' %self.username,_buff):
							raise CheungSSHError("您的账户没有配置sudo权限")
						elif re.search(self.base_prompt,_buff.split('\n')[-1]):
							
							cheungssh_info["status"]=True
							cheungssh_info["content"]=""	
							return cheungssh_info
				elif re.search(self.base_prompt,buff.split('\n')[-1]): 
					
					cheungssh_info["status"]=True
					return cheungssh_info
		except Exception,e:
			cheungssh_info["status"]=False
			cheungssh_info["content"]=str(e)
Beispiel #11
0
    def su_login(self):
        cheungssh_info = {"status": False, "content": ""}
        try:
            if self.username == "root": raise CheungSSHError("您当前已经是超级管理员!")
            self.shell.send("su  - root\n")
            buff = ''
            _buff = ""
            while True:
                buff += self.shell.recv(1024)
                if re.search("assword|密码", buff.split("\n")[-1]):
                    self.shell.send("%s\n" % self.su_password)
                    while True:
                        _buff += self.shell.recv(1024)
                        if re.search("^su",
                                     _buff.split("\n")[-2]) or re.search(
                                         "Authentication",
                                         _buff.split("\n")[-2]):
                            raise CheungSSHError("su密码错误")
                        elif re.search(self.base_prompt,
                                       _buff.split("\n")[-1]):
                            cheungssh_info["status"] = True
                            return cheungssh_info

        except Exception, e:
            cheungssh_info["status"] = False
            cheungssh_info["content"] = str(e)
Beispiel #12
0
	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}
Beispiel #14
0
	def upload(self,local_file='',remote_file='',tid=""):
		local_file=local_file.encode('utf-8')#####前端传递过来的时候是unicode
		#######默认全显示644
		transfer_type="upload"
		self.transfer_type="upload"
		cheungssh_info={"status":False,"content":""}
		try:
			if os.path.isdir(local_file):
				############目录传输
				raise CheungSSHError("CHB0000000018")
			else:
				#######普通文件上传
				if remote_file.endswith('/'):
					#########如果指定的就是一个正确的目录,有/结尾,则进行判断
					self.sftp.listdir(remote_file)
					#########当用户制定了/结尾的时候往往没有输入文件名,所以这里需要组装目录+文件名,如果没有修改,那么传输的时候回出现从错误:Failure
					remote_file=os.path.join(remote_file,os.path.basename(local_file))
				else:
					try:
						self.sftp.listdir(remote_file)  #########如果是目录,但是没有/结尾
	
						#########用户输入的是一个目录,但是没有/结尾,所以这里需要组装目录+文件名,如果没有修改,那么传输的时候回出现从错误:Failure
						remote_file=os.path.join(remote_file,os.path.basename(local_file))
					except Exception,e:
						e=str(e)
						if re.search('Permission',e):
							#####权限拒绝,直接 报错
							raise CheungSSHError(e)
						else:
							##########忽略,用户输入的可能是一个文件路径,不是文件夹
							pass
				data={"tid":tid,"content":"","status":True}
				callback= functools.partial(self.set_progress,data)
				print "本地文件路径:",local_file,"远程路径:",remote_file
				self.sftp.put(local_file,remote_file,callback=callback)
				self.sftp.chmod(remote_file,0755)#####赋予执行权限
				#########归属和数组可能修改失败
				cheungssh_info["remote_file"]=remote_file ######为了方便部署,把源文件和目标文件都传递
				cheungssh_info["local_file"]=local_file
				cheungssh_info["transfer_type"]=transfer_type
				cheungssh_info["tid"]=tid
				cheungssh_info["progress"]=0   #######初始化进度
				cheungssh_info["status"]=True
				###########可以考虑返回修改了归属和权限
		except Exception,e:
			e=str(e)
			if re.search('No such file',e):
				cheungssh_info["content"]="您指定的源文件路径或者服务器目标路径不存在,请检查"
			elif re.search('Permission',e):
				cheungssh_info["content"]="您当前Linux账号无权上传到目录[{remote_file}]下".format(remote_file=remote_file)
			else:
				cheungssh_info["content"]="未知错误:{e},请联系CheungSSH作者".format(e=e)
			cheungssh_info["status"]=False
			cheungssh_info["tid"]=tid
			REDIS.set("progress.%s"%tid,json.dumps(cheungssh_info,encoding="utf8",ensure_ascii=False))
    def execute_app(appid, username, is_super):
        cheungssh_info = {"content": [], "status": False}
        app_conf = False  #####设置默认的值,下面要对这个进行判断
        try:
            data = CheungSSHAppAdmin.get_app_list(username,
                                                  is_super)  #####获取全部app数据,
            if not data["status"]:
                raise CheungSSHError("读取应用配置出错![%s]" % data["content"])
            else:
                content = data["content"]
                app_conf = CheungSSHAppAdmin.check_appid_conf(
                    content, appid)  #####查询app对应的配置信息
                if not app_conf["status"]:
                    raise CheungSSHError("该APP不存在! [%s]" % app_conf["content"])
                #####得到了数据,获取sid
                sid = app_conf["content"]["sid"]  #####数据格式为alias,sid,command等
                server_conf = cheungssh_modul_controler.CheungSSHControler.convert_id_to_ip(
                    sid)  #####获取服务器的配置
                if not server_conf["status"]:
                    raise CheungSSHError("应用数据处理失败!%s" %
                                         server_conf["content"])
                #####在开始之前,修改app状态
                CheungSSHAppAdmin.set_app_status("执行中", "", appid)
                #####成功获取服务器配置信息,开始登录
                ssh = CheungSSH_SSH()
                tmp = ssh.login(**server_conf["content"])
                #####检查是否登录成功
                if not tmp["status"]:
                    raise CheungSSHError(tmp["content"])
                #####ssh登录成功了
                command = app_conf["content"]["app_command"]  #####app的命令
                check_command = app_conf["content"][
                    "app_check_command"]  #####检查命令

                command_result = ssh.execute(command,
                                             ignore=True)  #####APP的执行结果
                if re.search("echo *\$\? *$|^ *$", check_command):
                    print "执行系统定义的检查命令"
                    #####如果前端设定的检查命令是echo $?或者为空,则默认使用命令执行的结果;
                    cheungssh_info = command_result
                    print cheungssh_info, 55555555555555555555
                else:
                    check_command_result = ssh.execute(
                        check_command)  #####app执行后检查命令的结果
                    cheungssh_info["content"] = "%s </br> %s" % (
                        command_result["content"],
                        check_command_result["content"]
                    )  #####把执行命令和命令检查的结果叠加在一起
                    cheungssh_info["status"] = check_command_result[
                        "status"]  #####把命令检查的状态设定为命令最终执行的状态
        except Exception, e:
            cheungssh_info["content"] = str(e)
            cheungssh_info["status"] = False
Beispiel #16
0
	def upload_script_and_execute_command(self,**parameters):
		cheungssh_info={"status":False,"content":""}
		#if not parameters["username"]  == "root":
		#	raise CheungSSHError("非管理员账号,不能采集目标服务器资产")
		try:
			
			ssh=CheungSSH_SSH()
			login=ssh.login(**parameters)
			if not login["status"]:
				raise CheungSSHError("登录错误: %s" %login["content"])
			try:
				sid = parameters["id"]
				alias =parameters["alias"]
				
				info={} 
				#tmp_assets_conf=copy.deepcopy(assets_conf) 
				#custom_assets_class_list=REDIS.get("custom.assets.class.list")
				#if not custom_assets_class_list is None:
				#	custom_assets_class_list=msgpack.unpackb(custom_assets_class_list)
				#else:
				#	custom_assets_class_list={}
				#tmp_assets_conf=dict(tmp_assets_conf,**custom_assets_class_list)
				#for asset in tmp_assets_conf.keys():
				_assets_conf=copy.deepcopy(self.assets_conf)
				for asset in _assets_conf.keys():
					
					
					if _assets_conf[asset].has_key("asset_type"):
						if _assets_conf[asset]["asset_type"]=="static":
							
							continue
					if asset=="time":
						_assets_conf[asset]["value"]=self.time 
						continue
					cmd=_assets_conf[asset]["command"][ssh.kws['os_type']]
					data=ssh.execute(cmd=cmd,sid=parameters["id"],tid=0,ignore=True) 
					if data["status"] is True:
						
						try:
							result="\n".join(data["content"].split('\r\n')[1:-1])
							#print data['content'].split('OKD'),8888888888888888888888888888888888888888888
						except Exception,e:
							result="获取数据失败 %s" % str(e)
					else:
						result="获取数据失败 %s" % data["content"]
					_assets_conf[asset]["value"]=result 
				self.assets_data[sid]={"sid":sid,"alias":alias,"data":_assets_conf}
				print "已经取得资产信息."
			except ValueError:
				raise CheungSSHError("获取资产数据失败!")
Beispiel #17
0
	def download(self,remote_file='',local_file='',tid=""):
		transfer_type="download"
		self.transfer_type="download"
		cheungssh_info={"status":False,"content":""}
		try:
			_local_file=os.path.basename(local_file) ########处理用户输入的路径,只能上传downlod目录下的文件
			local_file=os.path.join(cheungssh_settings.download_dir,_local_file)
			###########本地已经存在文件了,需要重命名,重命名为IP后缀
			if os.path.isfile(local_file):local_file="%s_%s"%(local_file,self.ip)
			try:
				self.sftp.listdir(remote_file)
				#######下载目录模块
			except Exception,e:
				######单个文件下载
				try: #########开始下载文件
					data={"tid":tid,"content":"","status":True}
					callback= functools.partial(self.set_progress,data)
					self.sftp.get(remote_file,local_file,callback=callback)
					cheungssh_info["remote_file"]=remote_file ######为了方便部署,把源文件和目标文件都传递
					cheungssh_info["local_file"]=local_file
					cheungssh_info["transfer_type"]=transfer_type
					cheungssh_info["tid"]=tid
					cheungssh_info["progress"]=0   #######初始化进度
					cheungssh_info["status"]=True
				except Exception,e:
					if e==2:raise CheungSSHError("源文件[%s]不存在"%remote_file)######源文件不存在
					else:raise IOError(str(e))
Beispiel #18
0
 def command_controler(self, tid='', cmd='', sid=""):
     log_name = "log.%s.%s" % (tid, sid)
     log_content = {
         "content": "",
         "stage": "done",  #######running/done
         "status": False,  #####这里的status不是API的status。是用来显示命令执行状态的
     }
     cheungssh_info = {"content": "", "status": False}
     try:
         current = "current.%s" % tid
         data = self.connect(sid=sid)
         if data["status"]:
             #####登录成功
             ssh = data["content"]  #####获取shell
             #####开始成功的操作
             self.SSH.execute(cmd=cmd, sid=sid, tid=tid)
             ######执行结束后,记录进度
             cheungssh_info["status"] = True
         else:
             raise CheungSSHError(data["content"])  #####登录失败了,直接下去
     except Exception, e:
         print "程序错误", e
         log_content["content"] = str(e)
         #####程序错误,需要在这里写入命令的日志
         log_content = json.dumps(log_content,
                                  encoding="utf8",
                                  ensure_ascii=False)
         self.REDIS.rpush(log_name,
                          log_content)  #######写入当前服务器的日志,如果执行正确则在execute写入
         #######如果执行过程是正确的,那么在execute处写入日志
         cheungssh_info["content"] = str(e)
         cheungssh_info["status"] = False
Beispiel #19
0
    def command_controler(self, tid='', cmd='', sid=""):
        log_name = "log.%s.%s" % (tid, sid)
        log_content = {
            "content": "",
            "stage": "done",
            "status": False,
        }
        cheungssh_info = {"content": "", "status": False}
        try:
            current = "current.%s" % tid
            data = self.connect(sid=sid)
            if data["status"]:

                ssh = data["content"]

                self.SSH.execute(cmd=cmd, sid=sid, tid=tid)

                cheungssh_info["status"] = True
            else:
                raise CheungSSHError(data["content"])
        except Exception, e:
            print "程序错误", e
            log_content["content"] = str(e)

            log_content = json.dumps(log_content,
                                     encoding="utf8",
                                     ensure_ascii=False)
            self.REDIS.rpush(log_name, log_content)

            cheungssh_info["content"] = str(e)
            cheungssh_info["status"] = False
Beispiel #20
0
    def download(self, remote_file='', local_file='', tid=""):
        transfer_type = "download"
        self.transfer_type = "download"
        cheungssh_info = {"status": False, "content": ""}
        try:
            _local_file = os.path.basename(local_file)
            local_file = os.path.join(cheungssh_settings.download_dir,
                                      _local_file)

            if os.path.isfile(local_file):
                local_file = "%s_%s" % (local_file, self.ip)
            try:
                self.sftp.listdir(remote_file)

            except Exception, e:

                try:
                    data = {"tid": tid, "content": "", "status": True}
                    callback = functools.partial(self.set_progress, data)
                    self.sftp.get(remote_file, local_file, callback=callback)
                    cheungssh_info["remote_file"] = remote_file
                    cheungssh_info["local_file"] = local_file
                    cheungssh_info["transfer_type"] = transfer_type
                    cheungssh_info["tid"] = tid
                    cheungssh_info["progress"] = 0
                    cheungssh_info["status"] = True
                except Exception, e:
                    if e == 2: raise CheungSSHError("源文件[%s]不存在" % remote_file)
                    else: raise IOError(str(e))
	def run(self):
		cheungssh_info={"content":"","status":False}
		try:
			if self.parameters["task_type"]=="start":
				self.task_type="start"
			elif self.parameters["task_type"]=="stop":
				self.task_type="stop"
			elif self.parameters["task_type"]=="delete":
				self.task_type="rm -f"
			else:
				raise CheungSSHError("CHB0000000013")
			pool=CheungSSHPool()
			#####获取总的命令个数, 服务器个数 * 每个服务器的docker数量
			total_docker=0
			for sid in self.parameters["servers"].keys():
				docker_count=len(self.parameters["servers"][sid]) #####服务器对应的docker数量
				total_docker+=docker_count
			self.REDIS.set("log.%s.total" % self.tid,total_docker) #####总共的docker数量
			self.REDIS.set("log.%s.current" % self.tid,0)
			for sid in self.parameters["servers"].keys():
				cmd="docker  %s %s"
				containers=self.parameters["servers"][sid]
				data={
					"cmd":cmd,
					"containers":containers,
					"sid":sid,
				}
				pool.add_task(self.execute_docker,data)
			cheungssh_info["status"]=True
		except Exception,e:
			cheungssh_info["status"]=False
			cheungssh_info["content"]=str(e)
Beispiel #22
0
	def login(self,**kws):
		cheungssh_info={"status":False,"content":""}
		self.kws=kws
		try:
			self.owner=kws["owner"]
			self.su=kws["su"]
			self.sudo=kws["sudo"]
			self.username=kws["username"]
			self.password=kws["password"]
			self.port=kws["port"]
			self.ip=kws["ip"]
			self.sudo=kws["sudo"]
			self.sudo_password=kws["sudo_password"]
			self.su=kws["su"]
			self.su_password=kws["su_password"]
			self.port = int(self.port)
			self.os_type=kws["os_type"]
			self.sid=kws["id"]
			ssh = paramiko.SSHClient()
			ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
			ssh.connect(self.ip,self.port,self.username,self.password)
			self.channel=ssh
			self.sftp = self.channel.open_sftp()
			self.shell = ssh.invoke_shell(width=1000,height=1000)
			data=self.clean_buffer()
			if not data["status"]:raise CheungSSHError(data["content"])
			if self.sudo=="Y":
				_sudo=self.sudo_login()
				if _sudo["status"]:
					pass
				else:
					raise CheungSSHError(_sudo["content"])
			elif self.su=="Y":
				_su=self.su_login()
				if _su["status"]:
					#self.get_prompt()
					pass
				else:
					raise CheungSSHError(_su["content"])
			"""data=self.get_prompt()#换成设置统一prompt
			if not data["status"]:
				raise CheungSSHError(data["content"])
			else:
				cheungssh_info["content"]=data["content"]"""
			cheungssh_info["status"]=True
		except KeyError,e:
			cheungssh_info["content"]="缺少字段 %s" %str(e)
Beispiel #23
0
    def execute_app(appid, username, is_super):
        cheungssh_info = {"content": [], "status": False}
        app_conf = False
        try:
            data = CheungSSHAppAdmin.get_app_list(username, is_super)
            if not data["status"]:
                raise CheungSSHError("读取应用配置出错![%s]" % data["content"])
            else:
                content = data["content"]
                app_conf = CheungSSHAppAdmin.check_appid_conf(content, appid)
                if not app_conf["status"]:
                    raise CheungSSHError("该APP不存在! [%s]" % app_conf["content"])

                sid = app_conf["content"]["sid"]
                server_conf = cheungssh_modul_controler.CheungSSHControler.convert_id_to_ip(
                    sid)
                if not server_conf["status"]:
                    raise CheungSSHError("应用数据处理失败!%s" %
                                         server_conf["content"])

                CheungSSHAppAdmin.set_app_status("执行中", "", appid)

                ssh = CheungSSH_SSH()
                tmp = ssh.login(**server_conf["content"])

                if not tmp["status"]:
                    raise CheungSSHError(tmp["content"])

                command = app_conf["content"]["app_command"]
                check_command = app_conf["content"]["app_check_command"]

                command_result = ssh.execute(command, ignore=True)
                if re.search("echo *\$\? *$|^ *$", check_command):
                    print "执行系统定义的检查命令"

                    cheungssh_info = command_result
                    print cheungssh_info, 55555555555555555555
                else:
                    check_command_result = ssh.execute(check_command)
                    cheungssh_info["content"] = "%s </br> %s" % (
                        command_result["content"],
                        check_command_result["content"])
                    cheungssh_info["status"] = check_command_result["status"]
        except Exception, e:
            cheungssh_info["content"] = str(e)
            cheungssh_info["status"] = False
	def local_upload(self,sid="",sfile="",dfile="",owner="",stepid=""):
		cheungssh_info={"status":False,"content":""}
		try:
			host=cheungssh_modul_controler.CheungSSHControler.convert_id_to_ip(sid)
			if not host["status"]:raise CheungSSHError(host['content'])
			_host_info=host['content']
			sftp=CheungSSHLocalUpload(stepid)
			login=sftp.login(**_host_info)
			if not login["status"]:raise CheungSSHError(login["content"])
			cheungssh_info=sftp.upload(local_file=sfile,remote_file=dfile,tid=self.tid)
			if cheungssh_info["status"]:
				cheungssh_info["progress"]=100
				cheungssh_deployment_admin.DeploymentAdmin.set_progress(self.tid,stepid,cheungssh_info)
			
		except Exception,e:
			cheungssh_info["content"]=str(e)
			cheungssh_info["status"]=False
Beispiel #25
0
	def login_ssh(self,conf):#####服务器的配置信息dict
		#####在clone之前,先执行这
		cheungssh_info={"content":"","status":False}
		try:
			data=self.login(**conf)
			if not data["status"]:raise CheungSSHError(data["content"])
			cheungssh_info["status"]=True
		except Exception,e:
			cheungssh_info["content"]=str(e)
			cheungssh_info["status"]=False
Beispiel #26
0
 def execute_command(self):
     cheungssh_info = {"status": False, 'content': "", "ask": False}
     try:
         #####每次执行均产生一个新的ID,避免重复
         #########优先考虑POST,也可以用GET,字符可能需要转义
         parameter = self.r.POST.get("parameters") or self.r.GET.get(
             "parameters")  #####优先获取POST,如果没有则获取GET
         try:
             parameter = json.loads(parameter)
         except Exception, e:
             raise CheungSSHError("错误码:CHB0000000001")
         try:
             cmd = parameter["cmd"]
         except:
             raise CheungSSHError("错误码:CHB0000000002")
         ##### 检查是否命中黑名单
         try:
             _cmd = json.loads(cmd)
         except Exception, e:
             _cmd = [cmd]
Beispiel #27
0
 def get_command_result(self):
     #cheungssh_info={"content":{"content":"","stage":"running","status":None}, "status":True,"progress":0}
     cheungssh_info = {
         "content": {
             #"progress.1" :{"content":"","stage":"running","status":None}
         },
         "status": True,
         "progress": 0
     }
     tid = self.r.GET.get("tid")
     try:
         total = self.REDIS.get("total.%s" % tid)
         current = self.REDIS.get("current.%s" % tid)
         try:
             if total is None or current is None:
                 print "没有产生进度,默认为0"
                 progress = 0  #####进度还没有产生的可能
             else:
                 progress = "%0.2f" % (float(current) / float(total) * 100)
                 print "取得进度", progress, total, current
         except Exception, e:
             raise CheungSSHError("CHB0000000012")
         cheungssh_info["progress"] = progress
         all_sid = "{tid}.all_sid".format(tid=tid)
         all_sid = self.REDIS.get(all_sid)
         if all_sid is None:
             all_sid = []
         else:
             all_sid = json.loads(all_sid)
         for sid in all_sid:
             content = ""  #####存储读取到的命令集合
             log_name = "log.%s.%s" % (tid, sid)
             LLEN = self.REDIS.llen(log_name)  #####获取当前redeis队列的长度,一次读完
             if not LLEN == 0:
                 for i in xrange(LLEN):
                     _content = self.REDIS.lpop(log_name)
                     _content = json.loads(_content)  #####从redis读取后转换dict
                     content += _content["content"]
                     key = sid
                     content = re.sub("""\x1B\[[0-9;]*[mK]""", "", content)
                     cheungssh_info["content"][key] = {
                         "content": content,
                         "stage": _content["stage"],
                         "status": _content["status"]
                     }
                 # cheungssh_info["content"]["content"]=re.sub("""\\[6;1H|(\){1,}|\\[J\\[H""","",cheungssh_info["content"]["content"])
             if progress == "100.00":
                 self.REDIS.delete(log_name)
         if progress == "100.00":
             ##### 删除数据
             self.REDIS.delete("{tid}.all_sid".format(tid=tid))
             self.REDIS.delete("current.%s" % tid)
             self.REDIS.delete("total.%s" % tid)
         cheungssh_info["status"] = True
	def git(self,url,dest_dir,stepid):
		
		cheungssh_info={"status":False,"content":""}
		try:
			git_admin=CheungSSHGit()
			data=git_admin.login_ssh(self.conf)
			if not data["status"]:raise CheungSSHError(data["content"])
			cheungssh_info=git_admin.clone(url=url,dest_dir=dest_dir,taskid=self.tid,alias=self.alias,stepid=stepid)
		except Exception,e:
			cheungssh_info["content"]=str(e)
			cheungssh_info["status"]=False
Beispiel #29
0
	def add_server(self, **kws):
		try:
			######先查询
			if self.query_server(alias=kws["alias"])["status"]:
				raise CheungSSHError("别名【{alias}】早已存在,请更换。".format(alias=kws["alias"]))
			#######创建
			configuration = ServersList(**kws)
			configuration.save()
			self.cheungssh_info= {"content":configuration.id,"status":True}
		except Exception,e:
			self.cheungssh_info= {"status":False,"content":str(e)}
Beispiel #30
0
	def delete_crontab_list(self,data):
		cheungssh_info={"content":"","status":False}
		try:
			configuration = ServersInventory().get_server(sid=data["sid"])
			if not configuration["status"]:raise IOError(configuration["content"])
			ssh=CheungSSH_SSH()
			status=ssh.login(**configuration["content"])
			if not status["status"]:
				######登录是失败的
				raise CheungSSHError(status["content"])
			else:
				info=ssh.execute("whoami")
				if info["status"]:
					path=os.path.join("/var/spool/cron/",configuration["content"]["username"])
					info=ssh.execute("sed -i '{line_id}s/.*/#/' {path}".format(line_id=data["line_id"],path=path))#####删除远程服务器上的
					if not info["status"]:
						raise CheungSSHError(info["content"])
					cheungssh_info["status"]=True
		except Exception,e:
			cheungssh_info["status"]=False
			cheungssh_info["content"]=str(e)