Skip to content

Commit

Permalink
During initialization, avoid creating files in an empty (unused) data…
Browse files Browse the repository at this point in the history
… directory.

This is important for Docker etc, where the directory will already exist
but be empty, and the location configured externally.
  • Loading branch information
MattBlissett committed Aug 23, 2021
1 parent 5625d31 commit 13a031e
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 11 deletions.
2 changes: 1 addition & 1 deletion package/docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
FROM tomcat:8.5-jdk8
LABEL MAINTAINERS="Markus Döring <mdoering@gbif.org>, Matthew Blissett <mblissett@gbif.org>"
LABEL MAINTAINERS="Matthew Blissett <mblissett@gbif.org>"

ARG IPT_VERSION
ARG IPT_NAME=ROOT
Expand Down
10 changes: 5 additions & 5 deletions package/docker/README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ We regularly publish IPT releases as Docker images to https://hub.docker.com/r/g

== To run the Docker image

Refer to https://ipt.gbif.org/manual/en/ipt/2.5/installation/#_installation_using_docker[Installation using Docker] in the IPT User Manual.
Refer to https://ipt.gbif.org/manual/en/ipt/2.5/installation#installation-using-docker[Installation using Docker] in the IPT User Manual.

== Upgrading

Expand All @@ -18,10 +18,10 @@ Note that, for better consistency with the https://en.wikipedia.org/wiki/Filesys

(This process is for GBIF developers.)

. `docker build --pull --build-arg IPT_VERSION=2.4.2 -t gbif/ipt:2.4.2 .`
. `docker build --pull --build-arg IPT_VERSION=2.5.0 -t gbif/ipt:2.5.0 .`
. *Test the resulting image!*
+
`docker run --volume /full/path/to/data-directory:/srv/ipt --publish 8080:8080 gbif/ipt:2.4.2`
`docker run --volume /full/path/to/data-directory:/srv/ipt --publish 8080:8080 gbif/ipt:2.5.0`

. `docker push gbif/ipt:2.4.2` (stop here for a pre-release)
. `docker tag gbif/ipt:2.4.2 gbif/ipt:latest && docker push gbif/ipt:latest`
. `docker push gbif/ipt:2.5.0` (stop here for a pre-release)
. `docker tag gbif/ipt:2.5.0 gbif/ipt:latest && docker push gbif/ipt:latest`
2 changes: 1 addition & 1 deletion src/main/java/org/gbif/ipt/config/AppConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ protected void loadConfig() throws InvalidConfigException {
props.load(configStream);
LOG.debug("Loaded default configuration from application.properties in classpath");
}
if (dataDir.dataDir != null && dataDir.dataDir.exists()) {
if (dataDir.isConfigured()) {
// load user configuration properties from data dir ipt.properties (if it exists)
File userCfgFile = new File(dataDir.dataDir, "config/" + DATADIR_PROPFILE);
if (userCfgFile.exists()) {
Expand Down
12 changes: 10 additions & 2 deletions src/main/java/org/gbif/ipt/config/DataDir.java
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,14 @@ public File dataFile(String path) {
* @return true if a working data directory is configured
*/
public boolean isConfigured() {
return dataDir != null && dataDir.exists();
return dataDir != null && dataDir.isDirectory() && dataDir.list().length > 0;
}

/**
* @return true if a working data directory is configured, but is not yet set up
*/
public boolean isConfiguredButEmpty() {
return dataDir != null && dataDir.isDirectory() && dataDir.list().length == 0;
}

/**
Expand Down Expand Up @@ -327,7 +334,7 @@ public boolean setDataDir(File dataDir) throws InvalidConfigException {
throw new InvalidConfigException(TYPE.INVALID_DATA_DIR,
"DataDir " + dataDir.getAbsolutePath() + " exists already and is no IPT data dir.");
}
LOG.info("Reusing existing data dir.");
LOG.info("Reusing existing data dir {}", dataDir);
// persist location in WEB-INF
try {
persistLocation();
Expand All @@ -343,6 +350,7 @@ public boolean setDataDir(File dataDir) throws InvalidConfigException {

} else {
// NEW datadir
LOG.info("Setting up new data directory {}", dataDir);
try {
// create new main data dir. Populate later
FileUtils.forceMkdir(dataDir);
Expand Down
11 changes: 9 additions & 2 deletions src/main/java/org/gbif/ipt/config/SetupAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -174,14 +174,19 @@ public void setSetup2(boolean setup2) {
*/
public String setup() {
if (isHttpPost() && dataDirPath != null) {

// since IPT v2.2, user must check that they have read and understood disclaimer
if (!readDisclaimer) {
addFieldError("readDisclaimer", getText("admin.config.setup.read.error"));
return INPUT;
}
}

if ((dataDir.dataDir != null && (!dataDir.dataDir.exists() || dataDir.isConfiguredButEmpty())) ||
isHttpPost() && dataDirPath != null) {

File dd = new File(dataDirPath.trim());
LOG.info("Set up data directory {}", dataDir);

File dd = dataDirPath != null ? new File(dataDirPath.trim()) : dataDir.dataDir;
try {
if (dd.isAbsolute()) {
boolean created = configManager.setDataDir(dd);
Expand All @@ -206,8 +211,10 @@ public String setup() {
addActionError(msg);
}
}

if (dataDir.isConfigured()) {
// the data dir is already/now configured, skip the first setup step
LOG.info("Skipping setup step 1");
return SUCCESS;
}
return INPUT;
Expand Down

0 comments on commit 13a031e

Please sign in to comment.