def container_show(self, context, container_uuid): LOG.debug("container_show %s" % container_uuid) docker = self.get_docker_client(context, container_uuid) container = objects.Container.get_by_uuid(context, container_uuid) try: docker_id = self._find_container_by_name(docker, container_uuid) if not docker_id: LOG.exception( _LE("Can not find docker instance with %s," "set it to Error status"), container_uuid) container.status = obj_container.ERROR container.save() return container result = docker.inspect_container(docker_id) status = result.get('State') if status: if status.get('Error') is True: container.status = obj_container.ERROR elif status.get('Running'): container.status = obj_container.RUNNING elif status.get('Paused'): container.status = obj_container.PAUSED else: container.status = obj_container.STOPPED container.save() return container except errors.APIError as api_error: error_message = str(api_error) if '404' in error_message: container.status = obj_container.ERROR container.save() return container raise exception.ContainerException("Docker API Error : %s" % (error_message))
def container_list(self, context): LOG.debug("container_list") try: container_list = self.docker.containers() return container_list except errors.APIError as api_error: raise exception.ContainerException("Docker API Error : %s" % str(api_error))
def container_logs(self, context, container_uuid): LOG.debug("container_logs %s" % container_uuid) try: docker_id = self._find_container_by_name(container_uuid) return {'output': self.docker.get_container_logs(docker_id)} except errors.APIError as api_error: raise exception.ContainerException("Docker API Error : %s" % str(api_error))
def container_unpause(self, context, container_uuid): LOG.debug("container_unpause %s" % container_uuid) try: docker_id = self._find_container_by_name(container_uuid) return self.docker.unpause(docker_id) except errors.APIError as api_error: raise exception.ContainerException("Docker API Error : %s" % str(api_error))
def container_execute(self, context, container_uuid, command): LOG.debug("container_execute %s command %s" % (container_uuid, command)) try: docker_id = self._find_container_by_name(container_uuid) return {'output': self.docker.execute(docker_id, command)} except errors.APIError as api_error: raise exception.ContainerException("Docker API Error : %s" % str(api_error))
def container_start(self, context, container_uuid): LOG.debug("Starting container %s" % container_uuid) try: docker_id = self._find_container_by_name(container_uuid) LOG.debug("Found Docker container %s" % docker_id) return self.docker.start(docker_id) except errors.APIError as api_error: raise exception.ContainerException("Docker API Error : %s" % str(api_error))
def container_delete(self, context, container_uuid): LOG.debug("container_delete %s" % container_uuid) docker = self.get_docker_client(context, container_uuid) try: docker_id = self._find_container_by_name(docker, container_uuid) if not docker_id: return None return docker.remove_container(docker_id) except errors.APIError as api_error: raise exception.ContainerException("Docker API Error : %s" % str(api_error))
def wrapped(self, context, *args, **kwargs): try: return f(self, context, *args, **kwargs) except Exception as e: container_uuid = kwargs.get('container_uuid') if container_uuid is not None: LOG.exception(_LE("Error while connect to docker " "container %(name)s: %(error)s"), {'name': container_uuid, 'error': str(e)}) raise exception.ContainerException( "Docker internal Error: %s" % str(e))
def _container_action(self, context, container_uuid, status, docker_func): LOG.debug("container_%s %s" % (status, container_uuid)) docker = self.get_docker_client(context, container_uuid) try: docker_id = self._find_container_by_name(docker, container_uuid) result = getattr(docker, docker_func)(docker_id) container = objects.Container.get_by_uuid(context, container_uuid) container.status = status container.save() return result except errors.APIError as api_error: raise exception.ContainerException("Docker API Error : %s" % str(api_error))
def wrapped(self, context, *args, **kwargs): try: return f(self, context, *args, **kwargs) except Exception as e: container_uuid = None if 'container_uuid' in kwargs: container_uuid = kwargs.get('container_uuid') elif 'container' in kwargs: container_uuid = kwargs.get('container').uuid LOG.exception(_LE("Error while connect to docker " "container %s"), container_uuid) raise exception.ContainerException("Docker internal Error: %s" % str(e))
def container_create(self, context, name, container_uuid, container): image_id = container.image_id LOG.debug('Creating container with image %s name %s' % (image_id, name)) try: image_repo, image_tag = docker_utils.parse_docker_image(image_id) self.docker.pull(image_repo, tag=image_tag) self.docker.inspect_image(self._encode_utf8(container.image_id)) self.docker.create_container(image_id, name=name, hostname=container_uuid) return container except errors.APIError as api_error: raise exception.ContainerException("Docker API Error : %s" % str(api_error))
def container_exec(self, context, container_uuid, command): LOG.debug("container_exec %s command %s" % (container_uuid, command)) docker = self.get_docker_client(context, container_uuid) try: docker_id = self._find_container_by_name(docker, container_uuid) if docker_utils.is_docker_library_version_atleast('1.2.0'): create_res = docker.exec_create(docker_id, command, True, True, False) exec_output = docker.exec_start(create_res, False, False, False) else: exec_output = docker.execute(docker_id, command) return {'output': exec_output} except errors.APIError as api_error: raise exception.ContainerException("Docker API Error : %s" % str(api_error))
def container_create(self, context, name, container_uuid, container): docker = self.get_docker_client(context, container) image = container.image LOG.debug('Creating container with image %s name %s' % (image, name)) try: image_repo, image_tag = docker_utils.parse_docker_image(image) docker.pull(image_repo, tag=image_tag) docker.inspect_image(self._encode_utf8(container.image)) docker.create_container(image, name=name, hostname=container_uuid, command=container.command) container.status = obj_container.STOPPED return container except errors.APIError as api_error: container.status = obj_container.ERROR raise exception.ContainerException("Docker API Error : %s" % str(api_error)) finally: container.save()