Exemplo n.º 1
0
def group_add(request):
    # 新增资产组
    response = HttpResponse()

    if check_perms(request, 'cmdb.add_serverGroup', raise_exception=True):
        if request.method == "POST":
            user = request.user
            data = json.loads(request.POST.get('data', ''))

            groupName = data['group_name']

            group = ServerGroup.objects.filter(name=groupName)

            if group:
                response.write(json.dumps(u'尼玛,重复了!'))
            elif len(groupName) == 0:
                response.write(json.dumps(u'你闲的蛋疼么?字都懒得打!'))
            else:
                group = ServerGroup()
                group.created_by_id = user.id
                group.name = groupName
                group.comment = data['group_comment']
                try:
                    group.save()
                except:
                    response.write(json.dumps(u'异常'))
                else:
                    response.write(json.dumps(u'成功'))

        else:
            pass
    else:
        response.write(json.dumps(u'您没有权限操作@^@,请联系管理员!'))

    return response
Exemplo n.º 2
0
def kube_add(request):
    # 新增机器
    error = ""
    if check_perms(request, 'kube.add_kubeNode', raise_exception=True):
        if request.method == "POST":
            ip = request.POST.get('ip')
            # role = request.POST.get('role')
            # LB_ROLE = request.POST.get('LB_ROLE')
            k8sNode = KubeNode.objects.filter(ip_id=ip)

            try:
                form = KubeNodeForm(request.POST)
                print(form)
            except Exception as e:
                print(e)

            if k8sNode:
                error = u"该机器已存在!"
            elif ip == '':
                error = u"你闲的蛋疼么?字都懒得打!"
            else:
                if form.is_valid():
                    k8sNode = form.save(commit=False)
                    k8sNode.created_by = request.user
                    k8sNode.save()
                    # response.write(json.dumps(u'成功'))
                    return HttpResponseRedirect(reverse('kube_list'))
        # return render(request, 'error.html', {'request': request, 'error': error})
    else:
        error = u'您没有权限操作@^@,请联系管理员!'

    return render(request, 'error.html', {'request': request, 'error': error})
Exemplo n.º 3
0
def app_edit(request):
    # 编辑机器
    response = HttpResponse()
    if check_perms(request, 'release.change_appProject', raise_exception=True):
        data = json.loads(request.POST.get('data', ''))

        app_id = data['app_id']
        app_name_cn = data['app_name_cn']
        app_name_en = data['app_name_en']
        app_type = data['app_type']
        git_url = data['git_url']
        git_properties = data['git_properties']
        java_version = data['edit_java_version']
        platform = data['edit_platform']

        appProject = AppProject.objects.get(pk=app_id)
        appProject.app_name_cn = app_name_cn
        appProject.app_name_en = app_name_en
        appProject.app_type = app_type
        appProject.git_url = git_url
        appProject.git_properties = git_properties
        appProject.java_version = java_version
        appProject.platform_id = platform

        appProject.save()

        response.write(json.dumps(u'成功'))
    else:
        response.write(json.dumps(u'您没有权限操作@^@,请联系管理员!'))

    return response
Exemplo n.º 4
0
def idc_edit(request):
    # 编辑IDC

    response = HttpResponse()

    if check_perms(request, 'cmdb.change_idc', raise_exception=True):
        data = json.loads(request.POST.get('data', ''))

        id = data['id']

        idc = Idc.objects.get(pk=id)
        idc.name = data['idc_name']
        idc.contact = data['idc_contact']
        idc.phone = data['idc_phone']
        idc.address = data['idc_address']
        idc.intranet = data['idc_intranet']
        idc.extranet = data['idc_extranet']
        idc.operator = data['idc_operator']
        try:
            idc.save()
        except:
            response.write(json.dumps(u'失败'))
        else:
            response.write(json.dumps(u'成功'))
    else:
        response.write(json.dumps(u'您没有权限操作@^@,请联系管理员!'))

    return response
