def delete_image(image_id, region=None, force=None):
    """
    Initiate the deletion of the custom ParallelCluster image.

    :param image_id: Id of the image
    :type image_id: str
    :param region: AWS Region that the operation corresponds to.
    :type region: str
    :param force: Force deletion in case there are instances using the AMI or in case the AMI is shared
    (Defaults to 'false'.)
    :type force: bool

    :rtype: DeleteImageResponseContent
    """
    force = force or False
    imagebuilder = ImageBuilder(image_id=image_id)
    image, stack = _get_underlying_image_or_stack(imagebuilder)

    imagebuilder.delete(force=force)

    return DeleteImageResponseContent(
        image=ImageInfoSummary(
            image_id=image_id,
            image_build_status=ImageBuildStatus.DELETE_IN_PROGRESS,
            cloudformation_stack_status=CloudFormationStackStatus.DELETE_IN_PROGRESS if stack else None,
            cloudformation_stack_arn=stack.id if stack else None,
            region=os_lib.environ.get("AWS_DEFAULT_REGION"),
            version=stack.version if stack else image.version,
        )
    )
Ejemplo n.º 2
0
    def delete_image(image_id: str, region: str, force: bool = False):
        """
        Delete image and imagebuilder stack.

        :param image_id: Id for pcluster Image, the same as imagebuilder cfn stack name
        :param region: AWS region
        :param force: Delete image even if the image is shared or instance is using it
        """
        try:
            if region:
                os.environ["AWS_DEFAULT_REGION"] = region
            # retrieve imagebuilder config and generate model
            imagebuilder = ImageBuilder(image_id=image_id)
            image, _ = PclusterApi._get_underlying_image_or_stack(imagebuilder)
            imagebuilder.delete(force=force)
            if image:
                return ImageBuilderImageInfo(imagebuilder=imagebuilder)
            return ImageBuilderStackInfo(imagebuilder=imagebuilder)
        except Exception as e:
            return ApiFailure(str(e))