コード例 #1
0
ファイル: container.py プロジェクト: bobff/mutil-docker
def create_container(request, *args, **kwargs):
    ip          = request.POST.get('ip', '')
    port        = request.POST.get('port', '')
    image       = request.POST.get('image')
    hostname    = request.POST.get('hostname')
    cmd         = request.POST.get('cmd')
    cpu         = request.POST.get('cpu', '')
    mem         = request.POST.get('mem', '')
    swap        = request.POST.get('swap', '')
    disk        = request.POST.get('disk', '')
    volume      = request.POST.get('volume', '')
    ipv4pool    = request.POST.get('ipv4pool', '')
    name        = request.POST.get('name', '')
            
    ips = ip.split('/')
    ip = ips[0]
    if len(ips) > 1:
        ip_size = ips[1]
    else:
        ip_size = 24

    data = {
        'ip'        : ip, 
        'ip_size'   : ip_size, 
        'port'      : port, 
        'image'     : image,
        'hostname'  : hostname,
        'cmd'       : cmd,
        'cpu'       : cpu,
        'mem'       : mem,
        'swap'      : swap,
        'disk'      : disk,
        'volume'    : volume,
        'ipv4pool'  : ipv4pool,
        'name'      : name,
    }

    client = Client(hostname)
    c = client.create_container(data)
    return HttpResponse({'res':True, 'data':c.id})
コード例 #2
0
ファイル: container.py プロジェクト: bobff/mutil-docker
def auto_create_container(request, *args, **kwargs):
    
    num  = request.POST.get('num', None)
    end_date = request.POST.get('end_date', None)
    image = request.POST.get('image', None)

    cpu  = request.POST.get('cpu', '')
    mem  = request.POST.get('mem', '')
    disk = request.POST.get('disk', '')
    port = request.POST.get('port', '')
    volume = request.POST.get('volume', '')

    valid = True

    try:
        num = int(num)
    except:
        pass
    
    if valid and end_date != None and validate_date(end_date) == False:
        valid = False
        dicts = {'res': False, 'err': 'end_date 日期格式错误, 正确格式: yyyy-mm-dd'}

    if valid and num <= 0:
        dicts = {'res':False, 'err': 'num参数无效'}
        valid = False
    if valid:
        pems = CreatePermission.objects.filter(user = request.api_user)
        if not pems.exists():
            dicts = {'res':False, 'err': '没有创建权限'}
            valid = False
       

    # if valid and num > (pem.pool.ip_count - pem.pool.ip_used_count):
    #     dicts = {'res':False, 'err': 'ip地址不够'}
    #     valid = False

    if valid:
        created = []
        remain_count = num
        post_image = image
        for pem in pems:
            print pem.host.name, remain_count
            if remain_count <= 0:
                break
            if not post_image:
                image = pem.image
            else:
                image = post_image
            client = Client(pem.host.name)
            image_exist = False
            for i in client.get_images():
                if i['repotag'] == image:
                    image = i['regrepotag']
                    image_exist = True
                    continue
            if not image_exist:
                dicts = {'res':False, 'err': '镜像名有误'}
                valid = False
                continue

            exists_count = Container.objects.filter(name__startswith=pem.user.name, host=pem.host).count()
            limit = pem.num - exists_count
            limit = (limit < 0 and [0] or [limit])[0]
            
            pem_loop_num = (limit > remain_count and [remain_count] or [limit])[0]
            print 'pem_loop_num', pem_loop_num
            print 'exists_count', exists_count
            print 'remain_count', remain_count
            if volume == '' and pem.volume_bind not in (None, ''):
                volume = pem.volume_bind
            if port == ''  and pem.port_bind not in (None, ''):
                port = pem.port_bind
            if cpu == ''  and pem.cpu not in (None, ''):
                port = pem.cpu
            if mem == ''  and pem.mem not in (None, ''):
                port = pem.mem
            if disk == ''  and pem.disk not in (None, ''):
                port = pem.disk

            for ip in pem.pool.get_free_ip_range(pem_loop_num):
                print ip
                data = {
                    'ip'        : ip, 
                    'ip_size'   : pem.pool.size, 
                    'port'      : port, 
                    'image'     : image,
                    'hostname'  : pem.host.name,
                    'cmd'       : '',
                    'cpu'       : cpu,
                    'mem'       : mem,
                    'swap'      : '',
                    'disk'      : disk,
                    'volume'    : volume,
                    'ipv4pool'  : pem.pool.pk,
                    'name'      : pem.user.name,
                    'prename'   : True,
                    'end_date'  : end_date,
                }
                # client = Client(pem.host.name)
                res, c = client.create_container(data)
                print 'create ', ip, res
                if res:
                    info = {
                        'id'        : c.id,
                        'name'      : c.name,
                        'ipv4'      : c.network_ipv4,
                        'login_name': c.login_name,
                        'login_pwd' : c.login_pwd,
                        'cpu_share' : c.cpu_share,
                        'mem_limit' : c.mem_limit,
                        'swap_limit': c.swap_limit,
                        'disk_limit': c.disk_limit,
                        'port_bind' : c.port_bind,
                        'volume_bind': c.volume_bind,
                        'start_date': str(c.start_date),
                        'end_date'  : str(c.end_date),
                    }
                    remain_count -= 1
                    
                created.append(res and {'res':res, 'info':info} or {'res':res, 'err':c})
                    
        dicts = {'res': True, 'info': created}
        print dicts
    return HttpResponse(simplejson.dumps(dicts), mimetype = 'application/json')