Exemplo n.º 5
0
def server_add(request):
    # 新增机器
    error = ""
    response = HttpResponse()
    if check_perms(request, 'cmdb.add_server', raise_exception=True):
        if request.method == "POST":
            groups = request.POST.getlist('groups')
            new_in_ip = request.POST.get('in_ip')
            server = Server.objects.filter(in_ip=new_in_ip)

            form = ServerForm(request.POST)

            if server:
                error = u"该机器已存在!"
                # response.write(json.dumps(u'该机器已存在!'))
            elif new_in_ip == '':
                error = u"你闲的蛋疼么?字都懒得打!"
                # response.write(json.dumps(u'你闲的蛋疼么?字都懒得打!'))
                # return render(request, 'error.html', {'request': request, 'error': error})
            else:
                if form.is_valid():
                    server = form.save(commit=False)
                    server.author = request.user
                    server.save()
                    server.groups.clear()
                    server.groups.add(*groups)
                    # response.write(json.dumps(u'成功'))
                    return HttpResponseRedirect(reverse('server_list'))
        # return render(request, 'error.html', {'request': request, 'error': error})
    else:
        error = u'您没有权限操作@^@,请联系管理员!'

    return render(request, 'error.html', {'request': request, 'error': error})
Exemplo n.º 6
0
def app_add(request):
    # 新增机器
    error = ""
    if check_perms(request, 'release.add_appProject', raise_exception=True):
        if request.method == "POST":
            # groups = request.POST.getlist('groups')
            new_app_name_en = request.POST.get('app_name_en')
            appProject = AppProject.objects.filter(app_name_en=new_app_name_en)

            form = AppProjectForm(request.POST)

            if appProject:
                error = u"该应用已存在!"
            elif new_app_name_en == '':
                error = u"你闲的蛋疼么?字都懒得打!"
            else:
                if form.is_valid():
                    appProject = form.save(commit=False)
                    appProject.author = request.user
                    appProject.save()
                    # response.write(json.dumps(u'成功'))
                    return HttpResponseRedirect(reverse('app_list'))
    else:
        error = u'您没有权限操作@^@,请联系管理员!'

    return render(request, 'error.html', {'request': request, 'error': error})
Exemplo n.º 7
0
def system_user_edit(request):
    # 编辑机器
    response = HttpResponse()
    if check_perms(request, 'cmdb.change_systemUser', raise_exception=True):
        data = json.loads(request.POST.get('data', ''))

        id = data['id']
        name = data['name']
        username = data['username']
        password = data['password']
        comment = data['comment']

        user = SystemUser.objects.get(pk=id)
        # server.project_name = project_name
        # server.service_name = service_name
        user.name = name
        user.username = username
        user.password = password
        user.comment = comment
        user.save()
        response.write(json.dumps(u'成功'))
    else:
        response.write(json.dumps(u'您没有权限操作@^@,请联系管理员!'))

    return response
Exemplo n.º 8
0
def system_user_add(request):
    # 新增登录用户
    error = ""
    response = HttpResponse()
    if check_perms(request, 'cmdb.add_systemUser', raise_exception=True):
        if request.method == "POST":
            new_name = request.POST.get('name')
            user = SystemUser.objects.filter(name=new_name)

            form = SystemUserForm(request.POST)

            if user:
                error = u"该名称已存在!"
                # response.write(json.dumps(u'该机器已存在!'))
            elif new_name == '':
                error = u"你闲的蛋疼么?字都懒得打!"
                # response.write(json.dumps(u'你闲的蛋疼么?字都懒得打!'))
                # return render(request, 'error.html', {'request': request, 'error': error})
            else:
                if form.is_valid():
                    user = form.save(commit=False)
                    user.save()
                    # response.write(json.dumps(u'成功'))
                    return HttpResponseRedirect(reverse('system_user'))
        # return render(request, 'error.html', {'request': request, 'error': error})
    else:
        error = u'您没有权限操作@^@,请联系管理员!'

    return render(request, 'error.html', {'request': request, 'error': error})
