def list_raw_containers(user_list='ALL'): """ A running container is defined as a group of processes with the `pid` namespace different to the `init` process `pid` namespace. """ init_ns = namespace.get_pid_namespace(1) for p in psutil.process_iter(): pid = (p.pid() if hasattr(p.pid, '__call__') else p.pid) if pid == 1 or pid == '1': # don't confuse the init process as a container continue if user_list not in ['ALL', 'all', 'All']: if str(pid) not in user_list: # skip containers not in the list continue if misc.process_is_crawler(pid): # don't confuse the crawler process with a container continue curr_ns = namespace.get_pid_namespace(pid) if not curr_ns: # invalid container continue if curr_ns == init_ns: continue yield Container(pid, curr_ns)
def __init__( self, long_id, inspect=None, container_opts={}, process_namespace=None, ): # Some quick sanity checks if not isinstance(long_id, basestring): raise TypeError('long_id should be a string') if inspect and not isinstance(inspect, dict): raise TypeError('inspect should be a dict.') if container_opts and not isinstance(container_opts, dict): raise TypeError('container_opts should be a dict.') if not inspect: try: inspect = exec_dockerinspect(long_id) except HTTPError: raise ContainerNonExistent('No docker container with ID: %s' % long_id) state = inspect['State'] self.image = inspect['Image'] assert (long_id == inspect['Id']) self.long_id = long_id self.pid = str(state['Pid']) self.name = inspect['Name'] self.running = state['Running'] self.created = inspect['Created'] self.network_settings = inspect['NetworkSettings'] self.cmd = inspect['Config']['Cmd'] self.mounts = inspect.get('Mounts') self.volumes = inspect.get('Volumes') self.inspect = inspect self.process_namespace = (process_namespace or namespace.get_pid_namespace(self.pid)) # This short ID is mainly used for logging purposes self.short_id = long_id[:12] # Docker prepends a '/' to the name. Let's remove it. if self.name[0] == '/': self.name = self.name[1:] self._set_image_fields(inspect.get('RepoTag', '')) self._set_mounts_list() try: self.root_fs = get_docker_container_rootfs_path(self.long_id) except (HTTPError, RuntimeError, DockerutilsException) as e: logger.exception(e) self.root_fs = None self._set_logs_list_input() self._set_environment_specific_options(container_opts) self._set_logs_list()
def __init__( self, pid, container_opts={}, process_namespace=None, ): self.pid = str(pid) self.short_id = str(hash(pid)) self.long_id = str(hash(pid)) self.name = str(pid) self.namespace = str(pid) self.image = None self.root_fs = None self.log_prefix = None self.log_file_list = None self.process_namespace = (process_namespace or namespace.get_pid_namespace(pid))
def list_all_containers(user_list='ALL', namespace_opts={}, ): """ Returns a list of all running containers, as `Container` objects. A running container is defined as a process subtree with the `pid` namespace different to the `init` process `pid` namespace. """ all_docker_containers = list_docker_containers(namespace_opts) if user_list in ['ALL', 'all', 'All']: init_ns = namespace.get_pid_namespace(1) visited_ns = set() # visited PID namespaces # Start with all docker containers for container in all_docker_containers: curr_ns = namespace.get_pid_namespace(container.pid) if not curr_ns: continue if curr_ns not in visited_ns and curr_ns != init_ns: visited_ns.add(curr_ns) try: yield container except ContainerInvalidEnvironment as e: logger.exception(e) # Continue with all other containers not known to docker for p in psutil.process_iter(): pid = (p.pid() if hasattr(p.pid, '__call__') else p.pid) if pid == 1 or pid == '1': # don't confuse the init process as a container continue if misc.process_is_crawler(p): # don't confuse the crawler process with a container continue curr_ns = namespace.get_pid_namespace(pid) if not curr_ns: # invalid container continue if curr_ns not in visited_ns and curr_ns != init_ns: visited_ns.add(curr_ns) yield Container(pid) else: # User provided a list of containers user_containers = user_list.split(',') for container in all_docker_containers: short_id_match = container.short_id in user_containers long_id_match = container.long_id in user_containers if short_id_match or long_id_match: yield container
def list_all_containers( user_list='ALL', container_opts={}, ): """ Returns a list of all running containers, as `Container` objects. A running container is defined as a process subtree with the `pid` namespace different to the `init` process `pid` namespace. """ all_docker_containers = list_docker_containers(container_opts) if user_list in ['ALL', 'all', 'All']: init_ns = namespace.get_pid_namespace(1) visited_ns = set() # visited PID namespaces # Start with all docker containers for container in all_docker_containers: curr_ns = namespace.get_pid_namespace(container.pid) if not curr_ns: continue if curr_ns not in visited_ns and curr_ns != init_ns: visited_ns.add(curr_ns) try: yield container except ContainerInvalidEnvironment as e: logger.exception(e) # Continue with all other containers not known to docker for p in psutil.process_iter(): pid = (p.pid() if hasattr(p.pid, '__call__') else p.pid) if pid == 1 or pid == '1': # don't confuse the init process as a container continue if misc.process_is_crawler(pid): # don't confuse the crawler process with a container continue curr_ns = namespace.get_pid_namespace(pid) if not curr_ns: # invalid container continue if curr_ns not in visited_ns and curr_ns != init_ns: visited_ns.add(curr_ns) yield Container(pid) else: # User provided a list of containers user_containers = user_list.split(',') for container in all_docker_containers: short_id_match = container.short_id in user_containers long_id_match = container.long_id in user_containers if short_id_match or long_id_match: yield container