All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.bmuschko.gradle.docker.tasks.image.DockerSaveImage.groovy Maven / Gradle / Ivy

There is a newer version: 6.7.0
Show newest version
package com.bmuschko.gradle.docker.tasks.image

import com.bmuschko.gradle.docker.tasks.AbstractDockerRemoteApiTask
import com.bmuschko.gradle.docker.utils.IOUtils
import com.github.dockerjava.api.DockerClient
import com.github.dockerjava.api.command.SaveImageCmd
import org.gradle.api.GradleException
import org.gradle.api.file.RegularFileProperty
import org.gradle.api.provider.Property
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.Optional
import org.gradle.api.tasks.OutputFile

import java.util.zip.GZIPOutputStream

class DockerSaveImage extends AbstractDockerRemoteApiTask {

    /**
     * The image repository.
     */
    @Input
    final Property repository = project.objects.property(String)

    /**
     * The image's tag.
     */
    @Input
    @Optional
    final Property tag = project.objects.property(String)

    @Input
    @Optional
    final Property useCompression = project.objects.property(Boolean)

    /**
     * Where to save image.
     */
    @OutputFile
    final RegularFileProperty destFile = newOutputFile()

    DockerSaveImage() {
        useCompression.set(false)
    }

    @Override
    void runRemoteCommand(DockerClient dockerClient) {
        SaveImageCmd saveImageCmd = dockerClient.saveImageCmd(repository.get())

        if (tag.getOrNull()) {
            saveImageCmd.withTag(tag.get())
        }
        InputStream image = saveImageCmd.exec()
        OutputStream os
        try {
            FileOutputStream fs = new FileOutputStream(destFile.get().asFile)
            os = fs
            if( useCompression.get() ) {
                os = new GZIPOutputStream(fs)
            }
            try {
                IOUtils.copy(image, os)
            } catch (IOException e) {
                throw new GradleException("Can't save image.", e)
            } finally {
                IOUtils.closeQuietly(image)
            }
        }
        finally {
            IOUtils.closeQuietly(os)
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy