use of io.fabric8.maven.docker.access.DockerAccessException in project docker-maven-plugin by fabric8io.
the class DockerAccessWithHcClient method getImageId.
@Override
public String getImageId(String name) throws DockerAccessException {
HttpBodyAndStatus response = inspectImage(name);
if (response.getStatusCode() == HTTP_NOT_FOUND) {
return null;
}
JSONObject imageDetails = new JSONObject(response.getBody());
return imageDetails.getString("Id").substring(0, 12);
}
use of io.fabric8.maven.docker.access.DockerAccessException in project docker-maven-plugin by fabric8io.
the class DockerAccessWithHcClient method removeImage.
@Override
public boolean removeImage(String image, boolean... forceOpt) throws DockerAccessException {
boolean force = forceOpt != null && forceOpt.length > 0 && forceOpt[0];
try {
String url = urlBuilder.deleteImage(image, force);
HttpBodyAndStatus response = delegate.delete(url, new BodyAndStatusResponseHandler(), HTTP_OK, HTTP_NOT_FOUND);
if (log.isDebugEnabled()) {
logRemoveResponse(new JSONArray(response.getBody()));
}
return response.getStatusCode() == HTTP_OK;
} catch (IOException e) {
throw new DockerAccessException(e, "Unable to remove image [%s]", image);
}
}
use of io.fabric8.maven.docker.access.DockerAccessException in project docker-maven-plugin by fabric8io.
the class BuildService method buildImage.
/**
* Build an image
*
* @param imageConfig the image configuration
* @param params mojo params for the project
* @param noCache if not null, dictate the caching behaviour. Otherwise its taken from the build configuration
* @param buildArgs
* @throws DockerAccessException
* @throws MojoExecutionException
*/
protected void buildImage(ImageConfiguration imageConfig, MojoParameters params, boolean noCache, Map<String, String> buildArgs) throws DockerAccessException, MojoExecutionException {
String imageName = imageConfig.getName();
ImageName.validate(imageName);
BuildImageConfiguration buildConfig = imageConfig.getBuildConfiguration();
String oldImageId = null;
CleanupMode cleanupMode = buildConfig.cleanupMode();
if (cleanupMode.isRemove()) {
oldImageId = queryService.getImageId(imageName);
}
long time = System.currentTimeMillis();
if (buildConfig.getDockerArchive() != null) {
docker.loadImage(imageName, buildConfig.getAbsoluteDockerTarPath(params));
log.info("%s: Loaded tarball in %s", buildConfig.getDockerArchive(), EnvUtil.formatDurationTill(time));
return;
}
File dockerArchive = archiveService.createArchive(imageName, buildConfig, params, log);
log.info("%s: Created %s in %s", imageConfig.getDescription(), dockerArchive.getName(), EnvUtil.formatDurationTill(time));
Map<String, String> mergedBuildMap = prepareBuildArgs(buildArgs, buildConfig);
// auto is now supported by docker, consider switching?
BuildOptions opts = new BuildOptions(buildConfig.getBuildOptions()).dockerfile(getDockerfileName(buildConfig)).forceRemove(cleanupMode.isRemove()).noCache(noCache).buildArgs(mergedBuildMap);
String newImageId = doBuildImage(imageName, dockerArchive, opts);
log.info("%s: Built image %s", imageConfig.getDescription(), newImageId);
if (oldImageId != null && !oldImageId.equals(newImageId)) {
try {
docker.removeImage(oldImageId, true);
log.info("%s: Removed old image %s", imageConfig.getDescription(), oldImageId);
} catch (DockerAccessException exp) {
if (cleanupMode == CleanupMode.TRY_TO_REMOVE) {
log.warn("%s: %s (old image)%s", imageConfig.getDescription(), exp.getMessage(), (exp.getCause() != null ? " [" + exp.getCause().getMessage() + "]" : ""));
} else {
throw exp;
}
}
}
}
use of io.fabric8.maven.docker.access.DockerAccessException in project docker-maven-plugin by fabric8io.
the class QueryService method getLatestContainerForImage.
/**
* Get the id of the latest container started for an image
*
* @param image for which its container are looked up
* @return container or <code>null</code> if no container has been started for this image.
* @throws DockerAccessException if the request fails
*/
public Container getLatestContainerForImage(String image) throws DockerAccessException {
long newest = 0;
Container result = null;
for (Container container : getContainersForImage(image)) {
long timestamp = container.getCreated();
if (timestamp < newest) {
continue;
}
newest = timestamp;
result = container;
}
return result;
}
use of io.fabric8.maven.docker.access.DockerAccessException in project docker-maven-plugin by fabric8io.
the class RegistryService method pushImages.
/**
* Push a set of images to a registry
*
* @param imageConfigs images to push (but only if they have a build configuration)
* @param retries how often to retry
* @param registryConfig a global registry configuration
* @throws DockerAccessException
* @throws MojoExecutionException
*/
public void pushImages(Collection<ImageConfiguration> imageConfigs, int retries, RegistryConfig registryConfig) throws DockerAccessException, MojoExecutionException {
for (ImageConfiguration imageConfig : imageConfigs) {
BuildImageConfiguration buildConfig = imageConfig.getBuildConfiguration();
String name = imageConfig.getName();
if (buildConfig != null) {
String configuredRegistry = EnvUtil.fistRegistryOf(new ImageName(imageConfig.getName()).getRegistry(), imageConfig.getRegistry(), registryConfig.getRegistry());
AuthConfig authConfig = createAuthConfig(true, new ImageName(name).getUser(), configuredRegistry, registryConfig);
long start = System.currentTimeMillis();
docker.pushImage(name, authConfig, configuredRegistry, retries);
log.info("Pushed %s in %s", name, EnvUtil.formatDurationTill(start));
for (String tag : imageConfig.getBuildConfiguration().getTags()) {
if (tag != null) {
docker.pushImage(new ImageName(name, tag).getFullName(), authConfig, configuredRegistry, retries);
}
}
}
}
}
Aggregations