Пример #1
0
    def multi_cmd(self):
        print "---going to run cmd---"
        print self.request.POST
        selected_hosts = set(self.request.POST.getlist("selected_hosts[]"))
        cmd = self.request.POST.get("cmd")
        print "-->",selected_hosts,cmd
        #create task info
        task_obj = models.TaskLog(
            task_type = self.task_type,
            user_id = self.request.user.id,
            cmd = cmd,
        )
        task_obj.save()
        task_obj.hosts.add(*selected_hosts) # add many to many relationship
        #task_obj.hosts.add([1,2,3])

        #create task detailed record for all the hosts will be executed later
        for bind_host_id in selected_hosts:
            obj = models.TaskLogDetail(
                child_of_task_id = task_obj.id,
                bind_host_id = bind_host_id,
                event_log = "N/A"
            )
            obj.save()

        # invoke backend multitask script
        p = subprocess.Popen([
            'python',
            settings.MultiTaskScript,
            '-task_id', str(task_obj.id),
            '-run_type', settings.MultiTaskRunType,])
        #], preexec_fn=os.setsid)
        #print '----->pid:', p.pid
        return {"task_id":task_obj.id}
Пример #2
0
    def multi_cmd(self):
        print '---going to run cmd----'
        print '---', self.request.POST  #测试打印  字典
        selected_hosts = set(self.request.POST.getlist(
            "selected_hosts[]"))  #赋值主机列表  取列表用getlist
        cmd = self.request.POST.get("cmd")
        #create task info 数据库tasklog表数据创建  如果直接用object.create 的方法不会返回创建成功。 如果先赋值变量,再save可以看到返回创建成功与否
        task_obj = models.TaskLog(
            task_type=self.task_type,
            user_id=self.request.user.id,
            #manyto many 必须 在创建完纪录后再添加
            cmd=cmd,
        )
        task_obj.save()  #写入到数据库 相当于 create
        #多对多关系不能在创建记录的时候添加,如上的() 需要再创建记录有再添加。  必须在创建完纪录后再添加
        task_obj.hosts.add(
            *selected_hosts
        )  #添加m2m关系  manytomany方式添加列表 用add(*列表)  添加字典是两个* add(**字典)
        #测试打印  添加列表  用*号代替如下添加方式。 添加 manytomany
        #task_obj.hosts.add([1,2,3])

        #================================任务详情表创建信息 #create task detail record for all the hosts will be executed later
        for bind_host_id in selected_hosts:
            obj = models.TaskLogDetail(
                child_of_task_id=task_obj.id,  #总任务的id
                bind_host_id=bind_host_id,  #绑定主机的id
                event_log="N/A",  #现在先把详情 写空
                #剩下的是default 在表里面
            )
            obj.save(
            )  # from django.db import transaction 这里又一个点,如果我有几千台主机这样的话我创建一个任务就会 save相当于 在数据库里面 commit  创建一次任务就有好几千次 commit 压力可想而知;第二点 像上面的任务 执行成功提交,总任务已提交,但是下面执行的是循环,突然断电, 导致已执行的不能回滚,所以我们需要django的 tranaction方法来做到 前面的表格都创建和后面的表格都创建 统一commit。

        #================================任务详情表创建信息

        #这里其实是触发脚本 来在后台进行, 这里肯定是一个独立的脚本 起一个独立的进程,让操作系统维护,  host app下的新建的backends目录

        #调用 paramiko的脚本   脚本路径我们统一在setting中配置  下面添加 脚本的参数

        p = subprocess.Popen([
            'python',
            settings.MultiTaskScript,
            '-task_id',
            str(task_obj.id),
            '-run_type',
            settings.MultiTaskRunType,  #在setting中配置
        ])  #,preexec_fn=os.setsid)
        print '----->pid:', p.pid

        return {'task_id': task_obj.id}
Пример #3
0
    def multi_file_transfer(self):
        print '----going to handle file uploading/download'

        selected_hosts = set(self.request.POST.getlist("selected_hosts[]"))
        transfer_type = self.request.POST.get("file_transfer_type")
        remote_path = self.request.POST.get("remote_path")
        upload_files = self.request.POST.getlist("upload_files[]")
        #create task info
        data_dic = {
            'remote_path': remote_path,
            'upload_files': upload_files,
        }
        task_obj = models.TaskLog(
            task_type=transfer_type,
            user_id=self.request.user.id,
            #manyto many 必须 在创建完纪录后再添加
            cmd=json.dumps(
                data_dic),  #没有单独的存放文件路径 跟文件名的字段 我把信息存在cmd字段内  但是不能存字典 要序列化成字符串
        )
        task_obj.save()
        task_obj.hosts.add(*selected_hosts)  #添加m2m关系
        #task_obj.hosts.add([1,2,3])

        #create task detail record for all the hosts will be executed later
        for bind_host_id in selected_hosts:
            obj = models.TaskLogDetail(
                child_of_task_id=task_obj.id,
                bind_host_id=bind_host_id,
                event_log="N/A",
            )
            obj.save()

        #invoke backend multitask script
        p = subprocess.Popen([
            'python',
            settings.MultiTaskScript,
            '-task_id',
            str(task_obj.id),
            '-run_type',
            settings.MultiTaskRunType,
        ])  #,preexec_fn=os.setsid)
        print '----->pid:', p.pid

        return {'task_id': task_obj.id}
