Beispiel #1
0
def container_detail(request, cid):
    #print request.session['show_li']
    #print request
    #print request.POST.get('show_li')
    #print request.COOKIES['show_li']

    username = request.session.get('username')
    #print username
    container = Container.objects.get(id=cid)
    print type(container.container_ip)
    container_auditLog = container_auditLogs(login_user=str(username),
                                             host_ip=container.container_ip,
                                             hostname=container.hostname,
                                             action=4)
    container_auditLog.save()
    return render_to_response('container_detail.html', locals())
Beispiel #2
0
def container_stop(request):
    username = request.session.get('username')
    container_id = request.POST.get('container_id')
    container = Container.objects.get(id=container_id)
    stop_status = api.stop(container.host_ip, container.container_id)
    stop_status = json.loads(stop_status)
    if stop_status['code'] == 200:
        #print username,container.host_ip,container.hostname
        #print type(username)
        #print type(container.host_ip)
        #print type(container.hostname)
        container_auditLog = container_auditLogs(
            login_user=str(username),
            host_ip=container.container_ip,
            hostname=container.hostname,
            action=1)
        container_auditLog.save()
        return HttpResponse("停止成功")
    else:
        return HttpResponse("停止失败: %s" % stop_status['stop_status'])
Beispiel #3
0
def container_delete(request):
    username = request.session.get('username')
    container_id = request.POST.get('container_id')
    #print container_id
    container = Container.objects.get(id=container_id)

    # 删除容器
    print container.host_ip, container.container_id, container.mount_dev, container.mount_path
    # delete_status = api.remove(container.host_ip, container.container_id, container.mount_dev, container.mount_path)
    # delete_status = json.loads(delete_status)
    # print delete_status
    # if int(delete_status['code']) != 200:
    #     return HttpResponse("删除失败")
    #
    # # 从zeus中删除
    # username = '******'
    # password = '******'
    # url = 'http://10.125.211.2:3344/zapi/delete_asset/'
    # values = {"username": username, "password": password, "ip": container.container_ip}
    # values = json.dumps(values)
    # req = urllib2.Request(url, values)
    # response = urllib2.urlopen(req)
    # the_page = response.read()
    # print the_page
    # if the_page == "faild":
    #     return HttpResponse("删除失败")
    #
    # # 删除容器表记录
    # container.delete()
    delete_status = handle_container_delete(container)
    delete_status = json.loads(delete_status)
    try:
        container_auditLog = container_auditLogs(
            login_user=str(username),
            host_ip=container.container_ip,
            hostname=container.hostname,
            action=2)
        container_auditLog.save()
    except Exception, e:
        print e
Beispiel #4
0
def container_start(request):
    username = request.session.get('username')
    container_id = request.POST.get('container_id')
    container = Container.objects.get(id=container_id)
    host_ip = container.host_ip
    host = Host.objects.filter(ip=host_ip)[0]
    netmask = host.ip_range.split("/")[1]
    gateway = host.gateway
    ip_with_mask = "%s/%s" % (container.container_ip, netmask)
    start_status = api.start(container.host_ip, container.container_id,
                             ip_with_mask, gateway, container.mount_dev,
                             container.mount_path)
    start_status = json.loads(start_status)
    if start_status['code'] == 200 or start_status['code'] == 201:
        container_auditLog = container_auditLogs(
            login_user=str(username),
            host_ip=container.container_ip,
            hostname=container.hostname,
            action=0)
        container_auditLog.save()
        return HttpResponse("启动成功")
    else:
        return HttpResponse("启动失败:%s" % start_status['status'])
Beispiel #5
0
def container_reset(request):
    username = request.session.get('username')
    container_id = request.POST.get('container_id')
    container_all = Container.objects.filter(id="%s" % container_id)
    for container in container_all:
        host_name = container.hostname
        memory_total = container.memory + "000000000"
        cpu_total = container.cpu
        disk_size = int(container.disk)
        mount_path = container.mount_path
        host_ip = container.host_ip
        container_ip = container.container_ip
        container_ip_with_mask = container.container_ip + "/24"
        str1 = []
        container_gw = container_ip.split('.')
        container_gw[len(container_gw) - 1] = '254'
        container_gw = '.'.join(container_gw)
        #print container_ip
        image = Container.objects.filter(container_ip="%s" % container_ip)
        for image_all in image:
            pass
        #print image_all.container_id
        images = image_all.os
        #print images
        #        images = 'centos6.7'
        mount_dev = container.mount_dev
        mount_path = container.mount_path
        add_users = container.application.users_add
        add_users = add_users.split(' ')
        # print host_name,container_ip
        while '' in add_users:
            #     print add_users
            add_users.remove('')

        try:
            #停止容器
            stop_status = api.stop(image_all.host_ip, image_all.container_id)
            stop_status = json.loads(stop_status)

            #删除容器
            delete_status = rest_api_delete(container)
            print delete_status

        except Exception, e:
            print e
            pass

        # 创建容器
        create_status = container_create(host_name, memory_total, cpu_total,
                                         images, disk_size, mount_path,
                                         host_ip, container_ip, container_gw)

        create_status = json.loads(create_status)
        #print create_status
        cid = create_status["containers_ID"]
        #print cid
        ids = [cid]
        #        print cid
        #        print create_status

        # 启动容器
        #        print host_ip, cid, container_ip, container_gw, mount_dev, mount_path
        start_status = start(host_ip, cid, container_ip_with_mask,
                             container_gw, mount_dev, mount_path)
        #        print start_status
        # 更新Container表
        container.container_id = cid
        container.save()

        # 从zeus中删除
        url = zeus_url + 'zapi/delete_asset/'
        values = {
            "username": zeus_user,
            "password": zeus_password,
            "ip": container_ip
        }
        values = json.dumps(values)
        req = urllib2.Request(url, values)
        response = urllib2.urlopen(req, timeout=300)
        the_page = response.read()
        #       print the_page
        if the_page == "faild":
            fail_container.append(container.container_ip)
            continue

        # 推送用户
        url = zeus_url + 'zapi/add_cmdb/'
        values = {
            "username": zeus_user,
            "password": zeus_password,
            "ip": container_ip,
            "hostname": host_name,
            "add_users": add_users
        }
        values = json.dumps(values)
        req = urllib2.Request(url, values)
        response = urllib2.urlopen(req, timeout=300)
        the_page = response.read()
        container_auditLog = container_auditLogs(
            login_user=str(username),
            host_ip=container.container_ip,
            hostname=container.hostname,
            action=3)
        container_auditLog.save()
        #      print "aaa"
        #      print the_page

        return HttpResponse("容器重置成功!")
Beispiel #6
0
            ip_hostname += container.hostname
            status_type = 7

    if fail_container:
        return HttpResponse("操作失败:%s" % fail_container)
    else:
        ips_all = ips.strip(',').strip('\n')
        ip_hostname_all = ip_hostname.strip(',').strip('\n')
        #print ips_all
        #print "ccc"
        print ip_hostname_all
        #print ip_hostname_all,type(ip_hostname_all)
        try:
            container_auditLog = container_auditLogs(
                login_user=str(username),
                host_ip=ips_all,
                hostname=str(ip_hostname_all),
                action=status_type)
            container_auditLog.save()
            return HttpResponse("操作成功")
        except Exception, e:
            pass
            print "nosqloracler"
            print e
            return HttpResponse("操作成功")
        #  return "数据库无法写入,报错:{0}".format(e)


@require_perm('M')
def user_manage(request):
    user_all = User.objects.all()