Esempio n. 1
0
def perm_sudo_add(request, res, *args):
    """
    list sudo commands alias
    """
    res['operator'] = u"添加别名"
    response = {'success': False, 'error': ''}
    res['emer_content'] = 6
    if request.method == "POST":
        try:
            name = request.POST.get("sudo_name").strip().upper()
            comment = request.POST.get("sudo_comment")
            commands = request.POST.get("sudo_commands").strip()

            if not name or not commands:
                raise ServerError(u"sudo name 和 commands是必填项!")

            pattern = re.compile(r'[\n,\r]')
            deal_space_commands = list_drop_str(pattern.split(commands), u'')
            deal_all_commands = map(trans_all, deal_space_commands)
            commands = ', '.join(deal_all_commands)
            logger.debug(u'添加sudo %s: %s' % (name, commands))

            sudo_name_test = get_object(PermSudo, name=name)
            if sudo_name_test:
                raise ServerError(u"别名[%s]已存在" % name)

            sudo_uuid = str(uuid.uuid1())
            # TODO 保存数据到magicstack
            sudo = PermSudo.objects.create(uuid_id=sudo_uuid,
                                           name=name.strip(),
                                           comment=comment,
                                           commands=commands)

            # TODO 保存数据到proxy上的数据库
            proxy_list = Proxy.objects.all()
            data = {
                'uuid_id': sudo_uuid,
                'id': sudo.id,
                'name': name,
                'comment': comment,
                'commands': commands
            }
            data = json.dumps(data)
            execute_thread_tasks(proxy_list,
                                 THREAD_NUMBERS,
                                 role_proxy_operator,
                                 request.user.username,
                                 'PermSudo',
                                 data,
                                 obj_uuid=sudo.uuid_id,
                                 action='add')
            res['content'] = u"添加Sudo命令别名[%s]成功" % name
            res['emer_status'] = u"添加Sudo命令别名[%s]成功" % name
            response['success'] = True
        except ServerError as e:
            res['flag'] = 'false'
            res['content'] = e.message
            res['emer_status'] = u"添加Sudo命令别名失败:%s" % (e.message)
            response['error'] = res['emer_status']
    return HttpResponse(json.dumps(response), content_type='application/json')
Esempio n. 2
0
def perm_sudo_add(request, res, *args):
    """
    list sudo commands alias
    """
    res['operator'] = u"添加别名"
    response ={'success': False, 'error': ''}
    res['emer_content'] = 6
    if request.method == "POST":
        try:
            name = request.POST.get("sudo_name").strip().upper()
            comment = request.POST.get("sudo_comment")
            commands = request.POST.get("sudo_commands").strip()

            if not name or not commands:
                raise ServerError(u"sudo name 和 commands是必填项!")

            pattern = re.compile(r'[\n,\r]')
            deal_space_commands = list_drop_str(pattern.split(commands), u'')
            deal_all_commands = map(trans_all, deal_space_commands)
            commands = ', '.join(deal_all_commands)
            logger.debug(u'添加sudo %s: %s' % (name, commands))

            sudo_name_test = get_object(PermSudo, name=name)
            if sudo_name_test:
                raise ServerError(u"别名[%s]已存在" %name)

            sudo_uuid = str(uuid.uuid1())
            # TODO 保存数据到magicstack
            sudo = PermSudo.objects.create(uuid_id=sudo_uuid, name=name.strip(), comment=comment, commands=commands)

            # TODO 保存数据到proxy上的数据库
            proxy_list = Proxy.objects.all()
            data = {'uuid_id': sudo_uuid,
                    'id': sudo.id,
                    'name': name,
                    'comment': comment,
                    'commands': commands}
            data = json.dumps(data)
            execute_thread_tasks(proxy_list, THREAD_NUMBERS, role_proxy_operator, request.user.username, 'PermSudo', data,
                                 obj_uuid=sudo.uuid_id, action='add')
            res['content'] = u"添加Sudo命令别名[%s]成功" % name
            res['emer_status'] = u"添加Sudo命令别名[%s]成功" % name
            response['success'] = True
        except ServerError as e:
            res['flag'] = 'false'
            res['content'] = e.message
            res['emer_status'] = u"添加Sudo命令别名失败:%s" % (e.message)
            response['error'] = res['emer_status']
    return HttpResponse(json.dumps(response), content_type='application/json')