Exemplo n.º 9
0
def flushMonitorInfo(request):
    response = HttpResponse()
    if check_perms(request, 'cmdb.view_monitor', raise_exception=True):
        server = Server.objects.order_by('id')
        hostsInfo = server.values_list('in_ip', 'system_user__username', 'system_user__password')
        hostList = []
        for i in hostsInfo:
            hostDic = {
                "hostname": i[0],
                "ip": i[0],
                "port": '22',
                "username": i[1],
                "password": i[2],
            }
            hostList.append(hostDic)
        infoList = get_host_info(hostList)

        if not infoList:
            response.write(json.dumps(u'主机不可达'))
        else:
            for data in infoList:
                servers = Server.objects.filter(in_ip=data['ipadd_in'])
                if data['status']:
                    servers.update(mem_free=data['mem_free'], mem_rate=data['mem_rate'],
                                   disk_free=data['disk_free'], disk_rate=data['disk_rate'],
                                   swap_total=data['swap_total'],
                                   swap_free=data['swap_free'], swap_rate=data['swap_rate'])
                else:
                    servers.update(status=data['status'])
            response.write(json.dumps(u'批量刷新成功'))
    else:
        response.write(json.dumps(u'您没有权限操作@^@,请联系管理员!'))

    return response
Exemplo n.º 10
0
def release_add(request):
    # 新增发布计划
    error = ""
    if check_perms(request, 'release.add_releasePlan', raise_exception=True):
        if request.method == "POST":
            new_app_version = request.POST.get('app_version')
            new_app_id = request.POST.get('app')
            releasePlan = ReleasePlan.objects.filter(app_id=new_app_id)

            form = ReleasePlanForm(request.POST)

            if releasePlan:
                error = u"该应用发布计划存在已存在!"
            elif new_app_version == '':
                error = u"你闲的蛋疼么?字都懒得打!"
            else:
                if form.is_valid():
                    releasePlan = form.save(commit=False)
                    releasePlan.author = request.user
                    releasePlan.save()
                    # response.write(json.dumps(u'成功'))
                    return HttpResponseRedirect(reverse('release_plan'))
    else:
        error = u'您没有权限操作@^@,请联系管理员!'

    return render(request, 'error.html', {'request': request, 'error': error})
Exemplo n.º 11
0
def postmachineinfo(request):
    response = HttpResponse()
    if check_perms(request, 'cmdb.update_server', raise_exception=True):
        # 提交服务器信息
        data = json.loads(request.GET.get('data', ''))
        id = int(data['id'])
        server = Server.objects.get(pk=id)
        system_username = server.system_user.username
        system_passwd = server.system_user.password
        assets = [{
            "hostname": server.in_ip,
            "ip": server.in_ip,
            "port": '22',
            "username": system_username,
            "password": system_passwd,
        }]

        infoList = get_host_info(assets)
        logger.info('请求成功!采集该主机信息开始,host:{}'.format(server.in_ip))

        if not infoList[0]['status']:
            response.write(json.dumps(u'主机网络不可达'))
        else:
            for data in infoList:
                if data['status']:
                    server.os_version = data['sysinfo']
                    server.host_name = data['host_name']
                    server.os_kernel = data['os_kernel']
                    server.cpu_model = data['cpu']
                    server.cpu_count = data['cpu_count']
                    server.cpu_cores = data['cpu_cores']
                    server.mem = data['mem']
                    server.disk = data['disk']
                    server.status = data['status']

                    ulimit = get_ulimit(assets)
                    for limit in ulimit:
                        server.max_open_files = limit['ulimit']

                    uptime = get_uptime(assets)
                    for tim in uptime:
                        server.uptime = tim['uptime']

                else:
                    server.status = data['status']
                server.save()

            # set_service_port(server)  # 设置服务端口信息
            response.write(json.dumps(u'刷新成功'))
    else:
        response.write(json.dumps(u'您没有权限操作@^@,请联系管理员!'))

    return response
