Exemplo n.º 1
0
    def get(self, *args, **kwargs):
        cmd_name = self.get_argument('cmd_name', default=None, strip=True)
        page_size = self.get_argument('page', default=1, strip=True)
        limit = self.get_argument('limit', default=10000, strip=True)
        limit_start = (int(page_size) - 1) * int(limit)
        cmd_list = []

        with DBContext('readonly') as session:
            count = session.query(CmdList).count()
            cmd_info = session.query(CmdList).order_by(
                CmdList.cmd_id).offset(limit_start).limit(int(limit))

            if cmd_name:
                cmd_info = session.query(CmdList).filter(
                    CmdList.cmd_name.like(cmd_name + '%')).order_by(
                        CmdList.cmd_id).offset(limit_start).limit(int(limit))

        for msg in cmd_info:
            data_dict = model_to_dict(msg)
            data_dict['ctime'] = str(data_dict['ctime'])
            data_dict['utime'] = str(data_dict['utime'])
            cmd_list.append(data_dict)

        kwargs = {"data": cmd_list, "code": 0, "count": count, "msg": '获取成功'}
        self.write(kwargs)
Exemplo n.º 2
0
    def get(self, *args, **kwargs):
        page_size = self.get_argument('page', default=1, strip=True)
        list_history = self.get_argument('history', default=None, strip=True)
        limit = self.get_argument('limit', default=10, strip=True)
        limit_start = (int(page_size) - 1) * int(limit)
        task_list = []
        username = self.get_current_user().decode('utf-8')
        if list_history == 'history':
            with DBContext('readonly') as session:
                count = session.query(TaskList).filter(
                    TaskList.schedule == 'OK').count()
                task_info = session.query(TaskList).filter(
                    TaskList.schedule == 'OK').order_by(
                        -TaskList.stime,
                        -TaskList.list_id).offset(limit_start).limit(
                            int(limit))
        else:
            with DBContext('readonly') as session:
                count = session.query(TaskList).filter(
                    TaskList.schedule != 'OK').count()
                task_info = session.query(TaskList).filter(
                    TaskList.schedule != 'OK').order_by(
                        -TaskList.stime,
                        -TaskList.list_id).offset(limit_start).limit(
                            int(limit))

        for msg in task_info:
            data_dict = model_to_dict(msg)
            data_dict['username'] = username
            data_dict['ctime'] = str(data_dict['ctime'])
            data_dict['stime'] = str(data_dict['stime'])
            task_list.append(data_dict)

        kwargs = {"data": task_list, "code": 0, "count": count, "msg": '获取成功'}
        self.write(kwargs)
Exemplo n.º 3
0
    def get(self, *args, **kwargs):
        list_id = self.get_argument('list_id', default=None, strip=True)
        if not list_id:
            self.write(dict(status=-1, msg='订单ID不能为空'))
            return

        with DBContext('readonly') as session:
            task_info = session.query(TaskList).filter(
                TaskList.list_id == list_id).all()
            argsinfo = session.query(ArgsList.args_name,
                                     ArgsList.args_self).all()

        args_record = []
        new_agrs_dict = {}
        for msg in task_info:
            data_dict = model_to_dict(msg)
            data_dict['ctime'] = str(data_dict['ctime'])
            data_dict['stime'] = str(data_dict['stime'])
            data_dict['username'] = self.get_current_user().decode("utf-8")

        args_dict = literal_eval(data_dict.get('args', None))

        if args_dict:
            for k, v in args_dict.items():
                for i in argsinfo:
                    args_record.append(i[1])
                    if i[1] == k:
                        new_agrs_dict[i[0]] = v
                if k not in args_record:
                    new_agrs_dict[k] = v

        data_dict['new_agrs'] = new_agrs_dict
        data_dict['args_keys'] = list(new_agrs_dict.keys())

        self.write(dict(status=0, msg='获取订单信息成功', data=data_dict))
