Beispiel #1
0
def recordDetail(request):
    user = request.user.username
    hostsft = {}

    if 'jid' in request.GET:
        job_id = request.GET.get('jid')
        jid_record = OperateRecord.objects.get(jid=job_id)

        try:
            jidStatus = ReturnRecord.objects.get(jid=job_id)
            hostsft['sum'] = jidStatus.tgt_total
            hostsft['rsum'] = jidStatus.tgt_ret
            hostsft['unre'] = jidStatus.tgt_unret
            hostsft['unrestr'] = jidStatus.tgt_unret_list
            hostsft['fa'] = jidStatus.tgt_fail
            hostsft['tr'] = jidStatus.tgt_succ
        except:
            hostsft['sum'] = 'Null'
            hostsft['rsum'] = 'Null'
            hostsft['unre'] = 'Null'
            hostsft['unrestr'] = 'Null'
            hostsft['fa'] = 'Null'
            hostsft['tr'] = 'Null'

        # db = db_operate()
        # sql = 'select id,`return` from salt_returns where jid=%s'
        # jid_result = db.select_table(settings.RETURNS_MYSQL, sql, str(job_id))
        jid_result = mysqlReturns(job_id)
        ret, hostfa, hosttr = outFormat(jid_result)

    else:
        jid_record = ''
        ret = {}

    return render(
        request,
        'record_detail.html',
        {'jid_record': jid_record,
         'hostsft': hostsft,
         'ret': ret
         }
    )
Beispiel #2
0
def recordDetail(request):
    user = request.user.username
    hostsft = {}

    if 'jid' in request.GET:
        job_id = request.GET.get('jid')
        jid_record = OperateRecord.objects.get(jid=job_id)

        try:
            jidStatus = ReturnRecord.objects.get(jid=job_id)
            hostsft['sum'] = jidStatus.tgt_total
            hostsft['rsum'] = jidStatus.tgt_ret
            hostsft['unre'] = jidStatus.tgt_unret
            hostsft['unrestr'] = jidStatus.tgt_unret_list
            hostsft['fa'] = jidStatus.tgt_fail
            hostsft['tr'] = jidStatus.tgt_succ
        except:
            hostsft['sum'] = 'Null'
            hostsft['rsum'] = 'Null'
            hostsft['unre'] = 'Null'
            hostsft['unrestr'] = 'Null'
            hostsft['fa'] = 'Null'
            hostsft['tr'] = 'Null'

        # db = db_operate()
        # sql = 'select id,`return` from salt_returns where jid=%s'
        # jid_result = db.select_table(settings.RETURNS_MYSQL, sql, str(job_id))
        jid_result = mysqlReturns(job_id)
        ret, hostfa, hosttr = outFormat(jid_result)

    else:
        jid_record = ''
        ret = {}

    return render(request, 'record_detail.html', {
        'jid_record': jid_record,
        'hostsft': hostsft,
        'ret': ret
    })