Пример #4
0
    def multi_file_transfer(self):
        print "---going to upload/download files"
        print self.request.POST
        data_dic = {}
        selected_hosts = set(self.request.POST.getlist("selected_hosts[]"))
        transfer_type = self.request.POST.get("file_transfer_type")
        if self.request.POST.get("remote_path"):
            data_dic["remote_path"] = self.request.POST.get("remote_path")
        if self.request.POST.getlist("upload_files[]"):
            data_dic["upload_files"] = self.request.POST.getlist("upload_files[]")
        if self.request.POST.getlist("download_files[]"):
            data_dic["download_files"] = self.request.POST.getlist("download_files[]")


        # create task info
        task_obj = models.TaskLog(
            task_type=transfer_type,
            user_id=self.request.user.id,
            cmd=json.dumps(data_dic)
        )
        task_obj.save()
        task_obj.hosts.add(*selected_hosts)  # add many to many relationship
        # task_obj.hosts.add([1,2,3])

        # create task detailed record for all the hosts will be executed later
        for bind_host_id in selected_hosts:
            obj = models.TaskLogDetail(
                child_of_task_id=task_obj.id,
                bind_host_id=bind_host_id,
                event_log="N/A"
            )
            obj.save()

        # invoke backend multitask script
        p = subprocess.Popen([
            'python',
            settings.MultiTaskScript,
            '-task_id', str(task_obj.id),
            '-run_type', settings.MultiTaskRunType, ])
        # ], preexec_fn=os.setsid)
        # print '----->pid:', p.pid
        return {"task_id": task_obj.id}
Пример #5
0
    def program_update(self):
        print '=====>Ready to:program_update'
        print 'task_type: ', self.task_type
        print 'User id:', self.request.user.id
        selected_hosts = self.request.POST.getlist('host_id_list[]')
        # print selected_hosts

        # 在数据库创建任务信息记录
        task_obj = models.TaskInfo(
            task_type=self.task_type,
            user_id=self.request.user.id,
            cmd=selected_hosts,
        )
        task_obj.save()
        task_obj.hosts.add(*selected_hosts)  # 列表形式前面加*,传入列表的每一个值

        # 在taskInfo表里面创建任务信息时,同事初始化一条到TaskLog表
        for bind_host_id in selected_hosts:
            obj = models.TaskLog(
                child_of_task_id=task_obj.id,
                bind_host_id=bind_host_id,
                event_log="N/A",
            )
            obj.save()
        # invoke multi_task script-- 调用 multi_task.py 脚本
        p = subprocess.Popen([
            'python',
            settings.ProgramHandleScript,
            '-task_id',
            str(task_obj.id),
            '-task_type',
            self.task_type,
        ])
        '''
        for bind_host_id in selected_hosts:
            print models.BindHostToUser.objects.get(id = bind_host_id)
            selected_host_id = models.BindHostToUser.objects.get(id = bind_host_id).host_id
            selected_host_ip = models.Host.objects.get(id = selected_host_id).ip_addr
            print selected_host_ip
        '''
        return {'task_id': task_obj.id}
Пример #6
0
    def multi_script(self):
        print "---going to run script---"
        print self.request.POST
        selected_hosts = set(self.request.POST.getlist("selected_hosts[]"))
        script_type = self.request.POST.get("script_type")
        script_param = self.request.POST.get("script_param")
        script_content = self.request.POST.get("script_content")
        #print "--->",selected_hosts,script_type,script_param,script_content
        script_abs_path = utils.save_script_content(script_type,script_content)
        print "script_abs_path--->",script_abs_path
        #create task info
        task_obj = models.TaskLog(
            task_type = self.task_type,
            user_id = self.request.user.id,
            script_type = script_type,
            script_param = script_param,
            script_path = script_abs_path
        )
        task_obj.save()
        task_obj.hosts.add(*selected_hosts)

        #create task detailed record for all the hosts will be executed later
        for bind_host_id in selected_hosts:
            obj = models.TaskLogDetail(
                child_of_task_id = task_obj.id,
                bind_host_id = bind_host_id,
                event_log = "N/A"
            )
            obj.save()

        # invoke backend multitask script
        p = subprocess.Popen([
            'python',
            settings.MultiTaskScript,
            '-task_id', str(task_obj.id),
            '-run_type', settings.MultiTaskRunType, ])
        # ], preexec_fn=os.setsid)
        # print '----->pid:', p.pid

        return {"task_id": task_obj.id}
