Search in sources :

Example 1 with SaveImageCmd

use of com.github.dockerjava.api.command.SaveImageCmd in project hub-docker-inspector by blackducksoftware.

the class DockerClientManager method saveImageToFile.

private void saveImageToFile(final String imageName, final String tagName, final File imageTarFile) throws IOException, HubIntegrationException {
    InputStream tarInputStream = null;
    try {
        logger.info(String.format("Saving the docker image to : %s", imageTarFile.getCanonicalPath()));
        final DockerClient dockerClient = hubDockerClient.getDockerClient();
        final String imageToSave = String.format("%s:%s", imageName, tagName);
        final SaveImageCmd saveCommand = dockerClient.saveImageCmd(imageToSave);
        tarInputStream = saveCommand.exec();
        FileUtils.copyInputStreamToFile(tarInputStream, imageTarFile);
    } finally {
        IOUtils.closeQuietly(tarInputStream);
    }
}
Also used : DockerClient(com.github.dockerjava.api.DockerClient) InputStream(java.io.InputStream) SaveImageCmd(com.github.dockerjava.api.command.SaveImageCmd)

Example 2 with SaveImageCmd

use of com.github.dockerjava.api.command.SaveImageCmd in project developer-be by EdgeGallery.

the class ContainerImageServiceImpl method downloadHarborImage.

/**
 * download harbor image.
 *
 * @param imageId imageId
 * @return
 */
@Override
public ResponseEntity<InputStreamResource> downloadHarborImage(String imageId) {
    if (StringUtils.isEmpty(imageId)) {
        LOGGER.error("imageId is null");
        throw new IllegalRequestException("imageId is null", ResponseConsts.RET_REQUEST_PARAM_EMPTY);
    }
    ContainerImage containerImage = containerImageMapper.getContainerImage(imageId);
    if (containerImage == null) {
        LOGGER.error("imageId is incorrect");
        throw new IllegalRequestException("imageId is incorrect", ResponseConsts.RET_REQUEST_PARAM_ERROR);
    }
    String image = containerImage.getImagePath();
    String fileName = containerImage.getFileName();
    if (StringUtils.isEmpty(image) || StringUtils.isEmpty(fileName)) {
        String msg = "image or fileName of this record in db is empty";
        LOGGER.error(msg);
        throw new IllegalRequestException(msg, ResponseConsts.RET_QUERY_DATA_EMPTY);
    }
    try {
        DockerClient dockerClient = ContainerImageUtil.getDockerClient();
        // pull image
        dockerClient.pullImageCmd(image).exec(new PullImageResultCallback()).awaitCompletion().close();
        String[] images = image.trim().split(":");
        // save image
        SaveImageCmd saveImage = dockerClient.saveImageCmd(images[0]).withTag(images[1]);
        InputStream input = saveImage.exec();
        if (input == null) {
            String msg = "save image failed!";
            LOGGER.error(msg);
            throw new FileOperateException(msg, ResponseConsts.RET_SAVE_FILE_FAIL);
        }
        return ResponseEntity.ok().header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_OCTET_STREAM_VALUE).header(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=" + fileName).body(new InputStreamResource(input));
    } catch (InterruptedException | IOException e) {
        Thread.currentThread().interrupt();
        String msg = "download Harbor image occur exception!";
        LOGGER.error("download Harbor image failed! {}", e.getMessage());
        throw new FileOperateException(msg, ResponseConsts.RET_DOWNLOAD_FILE_FAIL);
    }
}
Also used : DockerClient(com.github.dockerjava.api.DockerClient) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) SaveImageCmd(com.github.dockerjava.api.command.SaveImageCmd) IOException(java.io.IOException) IllegalRequestException(org.edgegallery.developer.exception.IllegalRequestException) ContainerImage(org.edgegallery.developer.model.resource.container.ContainerImage) PullImageResultCallback(com.github.dockerjava.api.command.PullImageResultCallback) FileOperateException(org.edgegallery.developer.exception.FileOperateException) InputStreamResource(org.springframework.core.io.InputStreamResource)

Example 3 with SaveImageCmd

use of com.github.dockerjava.api.command.SaveImageCmd in project blackduck-docker-inspector by blackducksoftware.

the class DockerClientManager method saveImageToFile.

private void saveImageToFile(String imageName, String tagName, File imageTarFile) throws IOException {
    logger.info(String.format("Saving the docker image to : %s", imageTarFile.getCanonicalPath()));
    String imageToSave = String.format("%s:%s", imageName, tagName);
    SaveImageCmd saveCommand = dockerClient.saveImageCmd(imageToSave);
    try (InputStream tarInputStream = saveCommand.exec()) {
        FileUtils.copyInputStreamToFile(tarInputStream, imageTarFile);
    }
}
Also used : InputStream(java.io.InputStream) SaveImageCmd(com.github.dockerjava.api.command.SaveImageCmd)

Aggregations

SaveImageCmd (com.github.dockerjava.api.command.SaveImageCmd)3 InputStream (java.io.InputStream)3 DockerClient (com.github.dockerjava.api.DockerClient)2 PullImageResultCallback (com.github.dockerjava.api.command.PullImageResultCallback)1 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 FileOperateException (org.edgegallery.developer.exception.FileOperateException)1 IllegalRequestException (org.edgegallery.developer.exception.IllegalRequestException)1 ContainerImage (org.edgegallery.developer.model.resource.container.ContainerImage)1 InputStreamResource (org.springframework.core.io.InputStreamResource)1