Skip to content

Commit

Permalink
#16 if no port is specified, the spring.datasource.url will be parsed…
Browse files Browse the repository at this point in the history
… for its port value. If it is not set, the default port (5432) is set.

The default port setting in properties is changed from 5432 to null, so that it can pick up the spring.datasource.url.
  • Loading branch information
robert-bor committed Feb 13, 2017
1 parent ff98003 commit c510af5
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 5 deletions.
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ You can tweak the configuration to use for your Docker run.
| image-name | the image name to be used for deploying the container. Default: postgres |
| image-version | the version of the image to be used for deploying the container. Advice is to specify wherever possible. Default: latest |
| password | the password used to apply for the database on the container. Default: postgres |
| port | the port that you can access the database on. Will be mapped to the container's own 5432 port. Default: 5432 |
| port | the port that you can access the database on will be taken from spring's datasource URL. |
| startup-verification-text | the text that will be searched within the Docker log. If found, the Postgres container is available. Default: "PostgreSQL init process complete; ready for start up." |
| std-out-filename | the file to write the Docker output to. Default: "docker-std-out.log" |
| std-err-filename | the file to write the Docker errors to. Default: docker-std-err.log |
Expand All @@ -94,10 +94,9 @@ docker:
image-version: 9.6
force-clean: true
container-name: postgression2
port: 5434
```
Be sure to point your datasource url to the same port.
Be sure to point your datasource url to another port, for example 5434 (instead of 5432).
Note that the force-clean flag is useful for tests. There is no reason to keep the container in that case and you might as well remove it, when another version is found.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,21 @@

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 Integer DEFAULT_PORT = 5432;
private static final Logger LOGGER = LoggerFactory.getLogger(DockerPostgresBootSequence.class);

private final DockerPostgresProperties properties;
private final DataSourceProperties dataSourceProperties;

public DockerPostgresBootSequence(DockerPostgresProperties properties) {
public DockerPostgresBootSequence(DockerPostgresProperties properties, DataSourceProperties dataSourceProperties) {
this.properties = properties;
this.dataSourceProperties = dataSourceProperties;
}

public DockerPostgresContainer execute() throws IOException, InterruptedException {
Expand All @@ -23,6 +27,14 @@ public DockerPostgresContainer execute() throws IOException, InterruptedExceptio
LOGGER.info("| * Force clean: " + properties.isForceClean());
LOGGER.info("| * Timeout: " + properties.getTimeout());
LOGGER.info("| * Container name: " + properties.getContainerName());
if (properties.getPort() == null) {
if (dataSourceProperties.getUrl() != null) {
properties.setPort(determinePort(dataSourceProperties.getUrl()));
// Scrap the port from the JDBC URL
} else {
properties.setPort(DEFAULT_PORT);
}
}
LOGGER.info("| * Port: " + properties.getPort());
LOGGER.info("| * Password: " + properties.getPassword());
LOGGER.info("| * Startup Verification Text: [" + properties.getStartupVerificationText() + "]");
Expand Down Expand Up @@ -62,6 +74,18 @@ public DockerPostgresContainer execute() throws IOException, InterruptedExceptio
return postgresContainer;
}

private Integer determinePort(String url) {
if (url == null || url.length() == 0) {
throw new ExceptionInInitializerError("spring.datasource.url is empty. No port could be derived.");
}
int lastColonPos = url.lastIndexOf(':');
int slashAfterPortPos = url.indexOf('/', lastColonPos);
if (lastColonPos == -1 || slashAfterPortPos == -1 || slashAfterPortPos < lastColonPos + 2) {
throw new ExceptionInInitializerError("spring.datasource.url does not have port information: [" + url + "]. No port could be derived.");
}
return Integer.parseInt(url.substring(lastColonPos + 1, slashAfterPortPos));
}

private void applyAfterVerificationWait(Integer afterVerificationWait) throws InterruptedException {
if (afterVerificationWait > 0) {
LOGGER.info("| Applying after verification wait of " + afterVerificationWait + "ms");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class DockerPostgresProperties {

private String password = "postgres";

private Integer port = 5432;
private Integer port = null;

private String imageName = "postgres";

Expand Down

0 comments on commit c510af5

Please sign in to comment.