Beispiel #1
0
def get_network(id_or_name):
    if id_or_name.isdigit():
        n = Network.get(id_or_name)
    else:
        n = Network.get_by_name(id_or_name)
    if not n:
        abort(404, 'Network %s not found' % id_or_name)
    return n
Beispiel #2
0
def macvlan(id_or_name):
    host = _get_host(id_or_name)
    if request.method == 'GET':
        return host.list_vlans(g.start, g.limit)

    data = request.get_json()
    netname = data.get('network', '')
    network = Network.get_by_name(netname)
    if not network:
        abort(404, 'Network not found')

    vg = VLanGateway.get_by_host_and_network(host.id, network.id)
    if request.method == 'POST':
        return vg or network.acquire_gateway_ip(host)
    elif request.method == 'DELETE':
        if vg:
            vg.release()
        return {'r': 0, 'msg': consts.OK}
Beispiel #3
0
def bind_network(cid):
    data = request.get_json()
    appname = data.get('appname')
    c = Container.get_by_container_id(cid)
    if not (c and c.is_alive):
        raise EruAbortException(consts.HTTP_NOT_FOUND, 'Container %s not found' % cid)
    if c.appname != appname:
        raise EruAbortException(consts.HTTP_NOT_FOUND, 'Container does not belong to app')

    network_names = data.get('networks', [])
    networks = filter(None, [Network.get_by_name(n) for n in network_names])
    if not networks:
        raise EruAbortException(consts.HTTP_BAD_REQUEST, 'network empty')

    ips = filter(None, [n.acquire_ip() for n in networks])
    if not ips:
        raise EruAbortException(consts.HTTP_BAD_REQUEST, 'no ip available')

    nid = max([ip.network_id for ip in c.ips.all()] + [-1]) + 1
    bind_container_ip(c, ips, nid=nid)
    for ip in ips:
        ip.assigned_to_container(c)
    return {'r': 0, 'msg': ips}
Beispiel #4
0
def get_network_by_name(network_name):
    n = Network.get_by_name(network_name)
    if not n:
        raise EruAbortException(consts.HTTP_NOT_FOUND, 'Network %s not found' % network_name)
    return n