use of org.eclipse.che.plugin.docker.client.exception.ImageNotFoundException in project che by eclipse.
the class MachineProviderImpl method pullImage.
/**
* Pulls docker image for container creation.
*
* @param service
* service that provides description of image that should be pulled
* @param machineImageName
* name of the image that should be assigned on pull
* @param progressMonitor
* consumer of output
* @throws SourceNotFoundException
* if image for pulling not found
* @throws MachineException
* if any other error occurs
*/
protected void pullImage(CheServiceImpl service, String machineImageName, ProgressMonitor progressMonitor) throws MachineException {
DockerMachineSource dockerMachineSource = new DockerMachineSource(new MachineSourceImpl("image").setLocation(service.getImage()));
if (dockerMachineSource.getRepository() == null) {
throw new MachineException(format("Machine creation failed. Machine source is invalid. No repository is defined. Found '%s'.", dockerMachineSource));
}
try {
boolean isSnapshot = SNAPSHOT_LOCATION_PATTERN.matcher(dockerMachineSource.getLocation()).matches();
if (!isSnapshot || snapshotUseRegistry) {
PullParams pullParams = PullParams.create(dockerMachineSource.getRepository()).withTag(MoreObjects.firstNonNull(dockerMachineSource.getTag(), LATEST_TAG)).withRegistry(dockerMachineSource.getRegistry()).withAuthConfigs(dockerCredentials.getCredentials());
docker.pull(pullParams, progressMonitor);
}
String fullNameOfPulledImage = dockerMachineSource.getLocation(false);
try {
// tag image with generated name to allow sysadmin recognize it
docker.tag(TagParams.create(fullNameOfPulledImage, machineImageName));
} catch (ImageNotFoundException nfEx) {
throw new SourceNotFoundException(nfEx.getLocalizedMessage(), nfEx);
}
// remove unneeded tag if restoring snapshot from registry
if (isSnapshot && snapshotUseRegistry) {
docker.removeImage(RemoveImageParams.create(fullNameOfPulledImage).withForce(false));
}
} catch (IOException e) {
throw new MachineException("Can't create machine from image. Cause: " + e.getLocalizedMessage(), e);
}
}
use of org.eclipse.che.plugin.docker.client.exception.ImageNotFoundException in project che by eclipse.
the class DockerConnector method tag.
/**
* Tag the docker image into a repository.
*
* @throws ImageNotFoundException
* when docker api return 404 status
* @throws IOException
* when a problem occurs with docker api calls
*/
public void tag(final TagParams params) throws ImageNotFoundException, IOException {
try (DockerConnection connection = connectionFactory.openConnection(dockerDaemonUri).method("POST").path(apiVersionPathPrefix + "/images/" + params.getImage() + "/tag").query("repo", params.getRepository())) {
addQueryParamIfNotNull(connection, "force", params.isForce());
addQueryParamIfNotNull(connection, "tag", params.getTag());
final DockerResponse response = connection.request();
final int status = response.getStatus();
if (status == 404) {
throw new ImageNotFoundException(readAndCloseQuietly(response.getInputStream()));
}
if (status / 100 != 2) {
throw getDockerException(response);
}
}
}
use of org.eclipse.che.plugin.docker.client.exception.ImageNotFoundException in project che by eclipse.
the class DockerConnector method buildImage.
private String buildImage(final DockerConnection dockerConnection, final BuildImageParams params, final ProgressMonitor progressMonitor) throws IOException {
final String repository = params.getRepository();
try (DockerConnection connection = dockerConnection.method("POST").path(apiVersionPathPrefix + "/build").header("X-Registry-Config", authResolver.getXRegistryConfigHeaderValue(params.getAuthConfigs()))) {
addQueryParamIfNotNull(connection, "rm", params.isRemoveIntermediateContainer());
addQueryParamIfNotNull(connection, "forcerm", params.isForceRemoveIntermediateContainers());
addQueryParamIfNotNull(connection, "memory", params.getMemoryLimit());
addQueryParamIfNotNull(connection, "memswap", params.getMemorySwapLimit());
addQueryParamIfNotNull(connection, "pull", params.isDoForcePull());
addQueryParamIfNotNull(connection, "dockerfile", params.getDockerfile());
addQueryParamIfNotNull(connection, "nocache", params.isNoCache());
addQueryParamIfNotNull(connection, "q", params.isQuiet());
addQueryParamIfNotNull(connection, "cpusetcpus", params.getCpusetCpus());
addQueryParamIfNotNull(connection, "cpuperiod", params.getCpuPeriod());
addQueryParamIfNotNull(connection, "cpuquota", params.getCpuQuota());
if (params.getTag() == null) {
addQueryParamIfNotNull(connection, "t", repository);
} else {
addQueryParamIfNotNull(connection, "t", repository == null ? null : repository + ':' + params.getTag());
}
if (params.getBuildArgs() != null) {
addQueryParamIfNotNull(connection, "buildargs", URLEncoder.encode(GSON.toJson(params.getBuildArgs()), "UTF-8"));
}
final DockerResponse response = connection.request();
if (OK.getStatusCode() != response.getStatus()) {
throw getDockerException(response);
}
try (InputStream responseStream = response.getInputStream()) {
JsonMessageReader<ProgressStatus> progressReader = new JsonMessageReader<>(responseStream, ProgressStatus.class);
// Here do some trick to be able interrupt output streaming process.
// Current unix socket implementation of DockerConnection doesn't react to interruption.
// So to be able to close unix socket connection and free resources we use main thread.
// In case of any exception main thread cancels future and close connection.
// If Docker connection implementation supports interrupting it will stop streaming on interruption,
// if not it will be stopped by closure of unix socket
Future<String> imageIdFuture = executor.submit(() -> {
ProgressStatus progressStatus;
while ((progressStatus = progressReader.next()) != null) {
if (progressStatus.getError() != null) {
String errorMessage = progressStatus.getError();
if (errorMessage.matches("Error: image .+ not found")) {
throw new ImageNotFoundException(errorMessage);
}
}
final String buildImageId = getBuildImageId(progressStatus);
if (buildImageId != null) {
return buildImageId;
}
progressMonitor.updateProgress(progressStatus);
}
throw new DockerException("Docker image build failed. Image id not found in build output.", 500);
});
return imageIdFuture.get();
} catch (ExecutionException e) {
// unwrap exception thrown by task with .getCause()
if (e.getCause() instanceof ImageNotFoundException) {
throw new ImageNotFoundException(e.getCause().getLocalizedMessage());
} else {
throw new DockerException(e.getCause().getLocalizedMessage(), 500);
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new DockerException("Docker image build was interrupted", 500);
}
}
}
use of org.eclipse.che.plugin.docker.client.exception.ImageNotFoundException in project che by eclipse.
the class OpenShiftConnector method createImageStreamTag.
/**
* Creates a new ImageStreamTag
*
* @param sourceImageWithTag the image that the ImageStreamTag will track
* @param imageStreamTagName the name of the imageStream tag (e.g. {@code <ImageStream name>:<Tag name>})
* @return the created ImageStreamTag
* @throws IOException When {@code sourceImageWithTag} metadata cannot be found
*/
private ImageStreamTag createImageStreamTag(String sourceImageWithTag, String imageStreamTagName) throws IOException {
try {
openShiftClient.imageStreamTags().inNamespace(openShiftCheProjectName).createOrReplaceWithNew().withNewMetadata().withName(imageStreamTagName).endMetadata().withNewTag().withNewFrom().withKind("DockerImage").withName(sourceImageWithTag).endFrom().endTag().done();
// Wait for image metadata to be pulled
for (int waitCount = 0; waitCount < OPENSHIFT_IMAGESTREAM_MAX_WAIT_COUNT; waitCount++) {
Thread.sleep(OPENSHIFT_IMAGESTREAM_WAIT_DELAY);
ImageStreamTag createdTag = openShiftClient.imageStreamTags().inNamespace(openShiftCheProjectName).withName(imageStreamTagName).get();
if (createdTag != null) {
LOG.info(String.format("Created ImageStreamTag %s in namespace %s", createdTag.getMetadata().getName(), openShiftCheProjectName));
return createdTag;
}
}
throw new ImageNotFoundException(String.format("Image %s not found.", sourceImageWithTag));
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new IOException(e.getLocalizedMessage(), e);
}
}
Aggregations