Exemplo n.º 12
0
def flushAllHosts(request):
    response = HttpResponse()
    if check_perms(request, 'cmdb.update_server', raise_exception=True):
        server = Server.objects.order_by('id')
        hostsInfo = server.values_list('in_ip', 'system_user__username',
                                       'system_user__password')
        hostList = []
        for i in hostsInfo:
            hostDic = {
                "hostname": i[0],
                "ip": i[0],
                "port": '22',
                "username": i[1],
                "password": i[2],
            }
            hostList.append(hostDic)
        infoList = get_host_info(hostList)

        if not infoList:
            response.write(json.dumps(u'批量刷新异常'))
        else:
            for data in infoList:
                servers = Server.objects.filter(in_ip=data['ipadd_in'])
                if data['status']:
                    servers.update(os_version=data['sysinfo'],
                                   host_name=data['host_name'],
                                   os_kernel=data['os_kernel'],
                                   cpu_model=data['cpu'],
                                   cpu_count=data['cpu_count'],
                                   cpu_cores=data['cpu_cores'],
                                   mem=data['mem'],
                                   disk=data['disk'],
                                   status=data['status'])
                else:
                    servers.update(status=data['status'])

            ulimit = get_ulimit(hostList)
            for data in ulimit:
                servers = Server.objects.filter(in_ip=data['host_ip'])
                servers.update(max_open_files=data['ulimit'])

            uptime = get_uptime(hostList)
            for data in uptime:
                servers = Server.objects.filter(in_ip=data['host_ip'])
                servers.update(uptime=data['uptime'])

            # set_service_port(server)  # 设置服务端口信息
            response.write(json.dumps(u'批量刷新成功'))
    else:
        response.write(json.dumps(u'您没有权限操作@^@,请联系管理员!'))

    return response
Exemplo n.º 13
0
def server_delete(request):
    # 删除机器信息
    response = HttpResponse()
    if check_perms(request, 'cmdb.delete_server', raise_exception=True):

        data = json.loads(request.POST.get('data', ''))
        id = int(data['id'])
        Server.objects.get(pk=id).delete()
        response.write(json.dumps(u'成功'))
    else:
        response.write(json.dumps(u'您没有权限操作@^@,请联系管理员!'))

    return response
Exemplo n.º 14
0
def platform_delete(request):
    response = HttpResponse()

    if  check_perms(request, 'release.delete_platform', raise_exception=True):
        data = json.loads(request.POST.get('data', ''))

        id = int(data['id'])
        Platform.objects.get(pk=id).delete()

        response.write(json.dumps(u'成功'))
    else:
        response.write(json.dumps(u'您没有权限操作@^@,请联系管理员!'))

    return response
Exemplo n.º 15
0
def group_edit(request):
    # 编辑机器
    response = HttpResponse()
    if check_perms(request, 'cmdb.change_serverGroup', raise_exception=True):
        data = json.loads(request.POST.get('data', ''))

        id = data['id']
        group_name = data['group_name']
        group_comment = data['group_comment']

        group = ServerGroup.objects.get(pk=id)
        group.name = group_name
        group.comment = group_comment
        group.save()

        response.write(json.dumps(u'成功'))
    else:
        response.write(json.dumps(u'您没有权限操作@^@,请联系管理员!'))

    return response
Exemplo n.º 16
0
def postmachineinfo(request):
    response = HttpResponse()

    if check_perms(request, 'cmdb.update_server', raise_exception=True):
        # 提交服务器信息
        data = json.loads(request.GET.get('data', ''))
        id = int(data['id'])
        server = Server.objects.get(pk=id)
        assets = [
            {
                "hostname": 'host',
                "ip": server.in_ip,
                "port": '22',
                "username": '',
                "password": '',
            },
        ]
        data = get_info(assets)

        if not data:
            response.write(json.dumps(u'主机不可达'))
        else:
            server.os_version = data['sysinfo']
            server.host_name = data['host_name']
            server.os_kernel = data['os_kernel']
            server.cpu_model = data['cpu']
            server.cpu_count = data['cpu_count']
            server.cpu_cores = data['cpu_cores']
            server.mem = data['mem']
            server.disk = data['disk']
            server.status = True
            server.max_open_files = get_ulimit(assets)
            server.uptime = get_uptime(assets)
            server.save()

            # set_service_port(server)  # 设置服务端口信息
            response.write(json.dumps(u'成功'))
    else:
        response.write(json.dumps(u'您没有权限操作@^@,请联系管理员!'))

    return response
