예제 #1
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'])
예제 #2
0
 def process_stop_v1(self, request):
     return api.stop()
예제 #3
0
 def jsonrpc_stop(self):
     return api.stop()
예제 #4
0
def container_batch_handle(request):
    username = request.session.get('username')
    option = request.POST.get('option')
    containers = request.POST.get('containers')
    containers = json.loads(containers)
    print containers
    print "aaaccc"
    fail_container = []
    ips = ""
    ip_hostname = ""
    for cid in containers:
        container = Container.objects.get(id=cid)
        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)
        if option == 'start':

            start_status = api.start(container.host_ip, container.container_id,
                                     ip_with_mask, gateway,
                                     container.mount_dev, container.mount_path)
            print "bbb"
            start_status = json.loads(start_status)
            print "eeee"
            if start_status['code'] == 200 or start_status['code'] == 201:
                ips += ","
                ips += container.container_ip
                ip_hostname += ","
                ip_hostname += container.hostname
                status_type = 5
                pass
            else:
                fail_container.append(container.container_ip)

        if option == 'stop':
            stop_status = api.stop(container.host_ip, container.container_id)
            stop_status = json.loads(stop_status)
            if stop_status['code'] == 200:
                ips += ","
                ips += container.container_ip
                ip_hostname += ","
                ip_hostname += container.hostname
                status_type = 6
                pass
            else:
                fail_container.append(container.container_ip)
        if option == 'reset':
            container_all = Container.objects.filter(id="%s" % cid)
            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 = Application.objects.filter(result="IP:%s" % container_ip)
                image = Container.objects.filter(container_ip="%s" %
                                                 container_ip)
                #print image
                for image_all in image:
                    #print image_all.os
                    pass
                #print "tttddd"
                #print images_all
                images = image_all.os
                print images
                #print "cccaaa"
                #        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:
                    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)
                cid = create_status["containers_ID"]
                ids = [cid]

                # 启动容器
                start_status = start(host_ip, cid, container_ip_with_mask,
                                     container_gw, mount_dev, mount_path)
                # 更新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()
                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()
                ips += ","
                ips += container.container_ip
                ip_hostname += ","
                ip_hostname += container.hostname
                status_type = 8

        if option == 'delete':
            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:
                fail_container.append(container.container_ip)
                continue

            # 从zeus中删除
            url = zeus_url + 'zapi/delete_asset/'
            values = {
                "username": zeus_user,
                "password": zeus_password,
                "ip": container.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

            # 删除容器表记录
            container.delete()
            ips += ","
            ips += container.container_ip
            ip_hostname += ","
            ip_hostname += container.hostname
            status_type = 7
예제 #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("容器重置成功!")