def read_tar_image(self, image): with docker_utils.docker_client() as docker: LOG.debug('Reading local tar image %s ', image['path']) try: docker.read_tar_image(image) except Exception: LOG.warning("Unable to read image data from tarfile")
def search_image(self, context, repo, tag, exact_match): image_ref = docker_image.Reference.parse(repo) registry, image_name = image_ref.split_hostname() if registry and registry != 'docker.io': # Images searching is only supported in DockerHub msg = _('Image searching is not supported in registry: {0}') raise exception.OperationNotSupported(msg.format(registry)) with docker_utils.docker_client() as docker: try: # TODO(hongbin): search image by both name and tag images = docker.search(image_name) except errors.APIError as api_error: raise exception.ZunException(six.text_type(api_error)) except Exception as e: msg = _('Cannot search image in docker: {0}') raise exception.ZunException(msg.format(e)) if exact_match: images = [i for i in images if i['name'] == image_name] for image in images: image['metadata'] = {} for key in ('is_official', 'star_count'): value = image.pop(key, None) if value is not None: image['metadata'][key] = value # TODO(hongbin): convert images to a list of Zun Image object return images
def search_image(self, context, repo, tag, exact_match): image_ref = docker_image.Reference.parse(repo) registry, image_name = image_ref.split_hostname() if registry and registry != 'docker.io': # Images searching is only supported in DockerHub msg = _('Image searching is not supported in registry: {0}') raise exception.OperationNotSupported(msg.format(registry)) with docker_utils.docker_client() as docker: try: # TODO(hongbin): search image by both name and tag images = docker.search(image_name) except errors.APIError as api_error: raise exception.ZunException(str(api_error)) except Exception as e: msg = _('Cannot search image in docker: {0}') raise exception.ZunException(msg.format(e)) if exact_match: images = [i for i in images if i['name'] == image_name] for image in images: image['metadata'] = {} for key in ('is_official', 'star_count'): value = image.pop(key, None) if value is not None: image['metadata'][key] = value # TODO(hongbin): convert images to a list of Zun Image object return images
def list(self, context): id_to_container_map = {} uuids = [] with docker_utils.docker_client() as docker: docker_containers = docker.list_containers() id_to_container_map = {c['Id']: c for c in docker_containers} uuids = self._get_container_uuids(docker_containers) local_containers = self._get_local_containers(context, uuids) for container in local_containers: if container.status in (consts.CREATING, consts.DELETING, consts.DELETED): # Skip populating db record since the container is in a # unstable state. continue container_id = container.container_id docker_container = id_to_container_map.get(container_id) if not container_id or not docker_container: if container.auto_remove: container.status = consts.DELETED container.save(context) else: self.heal_with_rebuilding_container(context, container) continue self._populate_container(container, docker_container) return local_containers
def _pull_image(self, repo, tag, registry): auth_config = None image_ref = docker_image.Reference.parse(repo) registry_domain, remainder = image_ref.split_hostname() if registry and registry.username: auth_config = { 'username': registry.username, 'password': registry.password } elif (registry_domain and registry_domain == CONF.docker.default_registry and CONF.docker.default_registry_username): auth_config = { 'username': CONF.docker.default_registry_username, 'password': CONF.docker.default_registry_password } with docker_utils.docker_client() as docker: try: docker.pull(repo, tag=tag, auth_config=auth_config) except errors.NotFound as e: raise exception.ImageNotFound(message=str(e)) except errors.APIError: LOG.exception('Error on pulling image') message = _('Error on pulling image: %(repo)s:%(tag)s') % { 'repo': repo, 'tag': tag } raise exception.ZunException(message)
def create_sandbox(self, context, container, image='kubernetes/pause', requested_networks=None): with docker_utils.docker_client() as docker: network_api = zun_network.api(context=context, docker_api=docker) if not requested_networks: network = self._provision_network(context, container, network_api) requested_networks = [{'network': network['Name'], 'port': '', 'v4-fixed-ip': '', 'v6-fixed-ip': ''}] name = self.get_sandbox_name(container) sandbox = docker.create_container(image, name=name, hostname=name[:63]) container.set_sandbox_id(sandbox['Id']) addresses = self._setup_network_for_container( context, container, requested_networks, network_api) if addresses is None: raise exception.ZunException(_( "Unexpected missing of addresses")) container.addresses = addresses container.save(context) docker.start(sandbox['Id']) return sandbox['Id']
def network_attach(self, context, container, requested_network): with docker_utils.docker_client() as docker: security_group_ids = None if container.security_groups: security_group_ids = utils.get_security_group_ids( context, container.security_groups) network_api = zun_network.api(context, docker_api=docker) network = requested_network['network'] if network in container.addresses: raise exception.ZunException('Container %(container)s has' ' alreay connected to the network' '%(network)s.' % {'container': container.uuid, 'network': network}) self._get_or_create_docker_network(context, network_api, network) docker_net_name = self._get_docker_network_name(context, network) addrs = network_api.connect_container_to_network( container, docker_net_name, requested_network, security_groups=security_group_ids) if addrs is None: raise exception.ZunException(_( 'Unexpected missing of addresses')) update = {} update[network] = addrs addresses = container.addresses addresses.update(update) container.addresses = addresses container.save(context)
def check_container_exist(self, container): with docker_utils.docker_client() as docker: docker_containers = [c['Id'] for c in docker.list_containers()] if container.container_id not in docker_containers: return False return True
def remove_security_group(self, context, container, security_group): with docker_utils.docker_client() as docker: network_api = zun_network.api(context=context, docker_api=docker) network_api.remove_security_groups_from_ports(container, [security_group])
def kill(self, context, container, signal=None): with docker_utils.docker_client() as docker: if signal is None or signal == 'None': docker.kill(container.container_id) else: docker.kill(container.container_id, signal) return container
def get_host_info(self): with docker_utils.docker_client() as docker: info = docker.info() total = info['Containers'] paused = info['ContainersPaused'] running = info['ContainersRunning'] stopped = info['ContainersStopped'] cpus = info['NCPU'] architecture = info['Architecture'] os_type = info['OSType'] os = info['OperatingSystem'] kernel_version = info['KernelVersion'] labels = {} slabels = info['Labels'] if slabels: for l in slabels: kv = l.split("=") label = {kv[0]: kv[1]} labels.update(label) runtimes = [] if 'Runtimes' in info: for key in info['Runtimes']: runtimes.append(key) else: runtimes = ['runc'] return (total, running, paused, stopped, cpus, architecture, os_type, os, kernel_version, labels, runtimes)
def show_logs(self, context, container, stdout=True, stderr=True, timestamps=False, tail='all', since=None): with docker_utils.docker_client() as docker: try: tail = int(tail) except ValueError: tail = 'all' if since is None or since == 'None': return docker.logs(container.container_id, stdout, stderr, False, timestamps, tail, None) else: try: since = int(since) except ValueError: try: since = datetime.datetime.strptime( since, '%Y-%m-%d %H:%M:%S,%f') except Exception: raise return docker.logs(container.container_id, stdout, stderr, False, timestamps, tail, since)
def list(self, context): id_to_container_map = {} with docker_utils.docker_client() as docker: id_to_container_map = { c['Id']: c for c in docker.list_containers() } db_containers = objects.Container.list_by_host(context, CONF.host) for db_container in db_containers: if db_container.status in (consts.CREATING, consts.DELETED): # Skip populating db record since the container is in a # unstable state. continue container_id = db_container.container_id docker_container = id_to_container_map.get(container_id) if not docker_container: if db_container.auto_remove: db_container.status = consts.DELETED db_container.save(context) else: LOG.warning("Container was recorded in DB but missing in " "docker") continue self._populate_container(db_container, docker_container) return db_containers
def create_sandbox(self, context, container, requested_networks, requested_volumes, image='kubernetes/pause'): with docker_utils.docker_client() as docker: network_api = zun_network.api(context=context, docker_api=docker) self._provision_network(context, network_api, requested_networks) binds = self._get_binds(context, requested_volumes) host_config = {'binds': binds} name = self.get_sandbox_name(container) volumes = [b['bind'] for b in binds.values()] kwargs = { 'name': name, 'hostname': name[:63], 'volumes': volumes, } self._process_networking_config( context, container, requested_networks, host_config, kwargs, docker) kwargs['host_config'] = docker.create_host_config(**host_config) sandbox = docker.create_container(image, **kwargs) container.set_sandbox_id(sandbox['Id']) addresses = self._setup_network_for_container( context, container, requested_networks, network_api) if not addresses: raise exception.ZunException(_( "Unexpected missing of addresses")) container.addresses = addresses container.save(context) docker.start(sandbox['Id']) return sandbox['Id']
def _find_container_by_server_name(self, name): with docker_utils.docker_client() as docker: for info in docker.list_instances(inspect=True): if info['Config'].get('Hostname') == name: return info['Id'] raise exception.ZunException( _("Cannot find container with name %s") % name)
def list(self, context): non_existent_containers = [] with docker_utils.docker_client() as docker: docker_containers = docker.list_containers() id_to_container_map = {c['Id']: c for c in docker_containers} uuids = self._get_container_uuids(docker_containers) local_containers = self._get_local_containers(context, uuids) for container in local_containers: if container.status in (consts.CREATING, consts.DELETING, consts.DELETED): # Skip populating db record since the container is in a # unstable state. continue container_id = container.container_id docker_container = id_to_container_map.get(container_id) if not container_id or not docker_container: non_existent_containers.append(container) continue self._populate_container(container, docker_container) return local_containers, non_existent_containers
def delete_sandbox(self, context, sandbox_id): with docker_utils.docker_client() as docker: try: docker.remove_container(sandbox_id, force=True) except errors.APIError as api_error: if '404' in str(api_error): return raise
def reboot(self, container, timeout): with docker_utils.docker_client() as docker: if timeout: docker.restart(container.container_id, timeout=int(timeout)) else: docker.restart(container.container_id) container.status = fields.ContainerStatus.RUNNING return container
def stop(self, container, timeout): with docker_utils.docker_client() as docker: if timeout: docker.stop(container.container_id, timeout=int(timeout)) else: docker.stop(container.container_id) container.status = fields.ContainerStatus.STOPPED return container
def commit(self, context, container, repository=None, tag=None): with docker_utils.docker_client() as docker: repository = str(repository) if tag is None or tag == "None": return docker.commit(container.container_id, repository) else: tag = str(tag) return docker.commit(container.container_id, repository, tag)
def execute_create(self, context, container, command, interactive=False): stdin = True if interactive else False tty = True if interactive else False with docker_utils.docker_client() as docker: create_res = docker.exec_create( container.container_id, command, stdin=stdin, tty=tty) exec_id = create_res['Id'] return exec_id
def put_archive(self, context, container, path, data): with docker_utils.docker_client() as docker: try: docker.put_archive(container.container_id, path, data) except errors.APIError as api_error: if is_not_found(api_error): raise exception.Invalid(_("%s") % str(api_error)) raise
def _pull_image(self, repo, tag): with docker_utils.docker_client() as docker: for line in docker.pull(repo, tag=tag, stream=True): error = json.loads(line).get('errorDetail') if error: if "not found" in error['message']: raise exception.ImageNotFound(error['message']) else: raise exception.DockerError(error['message'])
def create_sandbox(self, context, container, image='kubernetes/pause'): with docker_utils.docker_client() as docker: name = self.get_sandbox_name(container) response = docker.create_container(image, name=name, hostname=name[:63]) sandbox_id = response['Id'] docker.start(sandbox_id) return sandbox_id
def reboot(self, context, container, timeout): with docker_utils.docker_client() as docker: if timeout: docker.restart(container.container_id, timeout=int(timeout)) else: docker.restart(container.container_id) container.status = consts.RUNNING container.status_reason = None return container
def kill(self, context, container, signal=None): with docker_utils.docker_client() as docker: if signal is None or signal == 'None': docker.kill(container.container_id) else: docker.kill(container.container_id, signal) container.status = consts.STOPPED container.status_reason = None return container
def stop(self, context, container, timeout): with docker_utils.docker_client() as docker: if timeout: docker.stop(container.container_id, timeout=int(timeout)) else: docker.stop(container.container_id) container.status = consts.STOPPED container.status_reason = None return container
def _find_server_by_container_id(self, container_id): with docker_utils.docker_client() as docker: try: info = docker.inspect_container(container_id) return info['Config'].get('Hostname') except errors.APIError as e: if e.response.status_code != 404: raise return None
def inspect_image(self, image, image_path=None): with docker_utils.docker_client() as docker: if image_path: LOG.debug('Loading local image in docker %s' % image) with open(image_path, 'r') as fd: docker.load_image(fd.read()) LOG.debug('Inspecting image %s' % image) image_dict = docker.inspect_image(image) return image_dict
def execute_run(self, exec_id, command): with docker_utils.docker_client() as docker: try: with eventlet.Timeout(CONF.docker.execute_timeout): output = docker.exec_start(exec_id, False, False, False) except eventlet.Timeout: raise exception.Conflict(_( "Timeout on executing command: %s") % command) inspect_res = docker.exec_inspect(exec_id) return output, inspect_res['ExitCode']
def delete(self, container, force): with docker_utils.docker_client() as docker: if container.container_id: try: docker.remove_container(container.container_id, force=force) except errors.APIError as api_error: if '404' in str(api_error): return raise
def _search_image_on_host(self, repo, tag): with docker_utils.docker_client() as docker: image = repo + ":" + tag LOG.debug('Inspecting image locally %s', image) try: image_dict = docker.inspect_image(image) if image_dict: return {'image': repo, 'path': None} except errors.NotFound: LOG.debug('Image %s not found locally', image) return None
def delete_image(self, context, img_id): LOG.debug('Delete an image %s in docker', img_id) with docker_utils.docker_client() as docker: try: docker.remove_image(img_id) except errors.ImageNotFound: return except errors.APIError as api_error: raise exception.ZunException(six.text_type(api_error)) except Exception as e: LOG.exception('Unknown exception occurred while deleting ' 'image %s in glance:%s', img_id, six.text_type(e)) raise exception.ZunException(six.text_type(e))
def _pull_image(self, repo, tag, registry): auth_config = None image_ref = docker_image.Reference.parse(repo) registry_domain, remainder = image_ref.split_hostname() if registry and registry.username: auth_config = {'username': registry.username, 'password': registry.password} elif (registry_domain and registry_domain == CONF.docker.default_registry and CONF.docker.default_registry_username): auth_config = {'username': CONF.docker.default_registry_username, 'password': CONF.docker.default_registry_password} with docker_utils.docker_client() as docker: try: docker.pull(repo, tag=tag, auth_config=auth_config) except errors.NotFound as e: raise exception.ImageNotFound(message=six.text_type(e)) except errors.APIError as e: LOG.exception('Error on pulling image') message = _('Error on pulling image: %(repo)s:%(tag)s') % { 'repo': repo, 'tag': tag} raise exception.ZunException(message)