Пример #7
0
    def multi_file_transfer(self):
        print "----going to handle file uploading/download"
        selected_hosts = set(self.request.POST.getlist("selected_hosts[]"))
        transfer_type = self.request.POST.get('file_transfer_type')
        remote_path = self.request.POST.get('remote_path')
        upload_files = self.request.POST.getlist('upload_files[]')

        #create task info
        data_dic = {
            'remote_path': remote_path,
            'upload_files': upload_files,
        }
        task_obj = models.TaskLog(
            task_type=transfer_type,
            user_id=self.request.user.id,
            #many to many必须在创建完记录后再添加
            cmd=json.dumps(data_dic),
        )
        task_obj.save()
        task_obj.hosts.add(*selected_hosts)  #添加m2m关系
        #create task detail record for all the hosts will be executed later
        for bind_host_id in selected_hosts:
            obj = models.TaskLogDetail(
                child_of_task_id=task_obj.id,
                bind_host_id=bind_host_id,
                event_log="N/A",
            )
            obj.save()

        #invoke backend multitask script
        p = subprocess.Popen([
            'python',
            settings.MultiTaskScript,
            '-task_id',
            str(task_obj.id),
            '-run_type',
            settings.MultiTaskRunType,
        ])

        return {'task_id': task_obj.id}  #返回任务id
Пример #8
0
 def create_task_log(self,
                     task_type,
                     hosts,
                     expire_time,
                     content,
                     note=None):
     task_log_obj = models.TaskLog(task_type=task_type,
                                   user=self.request.user.userprofile,
                                   cmd=content,
                                   expire_time=int(expire_time),
                                   note=note)
     task_log_obj.save()
     task_log_obj.hosts.add(*hosts)
     #init detail logs
     for h in hosts:
         task_log_detail_obj = models.TaskLogDetail(
             child_of_task_id=task_log_obj.id,
             bind_host_id=h.id,
             event_log='',
             result='unknown')
         task_log_detail_obj.save()
     return task_log_obj
Пример #9
0
    def multi_cmd(self):
        # 批量命令执行函数,负责处理前端提起的任务请求
        #print "====Begin to excute cmd!===="

        cmd = self.request.POST.get("cmd_text")
        selected_hosts = self.request.POST.getlist("selected_host[]")
        #print "=====>", self.task_type, selected_hosts, cmd
        # create task info
        task_obj = models.TaskInfo(
            task_type=self.task_type,
            user_id=self.request.user.id,
            cmd=cmd,
            # manytomany 必须在创建完后再添加
        )
        task_obj.save()
        task_obj.hosts.add(*selected_hosts)  # 列表形式前面加*,传入列表的每一个值

        # 在taskInfo表里面创建任务信息时,同事初始化一条到TaskLog表
        for bind_host_id in selected_hosts:
            obj = models.TaskLog(
                child_of_task_id=task_obj.id,
                bind_host_id=bind_host_id,
                event_log="N/A",
            )
            obj.save()

        # invoke multi_task script-- 调用 multi_task.py 脚本
        p = subprocess.Popen([
            'python',
            settings.MultiTaskScript,
            '-task_id',
            str(task_obj.id),
            '-run_type',
            settings.MultiTaskType,
        ])

        return {'task_id': task_obj.id}
Пример #10
0
    def multi_cmd(self):
        print "---going to run cmd---"
        print self.request.POST
        selected_hosts = set(self.request.POST.getlist("selected_hosts[]"))
        cmd = self.request.POST.get("cmd")
        print '--->', selected_hosts, cmd
        #create task info
        task_obj = models.TaskLog(
            task_type=self.task_type,
            user_id=self.request.user.id,
            #many to many必须在创建完记录后再添加
            cmd=cmd,
        )  #开始写入数据库
        task_obj.save()
        task_obj.hosts.add(*selected_hosts)  #添加m2m

        #create task detail record for all the hosts will be exec
        for bind_host_id in selected_hosts:
            obj = models.TaskLogDetail(
                child_of_task_id=task_obj.id,
                bind_host_id=bind_host_id,
                event_log="N/A",
            )
            obj.save()

        #返回前调脚本 invoke backend multi_task.py script
        p = subprocess.Popen([
            'python',
            settings.MultiTaskScript,
            '-task_id',
            str(task_obj.id),
            '-run_type',
            settings.MultiTaskRunType,
        ])

        return {'task_id': task_obj.id}  #返回任务id