Beispiel #3
0
def repairHistoryDataAPI(request):
    '''

    :param request:
    :return:
    '''
    user = request.user.username
    data_path = '/srv/salt/dzh_store/mobileserver/DATA/'
    state_module = 'state.sls'
    get_errors = []
    errors = []
    result_dict = {}

    if request.method == 'GET':
        module_detection = moduleDetection(state_module, user)
        if module_detection:
            get_errors.append(module_detection)
        if not request.GET.get('datacenter', ''):
            get_errors.append(u'需要指定目标机房,才能允许后续操作!')
        if not request.GET.get('stockexchange', ''):
            get_errors.append(u'亲,需要指定本次将要补数据的市场!')
        if not request.GET.get('sls', ''):
            get_errors.append(u'行情程序需要重启吗?请选择之一!')
        if not os.path.exists(data_path):
            get_errors.append(u'目录:{0} 不存在!'.format(data_path))
            log.error("The data path:{0} not exist.".format(data_path))

        if get_errors:
            for error in get_errors:
                errors.append(error.encode('utf8'))
            result_dict['errors'] = errors
        else:
            get_dc_str = request.GET.get('datacenter')
            get_exch_str = request.GET.get('stockexchange')
            get_sls = request.GET.get('sls')

            dc_clean = get_dc_str.strip(',')
            getdclist = dc_clean.split(',')
            exch_clean = get_exch_str.strip(',')
            getexchlist = exch_clean.split(',')
            getstatesls = get_sls

            if get_dc_str:
                result_host_set = datacenterToMinionID(getdclist)
            else:
                result_host_set = set([])

            stkexch_set = set(os.listdir(data_path))
            clear_dir = stkexch_set.difference(set(getexchlist))
            for exchcode in clear_dir:
                day_path = os.path.join(data_path, exchcode, 'history/day')
                day_files = os.listdir(day_path)
                if day_files:
                    for dyfile in day_files:
                        dyfile_path = os.path.join(day_path,dyfile)
                        os.remove(dyfile_path)
                    log.info('Delete Other Market Success')

            sapi = SaltAPI(
                url=settings.SALT_API['url'],
                username=settings.SALT_API['user'],
                password=settings.SALT_API['password'])

            module_lock = moduleLock(state_module, user)
            if '*' in getdclist:
                jid = sapi.asyncMasterToMinion(getstatesls)
            else:
                tgt_list_to_str = ','.join(list(result_host_set))
                jid = sapi.asyncMasterToMinion(tgt_list_to_str,getstatesls)
            module_unlock = moduleUnlock(state_module, user)

            if getexchlist:
                operate_tgt = getexchlist[0]
            else:
                operate_tgt = 'unknown'

            op_time = time.strftime("%Y-%m-%d %H:%M:%S",time.localtime(time.time()))
            op_user = getstatesls
            op_tgt = '%s...' % operate_tgt
            p1 = OperateRecord.objects.create(
                nowtime=op_time,
                username=user,
                user_operate=op_user,
                simple_tgt=op_tgt,
                jid=jid)

            find_job = findJob(result_host_set,jid)
            result = mysqlReturns(jid)

            ret, hostfa, hosttr = outFormat(result)

            recv_ips_list = ret.keys()
            send_recv_info = manageResult(result_host_set, recv_ips_list)
            send_recv_info['succeed'] = hosttr
            send_recv_info['failed'] = hostfa
            saveRecord = ReturnRecord.objects.create(
                jid=jid,
                tgt_total=send_recv_info['send_count'],
                tgt_ret=send_recv_info['recv_count'],
                tgt_succ=send_recv_info['succeed'],
                tgt_fail=send_recv_info['failed'],
                tgt_unret=send_recv_info['unrecv_count'],
                tgt_unret_list=send_recv_info['unrecv_strings']
            )
            result_dict['result'] = ret
            result_dict['info'] = send_recv_info
    ret_json = json.dumps(result_dict)

    return HttpResponse(ret_json, content_type='application/json')
