Skip to content

Commit

Permalink
added tagging of images during build and pushing of multiple tags
Browse files Browse the repository at this point in the history
Signed-off-by: Peter Leibiger <p.leibiger@codecraft.de>
  • Loading branch information
Peter Leibiger committed Feb 19, 2015
1 parent b45b8c2 commit 2a47d22
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 13 deletions.
3 changes: 3 additions & 0 deletions doc/changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# ChangeLog

* **0.11.2**
- Add support for tagging at build and push time

* **0.11.1**
- Add support for binding UDP ports (#83)
- "Entrypoint" supports now arguments (#84)
Expand Down
6 changes: 6 additions & 0 deletions doc/manual.md
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,8 @@ of an image configuration. The available subelements are
`<port>` elements, one for each port to expose.
* **volumes** contains a list of `volume` elements to create a container
volume.
* **tags** contains a list of additional `tag` elements with which an
image is to be tagged after the build.

From this configuration this Plugin creates an in-memory Dockerfile,
copies over the assembled files and calls the Docker daemon via its
Expand All @@ -248,6 +250,10 @@ Here's an example:
````xml
<build>
<from>java:8u40</from>
<tags>
<tag>latest</tag>
<tag>${project.version}</tag>
</tags>
<ports>
<port>8080</port>
</ports>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,7 @@ protected void checkImageWithAutoPull(DockerAccess docker, String name, String r
if (registry != null && !imageName.hasRegistry()) {
// If coming from a registry which was not contained in the original name, add a tag from the
// short name with no-registry to the full name with the registry.
docker.tag(imageName.getFullNameWithTag(registry),name);
docker.tag(imageName.getFullNameWithTag(registry), name, false);
}
}
else {
Expand Down
24 changes: 19 additions & 5 deletions src/main/java/org/jolokia/docker/maven/BuildMojo.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.jolokia.docker.maven;

import java.io.File;
import java.util.List;

import org.apache.maven.archiver.MavenArchiveConfiguration;
import org.apache.maven.execution.MavenSession;
Expand Down Expand Up @@ -56,14 +57,15 @@ protected void executeInternal(DockerAccess dockerAccess) throws DockerAccessExc
for (ImageConfiguration imageConfig : getImages()) {
BuildImageConfiguration buildConfig = imageConfig.getBuildConfiguration();
if (buildConfig != null) {
buildImage(imageConfig, dockerAccess);
String imageName = getImageName(imageConfig.getName());
buildImage(imageName, imageConfig, dockerAccess);
tagImage(imageName, imageConfig, dockerAccess);
}
}
}
private void buildImage(ImageConfiguration imageConfig, DockerAccess dockerAccess)

private void buildImage(String imageName, ImageConfiguration imageConfig, DockerAccess dockerAccess)
throws DockerAccessException, MojoExecutionException {
String imageName = getImageName(imageConfig.getName());
info("Creating image " + imageConfig.getDescription());

String fromImage = imageConfig.getBuildConfiguration().getFrom();
Expand All @@ -77,8 +79,20 @@ private void buildImage(ImageConfiguration imageConfig, DockerAccess dockerAcces
File dockerArchive = dockerAssemblyManager.create(params, imageConfig.getBuildConfiguration());

dockerAccess.buildImage(imageName, dockerArchive);
debug("Build successful!");
debug("Creating image successful!");
}

private void tagImage(String imageName, ImageConfiguration imageConfig, DockerAccess dockerAccess)
throws DockerAccessException, MojoExecutionException {
info("Tagging image " + imageConfig.getDescription());

for (String tag : imageConfig.getBuildConfiguration().getTags()) {
if (tag != null) {
dockerAccess.tag(imageName, new ImageName(imageName).getFullNameWithCustomTag(tag), true);
}
}

debug("Tagging image successful!");
}

}
13 changes: 12 additions & 1 deletion src/main/java/org/jolokia/docker/maven/PushMojo.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package org.jolokia.docker.maven;

import org.apache.maven.plugin.MojoExecutionException;
import org.jolokia.docker.maven.access.AuthConfig;
import org.jolokia.docker.maven.access.DockerAccess;
import org.jolokia.docker.maven.access.DockerAccessException;
import org.jolokia.docker.maven.config.*;
import org.jolokia.docker.maven.util.ImageName;

/**
* Goal for pushing a data-docker container
Expand All @@ -21,7 +23,16 @@ public void executeInternal(DockerAccess docker) throws DockerAccessException, M
BuildImageConfiguration buildConfig = imageConfig.getBuildConfiguration();
String name = getImageName(imageConfig.getName());
if (buildConfig != null) {
docker.pushImage(name, prepareAuthConfig(name), getRegistry(imageConfig));
AuthConfig authConfig = prepareAuthConfig(name);
String registry = getRegistry(imageConfig);

docker.pushImage(name, authConfig, registry);

for (String tag : imageConfig.getBuildConfiguration().getTags()) {
if (tag != null) {
docker.pushImage(new ImageName(name).getFullNameWithCustomTag(tag), authConfig, registry);
}
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,9 +153,10 @@ public interface DockerAccess {
*
* @param sourceImage full name (including tag) of the image to alias
* @param targetImage the alias name
* @param force forces the tagging
* @throws DockerAccessException if the original image doesnt exist or another error occurs somehow.
*/
void tag(String sourceImage, String targetImage) throws DockerAccessException;
void tag(String sourceImage, String targetImage, boolean force) throws DockerAccessException;

/**
* Remove an image from this docker installation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ public Map<String, Integer> queryContainerPortMapping(String containerId) throws
/** {@inheritDoc} */
@Override
public List<String> getContainersForImage(String image) throws DockerAccessException {
return getContainerIds(image,false);
return getContainerIds(image, false);
}

@Override
Expand Down Expand Up @@ -250,7 +250,7 @@ private String tagTemporaryImage(ImageName name, String registry) throws DockerA
String targetImage = name.getFullNameWithTag(registry);

if (!name.hasRegistry() && registry != null && !hasImage(targetImage)) {
tag(name.getFullNameWithTag(null), targetImage);
tag(name.getFullNameWithTag(null), targetImage, false);
return targetImage;
} else {
return null;
Expand All @@ -259,12 +259,15 @@ private String tagTemporaryImage(ImageName name, String registry) throws DockerA

/** {@inheritDoc} */
@Override
public void tag(String sourceImage, String targetImage) throws DockerAccessException {
public void tag(String sourceImage, String targetImage, boolean force) throws DockerAccessException {
ImageName source = new ImageName(sourceImage);
ImageName target = new ImageName(targetImage);
String url = baseUrl + "/images/" + encode(source.getFullNameWithTag(null)) + "/tag";
url = addRepositoryParam(url, target.getFullName(null));
url = addTagParam(url, target.getTag());
if (force) {
url = addQueryParam(url, "force", "1");
}
HttpUriRequest req = newPost(url, null);
HttpResponse resp = request(req);
checkReturnCode("Adding tag " + targetImage + " to " + sourceImage, resp, 201);
Expand Down Expand Up @@ -447,7 +450,7 @@ private void dump(HttpUriRequest req) {
log.debug("|||| " + h.getName() + "=" + h.getValue());
}
}
if (req.getMethod() == "POST") {
if ("POST".equals(req.getMethod())) {
HttpPost post = (HttpPost) req;
log.debug("---- " + (post.getEntity() != null ? EntityUtils.toString(post.getEntity()) : "[empty]"));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ public class BuildImageConfiguration {
* @paramter
*/
private List<String> volumes;

/**
* @paramter
*/
private List<String> tags;

/**
* @parameter
Expand Down Expand Up @@ -67,7 +72,11 @@ public List<String> getPorts() {
public List<String> getVolumes() {
return (volumes != null) ? volumes : Collections.<String>emptyList();
}


public List<String> getTags() {
return (tags != null) ? tags : Collections.<String>emptyList();
}

public Map<String, String> getEnv() {
return env;
}
Expand Down Expand Up @@ -103,6 +112,11 @@ public Builder volumes(List<String> volumes) {
config.volumes = volumes;
return this;
}

public Builder tags(List<String> tags) {
config.tags = tags;
return this;
}

public Builder env(Map<String, String> env) {
config.env = env;
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/org/jolokia/docker/maven/util/ImageName.java
Original file line number Diff line number Diff line change
Expand Up @@ -144,4 +144,8 @@ public String getFullNameWithTag() {
public String getFullNameWithTag(String optionalRegistry) {
return getFullName(optionalRegistry) + ":" + (tag != null ? tag : "latest");
}

public String getFullNameWithCustomTag(String tag) {
return getFullName() + ":" + tag;
}
}

0 comments on commit 2a47d22

Please sign in to comment.