Ejemplo n.º 1
0
def SaltsfuncLog(request,
                 exec_hosts,
                 saltmethod,
                 minion=None,
                 minions_List=None):
    BindHostsObjLis = []
    if saltmethod == 'key.accept':

        for hostname in exec_hosts['minions']:
            HostInfoObj = models.HostInfo.objects.get(hostname=hostname)
            Hosts = models.Hosts.objects.filter(hostinfo=HostInfoObj.id)
            BindHostsObj = models.BindHosts.objects.filter(host=Hosts[0].id)
            BindHostsObjLis.append(BindHostsObj[0])
        M = host_mgr.MultiTask('SaltsFuncWriteLog', request)
        content = '服务器已经开始加入集群动作'
        task_expire_time = '20'
        taskid, task_log_detail_obj_ids = M.SaltsFuncWriteLog(
            'saltstack', BindHostsObjLis, task_expire_time, content)
        return taskid, task_log_detail_obj_ids
    elif saltmethod == 'key.delete':
        # print('minions_List',minions_List,'minion',minion)
        if minion not in minions_List:
            HostInfoObj = models.HostInfo.objects.get(hostname=minion)
            Hosts = models.Hosts.objects.filter(hostinfo=HostInfoObj.id)
            BindHostsObj = models.BindHosts.objects.filter(host=Hosts[0].id)
            BindHostsObjLis.append(BindHostsObj[0])
            # print('BindHostsObjLis', BindHostsObjLis, type(BindHostsObjLis),'Hosts ',Hosts,'HostInfoObj',HostInfoObj )
        M = host_mgr.MultiTask('SaltsFuncWriteLog', request)
        content = '主机名为: ' + minion + '服务器已经开始进行剥离集群动作'
        task_expire_time = '20'
        # print('BindHostsObjLis',BindHostsObjLis,type(BindHostsObjLis))
        taskid, task_log_detail_obj_ids = M.SaltsFuncWriteLog(
            'saltstack', BindHostsObjLis, task_expire_time, content)
        return taskid, task_log_detail_obj_ids
Ejemplo n.º 2
0
def multitask_task_action(request):
    if request.method == 'POST':
        # print('request.POST!!!!!!!!!!!!',request.POST)
        action = request.POST.get('action')
        m = host_mgr.MultiTask(action, request)
        res = m.run()
        return HttpResponse(json.dumps(res))
Ejemplo n.º 3
0
def InitScriptLog(request, BindHostsObjLisArg, ScriptName, expire_time):

    M = host_mgr.MultiTask('SaltsFuncWriteLog', request)
    content = '脚本名为: ' + ScriptName[0] + '开始执行'
    task_expire_time = expire_time
    taskid, task_log_detail_obj_ids = M.SaltsFuncWriteLog(
        'saltstack', BindHostsObjLisArg, task_expire_time, content)
    return taskid, task_log_detail_obj_ids
Ejemplo n.º 4
0
def multitask_cmd(request):

    task_type = request.POST.get('task_type')
    multi_task = host_mgr.MultiTask(task_type, request)
    task_id = multi_task.run()
    if task_id:
        return HttpResponse(task_id)
    else:
        return HttpResponse("TaskCreatingError")
Ejemplo n.º 5
0
def DeployAgent(request):
    # print(request.POST)
    task_type = 'run_cmd'
    cmd = SaltMinionShellScript()
    # 取出id值
    host_ids = [i.get('id') for i in json.loads(request.POST.get("jdata"))]
    # 过期时间
    task_expire_time = '30'
    # 执行主机
    exec_hosts = models.BindHosts.objects.filter(id__in=host_ids)
    random_str = None

    deploy = 'SaltAgent'
    M = host_mgr.MultiTask(task_type, request)
    task_id = M.run_cmd(cmd, task_expire_time, exec_hosts, random_str, deploy)
    if task_id:
        return HttpResponse(task_id)
    else:
        return HttpResponse("TaskCreatingError")
