def __init__(self, manager, component, link_config): # The component that exports the link. self.component = component # The configuration for the component link. self.link_config = link_config # The kind of the link. self.kind = 'http' if link_config.kind.lower() == 'http' else 'tcp' # The port of the link inside the running container. self.container_port = link_config.port # The address of the link under the proxy (None if the link is not running). self.address = None # The port of the link under the proxy (None if the link is not running). self.exposed_port = None # Whether the link is currently running. self.running = False # Lookup the runtime information for the link. client = getDockerClient() container = component.getPrimaryContainer() if container: container_ip = containerutil.getContainerIPAddress(client, container) self.address = client.inspect_container(container)['NetworkSettings']['Gateway'] # The host's IP address. self.exposed_port = link_config.getHostPort() self.running = True
def updateProxy(self): """ Updates the proxy used for port mapping to conform to the current running container list. """ client = getDockerClient() # Clear all routes in the proxy. # TODO: When this is in daemon mode, don't need do this. We could selectively # edit it instead. self.proxy.clear_routes() # Add routes for the non-draining containers and collect the draining containers to # watch. report('Finding running containers...', level=ReportLevels.EXTRA) draining_containers = [] starting_containers = [] for component in self.components.values(): for container in component.getAllContainers(client): if getContainerStatus(container) != 'draining': container_ip = containerutil.getContainerIPAddress(client, container) starting_containers.append(container) # Add the normal exposed ports. for mapping in component.config.ports: route = Route(mapping.kind == 'http', mapping.external, container_ip, mapping.container) self.proxy.add_route(route) # Add the container link ports. for link in component.config.defined_component_links: route = Route(link.kind == 'http', link.getHostPort(), container_ip, link.port) self.proxy.add_route(route) else: draining_containers.append(container) # Commit the changes to the proxy. if draining_containers or starting_containers: report('Updating proxy...', level=ReportLevels.EXTRA) self.proxy.commit() else: report('Shutting down proxy...', level=ReportLevels.EXTRA) self.proxy.shutdown() # Mark the starting containers as running. for container in starting_containers: setContainerStatus(container, 'running')
def getContainerIPAddress(self, container): """ Returns the IP address on which the container is running. """ client = getDockerClient() return containerutil.getContainerIPAddress(client, container)