Esempio n. 3
0
def perm_sudo_edit(request, res, *args):
    """
    编辑别名
    """
    res['operator'] = "编辑别名"
    res['emer_content'] = 6
    if request.method == "GET":
        sudo_id = request.GET.get("id")
        sudo = PermSudo.objects.get(id=sudo_id)
        rest = {}
        rest['Id'] = sudo.id
        rest['name'] = sudo.name
        rest['commands'] = sudo.commands
        rest['comment'] = sudo.comment
        return HttpResponse(json.dumps(rest), content_type='application/json')
    else:
        response = {'success': False, 'error': ''}
        try:
            sudo_id = request.GET.get("id")
            sudo = PermSudo.objects.get(id=int(sudo_id))
            name = request.POST.get("sudo_name").upper()
            commands = request.POST.get("sudo_commands")
            comment = request.POST.get("sudo_comment")

            if not name or not commands:
                raise ServerError(u"sudo name 和 commands是必填项!")

            old_name = sudo.name
            if old_name == name:
                if len(PermSudo.objects.filter(name=name)) > 1:
                    raise ServerError(u'别名[%s]已存在' % name)
            else:
                if len(PermSudo.objects.filter(name=name)) > 0:
                    raise ServerError(u'别名[%s]已存在' % name)

            pattern = re.compile(r'[\n,\r]')
            deal_space_commands = list_drop_str(pattern.split(commands), u'')
            deal_all_commands = map(trans_all, deal_space_commands)
            commands = ', '.join(deal_all_commands).strip()
            sudo.name = name.strip()
            sudo.commands = commands
            sudo.comment = comment
            sudo.save()
            proxy_list = Proxy.objects.all()
            # 更新proxy上的数据
            data = {
                'name': name.strip(),
                'comment': comment,
                'commands': commands
            }
            data = json.dumps(data)
            execute_thread_tasks(proxy_list,
                                 THREAD_NUMBERS,
                                 role_proxy_operator,
                                 request.user.username,
                                 'PermSudo',
                                 data,
                                 obj_uuid=sudo.uuid_id,
                                 action='update')

            msg = u"编辑Sudo命令别名[%s]成功" % sudo.name
            res['content'] = msg
            res['emer_status'] = msg
            response['success'] = True
        except ServerError as e:
            res['flag'] = 'false'
            res['content'] = e.message
            res['emer_status'] = u"编辑Sudo命令别名失败:%s" % (e.message)
            response['error'] = res['emer_status']
        return HttpResponse(json.dumps(response),
                            content_type='application/json')
Esempio n. 4
0
def perm_sudo_edit(request, res, *args):
    """
    编辑别名
    """
    res['operator'] = "编辑别名"
    res['emer_content'] = 6
    if request.method == "GET":
        sudo_id = request.GET.get("id")
        sudo = PermSudo.objects.get(id=sudo_id)
        rest = {}
        rest['Id'] = sudo.id
        rest['name'] = sudo.name
        rest['commands'] = sudo.commands
        rest['comment'] = sudo.comment
        return HttpResponse(json.dumps(rest), content_type='application/json')
    else:
        response = {'success': False, 'error': ''}
        try:
            sudo_id = request.GET.get("id")
            sudo = PermSudo.objects.get(id=int(sudo_id))
            name = request.POST.get("sudo_name").upper()
            commands = request.POST.get("sudo_commands")
            comment = request.POST.get("sudo_comment")

            if not name or not commands:
                raise ServerError(u"sudo name 和 commands是必填项!")

            old_name = sudo.name
            if old_name == name:
                if len(PermSudo.objects.filter(name=name)) > 1:
                    raise ServerError(u'别名[%s]已存在' % name)
            else:
                if len(PermSudo.objects.filter(name=name)) > 0:
                    raise ServerError(u'别名[%s]已存在' % name)

            pattern = re.compile(r'[\n,\r]')
            deal_space_commands = list_drop_str(pattern.split(commands), u'')
            deal_all_commands = map(trans_all, deal_space_commands)
            commands = ', '.join(deal_all_commands).strip()
            sudo.name = name.strip()
            sudo.commands = commands
            sudo.comment = comment
            sudo.save()
            proxy_list = Proxy.objects.all()
            # 更新proxy上的数据
            data = {'name': name.strip(),
                    'comment': comment,
                    'commands': commands}
            data = json.dumps(data)
            execute_thread_tasks(proxy_list, THREAD_NUMBERS, role_proxy_operator, request.user.username, 'PermSudo', data,
                                 obj_uuid=sudo.uuid_id, action='update')

            msg = u"编辑Sudo命令别名[%s]成功" % sudo.name
            res['content'] = msg
            res['emer_status'] = msg
            response['success'] = True
        except ServerError as e:
            res['flag'] = 'false'
            res['content'] = e.message
            res['emer_status'] = u"编辑Sudo命令别名失败:%s"%(e.message)
            response['error'] = res['emer_status']
        return HttpResponse(json.dumps(response), content_type='application/json')