Beispiel #4
0
def deployProgramApi(request):
    '''
    模块部署功能,前端页面提交的数据由该函数处理,执行完毕后返回json格式数据到api;
    :param request:
    :return:
    '''
    user = request.user.username
    salt_module = 'state.sls'
    get_errors = []
    errors = []
    result_dict = {}

    if request.method == 'GET':
        check_tgt = request.GET.get('tgt', '')
        check_dc_list = request.GET.get('datacenter', '')
        check_arg = request.GET.get('sls', '')

        module_detection = moduleDetection(salt_module, user)

        if module_detection:
            get_errors.append(module_detection)
            log.debug('{0}'.format(str(module_detection)))
        if not (check_tgt or check_dc_list):
            get_errors.append(u'需要输入服务器IP或选择机房!')
            log.error('Did not enter servers ip or choose data center.')
        if not check_arg:
            get_errors.append(u'请选择将要进行的操作!')
            log.error('Not select the file of salt state.')

        if get_errors:
            for error in get_errors:
                errors.append(error.encode('utf-8'))
            result_dict['errors'] = errors
        else:
            tgt = request.GET.get('tgt', '')
            dc = request.GET.get('datacenter', '')
            arg = request.GET.get('sls', '')

            dc_clean = dc.strip(',')
            log.debug(str(dc_clean))
            dc_list = dc_clean.split(',')
            target_list = tgt.split(',')
            tgt_mixture_list = copy.deepcopy(dc_list)
            tgt_mixture_list.extend(target_list)

            if tgt:
                minion_id_from_tgt_set = targetToMinionID(tgt)
            else:
                minion_id_from_tgt_set = set([])
            if dc:
                log.debug(str(dc_list))
                minion_id_from_dc_set = datacenterToMinionID(dc_list)
            else:
                minion_id_from_dc_set = set([])
            all_minion_id_set = minion_id_from_tgt_set.union(
                minion_id_from_dc_set)
            # log.debug('The all target minion id set: {0}'.format(str(all_minion_id_set)))

            if all_minion_id_set:
                sapi = SaltAPI(url=settings.SALT_API['url'],
                               username=settings.SALT_API['user'],
                               password=settings.SALT_API['password'])

                module_lock = moduleLock(salt_module, user)

                if '*' in tgt_mixture_list:
                    jid = sapi.asyncMasterToMinion('*', salt_module, arg)
                else:
                    tgt_list_to_str = ','.join(list(all_minion_id_set))
                    jid = sapi.asyncMasterToMinion(tgt_list_to_str,
                                                   salt_module, arg)

                if dc_list:
                    operate_tgt = dc_list[0]
                elif tgt:
                    operate_tgt = target_list[0]
                else:
                    operate_tgt = 'unknown'

                op_time = time.strftime("%Y-%m-%d %H:%M:%S",
                                        time.localtime(time.time()))
                op_user = arg
                op_tgt = '%s...' % operate_tgt
                p1 = OperateRecord.objects.create(nowtime=op_time,
                                                  username=user,
                                                  user_operate=op_user,
                                                  simple_tgt=op_tgt,
                                                  jid=jid)

                find_job = findJob(all_minion_id_set, jid)
                result = mysqlReturns(jid)
                module_unlock = moduleUnlock(salt_module, user)
                ret, hostfa, hosttr = outFormat(result)

                # log.debug(str(ret))
                recv_ips_list = ret.keys()
                send_recv_info = manageResult(all_minion_id_set, recv_ips_list)
                send_recv_info['succeed'] = hosttr
                send_recv_info['failed'] = hostfa
                saveRecord = ReturnRecord.objects.create(
                    jid=jid,
                    tgt_total=send_recv_info['send_count'],
                    tgt_ret=send_recv_info['recv_count'],
                    tgt_succ=send_recv_info['succeed'],
                    tgt_fail=send_recv_info['failed'],
                    tgt_unret=send_recv_info['unrecv_count'],
                    tgt_unret_list=send_recv_info['unrecv_strings'])
                result_dict['result'] = ret
                result_dict['info'] = send_recv_info
            else:
                log.info('The all target minion id set is Null.')
                set_null = u'数据库中没有找到输入的主机,请确认输入是否正确!'
                result_dict['errors'] = set_null.encode('utf-8')
    ret_json = json.dumps(result_dict)

    return HttpResponse(ret_json, content_type='application/json')
Beispiel #5
0
                    operate_tgt = 'unknown'

                op_time = time.strftime("%Y-%m-%d %H:%M:%S",
                                        time.localtime(time.time()))
                op_user = arg
                op_tgt = '%s...' % operate_tgt
                p1 = OperateRecord.objects.create(nowtime=op_time,
                                                  username=user,
                                                  user_operate=op_user,
                                                  simple_tgt=op_tgt,
                                                  jid=jid)

                find_job = findJob(all_minion_id_set, jid)
                result = mysqlReturns(jid)
                module_unlock = moduleUnlock('cmd.run', user)
                ret, hostfa, hosttr = outFormat(result)

                # log.debug(str(ret))
                recv_ips_list = ret.keys()
                send_recv_info = manageResult(all_minion_id_set, recv_ips_list)
                saveRecord = ReturnRecord.objects.create(
                    jid=jid,
                    tgt_total=send_recv_info['send_count'],
                    tgt_ret=send_recv_info['recv_count'],
                    tgt_unret=send_recv_info['unrecv_count'],
                    tgt_unret_list=send_recv_info['unrecv_strings'])
                result_dict['result'] = ret
                result_dict['info'] = send_recv_info
            else:
                log.info('The all target minion id set is Null.')
                set_null = u'数据库中没有找到输入的主机,请确认输入是否正确!'
