Skip to content
This repository has been archived by the owner on Jun 22, 2018. It is now read-only.

Commit

Permalink
Added remove() logging and removed flag
Browse files Browse the repository at this point in the history
  • Loading branch information
frankscholten committed Aug 24, 2015
1 parent 1c5c1b9 commit c4ec773
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
Expand Up @@ -24,6 +24,8 @@ public abstract class AbstractContainer {

private String containerId = "";

private boolean removed;

protected AbstractContainer(DockerClient dockerClient) {
this.dockerClient = dockerClient;
}
Expand Down Expand Up @@ -51,7 +53,9 @@ public void start() {
@Override
public void run() {
LOGGER.info("Shutdown hook - Removing container " + AbstractContainer.this.getName());
remove();
if (!isRemoved()) {
remove();
}
}
});

Expand Down Expand Up @@ -94,7 +98,12 @@ public String getName() {
* Removes a container with force
*/
public void remove() {
dockerClient.removeContainerCmd(containerId).withForce().withRemoveVolumes(true).exec();
try {
dockerClient.removeContainerCmd(containerId).withForce().withRemoveVolumes(true).exec();
this.removed = true;
} catch (Exception e) {
LOGGER.error("Could not remove container " + getName(), e);
}
}

protected void pullImage(String imageName, String registryTag) {
Expand Down Expand Up @@ -126,4 +135,8 @@ public Boolean call() throws Exception {
@Override public String toString() {
return String.format("Container: %s (%s)", this.getName(), this.getContainerId());
}

public boolean isRemoved() {
return removed;
}
}
27 changes: 27 additions & 0 deletions src/main/java/org/apache/mesos/mini/mesos/MesosContainer.java
Expand Up @@ -3,15 +3,26 @@
import com.github.dockerjava.api.DockerClient;
import com.github.dockerjava.api.command.CreateContainerCmd;
import com.github.dockerjava.api.model.Bind;
import com.github.dockerjava.api.model.Container;
import com.github.dockerjava.api.model.ExposedPort;
import com.github.dockerjava.api.model.Link;
import com.github.dockerjava.api.model.Volume;
import com.github.dockerjava.core.DockerClientBuilder;
import com.github.dockerjava.core.DockerClientConfig;
import com.mashape.unirest.http.Unirest;
import org.apache.commons.lang.StringUtils;
import org.apache.http.HttpHost;
import org.apache.log4j.Logger;
import org.apache.mesos.mini.container.AbstractContainer;

import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.List;

public class MesosContainer extends AbstractContainer {

private static Logger LOGGER = Logger.getLogger(MesosContainer.class);

private static final String MESOS_LOCAL_IMAGE = "containersol/mesos-local";
public static final String REGISTRY_TAG = "latest";
private final MesosClusterConfig clusterConfig;
Expand Down Expand Up @@ -65,4 +76,20 @@ protected CreateContainerCmd dockerCommand() {
.withVolumes(new Volume("/sys/fs/cgroup"))
.withBinds(Bind.parse("/sys/fs/cgroup:/sys/fs/cgroup:rw"));
}

@Override
public void remove() {
DockerClientConfig.DockerClientConfigBuilder innerDockerConfigBuilder = DockerClientConfig.createDefaultConfigBuilder();
innerDockerConfigBuilder.withUri("http://" + getIpAddress() + ":2376");
DockerClient innerDockerClient = DockerClientBuilder.getInstance(innerDockerConfigBuilder.build()).build();

List<Container> innerContainers = innerDockerClient.listContainersCmd().exec();
for (Container innerContainer : innerContainers) {
LOGGER.info("Removing Mesos-Local inner container including volumes: " + innerContainer.getNames()[0]);
innerDockerClient.removeContainerCmd(innerContainer.getId());
}

LOGGER.info("Removing Mesos-Local container");
super.remove();
}
}

0 comments on commit c4ec773

Please sign in to comment.