def bind_network(id_or_cid): c = _get_container(id_or_cid) data = request.get_json() appname = data['appname'] if not c.is_alive: abort(404, 'Container %s not alive' % c.container_id) if c.appname != appname: abort(404, 'Container does not belong to app') if c.network_mode == 'host': abort(400, 'Container use host network mode') network_names = data.get('networks', []) networks = [ipam.get_pool(n) for n in network_names] if not networks: abort(400, 'network empty') cidrs = [n.cidr for n in networks if n] bind_container_ip(c, cidrs) return {'cidrs': cidrs}
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}