コード例 #3
0
ファイル: views.py プロジェクト: bobff/mutil-docker
def auto_create_container(request, *args, **kwargs):
    
    num  = request.POST.get('num', None)
    end_date = request.POST.get('end_date', None)
    image = request.POST.get('image', None)

    cpu  = request.POST.get('cpu', '')
    mem  = request.POST.get('mem', '')
    disk = request.POST.get('disk', '')
    port = request.POST.get('port', '')
    volume = request.POST.get('volume', '')

    valid = True

    try:
        num = int(num)
    except:
        pass
    
    if valid and end_date != None and validate_date(end_date) == False:
        valid = False
        dicts = {'res': False, 'err': 'end_date 日期格式错误, 正确格式: yyyy-mm-dd'}

    if valid and num <= 0:
        dicts = {'res':False, 'err': 'num参数无效'}
        valid = False
    if valid:
        pem = CreatePermission.objects.filter(user = request.api_user)
        if not pem.exists():
            dicts = {'res':False, 'err': '没有创建权限'}
            valid = False
        pem = pem[0]

    if valid:
        if not image:
            image = pem.image
        client = Client(pem.host.name)
        image_exist = False
        for i in client.get_images():
            if i['repotag'] == image:
                image = i['regrepotag']
                image_exist = True
                break
        if not image_exist:
            dicts = {'res':False, 'err': '镜像名有误'}
            valid = False
    if valid and num > pem.num:
        dicts = {'res':False, 'err': '超出创建容器数量限制'}
        valid = False

    if valid and num > (pem.pool.ip_count - pem.pool.ip_used_count):
        dicts = {'res':False, 'err': 'ip地址不够'}
        valid = False

    if valid:
        created = []
        for ip in pem.pool.get_free_ip_range(num):
            data = {
                'ip'        : ip, 
                'ip_size'   : pem.pool.size, 
                'port'      : port, 
                'image'     : image,
                'hostname'  : pem.host.name,
                'cmd'       : '',
                'cpu'       : cpu,
                'mem'       : mem,
                'swap'      : '',
                'disk'      : disk,
                'volume'    : volume,
                'ipv4pool'  : pem.pool.pk,
                'name'      : pem.user.name,
                'end_date'  : end_date,
            }
            # client = Client(pem.host.name)
            res, c = client.create_container(data)
            if res:
                info = {
                    'id'        : c.id,
                    'name'      : c.name,
                    'ipv4'      : c.network_ipv4,
                    'login_name': c.login_name,
                    'login_pwd' : c.login_pwd,
                    'cpu_share' : c.cpu_share,
                    'mem_limit' : c.mem_limit,
                    'swap_limit': c.swap_limit,
                    'disk_limit': c.disk_limit,
                    'port_bind' : c.port_bind,
                    'volume_bind': c.volume_bind,
                    'start_date': str(c.start_date),
                    'end_date'  : str(c.end_date),
                }
            created.append(res and {'res':res, 'info':info} or {'res':res, 'err':c})
        dicts = {'res': True, 'info': created}

    return HttpResponse(simplejson.dumps(dicts), mimetype = 'application/json')