Exemplo n.º 4
0
    def get(self, *args, **kwargs):
        page_size = self.get_argument('page', default=1, strip=True)
        limit = self.get_argument('limit', default=200, strip=True)
        limit_start = (int(page_size) - 1) * int(limit)
        temp_list = []

        with DBContext('readonly') as session:
            count = session.query(TempList).count()
            temp_info = session.query(TempList).order_by(
                TempList.temp_id).offset(limit_start).limit(int(limit))

        for msg in temp_info:
            data_dict = model_to_dict(msg)
            data_dict.pop('ctime')
            data_dict.pop('utime')
            temp_list.append(data_dict)

        kwargs = {"data": temp_list, "code": 0, "count": count, "msg": '获取成功'}
        self.write(kwargs)
Exemplo n.º 5
0
    def get(self, *args, **kwargs):
        temp_id = self.get_argument('temp_id', default=None, strip=True)
        temp_list = []
        if not temp_id:
            self.write(dict(status=-1, msg='模板ID不能为空'))
            return

        with DBContext('readonly') as session:
            print(
                session.query(TempDetails).filter(
                    TempDetails.temp_id == temp_id).order_by(
                        TempDetails.group, TempDetails.level))
            temp_info = session.query(TempDetails).filter(
                TempDetails.temp_id == temp_id).order_by(
                    TempDetails.group, TempDetails.level).all()

        for msg in temp_info:
            data_dict = model_to_dict(msg)
            data_dict.pop('utime')
            temp_list.append(data_dict)

        kwargs = {"data": temp_list, "code": 0, "msg": '获取成功'}
        self.write(kwargs)
Exemplo n.º 6
0
    def put(self, *args, **kwargs):
        data = json.loads(self.request.body.decode("utf-8"))
        list_id = data.get('list_id', None)
        sched_id = data.get('sched_id', None)
        task_handle = data.get('task_handle', None)
        hand_task = data.get('hand_task', None)

        if task_handle == "all_hand" and list_id:
            if not hand_task:
                self.write(dict(status=-1, msg='任务名称不正确'))
                return

            ### 审批所有
            with DBContext('default') as session:
                session.query(TaskSched).filter(
                    TaskSched.list_id == list_id,
                    TaskSched.task_name == hand_task).update({
                        TaskSched.task_status:
                        '1',
                        TaskSched.trigger:
                        'pass_hand'
                    })
                session.commit()
            self.write(dict(status=0, msg='审核成功'))
            return

        if not list_id or not sched_id:
            self.write(dict(status=-1, msg='参数不能为空'))
            return

        with DBContext('readonly') as session:
            sched_info = session.query(TaskSched).filter(
                TaskSched.sched_id == sched_id)

        for msg in sched_info:
            data_dict = model_to_dict(msg)

        if task_handle == "redo":
            ### 重做,把任务修改为等待运行
            with DBContext('default') as session:
                session.query(TaskSched).filter(
                    TaskSched.list_id == list_id,
                    TaskSched.exec_ip == data_dict['exec_ip'],
                    TaskSched.task_group == data_dict['task_group'],
                    TaskSched.task_level >= data_dict['task_level'],
                    TaskSched.task_status != '5').update(
                        {TaskSched.task_status: '1'})
                session.commit()
            self.write(dict(status=0, msg='成功'))
            return

        elif task_handle == "stop":
            ### 终止当前,把当前主机任务组修改为已完成
            with DBContext('default') as session:
                session.query(TaskSched).filter(
                    TaskSched.list_id == list_id,
                    TaskSched.exec_ip == data_dict['exec_ip'],
                    TaskSched.task_group == data_dict['task_group']).update(
                        {TaskSched.task_status: '3'})
                session.commit()
            self.write(dict(status=0, msg='终止当前组成功'))
            return

        elif task_handle == "hand":
            ### 手动审批
            with DBContext('default') as session:
                session.query(TaskSched).filter(
                    TaskSched.sched_id == sched_id).update({
                        TaskSched.task_status:
                        '1',
                        TaskSched.trigger:
                        'pass_hand'
                    })
                session.commit()
            self.write(dict(status=0, msg='手动审核成功'))
            return

        self.write(dict(status=-2, msg='参数有误'))
