Example #1
0
def build_one_container(image_name, memory, code_address):
    """
    生成一个容器
    :param image_name: 镜像名字
    :param memory: 内存
    :param code_address: 代码zip地址
    :return: dict
    """

    port = get_one_able_post()
    system_image = get_realname_from_image_name(image_name)
    command = "docker run -d -m %dm -p %d:80 %s /bin/bash /tmp/start.sh '%s'" % (int(memory), port, system_image, code_address)

    code, _ = subprocess.getstatusoutput(command)
    if code != 0:
        return {"code": 10004}

    code, result = subprocess.getstatusoutput("docker ps | grep -v grep |grep 0.0.0.0:%d|awk -F ' ' '{print $1}'" % port)
    if code != 0:
        return {"code": 10004}

    # 记录新建容器端口情况
    obj = ContainerModel(port=port, container_id=result, memory=memory, code_address=code_address,
                         image_name=system_image, create_time=datetime.datetime.now())
    obj.save()

    return {"code": 0, "result": {"containerId": result, "port": port}}
Example #2
0
def build_one_container(image_name, memory, code_address):
    """
    生成一个容器
    :param image_name: 镜像名字
    :param memory: 内存
    :param code_address: 代码zip地址
    :return: dict
    """

    port = get_one_able_post()
    system_image = get_realname_from_image_name(image_name)
    command = "docker run -d -m %dm -p %d:80 %s /bin/bash /tmp/start.sh '%s'" % (
        int(memory), port, system_image, code_address)

    code, _ = subprocess.getstatusoutput(command)
    if code != 0:
        return {"code": 10004}

    code, result = subprocess.getstatusoutput(
        "docker ps | grep -v grep |grep 0.0.0.0:%d|awk -F ' ' '{print $1}'" %
        port)
    if code != 0:
        return {"code": 10004}

    # 记录新建容器端口情况
    obj = ContainerModel(port=port,
                         container_id=result,
                         memory=memory,
                         code_address=code_address,
                         image_name=system_image,
                         create_time=datetime.datetime.now())
    obj.save()

    return {"code": 0, "result": {"containerId": result, "port": port}}
Example #3
0
def analy():
    """
    获取主机容器状态
    :return: dict
    """

    token = request.forms.get('token', None)
    if not check_token(token):
        return {"code": 10001}

    count = ContainerModel.select().count()
    memory = ContainerModel.get_total_memory()

    return {"code": 0, "result": {"count": count, "totalMemory": memory}}
Example #4
0
def analy():
    """
    获取主机容器状态
    :return: dict
    """

    token = request.forms.get('token', None)
    if not check_token(token):
        return {"code": 10001}

    count = ContainerModel.select().count()
    memory = ContainerModel.get_total_memory()

    return {"code": 0, "result": {"count": count, "totalMemory": memory}}
Example #5
0
def remove():
    """
    删除容器
    :return:
    """

    container_id = request.forms.get('containerId', None)
    token = request.forms.get('token', None)

    r = option_container(container_id, token, "rm -f")

    if r['code'] == 0:
        dao = ContainerModel.delete().filter(container_id=container_id)
        dao.execute()
    return r
Example #6
0
def remove():
    """
    删除容器
    :return:
    """

    container_id = request.forms.get('containerId', None)
    token = request.forms.get('token', None)

    r = option_container(container_id, token, "rm -f")

    if r['code'] == 0:
        dao = ContainerModel.delete().filter(container_id=container_id)
        dao.execute()
    return r
Example #7
0
def get_one_able_post():
    """
    获取一个可用端口
    :return: int
    """
    port = 10000
    objs = ContainerModel.select()
    while port < 50000:
        sign = True
        for obj in objs:
            if obj.port == port:
                sign = False
                break
        if sign:
            return port
        port += 1
    return port
Example #8
0
def get_one_able_post():
    """
    获取一个可用端口
    :return: int
    """
    port = 10000
    objs = ContainerModel.select()
    while port < 50000:
        sign = True
        for obj in objs:
            if obj.port == port:
                sign = False
                break
        if sign:
            return port
        port += 1
    return port