Beispiel #1
0
    def _sanity_check(self):
        """
        Check if the image path already exists,
        and if so raise an error.
        :raises: sponson.image.build.BuilderError
        """
        self.image_path = os.path.join(BASE_IMAGE_DIR, self.name, self.version)
        self.config_path = os.path.join(BASE_CONF_DIR, self.name,
                                        "{}.yaml".format(self.version))

        if os.path.exists(self.image_path):
            raise BuilderError("Image path already exists")

        try:
            os.makedirs(self.image_path)
        except PermissionError:
            raise BuilderError("Insufficient permissions to make image")

        if not os.path.exists(os.path.dirname(self.config_path)):
            try:
                os.makedirs(os.path.dirname(self.config_path))
            except PermissionError:
                raise BuilderError("Insufficient permissions to save config")

        if self.srcimage:
            srcimage = check_if_versioned(self.srcimage)
            if not srcimage or not os.path.exists(srcimage):
                raise BuilderError("Cannot find source image")
Beispiel #2
0
    def __init__(self, config, runtime_config, version="latest", update=True):
        """
        Rebuild an image.

        :param config: image configuration
        :type config: dict
        :param runtime_config: local runtime configuration
        :type runtime_config: dict
        :param version: new image version, defaults to "latest"
        :type version: str
        :param update: update any source images to the latest versions,
            defaults to running updates
        :type update: bool
        """
        super().__init__(config, runtime_config)

        if version == "latest" or self.all_config["image"]["version"] == "now":
            self.version = datetime.now().strftime("%Y%m%dT%H%M%S")
        else:
            self.version = version
        self.config["version"] = self.version

        if update and self.srcimage:
            srcimage_path = check_if_versioned(self.srcimage)
            if srcimage_path:
                self.srcimage = os.path.relpath(
                    os.path.realpath(srcimage_path), BASE_IMAGE_DIR)
Beispiel #3
0
    def _check_if_version(self, image):
        """
        Checks if supplied image is versioned.

        :param image: image with possible version to check
        :type image: str
        :return: sanitised/checked image string
        :rtype: str
        :raises: BuilderError
        """
        version = check_if_versioned(image)
        if version:
            return version
        else:
            raise BuilderError("Cannot determine version of the image")
Beispiel #4
0
    def _check_if_version(self, image):
        """
        Checks if supplied image is versioned.

        :param image: image with possible version to check
        :type image: str
        :return: sanitised/checked image string
        :rtype: str
        """
        # Check if the image we've been given
        # is the name or the actual version
        version = check_if_versioned(image)

        if version:
            return version
        else:
            raise NewContainerError("Cannot determine version of the image")
Beispiel #5
0
    def mount_srcimage(self, image_name, image_version, srcimage):
        """
        Mount source image(s) into the image.

        :param image_name: image to mount into
        :type image_name: str
        :param image_version: version of the image
        :type image_version: str
        :param srcimage: source images to mount on
        :type srcimage: str
        """
        srcimage = check_if_versioned(srcimage)
        image_mount = os.path.join(BASE_IMAGE_DIR, image_name, image_version)

        overlay_path = os.path.join(MOUNT_OVERLAY_IMAGE, image_name,
                                    image_version)

        self._lower_overlay(overlay_path, srcimage, image_mount)