-
Notifications
You must be signed in to change notification settings - Fork 645
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add environment variable when starting containers.
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
Showing
5 changed files
with
198 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 102 additions & 0 deletions
102
src/main/java/org/jolokia/docker/maven/util/ContainerLabel.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | ||
} | ||
} |
87 changes: 87 additions & 0 deletions
87
src/test/java/org/jolokia/docker/maven/util/ContainerLabelTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
{ | ||
"Env": ["foo=bar"], | ||
"User": "user", | ||
"Entrypoint": [ "entrypoint" ], | ||
"Memory": 1, | ||
|