container stop, delete feature.
diff --git a/taverna-docker-activity/src/main/java/org/apache/taverna/activities/docker/DockerActivity.java b/taverna-docker-activity/src/main/java/org/apache/taverna/activities/docker/DockerActivity.java
index a80e02a..4ac2acf 100644
--- a/taverna-docker-activity/src/main/java/org/apache/taverna/activities/docker/DockerActivity.java
+++ b/taverna-docker-activity/src/main/java/org/apache/taverna/activities/docker/DockerActivity.java
@@ -57,12 +57,16 @@
public static final String START_CONTAINER = "start-container";
+ public static final String DELETE_CONTAINER = "delete-container";
+
public static final String STOP_CONTAINER = "stop-container";
public static final String LIST_CONTAINERS = "list-containers";
public static final String CONTAINER_ID = "container-id";
+ public static final String CONTAINER_NAME = "container-name";
+
public static final String OUT_IMAGE_ID = "image-id";
public static final String OUT_IMAGE_AUTHOR = "image-author";
@@ -134,14 +138,42 @@
} else if (START_CONTAINER.equalsIgnoreCase(action)) {
- String id = getRenderedParam(referenceService, context, map.get(CONTAINER_ID));
- remoteClient.startContainer(id);
- outJson = factory.objectNode();
- ((ObjectNode)outJson).put("started", id);
+ String name = getRenderedParam(referenceService, context, map.get(CONTAINER_NAME));
+ Container container = getContainerFromName(remoteClient, name);
+ if(!isStarted(container)) {
+ remoteClient.startContainer(container.getId());
+ outJson = factory.objectNode();
+ ((ObjectNode) outJson).put("started", container.getId());
+ } else {
+ outJson = factory.objectNode();
+ ((ObjectNode) outJson).put("already-started", container.getId());
+ }
} else if (STOP_CONTAINER.equalsIgnoreCase(action)) {
- // TODO
+ String name = getRenderedParam(referenceService, context, map.get(CONTAINER_NAME));
+ Container container = getContainerFromName(remoteClient, name);
+ if(isStarted(container)) {
+ remoteClient.stopContainer(container.getId());
+ outJson = factory.objectNode();
+ ((ObjectNode) outJson).put("stopped", container.getId());
+ } else {
+ outJson = factory.objectNode();
+ ((ObjectNode) outJson).put("already-stopped", container.getId());
+ }
+
+ } else if (DELETE_CONTAINER.equalsIgnoreCase(action)) {
+
+ String name = getRenderedParam(referenceService, context, map.get(CONTAINER_NAME));
+ Container container = getContainerFromName(remoteClient, name);
+ if(container != null) {
+ remoteClient.deleteContainer(container.getId());
+ outJson = factory.objectNode();
+ ((ObjectNode) outJson).put("deleted", container.getId());
+ } else {
+ outJson = factory.objectNode();
+ ((ObjectNode) outJson).put("container-not-found", name);
+ }
} else {
// Creates empty node
@@ -172,4 +204,23 @@
out.put("network-mode",container.getHostConfig().getNetworkMode());
return out;
}
+
+ private boolean isStarted(Container container){
+ return container.getStatus() != null
+ && container.getStatus().startsWith("Up");
+ }
+
+ private Container getContainerFromName(RemoteClient remoteClient, String containerName){
+ List<Container> containerList = remoteClient.listContainers();
+ for(Container container : containerList){
+ if(container.getNames().length > 0){
+ for(String name : container.getNames()){
+ if(name.endsWith(containerName)){
+ return container;
+ }
+ }
+ }
+ }
+ return null;
+ }
}
diff --git a/taverna-docker-activity/src/test/java/org/apache/taverna/activities/docker/test/DockerActivityTest.java b/taverna-docker-activity/src/test/java/org/apache/taverna/activities/docker/test/DockerActivityTest.java
index 79f5d48..751c247 100755
--- a/taverna-docker-activity/src/test/java/org/apache/taverna/activities/docker/test/DockerActivityTest.java
+++ b/taverna-docker-activity/src/test/java/org/apache/taverna/activities/docker/test/DockerActivityTest.java
@@ -51,7 +51,11 @@
public static final String DOCKER_REGISTRY = "https://registry-1.docker.io/v2";
-
+ public static void main(String[] args) throws Exception {
+ DockerActivityTest activityTest = new DockerActivityTest();
+ activityTest.setup();
+ activityTest.testAll();
+ }
@Before
public void setup() throws Exception {
@@ -75,12 +79,20 @@
}
+ @Test
+ public void testAll() throws Exception {
+// testCreateContainer();
+ testInspectImage();
+// testListContainers();
+// testStartContainer();
+// testStopContainer();
+// testDeleteContainer();
+ }
- /**
- * Tests a simple script (String output = input + "_returned") to ensure the script is invoked correctly.
- * @throws Exception
- */
- @Test
+ /**
+ * Tests a simple script (String output = input + "_returned") to ensure the script is invoked correctly.
+ * @throws Exception
+ */
public void testInspectImage() throws Exception {
containerConfiguration.getInternalPropertyMap().put(DockerContainerConfiguration.IMAGE,IMAGE_NAME);
DockerActivity activity = new DockerActivity(containerConfiguration);
@@ -102,7 +114,6 @@
* Creates container with a given container configuration
* @throws Exception
*/
- @Test
public void testCreateContainer() throws Exception {
RemoteClient remoteClient = new RemoteClient(containerConfiguration);
Container container = getContainerFromName(remoteClient.listContainers(), CONTAINER_NAME);
@@ -133,7 +144,6 @@
}
- @Test
public void testListContainers() throws Exception {
DockerActivity activity = new DockerActivity(containerConfiguration);
activity.configure(activityConfiguration);
@@ -149,14 +159,45 @@
Assert.assertNotNull(outputs.get(DockerActivity.RESPONSE_BODY_KEY));
}
- @Test
public void testStartContainer() throws Exception {
- String id = "9e7a5252c1a948149c4e0ca6b4ef7945c9ef11b27cfa251b37abc7d43391680d";
DockerActivity activity = new DockerActivity(containerConfiguration);
activity.configure(activityConfiguration);
Map<String,Object> inputs = new HashMap<String,Object>();
inputs.put(DockerActivity.ACTION, DockerActivity.START_CONTAINER);
- inputs.put(DockerActivity.CONTAINER_ID, id);
+ inputs.put(DockerActivity.CONTAINER_NAME, CONTAINER_NAME);
+ inputs.put(DockerActivity.IN_CONTAINER_START_CMD, "python app.py");
+
+ Map<String, Class<?>> expectedOutputs = new HashMap<String, Class<?>>();
+ expectedOutputs.put(DockerActivity.RESPONSE_BODY_KEY, String.class);
+
+ Map<String,Object> outputs = ActivityInvoker.invokeAsyncActivity(activity, inputs, expectedOutputs);
+ System.out.println(outputs.get(DockerActivity.RESPONSE_BODY_KEY));
+ Assert.assertNotNull(outputs.get(DockerActivity.RESPONSE_BODY_KEY));
+ }
+
+ public void testStopContainer() throws Exception {
+ DockerActivity activity = new DockerActivity(containerConfiguration);
+ activity.configure(activityConfiguration);
+ Map<String,Object> inputs = new HashMap<String,Object>();
+ inputs.put(DockerActivity.ACTION, DockerActivity.STOP_CONTAINER);
+ inputs.put(DockerActivity.CONTAINER_NAME, CONTAINER_NAME);
+ inputs.put(DockerActivity.IN_CONTAINER_START_CMD, "python app.py");
+
+ Map<String, Class<?>> expectedOutputs = new HashMap<String, Class<?>>();
+ expectedOutputs.put(DockerActivity.RESPONSE_BODY_KEY, String.class);
+
+ Map<String,Object> outputs = ActivityInvoker.invokeAsyncActivity(activity, inputs, expectedOutputs);
+ System.out.println(outputs.get(DockerActivity.RESPONSE_BODY_KEY));
+ Assert.assertNotNull(outputs.get(DockerActivity.RESPONSE_BODY_KEY));
+ }
+
+
+ public void testDeleteContainer() throws Exception {
+ DockerActivity activity = new DockerActivity(containerConfiguration);
+ activity.configure(activityConfiguration);
+ Map<String,Object> inputs = new HashMap<String,Object>();
+ inputs.put(DockerActivity.ACTION, DockerActivity.DELETE_CONTAINER);
+ inputs.put(DockerActivity.CONTAINER_NAME, CONTAINER_NAME);
inputs.put(DockerActivity.IN_CONTAINER_START_CMD, "python app.py");
Map<String, Class<?>> expectedOutputs = new HashMap<String, Class<?>>();