-
Notifications
You must be signed in to change notification settings - Fork 0
/
ContainerStopper.java
92 lines (83 loc) · 2.84 KB
/
ContainerStopper.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
package com.kazurayam.subprocessj.docker;
import com.kazurayam.subprocessj.CommandLocator;
import com.kazurayam.subprocessj.Subprocess;
import com.kazurayam.subprocessj.Subprocess.CompletedProcess;
import com.kazurayam.subprocessj.docker.model.ContainerId;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
/**
* <PRE>
* $ docker stop d4d4a795d76d
* d4d4a795d76d
* </PRE>
*/
public class ContainerStopper {
public ContainerStopper() {}
public static ContainerStoppingResult stopContainer(
ContainerId containerId)
throws IOException, InterruptedException
{
Objects.requireNonNull(containerId);
CommandLocator.CommandLocatingResult clr = DockerCommandLocator.find();
if (clr.returncode() == 0) {
String dockerCommand = clr.stdout().get(0).trim();
List<String> args = Arrays.asList(
dockerCommand, "stop", containerId.id()
);
CompletedProcess cp =
new Subprocess().run(args);
ContainerStoppingResult csr = new ContainerStoppingResult(cp);
if (cp.returncode() == 0) {
csr.setReturncode(0);
} else {
csr.setReturncode(cp.returncode());
csr.setMessage("docker stop command failed");
}
return csr;
} else {
throw new IllegalStateException(clr.toString());
}
}
public static class ContainerStoppingResult {
private final CompletedProcess cp;
private int returncode;
private String message;
public ContainerStoppingResult(CompletedProcess cp) {
this.cp = cp;
this.returncode = -1;
this.message = "";
}
public void setReturncode(int returncode) {
this.returncode = returncode;
}
public void setMessage(String message) {
this.message = message;
}
public int returncode() {
return this.returncode;
}
public String message() {
return this.message;
}
public CompletedProcess completedProcess() {
return this.cp;
}
@Override
public String toString() {
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(new BufferedWriter(sw));
pw.println("<container-stopping-result rc=\"" + this.returncode() + "\">");
pw.println("<message>" + this.message() + "</message>");
pw.print(this.completedProcess().toString());
pw.println("</container-stopping-result>");
pw.flush();
pw.close();
return sw.toString();
}
}
}