-
Notifications
You must be signed in to change notification settings - Fork 8
/
ContainerService.java
166 lines (141 loc) · 6 KB
/
ContainerService.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package grimsi.accservermanager.backend.service;
import com.spotify.docker.client.DefaultDockerClient;
import com.spotify.docker.client.DockerClient;
import com.spotify.docker.client.exceptions.DockerException;
import com.spotify.docker.client.messages.*;
import grimsi.accservermanager.backend.configuration.ApplicationConfiguration;
import grimsi.accservermanager.backend.dto.InstanceDto;
import grimsi.accservermanager.backend.entity.Instance;
import grimsi.accservermanager.backend.exception.ContainerException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
@Service
public class ContainerService {
private final DockerClient docker;
@Autowired
InstanceService instanceService;
@Autowired
FileSystemService fileSystemService;
@Autowired
Environment env;
@Autowired
ApplicationConfiguration config;
private Logger log = LoggerFactory.getLogger(ContainerService.class);
@Autowired
public ContainerService(UtilityService utilityService) {
switch (utilityService.getHostOS()){
case WINDOWS:
docker = new DefaultDockerClient("http://localhost:2375");
break;
case UNIX:
docker = new DefaultDockerClient("unix:///var/run/docker.sock");
break;
case MAC:
docker = new DefaultDockerClient("unix:///var/run/docker.sock");
break;
default:
docker = new DefaultDockerClient("unix:///var/run/docker.sock");
break;
}
}
public void deployInstance(InstanceDto instance) {
String[] ports = {
(instance.getConfig().getConfigurationJson().getTcpPort() + "/tcp"),
(instance.getConfig().getConfigurationJson().getUdpPort() + "/udp")
};
Map<String, List<PortBinding>> portBindings = new HashMap<>();
for (String port : ports) {
portBindings.put(port, Collections.singletonList(PortBinding.of("0.0.0.0", port)));
}
HostConfig hostConfig = HostConfig.builder()
.portBindings(portBindings)
.binds(fileSystemService.getInstanceFolderPath(instance) + ":/opt/server")
.build();
try{
List<Image> images = docker.listImages().stream().filter(
image -> image.repoTags().stream()
.anyMatch(tag -> tag.equals(config.getContainerImage())
)
).collect(Collectors.toList());
if(images.isEmpty()){
docker.pull(config.getContainerImage());
}
} catch (DockerException | InterruptedException e){
throw new ContainerException("Could not find/pull image '" + config.getContainerImage() + "': " + e.getMessage());
}
final ContainerConfig containerConfig = ContainerConfig.builder()
.hostConfig(hostConfig)
.image(config.getContainerImage())
.exposedPorts(ports)
.build();
try {
String containerName = buildContainerName(instance);
ContainerCreation container = docker.createContainer(containerConfig, containerName);
instance.setContainer(container.id());
instanceService.save(instance);
} catch (DockerException | InterruptedException e) {
throw new ContainerException("Could not create container for instance '" + instance.getId() + "': " + e.getMessage());
}
}
public void startInstance(InstanceDto instance) {
try {
docker.startContainer(instance.getContainer());
} catch (DockerException | InterruptedException e) {
throw new ContainerException("Could not start container '" + instance.getContainer() +"': " + e.getMessage());
}
}
public void stopInstance(InstanceDto instance) {
try {
docker.stopContainer(instance.getContainer(), 10);
} catch (DockerException | InterruptedException e) {
throw new ContainerException("Could not stop container '" + instance.getContainer() +"': " + e.getMessage());
}
}
public void pauseInstance(InstanceDto instance) {
try {
docker.pauseContainer(instance.getContainer());
} catch (DockerException | InterruptedException e) {
throw new ContainerException("Could not pause container '" + instance.getContainer() +"': " + e.getMessage());
}
}
public void resumeInstance(InstanceDto instance) {
try {
docker.unpauseContainer(instance.getContainer());
} catch (DockerException | InterruptedException e){
throw new ContainerException("Could not resume container '" + instance.getContainer() +"': " + e.getMessage());
}
}
public void deleteInstance(InstanceDto instance){
try {
stopInstance(instance);
docker.removeContainer(instance.getContainer());
} catch (DockerException | InterruptedException e){
throw new ContainerException("Could not delete container '" + instance.getContainer() +"': " + e.getMessage());
}
}
public ContainerStats getContainerStats(InstanceDto instance) {
try {
return docker.stats(instance.getContainer());
} catch (DockerException | InterruptedException e) {
log.error(e.toString());
}
return null;
}
private String buildContainerName(InstanceDto instance){
/* main part */
String name = instance.getName();
/* Postfix */
if(config.isContainerNamePostfixEnabled()){
name = name.concat("-v").concat(instance.getVersion());
}
return name;
}
}