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);
}
}
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);
}
}
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);
}
}
Aggregations