Beispiel #6
0
def deployProgramApi(request):
    '''
    模块部署功能,前端页面提交的数据由该函数处理,执行完毕后返回json格式数据到api;
    :param request:
    :return:
    '''
    user = request.user.username
    salt_module = 'state.sls'
    get_errors = []
    errors = []
    result_dict = {}

    if request.method == 'GET':
        check_tgt = request.GET.get('tgt', '')
        check_dc_list = request.GET.get('datacenter', '')
        check_arg = request.GET.get('sls', '')

        module_detection = moduleDetection(salt_module, user)

        if module_detection:
            get_errors.append(module_detection)
            log.debug('{0}'.format(str(module_detection)))
        if not (check_tgt or check_dc_list):
            get_errors.append(u'需要输入服务器IP或选择机房!')
            log.error('Did not enter servers ip or choose data center.')
        if not check_arg:
            get_errors.append(u'请选择将要进行的操作!')
            log.error('Not select the file of salt state.')

        if get_errors:
            for error in get_errors:
                errors.append(error.encode('utf-8'))
            result_dict['errors'] = errors
        else:
            tgt = request.GET.get('tgt', '')
            dc = request.GET.get('datacenter', '')
            arg = request.GET.get('sls', '')

            dc_clean = dc.strip(',')
            log.debug(str(dc_clean))
            dc_list = dc_clean.split(',')
            target_list = tgt.split(',')
            tgt_mixture_list = copy.deepcopy(dc_list)
            tgt_mixture_list.extend(target_list)

            if tgt:
                minion_id_from_tgt_set = targetToMinionID(tgt)
            else:
                minion_id_from_tgt_set = set([])
            if dc:
                log.debug(str(dc_list))
                minion_id_from_dc_set = datacenterToMinionID(dc_list)
            else:
                minion_id_from_dc_set = set([])
            all_minion_id_set = minion_id_from_tgt_set.union(minion_id_from_dc_set)
            # log.debug('The all target minion id set: {0}'.format(str(all_minion_id_set)))

            if all_minion_id_set:
                sapi = SaltAPI(
                    url=settings.SALT_API['url'],
                    username=settings.SALT_API['user'],
                    password=settings.SALT_API['password'])

                module_lock = moduleLock(salt_module, user)

                if '*' in tgt_mixture_list:
                    jid = sapi.asyncMasterToMinion('*', salt_module, arg)
                else:
                    tgt_list_to_str = ','.join(list(all_minion_id_set))
                    jid = sapi.asyncMasterToMinion(tgt_list_to_str, salt_module, arg)

                if dc_list:
                    operate_tgt = dc_list[0]
                elif tgt:
                    operate_tgt = target_list[0]
                else:
                    operate_tgt = 'unknown'

                op_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time()))
                op_user = arg
                op_tgt = '%s...' % operate_tgt
                p1 = OperateRecord.objects.create(
                    nowtime=op_time,
                    username=user,
                    user_operate=op_user,
                    simple_tgt=op_tgt,
                    jid=jid)

                find_job = findJob(all_minion_id_set, jid)
                result = mysqlReturns(jid)
                module_unlock = moduleUnlock(salt_module, user)
                ret, hostfa, hosttr = outFormat(result)

                # log.debug(str(ret))
                recv_ips_list = ret.keys()
                send_recv_info = manageResult(all_minion_id_set, recv_ips_list)
                send_recv_info['succeed'] = hosttr
                send_recv_info['failed'] = hostfa
                saveRecord = ReturnRecord.objects.create(
                    jid=jid,
                    tgt_total=send_recv_info['send_count'],
                    tgt_ret=send_recv_info['recv_count'],
                    tgt_succ=send_recv_info['succeed'],
                    tgt_fail=send_recv_info['failed'],
                    tgt_unret=send_recv_info['unrecv_count'],
                    tgt_unret_list=send_recv_info['unrecv_strings']
                )
                result_dict['result'] = ret
                result_dict['info'] = send_recv_info
            else:
                log.info('The all target minion id set is Null.')
                set_null = u'数据库中没有找到输入的主机,请确认输入是否正确!'
                result_dict['errors'] = set_null.encode('utf-8')
    ret_json = json.dumps(result_dict)

    return HttpResponse(ret_json, content_type='application/json')
Beispiel #7
0
                    operate_tgt = 'unknown'

                op_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time()))
                op_user = arg
                op_tgt = '%s...' % operate_tgt
                p1 = OperateRecord.objects.create(
                    nowtime=op_time,
                    username=user,
                    user_operate=op_user,
                    simple_tgt=op_tgt,
                    jid=jid)

                find_job = findJob(all_minion_id_set, jid)
                result = mysqlReturns(jid)
                module_unlock = moduleUnlock('cmd.run', user)
                ret, hostfa, hosttr = outFormat(result)

                # log.debug(str(ret))
                recv_ips_list = ret.keys()
                send_recv_info = manageResult(all_minion_id_set, recv_ips_list)
                saveRecord = ReturnRecord.objects.create(
                    jid=jid,
                    tgt_total=send_recv_info['send_count'],
                    tgt_ret=send_recv_info['recv_count'],
                    tgt_unret=send_recv_info['unrecv_count'],
                    tgt_unret_list=send_recv_info['unrecv_strings']
                )
                result_dict['result'] = ret
                result_dict['info'] = send_recv_info
            else:
                log.info('The all target minion id set is Null.')
