Пример #1
0
class DeleteImage(Delete):
    """
    Delete images.
    """
    def __init__(self, name, config, runtime_config):
        """
        Delete an image.

        :param name: image name, not strictly needed.
        :type name: str
        :param config: configuration of the image
        :type config: dict
        :param runtime_config: runtime configuration
        :type runtime_config: dict
        """
        super().__init__(name, config, runtime_config)
        self.path = os.path.join(BASE_IMAGE_DIR, self.config["image"]["name"],
                                 self.config["image"]["version"])
        self.systemd = SystemdImage(runtime_config)
        self.unit_name = "{}.mount".format(self.systemd._escape(self.path))

    def _sanity_check(self):
        """
        Image deletion sanity check.
        """
        containers = []
        for container_fname in os.listdir(ETC_CONTAINER_CONF_DIR):
            container_config = read_config_file(
                os.path.join(ETC_CONTAINER_CONF_DIR, container_fname))

            if (container_config["image"]["name"] ==
                    self.config["image"]["name"] and
                    container_config["image"]["version"] ==
                    self.config["image"]["version"]):
                containers.append(container_config["container"]["name"])

        if containers:
            raise DeleteError("Containers require this image: \n\t"
                              "{}".format("\n\t".join(containers)))

    def _clean_sponson_conf(self):
        """
        Remove sponson image configuration files.
        """
        config_base_path = os.path.join(BASE_CONF_DIR,
                                        self.config["image"]["name"])
        config_path = os.path.join(
            config_base_path,
            "{}.yaml".format(self.config["image"]["version"]))
        latest_path = os.path.join(config_base_path, "latest.yaml")

        os.remove(config_path)

        if os.readlink(latest_path) == config_path:
            os.unlink(latest_path)

            remaining = sorted(os.listdir(config_base_path))

            if remaining:
                new_latest = os.path.join(config_base_path, remaining.pop())
                os.link(new_latest, latest_path)

    def _clean_dir(self):
        """
        Image removal specific cleaning function,
        to relink the "latest" image version
        if the removed directory was the previous "latest" version.
        """
        super()._clean_dir()

        link_latest_image(os.path.join(BASE_IMAGE_DIR,
                                       self.config["image"]["name"]))