Skip to content

Commit

Permalink
Add a user parameter to the assembly config
Browse files Browse the repository at this point in the history
This allows the assembly files to be added under a specific user id. See also
the user manual for the proper format. Fixes #53 and fixes #175.
  • Loading branch information
rhuss committed May 22, 2015
1 parent a59ec0d commit 22acd56
Show file tree
Hide file tree
Showing 9 changed files with 89 additions and 16 deletions.
3 changes: 3 additions & 0 deletions doc/changelog.md
@@ -1,13 +1,16 @@
# ChangeLog

* **0.11.5-M1**
- Add a `user` parameter to the assembly configuration so that the added files are created for this user
(#53, #175)
- Fix problem when creating intermediate archive for collecting assembly files introduced with #139. The
container can be now set with "mode" in the assembly configuration with the possible values `dir`, `tar`, `tgz`
and `zip` (#171)
- Workaround Docker problem when using an implicit registry `index.docker.io` when no registry is explicitly given.
- Fixed references to docker hub in documentation (#169)
- Fixed registry authentication lookup (#146)


* **0.11.4**
- Fixed documentation for available properties
- Changed property `docker.assembly.exportBase` to `docker.assembly.exportBaseDir` (#164)
Expand Down
13 changes: 12 additions & 1 deletion doc/manual.md
Expand Up @@ -8,6 +8,7 @@
- [`docker:push`](#dockerpush)
- [`docker:remove`](#dockerremove)
- [`docker:logs`](#dockerlogs)
* [Assembly Configuration](#build-assembly)
* [External Configuration](#external-configuration)
* [Registry Handling](#registry-handling)
* [Authentication](#authentication)
Expand Down Expand Up @@ -300,8 +301,18 @@ Here's an example:
* **mode** specifies how the assembled files should be collected. By default the files a simply
copied (`dir`), but can be set to be a Tar- (`tar`), compressed Tar- (`tgz`) or Zip- (`zip`) Archive.
The archive formats have the advantage that file permission can be preserved better (since the copying is
independent from the underlying files systems), but might trigges internal bugs from the Maven assembler (as
independent from the underlying files systems), but might triggers internal bugs from the Maven assembler (as
it has been in #171)
* **user** can be used to specify the user and group under which the files should be added. It has the general format
`user[:group[:run-user]]`. The user and group can be given either as numeric user- and group-id or as names. The group
id is optional. If a third part is given, then the build changes to user `root` before changing the ownerships,
changes the ownerships and then change to user `run-user` which is then used for the final command to execute. This feature
might be needed, if the base image already changed the user (e.g. to 'jboss') so that a `chown` from root to this user would fail.
For example, the image `jboss/wildfly` use a "jboss" user under which all commands are executed. Adding files in Docker
always happens under the UID root. These files can only be changed to "jboss" is the `chown` command is executed as root.
For the following commands to be run again as "jboss" (like the final `standalone.sh`), the plugin switches back to
user `jboss` (this is this "run-user") after changing the file ownership. For this example a specification of
`jboss:jboss:jboss` would be required.

In the event you do not need to include any artifacts with the image, you may
safely omit this element from the configuration.
Expand Down
39 changes: 39 additions & 0 deletions samples/data-jolokia-demo/pom.xml
Expand Up @@ -356,6 +356,45 @@
</build>
</profile>


<profile>
<id>wildfly</id>
<build>
<plugins>
<plugin>
<groupId>org.jolokia</groupId>
<artifactId>docker-maven-plugin</artifactId>
<configuration combine.self="override">
<images>
<image>
<!-- Artifact Image-->
<name>jolokia/${project.artifactId}-wildfly:${project.version}</name>
<build>
<from>jboss/wildfly:8.2.0.Final</from>
<assembly>
<user>jboss:jboss:jboss</user>
<basedir>/opt/jboss/wildfly/standalone/deployments</basedir>
<descriptor>assembly.xml</descriptor>
</assembly>
</build>
<run>
<ports>
<port>jolokia.port:8080</port>
</ports>
<wait>
<!-- The plugin waits until this URL is reachable via HTTP ... -->
<url>http://${docker.host.address}:${jolokia.port}/jolokia</url>
<time>10000</time>
</wait>
</run>
</image>
</images>
</configuration>
</plugin>
</plugins>
</build>
</profile>

<profile>
<id>tomcat</id>
<properties>
Expand Down
Expand Up @@ -364,7 +364,7 @@ protected String getConfiguredRegistry(ImageConfiguration imageConfig) {
protected void checkImageWithAutoPull(DockerAccess docker, String name, String registry) throws DockerAccessException, MojoExecutionException {
if (!docker.hasImage(name)) {
if (autoPull) {
docker.pullImage(name, prepareAuthConfig(image,registry), registry);
docker.pullImage(name, prepareAuthConfig(name,registry), registry);
ImageName imageName = new ImageName(name);
if (registry != null && !imageName.hasRegistry()) {
// If coming from a registry which was not contained in the original name, add a tag from the
Expand Down
Expand Up @@ -50,7 +50,7 @@ public ContainerCreateConfig entrypoint(String entrypoint) {

private JSONArray splitOnWhiteSpace(String entrypoint) {
JSONArray a = new JSONArray();
for (String s : EnvUtil.splitWOnSpaceWithEscape(entrypoint)) {
for (String s : EnvUtil.splitOnSpaceWithEscape(entrypoint)) {
a.put(s);
}
return a;
Expand Down
Expand Up @@ -151,7 +151,8 @@ DockerFileBuilder createDockerFileBuilder(BuildImageConfiguration buildConfig, A
if (assemblyConfig != null) {
builder.add("maven", "")
.basedir(assemblyConfig.getBasedir())
.exportBasedir(assemblyConfig.exportBasedir());
.user(assemblyConfig.getUser())
.exportBasedir(assemblyConfig.exportBasedir());
} else {
builder.exportBasedir(false);
}
Expand All @@ -160,7 +161,7 @@ DockerFileBuilder createDockerFileBuilder(BuildImageConfiguration buildConfig, A
builder.command((String[]) null); // Use command from base image (gets overwritten below if explicitly set)

if (buildConfig.getCommand() != null) {
builder.command(EnvUtil.splitWOnSpaceWithEscape(buildConfig.getCommand()));
builder.command(EnvUtil.splitOnSpaceWithEscape(buildConfig.getCommand()));
}

return builder;
Expand Down
Expand Up @@ -4,6 +4,7 @@
import java.io.IOException;
import java.util.*;

import org.apache.commons.lang3.StringUtils;
import org.codehaus.plexus.util.FileUtils;

/**
Expand All @@ -28,7 +29,10 @@ public class DockerFileBuilder {
private String[] arguments = new String[0];

private Boolean exportBasedir = null;


// User under which the files should be added
private String user;

// List of files to add. Source and destination follow except that destination
// in interpreted as a relative path to the exportDir
// See also http://docs.docker.io/reference/builder/#add
Expand Down Expand Up @@ -67,15 +71,10 @@ public String content() throws IllegalArgumentException {
b.append("FROM ").append(baseImage != null ? baseImage : DockerAssemblyManager.DEFAULT_DATA_BASE_IMAGE).append("\n");
b.append("MAINTAINER ").append(maintainer).append("\n");

// Environment variable support
addEnv(b);
// Ports
addPorts(b);
// Volume export
addVolumes(b);
// Entries
addEntries(b);
// Default command mit args
addCommands(b);

return b.toString();
Expand All @@ -90,11 +89,26 @@ private void addCommands(StringBuilder b) {
b.append("]").append("\n");
}
}

private void addEntries(StringBuilder b) {
List<String> destinations = new ArrayList<>();
for (AddEntry entry : addEntries) {
b.append("COPY ").append(entry.source).append(" ")
.append(basedir).append("/").append(entry.destination).append("\n");
String dest = (basedir.equals("/") ? "" : basedir) + "/" + entry.destination;
destinations.add(dest);
b.append("COPY ").append(entry.source).append(" ").append(dest).append("\n");
}
if (user != null) {
String[] userParts = StringUtils.split(user,':');
String userArg = userParts.length > 1 ? userParts[0] + ":" + userParts[1] : userParts[0];
String cmd = "RUN [\"chown\", \"-R\", \"" + userArg + "\",\"" +
StringUtils.join(destinations, "\",\"") + "\"]\n";
if (userParts.length > 2) {
b.append("USER root\n");
b.append(cmd);
b.append("USER ").append(userParts[2]).append("\n");
} else {
b.append(cmd);
}
}
}

Expand Down Expand Up @@ -169,6 +183,11 @@ public DockerFileBuilder command(String ... args) {
return this;
}

public DockerFileBuilder user(String user) {
this.user = user;
return this;
}

public DockerFileBuilder add(String source, String destination) {
this.addEntries.add(new AddEntry(source, destination));
return this;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/jolokia/docker/maven/util/EnvUtil.java
Expand Up @@ -83,7 +83,7 @@ public static List<String[]> splitOnLastColon(List<String> listToSplit) {
return Collections.emptyList();
}

public static String[] splitWOnSpaceWithEscape(String toSplit) {
public static String[] splitOnSpaceWithEscape(String toSplit) {
String[] split = toSplit.split("(?<!" + Pattern.quote("\\") + ")\\s+");
String[] res = new String[split.length];
for (int i = 0; i < split.length; i++) {
Expand Down
Expand Up @@ -40,7 +40,7 @@ public void splitOnSpace() {
"bla blub\\ blubber", new String[] { "bla", "blub blubber"}
};
for (int i = 0; i < data.length; i += 2) {
String[] result = EnvUtil.splitWOnSpaceWithEscape((String) data[i]);
String[] result = EnvUtil.splitOnSpaceWithEscape((String) data[i]);
String[] expected = (String[]) data[i+1];
assertEquals(expected.length, result.length);
for (int j = 0; j < expected.length; j++) {
Expand Down

0 comments on commit 22acd56

Please sign in to comment.