Beispiel #8
0
def remoteExecuteApi(request):
    '''
    远程执行的命令通过JQuery(ajax)提交到这里,处理后返回结果json;
    :param request:
    :return:
    '''
    user = request.user.username
    get_errors = []
    errors = []
    result_dict = {}
    danger_cmd = ('rm', 'reboot', 'init', 'shutdown', 'poweroff')

    if request.method == 'GET':
        check_tgt = request.GET.get('tgt', '')
        check_dc_list = request.GET.get('datacenter', '')
        check_arg = request.GET.get('arg', '')

        module_detection = moduleDetection('cmd.run', user)

        if module_detection:
            get_errors.append(module_detection)
        if not (check_tgt or check_dc_list):
            get_errors.append(u'需要指定目标主机或目标机房!')
        if not check_arg:
            get_errors.append(u'请输入将要执行的命令!')
        else:
            arg_list = check_arg.split(';')
            for i in arg_list:
                command = i.split()[0]
                if command in danger_cmd:
                    get_errors.append(u'%s 命令危险,不允许使用!' % command)

        if get_errors:
            for error in get_errors:
                errors.append(error.encode('utf-8'))
            result_dict['errors'] = errors
        else:
            tgt = request.GET.get('tgt', '')
            dc = request.GET.get('datacenter', '')
            arg = request.GET.get('arg', '')

            dc_clean = dc.strip(',')
            log.debug(str(dc_clean))
            dc_list = dc_clean.split(',')
            target_list = tgt.split(',')
            tgt_mixture_list = copy.deepcopy(dc_list)
            tgt_mixture_list.extend(target_list)

            if tgt:
                minion_id_from_tgt_set = targetToMinionID(tgt)
            else:
                minion_id_from_tgt_set = set([])
            if dc:
                log.debug(str(dc_list))
                minion_id_from_dc_set = datacenterToMinionID(dc_list)
            else:
                minion_id_from_dc_set = set([])
            all_minion_id_set = minion_id_from_tgt_set.union(minion_id_from_dc_set)
            log.debug('The all target minion id set: {0}'.format(str(all_minion_id_set)))

            if all_minion_id_set:
                sapi = SaltAPI(
                    url=settings.SALT_API['url'],
                    username=settings.SALT_API['user'],
                    password=settings.SALT_API['password'])

                # module_lock = moduleLock('cmd.run', user)

                if '*' in tgt_mixture_list:
                    jid = sapi.asyncMasterToMinion('*', 'cmd.run', arg)
                else:
                    tgt_list_to_str = ','.join(list(all_minion_id_set))
                    jid = sapi.asyncMasterToMinion(tgt_list_to_str, 'cmd.run', arg)

                if dc_list:
                    operate_tgt = dc_list[0]
                elif tgt:
                    operate_tgt = tgt_list[0]
                else:
                    operate_tgt = 'unknown'

                op_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time()))
                op_user = arg
                op_tgt = '%s...' % operate_tgt
                p1 = OperateRecord.objects.create(
                    nowtime=op_time,
                    username=user,
                    user_operate=op_user,
                    simple_tgt=op_tgt,
                    jid=jid)

                find_job = findJob(all_minion_id_set, jid)
                result = mysqlReturns(jid)
                # module_unlock = moduleUnlock('cmd.run', user)
                ret, hostfa, hosttr = outFormat(result)

                # log.debug(str(ret))
                recv_ips_list = ret.keys()
                send_recv_info = manageResult(all_minion_id_set, recv_ips_list)
                saveRecord = ReturnRecord.objects.create(
                    jid=jid,
                    tgt_total=send_recv_info['send_count'],
                    tgt_ret=send_recv_info['recv_conut'],
                    tgt_unret=send_recv_info['unrecv_conut'],
                    tgt_unret_list=send_recv_info['unrecv_strings']
                )
                result_dict['result'] = ret
                result_dict['info'] = send_recv_info
            else:
                log.info('The all target minion id set is Null.')
                set_null = u'数据库中没有找到输入的主机,请确认输入是否正确!'
                result_dict['errors'] = set_null.encode('utf-8')
    ret_json = json.dumps(result_dict)

    return HttpResponse(ret_json, content_type='application/json')