Implement ContainerService (3.2.3)
This commit is contained in:
parent
5da11730ca
commit
ffbd44826b
@ -0,0 +1,59 @@
|
|||||||
|
package dst.ass3.elastic.impl;
|
||||||
|
|
||||||
|
import com.github.dockerjava.api.DockerClient;
|
||||||
|
import com.github.dockerjava.core.DockerClientBuilder;
|
||||||
|
import dst.ass3.elastic.ContainerException;
|
||||||
|
import dst.ass3.elastic.ContainerInfo;
|
||||||
|
import dst.ass3.elastic.ContainerNotFoundException;
|
||||||
|
import dst.ass3.elastic.IContainerService;
|
||||||
|
import dst.ass3.messaging.Region;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class ContainerService implements IContainerService {
|
||||||
|
private List<ContainerInfo> containers = new ArrayList<>();
|
||||||
|
private DockerClient dockerClient;
|
||||||
|
|
||||||
|
public ContainerService() {
|
||||||
|
dockerClient = DockerClientBuilder.getInstance("tcp://192.168.99.99:2375").build();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<ContainerInfo> listContainers() throws ContainerException {
|
||||||
|
return containers;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void stopContainer(String containerId) throws ContainerException {
|
||||||
|
containers.forEach(containerInfo -> {
|
||||||
|
if (containerInfo.getContainerId().equals(containerId)) {
|
||||||
|
containers.remove(containerInfo);
|
||||||
|
dockerClient.stopContainerCmd(containerId).exec();
|
||||||
|
dockerClient.removeContainerCmd(containerId).exec();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
throw new ContainerNotFoundException("Container with id " + containerId + " not found");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ContainerInfo startWorker(Region region) throws ContainerException {
|
||||||
|
ContainerInfo containerInfo = new ContainerInfo();
|
||||||
|
containerInfo.setWorkerRegion(region);
|
||||||
|
containerInfo.setImage("dst/ass3-worker");
|
||||||
|
containerInfo.setRunning(true);
|
||||||
|
|
||||||
|
String containerId = dockerClient.createContainerCmd("dst/ass3-worker")
|
||||||
|
.withCmd(region.toString().toLowerCase())
|
||||||
|
.withAttachStdout(true)
|
||||||
|
.withAttachStderr(true)
|
||||||
|
.exec().getId();
|
||||||
|
|
||||||
|
dockerClient.startContainerCmd(containerId).exec();
|
||||||
|
|
||||||
|
containerInfo.setContainerId(containerId);
|
||||||
|
containers.add(containerInfo);
|
||||||
|
return containerInfo;
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user