def _mock_out_binding(self, endpoint_id, neutron_port, neutron_subnets): self.mox.StubOutWithMock(binding, 'port_bind') fake_binding_response = ( 'fake-veth', 'fake-veth_c', ('fake stdout', '')) binding.port_bind(endpoint_id, neutron_port, neutron_subnets).AndReturn(fake_binding_response) self.mox.ReplayAll() return fake_binding_response
def _mock_out_binding(self, endpoint_id, neutron_port, neutron_subnets): self.mox.StubOutWithMock(binding, 'port_bind') fake_binding_response = ('fake-veth', 'fake-veth_c', ('fake stdout', '')) binding.port_bind(endpoint_id, neutron_port, neutron_subnets).AndReturn(fake_binding_response) self.mox.ReplayAll() return fake_binding_response
def _port_bind_with_exeption(self, docker_endpiont_id, neutron_port, neutron_subnets, ex): fake_ifname = 'fake-veth' fake_binding_response = ( fake_ifname, fake_ifname + binding.CONTAINER_VETH_POSTFIX, ('fake stdout', '') ) self.mox.StubOutWithMock(binding, 'port_bind') if ex: binding.port_bind( docker_endpiont_id, neutron_port, neutron_subnets).AndRaise(ex) else: binding.port_bind( docker_endpiont_id, neutron_port, neutron_subnets).AndReturn( fake_binding_response) self.mox.ReplayAll() return fake_binding_response
def network_driver_join(): """Binds a Neutron Port to a network interface attached to a container. This function takes the following JSON data, creates a veth pair, put one end inside of the container and binds another end to the Neutron Port specified in the request. :: { "NetworkID": string, "EndpointID": string, "SandboxKey": string, "Options": { ... } } If the binding is succeeded, the following JSON response is returned.:: { "InterfaceName": { SrcName: string, DstPrefix: string }, "Gateway": string, "GatewayIPv6": string, "StaticRoutes": [{ "Destination": string, "RouteType": int, "NextHop": string, }, ...] } See the following link for more details about the spec: https://github.com/docker/libnetwork/blob/master/docs/remote.md#join # noqa """ json_data = flask.request.get_json(force=True) app.logger.debug( "Received JSON data {0} for /NetworkDriver.Join".format(json_data)) jsonschema.validate(json_data, schemata.JOIN_SCHEMA) neutron_network_tags = utils.make_net_tags(json_data['NetworkID']) endpoint_id = json_data['EndpointID'] filtered_networks = _get_networks_by_attrs(tags=neutron_network_tags) if not filtered_networks: return flask.jsonify({ 'Err': "Neutron network associated with tags {0} doesn't exit.".format( neutron_network_tags) }) else: neutron_network_id = filtered_networks[0]['id'] neutron_port_name = utils.get_neutron_port_name(endpoint_id) filtered_ports = _get_ports_by_attrs(name=neutron_port_name) if not filtered_ports: raise exceptions.NoResourceException( "The port doesn't exist for the name {0}".format( neutron_port_name)) neutron_port = filtered_ports[0] all_subnets = _get_subnets_by_attrs(network_id=neutron_network_id) try: ifname, peer_name, (stdout, stderr) = binding.port_bind( endpoint_id, neutron_port, all_subnets) app.logger.debug(stdout) if stderr: app.logger.error(stderr) except exceptions.VethCreationFailure as ex: with excutils.save_and_reraise_exception(): app.logger.error( _LE('Preparing the veth ' 'pair was failed: {0}.').format(ex)) except processutils.ProcessExecutionError: with excutils.save_and_reraise_exception(): app.logger.error( _LE('Could not bind the Neutron port to the veth endpoint.' )) join_response = { "InterfaceName": { "SrcName": peer_name, "DstPrefix": config.CONF.binding.veth_dst_prefix }, "StaticRoutes": [] } for subnet in all_subnets: if subnet['ip_version'] == 4: join_response['Gateway'] = subnet.get('gateway_ip', '') else: join_response['GatewayIPv6'] = subnet.get('gateway_ip', '') host_routes = subnet.get('host_routes', []) for host_route in host_routes: static_route = {'Destination': host_route['destination']} if host_route.get('nexthop', None): static_route['RouteType'] = const.TYPES['NEXTHOP'] static_route['NextHop'] = host_route['nexthop'] else: static_route['RouteType'] = const.TYPES['CONNECTED'] join_response['StaticRoutes'].append(static_route) return flask.jsonify(join_response)
def network_driver_join(): """Binds a Neutron Port to a network interface attached to a container. This function takes the following JSON data, creates a veth pair, put one end inside of the container and binds another end to the Neutron Port specified in the request. :: { "NetworkID": string, "EndpointID": string, "SandboxKey": string, "Options": { ... } } If the binding is succeeded, the following JSON response is returned.:: { "InterfaceName": { SrcName: string, DstPrefix: string }, "Gateway": string, "GatewayIPv6": string, "StaticRoutes": [{ "Destination": string, "RouteType": int, "NextHop": string, }, ...] } See the following link for more details about the spec: https://github.com/docker/libnetwork/blob/master/docs/remote.md#join # noqa """ json_data = flask.request.get_json(force=True) app.logger.debug("Received JSON data {0} for /NetworkDriver.Join" .format(json_data)) jsonschema.validate(json_data, schemata.JOIN_SCHEMA) neutron_network_name = json_data['NetworkID'] endpoint_id = json_data['EndpointID'] filtered_networks = _get_networks_by_attrs(name=neutron_network_name) if not filtered_networks: return flask.jsonify({ 'Err': "Neutron network associated with ID {0} doesn't exit." .format(neutron_network_name) }) else: neutron_network_id = filtered_networks[0]['id'] neutron_port_name = utils.get_neutron_port_name(endpoint_id) filtered_ports = _get_ports_by_attrs(name=neutron_port_name) if not filtered_ports: raise exceptions.NoResourceException( "The port doesn't exist for the name {0}" .format(neutron_port_name)) neutron_port = filtered_ports[0] all_subnets = _get_subnets_by_attrs(network_id=neutron_network_id) try: ifname, peer_name, (stdout, stderr) = binding.port_bind( endpoint_id, neutron_port, all_subnets) app.logger.debug(stdout) if stderr: app.logger.error(stderr) except exceptions.VethCreationFailure as ex: with excutils.save_and_reraise_exception(): app.logger.error('Preparing the veth pair was failed: {0}.' .format(ex)) except processutils.ProcessExecutionError: with excutils.save_and_reraise_exception(): app.logger.error( 'Could not bind the Neutron port to the veth endpoint.') join_response = { "InterfaceName": { "SrcName": peer_name, "DstPrefix": config.CONF.binding.veth_dst_prefix }, "StaticRoutes": [] } for subnet in all_subnets: if subnet['ip_version'] == 4: join_response['Gateway'] = subnet.get('gateway_ip', '') else: join_response['GatewayIPv6'] = subnet.get('gateway_ip', '') host_routes = subnet.get('host_routes', []) for host_route in host_routes: static_route = { 'Destination': host_route['destination'] } if host_route.get('nexthop', None): static_route['RouteType'] = constants.TYPES['NEXTHOP'] static_route['NextHop'] = host_route['nexthop'] else: static_route['RouteType'] = constants.TYPES['CONNECTED'] join_response['StaticRoutes'].append(static_route) return flask.jsonify(join_response)
def _mock_out_binding(self, endpoint_id, neutron_port, neutron_subnets): self.mox.StubOutWithMock(binding, "port_bind") fake_binding_response = ("fake-veth", "fake-veth_c", ("fake stdout", "")) binding.port_bind(endpoint_id, neutron_port, neutron_subnets).AndReturn(fake_binding_response) self.mox.ReplayAll() return fake_binding_response