Skip to content

Commit

Permalink
Add environment variable when starting containers.
Browse files Browse the repository at this point in the history
This env variable WHALE_DMP_LABEL is used to mark containers to belong to a certain
build so that can be recognized when calling 'docker:stop' #87
  • Loading branch information
rhuss committed Dec 1, 2015
1 parent bff284e commit 2849276
Show file tree
Hide file tree
Showing 5 changed files with 198 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public abstract class AbstractDockerMojo extends AbstractMojo implements Context
public static final String CONTEXT_KEY_START_CALLED = "CONTEXT_KEY_DOCKER_START_CALLED";

// Key holding the log dispatcher
public static final Object CONTEXT_KEY_LOG_DISPATCHER = "CONTEXT_KEY_DOCKER_LOG_DISPATCHER";
public static final String CONTEXT_KEY_LOG_DISPATCHER = "CONTEXT_KEY_DOCKER_LOG_DISPATCHER";

// Standard HTTPS port (IANA registered). The other 2375 with plain HTTP is used only in older
// docker installations.
Expand Down
16 changes: 8 additions & 8 deletions src/main/java/org/jolokia/docker/maven/StartMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/

import java.io.IOException;
import java.util.ArrayList;
import java.util.Properties;
Expand All @@ -16,20 +17,18 @@
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.maven.plugin.MojoExecutionException;
import org.codehaus.plexus.util.StringUtils;
import org.jolokia.docker.maven.access.DockerAccess;
import org.jolokia.docker.maven.access.DockerAccessException;
import org.jolokia.docker.maven.access.PortMapping;
import org.jolokia.docker.maven.access.*;
import org.jolokia.docker.maven.access.log.LogCallback;
import org.jolokia.docker.maven.access.log.LogGetHandle;
import org.jolokia.docker.maven.config.ImageConfiguration;
import org.jolokia.docker.maven.config.LogConfiguration;
import org.jolokia.docker.maven.config.RunImageConfiguration;
import org.jolokia.docker.maven.config.WaitConfiguration;
import org.jolokia.docker.maven.config.*;
import org.jolokia.docker.maven.log.LogDispatcher;
import org.jolokia.docker.maven.service.*;
import org.jolokia.docker.maven.util.StartOrderResolver;
import org.jolokia.docker.maven.util.Timestamp;
import org.jolokia.docker.maven.util.WaitUtil;
import org.jolokia.docker.maven.service.QueryService;
import org.jolokia.docker.maven.service.RunService;
import org.jolokia.docker.maven.util.*;


/**
Expand Down Expand Up @@ -64,7 +63,8 @@ public synchronized void executeInternal(final ServiceHub hub) throws DockerAcce
RunService runService = hub.getRunService();

LogDispatcher dispatcher = getLogDispatcher(hub);


ContainerLabel label = new ContainerLabel(project.getGroupId(),project.getArtifactId(),project.getVersion());
PortMapping.PropertyWriteHelper portMappingPropertyWriteHelper = new PortMapping.PropertyWriteHelper(portPropertyFile);

boolean success = false;
Expand Down
102 changes: 102 additions & 0 deletions src/main/java/org/jolokia/docker/maven/util/ContainerLabel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package org.jolokia.docker.maven.util;/*
*
* Copyright 2014 Roland Huss
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import java.util.UUID;

/**
* Label used to mark a container belonging to a certain build.
*
* @author roland
* @since 31/03/15
*/
public class ContainerLabel {

private String mavenCoordinates;
private String runId;


// Environment variable used to label containers
public static final String CONTAINER_LABEL_NAME = "docker.maven.plugin.container";

/**
* Construct from a given label
*
* @param label label as stored with the container
*/
public ContainerLabel(String label) {
String[] parts = label.split(":");
if (parts.length != 4 && parts.length != 3) {
throw new IllegalArgumentException("Label '" + label +
"' has not the format <group>:<artifact>:<version>[:<runId>]");
}
mavenCoordinates = parts[0] + ":" + parts[1] + ":" + parts[2];
runId = parts.length == 4 ? parts[3] : null;
}

/**
* Construct from maven coordinates. A random run-ID is created automatically.
* @param groupId Maven group
* @param artifactId Maven artifact
* @param version version
*/
public ContainerLabel(String groupId, String artifactId, String version) {
this(groupId, artifactId, version, UUID.randomUUID().toString());
}

/**
* Construct from maven coordinates and run ID. If the runId is <code>null</code> this label
* will.
*
* @param groupId Maven group
* @param artifactId Maven artifact
* @param version version
* @param runId a run id or <code>null</code>.
*/
public ContainerLabel(String groupId, String artifactId, String version, String runId) {
mavenCoordinates = groupId + ":" + artifactId + ":" + version;
this.runId = runId;
}

/**
* Get this label in string representation
* @return this label as string
*/
public String toString() {
return mavenCoordinates + (runId != null ? ":" + runId : "");
}

/**
* Check whether this label matches another. For a match the maven coordinates must fit and if this
* runId is set, the runId must match, too.
*
* @param other label to match
* @return true for a match
*/
public boolean matches(ContainerLabel other) {
return other.mavenCoordinates.equals(mavenCoordinates) &&
(runId == null || runId.equals(other.runId));
}

/**
* Get the label name
*
* @return the label name to use to mark a container belonging to this build
*/
public String getName() {
return "docker.maven.plugin.container";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package org.jolokia.docker.maven.util;

import org.junit.Test;

import static org.junit.Assert.*;
/*
*
* Copyright 2014 Roland Huss
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/**
* @author roland
* @since 31/03/15
*/
public class ContainerLabelTest {

String g = "org.jolokia";
String a = "demo";
String v = "0.0.1";
String coord = g + ":" + a + ":" + v;

@Test
public void simple() throws Exception {
ContainerLabel label = new ContainerLabel(g,a,v);
assertTrue(label.toString().startsWith(coord));
assertTrue(label.toString().length() > coord.length());
}

@Test
public void withNullRunId() {
ContainerLabel label = new ContainerLabel(g,a,v,null);
assertEquals(label.toString(), coord);
}

@Test
public void withRunId() {
ContainerLabel label = new ContainerLabel(g,a,v,"blub");
assertEquals(label.toString(),coord + ":blub");
}

@Test
public void matchesAll() throws Exception {
ContainerLabel label = new ContainerLabel(g, a, v,null);
assertTrue(label.matches(new ContainerLabel(g, a, v)));
}

@Test
public void dontMatch() {
ContainerLabel label = new ContainerLabel(g, a, v);
assertFalse(label.matches(new ContainerLabel(g, a, v)));
}

@Test
public void match() {
ContainerLabel label = new ContainerLabel(g, a, v, "bla");
assertTrue(label.matches(new ContainerLabel(g, a, v, "bla")));
}

@Test
public void parse() {
ContainerLabel label = new ContainerLabel(coord);
assertEquals(coord, label.toString());
}

@Test
public void parse2() {
ContainerLabel label = new ContainerLabel(coord + ":blub");
assertEquals(coord + ":blub",label.toString());
}

@Test(expected = IllegalArgumentException.class)
public void invalid() {
new ContainerLabel("bla");
}
}
1 change: 0 additions & 1 deletion src/test/resources/docker/containerCreateConfigAll.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
{
"Env": ["foo=bar"],
"User": "user",
"Entrypoint": [ "entrypoint" ],
"Memory": 1,
Expand Down

0 comments on commit 2849276

Please sign in to comment.