Ejemplo n.º 6
0
def multitask_file(request):
    multi_task = host_mgr.MultiTask(request.POST.get('task_type'), request)
    task_result = multi_task.run()
    return HttpResponse(task_result)
Ejemplo n.º 7
0
def multitask_res(request):
    multi_task = host_mgr.MultiTask('get_task_result', request)
    task_result = multi_task.run()
    return HttpResponse(task_result)
Ejemplo n.º 8
0
    def host_token(self):

        TaskInfoList = []
        BindhostObjlist = []
        TaskDic = {}
        # 获取服务器主机id
        host_ids = [
            i for i in json.loads(self.request.POST.get("selected_hosts"))
        ]
        UserIdList = [
            i for i in json.loads(self.request.POST.get("UserIdList"))
        ]
        TokenExpireTime = self.request.POST.get('TokenExpireTime')
        # 找到主机对象
        for userid in UserIdList:
            for hostid in host_ids:
                objList = []
                host_obj = models.BindHosts.objects.get(id=int(hostid))
                UserObj = models.UserProfile.objects.get(id=int(userid))
                # 根据主机id和用户id找到最新的token对象
                latest_token_obj = models.Token.objects.filter(
                    host_id=int(hostid), user_id=int(userid)).last()
                # print('!!!!!!!!!!!!host_obj ',host_obj ,'hostid',hostid)
                token_gen_flag = False
                # 如果有,需要检查是否过期
                if latest_token_obj:
                    # time.struct_time(tm_year=2018, tm_mon=12, tm_mday=6, tm_hour=10, tm_min=28, tm_sec=2, tm_wday=3, tm_yday=340, tm_isdst=0)
                    # timetuple()会被时间分成9个数值的元组对象
                    # mktime变成秒数表示的时间1544063282.0
                    token_gen_time_stamp = time.mktime(
                        latest_token_obj.date.timetuple())
                    # 当前时间秒数
                    current_time = time.mktime(
                        django.utils.timezone.now().timetuple())
                    # 如果当前时间减去token的存活时间,大于token的300秒
                    if current_time - token_gen_time_stamp > latest_token_obj.expire:  #token expired
                        token_gen_flag = True

                else:
                    token_gen_flag = True
                # 如果超时就新算个给他
                if token_gen_flag:
                    # 生成六位随机数
                    token = ''.join(
                        random.sample('zyxwvutsrqponmlkjihgfedcba1234567890',
                                      6))
                    # 将相关信息存进数据库中
                    models.Token.objects.create(
                        user=UserObj,
                        host=host_obj,
                        token=token,
                        expire=(int(TokenExpireTime) * 60),
                    )
                else:
                    # 如果时间没超过,则返回最后那个token值给前端
                    token = latest_token_obj.token

                #task_log写入任务启动阶段

                # BindhostObjlist.append(host_obj)
                objList.append(UserObj.email)
                objList.append(UserObj.name)
                objList.append(token)
                # objList.append(host_obj.host.hostinfo.hostname)
                objList.append('TempMakeHostname')
                objList.append(host_obj.host.ip_addr)
                if (objList in TaskInfoList):
                    pass
                else:
                    BindhostObjlist.append(host_obj)
                    TaskInfoList.append(objList)
        taskmeg = ' 执行授权登录生成临时令牌以及发送邮件!!'
        TaskDic['task_type'] = 'Accredit'
        TaskDic['hosts'] = BindhostObjlist
        TaskDic['expire_time'] = TokenExpireTime
        TaskDic['content'] = str(taskmeg)
        # print('TaskDic',TaskDic)
        m = host_mgr.MultiTask('Accredit', self.request, parameter=TaskDic)
        TaskId, task_log_detail_obj_ids = m.run()
        # task_log_detail_obj_ids 详细日志行号数组
        # 写入完毕

        return TaskId, TaskInfoList, task_log_detail_obj_ids