Exemplo n.º 17
0
def idc_add(request):
    # 新增IDC
    response = HttpResponse()

    if check_perms(request, 'cmdb.add_idc', raise_exception=True):
        error = ""
        if request.method == "POST":
            user = request.user
            data = json.loads(request.POST.get('data', ''))

            idcName = data['idc_name']
            idc = Idc.objects.filter(name=idcName)

            if idc:
                response.write(json.dumps(u'尼玛,重复了!'))
            elif len(idcName) == 0:
                response.write(json.dumps(u'你闲的蛋疼么?字都懒得打!'))
            else:
                try:
                    idc = Idc()
                    idc.created_by_id = user.id
                    idc.name = idcName
                    idc.contact = data['idc_contact']
                    idc.phone = data['idc_phone']
                    idc.address = data['idc_address']
                    idc.intranet = data['idc_intranet']
                    idc.extranet = data['idc_extranet']
                    idc.operator = data['idc_operator']
                    idc.save()
                except:
                    response.write(json.dumps(u'异常'))
                else:
                    response.write(json.dumps(u'成功'))

        else:
            pass
    else:
        response.write(json.dumps(u'您没有权限操作@^@,请联系管理员!'))

    return response
Exemplo n.º 18
0
def server_edit(request):
    # 编辑机器
    response = HttpResponse()
    if check_perms(request, 'cmdb.change_server', raise_exception=True):
        data = json.loads(request.POST.get('data', ''))

        id = data['id']
        # project_name = data['project_name']
        # service_name = data['service_name']
        groups_id = data['groups']
        in_ip = data['in_ip']
        ex_ip = data['ex_ip']
        idc = data['idc']
        app = data['app']
        user = data['user']

        server = Server.objects.get(pk=id)
        # server.project_name = project_name
        # server.service_name = service_name
        server.in_ip = in_ip
        server.ex_ip = ex_ip
        server.idc_id = idc
        server.system_user_id = user
        server.app_project_id = app
        try:
            server.groups.clear()
            if groups_id:
                groups = ServerGroup.objects.filter(id__in=groups_id)
                server.groups.add(*groups)
        except Exception as e:
            print(e)
        server.save()

        response.write(json.dumps(u'成功'))
    else:
        response.write(json.dumps(u'您没有权限操作@^@,请联系管理员!'))

    return response
Exemplo n.º 19
0
def platform_add(request):
    # 新增IDC
    response = HttpResponse()

    if check_perms(request, 'release.add_platform', raise_exception=True):
        error = ""
        if request.method == "POST":
            user = request.user
            data = json.loads(request.POST.get('data', ''))

            new_platform = data['add_platform']
            platform = Platform.objects.filter(name=new_platform)

            if platform:
                response.write(json.dumps(u'尼玛,重复了!'))
            elif len(new_platform) == 0:
                response.write(json.dumps(u'你闲的蛋疼么?字都懒得打!'))
            else:
                try:
                    platform = Platform()
                    platform.created_by_id = user.id
                    platform.name = new_platform
                    platform.owner = data['add_owner']
                    platform.phone = data['add_phone']
                    platform.comment = data['add_comment']
                    platform.save()
                except:
                    response.write(json.dumps(u'异常'))
                else:
                    response.write(json.dumps(u'成功'))

        else:
            pass
    else:
        response.write(json.dumps(u'您没有权限操作@^@,请联系管理员!'))

    return response
Exemplo n.º 20
0
def platform_edit(request):

    response = HttpResponse()

    if check_perms(request, 'release.change_platform', raise_exception=True):
        data = json.loads(request.POST.get('data', ''))

        id = data['id']

        platform = Platform.objects.get(pk=id)
        platform.name = data['platform']
        platform.owner = data['owner']
        platform.phone = data['phone']
        platform.comment = data['comment']
        try:
            platform.save()
        except:
            response.write(json.dumps(u'失败'))
        else:
            response.write(json.dumps(u'成功'))
    else:
        response.write(json.dumps(u'您没有权限操作@^@,请联系管理员!'))

    return response