-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathDockerContainerDatabase.java
More file actions
148 lines (117 loc) · 4.65 KB
/
DockerContainerDatabase.java
File metadata and controls
148 lines (117 loc) · 4.65 KB
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
package io.kurrent.dbclient.databases;
import io.kurrent.dbclient.*;
import com.github.dockerjava.api.model.HealthCheck;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.containers.wait.strategy.Wait;
import java.io.File;
import java.io.FileNotFoundException;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;
public class DockerContainerDatabase extends GenericContainer<DockerContainerDatabase> implements Database {
public static final String DEFAULT_IMAGE = "docker.cloudsmith.io/eventstore/kurrent-staging/kurrentdb:ci";
public static class Builder {
String image;
boolean secure;
boolean anonymous;
Map<String, String> env;
public Builder() {
this.image = DEFAULT_IMAGE;
this.env = new HashMap<>();
}
public Builder secure(boolean secure) {
this.secure = secure;
return this;
}
public Builder anonymous(boolean anonymous) {
this.anonymous = anonymous;
return this;
}
public Builder image(String image) {
this.image = image;
return this;
}
public Builder env(String envVar, String value) {
this.env.put(envVar, value);
return this;
}
public DockerContainerDatabase build() {
return new DockerContainerDatabase(this);
}
}
private final Builder builder;
private final ClientTracker clientTracker;
public DockerContainerDatabase(Builder builder) {
super(builder.image);
addExposedPorts(1113, 2113);
withEnv("EVENTSTORE_RUN_PROJECTIONS", "ALL");
withEnv("EVENTSTORE_START_STANDARD_PROJECTIONS", "true");
withEnv("EVENTSTORE_ENABLE_ATOM_PUB_OVER_HTTP", "true");
if (builder.secure) {
verifyCertificatesExist();
String certsDir = Paths.get(System.getProperty("user.dir"), "certs").toAbsolutePath().toString();
withEnv("EVENTSTORE_CERTIFICATE_FILE", "/etc/eventstore/certs/node/node.crt");
withEnv("EVENTSTORE_CERTIFICATE_PRIVATE_KEY_FILE", "/etc/eventstore/certs/node/node.key");
withEnv("EVENTSTORE_TRUSTED_ROOT_CERTIFICATES_PATH", "/etc/eventstore/certs/ca");
withFileSystemBind(certsDir, "/etc/eventstore/certs");
} else {
withEnv("EVENTSTORE_INSECURE", "true");
}
builder.env.forEach((envVar, value) -> withEnv(envVar, value));
this.builder = builder;
this.clientTracker = new ClientTracker();
withCreateContainerCmdModifier(cmd -> cmd.withHealthcheck(new HealthCheck()
.withInterval(1000000000L)
.withTimeout(1000000000L)
.withRetries(10)));
waitingFor(Wait.forHealthcheck());
start();
}
@Override
public ConnectionSettingsBuilder defaultSettingsBuilder() {
ConnectionSettingsBuilder settingsBuilder = KurrentDBClientSettings.builder().addHost(getHost(), getMappedPort(2113));
if (!builder.anonymous)
settingsBuilder.defaultCredentials("admin", "changeit");
if (builder.secure)
settingsBuilder.tls(true).tlsVerifyCert(false);
else
settingsBuilder.tls(false);
return settingsBuilder;
}
@Override
public ClientTracker getClientTracker() {
return clientTracker;
}
@Override
public void cleanup() {
try {
ExecResult result = execInContainer("tar", "-czvf", "/tmp/esdb_logs.tar.gz", "/var/log/eventstore");
if (result.getExitCode() != 0) {
logger().error(result.getStderr());
throw new RuntimeException("Error when compressing server logs");
}
copyFileFromContainer("/tmp/esdb_logs.tar.gz", "/tmp/esdb_logs.tar.gz");
} catch (Exception e) {
logger().error("Error when cleanup docker container", e);
} finally {
stop();
}
}
public static Builder builder() {
return new Builder();
}
private static void verifyCertificatesExist() {
String currentDir = System.getProperty("user.dir");
String[][] files = {
{"ca", "ca.crt"},
{"ca", "ca.key"},
{"node", "node.crt"},
{"node", "node.key"},
};
for (String[] strings : files) {
File file = Paths.get(currentDir, "certs", strings[0], strings[1]).toAbsolutePath().toFile();
if (!file.exists())
throw new RuntimeException(new FileNotFoundException(file.getAbsolutePath()));
}
}
}