]> git.somenet.org - pub/jan/dst18.git/blob - ass3-elastic/src/main/java/dst/ass3/elastic/impl/ContainerService.java
GITOLITE.txt
[pub/jan/dst18.git] / ass3-elastic / src / main / java / dst / ass3 / elastic / impl / ContainerService.java
1 package dst.ass3.elastic.impl;
2
3 import com.github.dockerjava.api.DockerClient;
4 import com.github.dockerjava.api.command.CreateContainerResponse;
5 import com.github.dockerjava.api.command.InspectContainerResponse;
6 import com.github.dockerjava.api.exception.NotFoundException;
7 import com.github.dockerjava.api.model.Container;
8 import com.github.dockerjava.core.DefaultDockerClientConfig;
9 import com.github.dockerjava.core.DockerClientBuilder;
10 import com.github.dockerjava.core.DockerClientConfig;
11 import dst.ass3.elastic.ContainerException;
12 import dst.ass3.elastic.ContainerInfo;
13 import dst.ass3.elastic.ContainerNotFoundException;
14 import dst.ass3.elastic.IContainerService;
15 import dst.ass3.messaging.RequestType;
16
17 import java.util.ArrayList;
18 import java.util.List;
19
20 public class ContainerService implements IContainerService {
21     final static DockerClientConfig config = DefaultDockerClientConfig.createDefaultConfigBuilder().withDockerHost("tcp://192.168.99.99:2375").build();
22
23     @Override
24     public List<ContainerInfo> listContainers() throws ContainerException {
25         DockerClient docker = DockerClientBuilder.getInstance(config).build();
26
27         ArrayList<ContainerInfo> ret = new ArrayList<>();
28         for (Container c : docker.listContainersCmd().exec()) {
29             ContainerInfo ci = new ContainerInfo();
30             ci.setContainerId(c.getId());
31             ci.setImage(c.getImage());
32             ci.setRunning(true);
33             ci.setWorkerType(RequestType.valueOf((c.getCommand().split(" "))[2].toUpperCase()));
34             ret.add(ci);
35         }
36         return ret;
37     }
38
39     @Override
40     public void stopContainer(String containerId) throws ContainerException {
41         DockerClient docker = DockerClientBuilder.getInstance(config).build();
42         try {
43             docker.stopContainerCmd(containerId).exec();
44             docker.removeContainerCmd(containerId).exec();
45         } catch (NotFoundException e) {
46             throw new ContainerNotFoundException(e);
47         }
48     }
49
50     @Override
51     public ContainerInfo startWorker(RequestType type) throws ContainerException {
52         DockerClient docker = DockerClientBuilder.getInstance(config).build();
53
54         CreateContainerResponse container = docker.createContainerCmd("dst/ass3-worker").withCmd(type.toString().toLowerCase()).exec();
55         docker.startContainerCmd(container.getId()).exec();
56
57         ContainerInfo ci = new ContainerInfo();
58         ci.setWorkerType(type);
59         ci.setImage("dst/ass3-worker");
60         ci.setContainerId(container.getId());
61
62         InspectContainerResponse inspect = docker.inspectContainerCmd(container.getId()).exec();
63
64         try {
65             ci.setRunning(inspect.getState().getRunning());
66         } catch (NullPointerException e) {
67             ci.setRunning(false);
68         }
69         return ci;
70     }
71 }