-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDockerPostgresBootSequence.java
114 lines (97 loc) · 5.7 KB
/
DockerPostgresBootSequence.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
package nl._42.boot.docker.postgres;
import nl._42.boot.docker.postgres.containers.DockerContainerInformation;
import nl._42.boot.docker.postgres.containers.DockerContainerInformationCommand;
import nl._42.boot.docker.postgres.images.DockerImageInformationCommand;
import nl._42.boot.docker.postgres.shared.DockerHeaderMismatch;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import java.io.IOException;
public class DockerPostgresBootSequence {
private static final Logger LOGGER = LoggerFactory.getLogger(DockerPostgresBootSequence.class);
private final DockerPostgresProperties properties;
private final DataSourceProperties dataSourceProperties;
public DockerPostgresBootSequence(DockerPostgresProperties properties, DataSourceProperties dataSourceProperties) {
this.properties = properties;
this.dataSourceProperties = dataSourceProperties;
}
public DockerStartContainerCommand execute() throws IOException, InterruptedException {
properties.init(dataSourceProperties.getUrl());
LOGGER.info("| Docker Postgres Properties");
LOGGER.info("| * Image name: " + properties.getImageName());
LOGGER.info("| * Image version: " + properties.getImageVersion());
LOGGER.info("| * Force clean: " + properties.isForceClean());
LOGGER.info("| * Stop port occupying container: " + properties.isStopPortOccupyingContainer());
LOGGER.info("| * Timeout: " + properties.getTimeout());
LOGGER.info("| * Container name: " + properties.getContainerName());
LOGGER.info("| * Port: " + properties.getPort());
LOGGER.info("| * Password: " + properties.getPassword());
LOGGER.info("| * Startup Verification Text: [" + properties.getStartupVerificationText() + "]");
LOGGER.info("| * Times expected verification text: " + properties.getTimesExpectedVerificationText() + "x");
LOGGER.info("| * After verification wait: " + properties.getAfterVerificationWait() + "ms");
LOGGER.info("| * Docker command: [" + properties.getDockerCommand() + "]");
LOGGER.info("| * Use Docker command: [" + properties.getUseDockerCommand() + "]");
LOGGER.info("| * Custom variables (" + properties.getCustomVariables().size() + ")");
for (String key : properties.getCustomVariables().keySet()) {
LOGGER.info("| - " + key + ": " + properties.getCustomVariables().get(key));
}
LOGGER.info("| * In memory: " + properties.isInMemory());
LOGGER.info("| * In memory mount destinations");
for (String inMemoryMountDestination : properties.getInMemoryMountDestinations()) {
LOGGER.info("| - " + inMemoryMountDestination);
}
LOGGER.info("| * Std out: " + properties.getStdOutFilename());
LOGGER.info("| * Std err: " + properties.getStdErrFilename());
// Verify if Docker is available on the command-line
new DockerAvailableCheckCommand(properties).tryDocker();
// Read the container list
final DockerContainerInformation containers;
try {
containers = new DockerContainerInformationCommand(properties).interpretDockerContainerListing();
} catch (DockerHeaderMismatch ex) {
throw new ExceptionInInitializerError(ex.getMessage());
}
// Force clean the old container
if ( properties.isForceClean() &&
containers.hasContainer(properties.getContainerName())) {
new DockerForceRemoveContainerCommand(properties).forceRemove();
}
// Check if the port is already in use
String containerWithPort = containers.portOccupied(properties.getPort());
if (containerWithPort != null && !containerWithPort.equals(properties.getContainerName())) {
if (properties.isStopPortOccupyingContainer()) {
properties.setContainerOccupyingPort(containerWithPort);
new DockerStopContainerCommand(properties).stopContainer();
} else {
LOGGER.warn("| The port is already in use by container '" + containerWithPort + "'. THIS DOCKER RUN IS LIKELY TO FAIL");
}
}
// Verify if the image is downloaded (influences the timeout)
boolean imageDownloaded = false;
try {
imageDownloaded = new DockerImageInformationCommand(properties)
.interpretDockerImageListing()
.hasImage(properties.getImageName(), properties.getImageVersion());
} catch (DockerHeaderMismatch ex) {
// Non-fatal; continue, just assume the image not to have been downloaded
LOGGER.warn("| The image information was not read, assuming download not to have taken place");
}
// Start up the Docker Postgres container (blocking thread)
DockerStartContainerCommand postgresContainer = new DockerStartContainerCommand(properties, imageDownloaded);
postgresContainer.start();
if (postgresContainer.verify()) {
applyAfterVerificationWait(properties.getAfterVerificationWait());
LOGGER.info("| Postgres container successfully started");
} else {
LOGGER.error("| Postgres failed to initialize");
throw new ExceptionInInitializerError("The Docker Container failed to properly initialize.");
}
return postgresContainer;
}
private void applyAfterVerificationWait(Integer afterVerificationWait) throws InterruptedException {
if (afterVerificationWait > 0) {
LOGGER.info("| Applying after verification wait of " + afterVerificationWait + "ms");
Thread.sleep(afterVerificationWait);
}
}
}