Exemplo n.º 7
0
    def get(self, *args, **kwargs):
        list_id = self.get_argument('list_id', default=1, strip=True)
        group = self.get_argument('task_group', default=None, strip=True)
        hosts = self.get_argument('exec_ip', default=None, strip=True)
        group_list = []
        sched_list = []
        hand_list = []
        args_record = []
        new_agrs_dict = {}
        username = self.get_current_user().decode('utf-8')

        if not list_id:
            self.write(dict(status=-1, msg='订单ID不能为空'))
            return

        ### 取出手动干预任务
        with DBContext('readonly') as session:
            task_info = session.query(TaskList).filter(
                TaskList.list_id == list_id).first()
            hand_task = session.query(TempDetails.cmd_name).filter(
                TempDetails.temp_id == task_info.temp_id,
                TempDetails.trigger == 'hand').all()
            ### 取出参数
            argsinfo = session.query(ArgsList.args_name,
                                     ArgsList.args_self).all()
            args_dict = literal_eval(task_info.args)

        if args_dict:
            for k, v in args_dict.items():
                for i in argsinfo:
                    args_record.append(i[1])
                    if i[1] == k:
                        new_agrs_dict[i[0]] = v
                if k not in args_record:
                    new_agrs_dict[k] = v

        for h in hand_task:
            hand_list.append(h[0])

        ### 根据权限显示操作按钮
        bt_hidden = 'true'
        if username == task_info.executor or self.is_superuser():
            bt_hidden = 'false'

        ### 根据组和执行主机获取数据
        if group and hosts and list_id:
            with DBContext('readonly') as session:
                sched_info = session.query(TaskSched).filter(
                    TaskSched.list_id == list_id,
                    TaskSched.task_group == group,
                    TaskSched.exec_ip == hosts).order_by(
                        TaskSched.task_group, TaskSched.task_level).all()
            for msg in sched_info:
                data_dict = model_to_dict(msg)
                data_dict['bt_hidden'] = bt_hidden
                sched_list.append(data_dict)
            self.write(dict(code=0, msg='成功', data=sched_list))
            return

        with DBContext('readonly') as session:
            sched_info = session.query(TaskSched).filter(
                TaskSched.list_id == list_id).order_by(
                    TaskSched.task_group, TaskSched.task_level).all()
            all_group = session.query(TaskSched.task_group).filter(
                TaskSched.list_id == list_id).group_by(
                    TaskSched.task_group).all()

        for msg in sched_info:
            data_dict = model_to_dict(msg)
            data_dict['bt_hidden'] = bt_hidden
            sched_list.append(data_dict)

        task_hosts = literal_eval(task_info.hosts)

        for g in all_group:
            hosts = task_hosts.get(g[0], '10.10.10.10').split(',')
            hosts_status = {}
            for h in hosts:
                with DBContext('readonly') as session:
                    slist = session.query(TaskSched.task_status).filter(
                        TaskSched.list_id == list_id,
                        TaskSched.task_group == g[0],
                        TaskSched.exec_ip == h).order_by(
                            TaskSched.task_group, TaskSched.task_level).all()

                status_list = []
                for s in slist:
                    status_list.append(s[0])
                if '0' in status_list:
                    status = '0'
                if '1' in status_list:
                    status = '1'
                if '2' in status_list:
                    status = '2'
                if '5' in status_list and '1' not in status_list and '2' not in status_list:
                    status = '5'
                if '4' in status_list:
                    status = '4'
                if '3' in status_list and len(list(set(status_list))) == 1:
                    status = '3'

                hosts_status[str(h)] = status
            group_list.append(
                dict(group=g[0], hosts=hosts, task_status=hosts_status))

        kwargs = {
            "data": sched_list,
            "task_time": str(task_info.stime),
            "group_list": group_list,
            "hand_list": hand_list,
            "new_agrs": new_agrs_dict,
            "args_keys": list(new_agrs_dict.keys()),
            "code": 0,
            "msg": '获取成功'
        }
        self.write(kwargs)