From 465233787ea34aa139d19bd11b612a1cc2b564cf Mon Sep 17 00:00:00 2001 From: Jason Tedor Date: Wed, 28 Nov 2018 16:55:59 -0500 Subject: [PATCH] Move creation of temporary directory to Java (#36002) In the long run we want to move all of startup to a Java program. This will simplify our startup scripts and make maintenance of startup less dependent on the underlying platform that we run on. This commit moves the creation of the temporary directory off of system-dependent commands and onto a simple Java program. --- distribution/src/bin/elasticsearch-env | 10 +--- distribution/src/bin/elasticsearch-env.bat | 5 +- .../tools/launchers/Launchers.java | 10 ++++ .../tools/launchers/TempDirectory.java | 59 +++++++++++++++++++ 4 files changed, 71 insertions(+), 13 deletions(-) create mode 100644 distribution/tools/launchers/src/main/java/org/elasticsearch/tools/launchers/TempDirectory.java diff --git a/distribution/src/bin/elasticsearch-env b/distribution/src/bin/elasticsearch-env index cc16f710345..fc8b4a809fe 100644 --- a/distribution/src/bin/elasticsearch-env +++ b/distribution/src/bin/elasticsearch-env @@ -81,13 +81,5 @@ ES_DISTRIBUTION_FLAVOR=${es.distribution.flavor} ES_DISTRIBUTION_TYPE=${es.distribution.type} if [ -z "$ES_TMPDIR" ]; then - set +e - mktemp --version 2>&1 | grep coreutils > /dev/null - mktemp_coreutils=$? - set -e - if [ $mktemp_coreutils -eq 0 ]; then - ES_TMPDIR=`mktemp -d --tmpdir "elasticsearch.XXXXXXXX"` - else - ES_TMPDIR=`mktemp -d -t elasticsearch` - fi + ES_TMPDIR=`"$JAVA" -cp "$ES_CLASSPATH" org.elasticsearch.tools.launchers.TempDirectory` fi diff --git a/distribution/src/bin/elasticsearch-env.bat b/distribution/src/bin/elasticsearch-env.bat index b9907670920..7c4b8dc49f4 100644 --- a/distribution/src/bin/elasticsearch-env.bat +++ b/distribution/src/bin/elasticsearch-env.bat @@ -57,8 +57,5 @@ set ES_DISTRIBUTION_FLAVOR=${es.distribution.flavor} set ES_DISTRIBUTION_TYPE=${es.distribution.type} if not defined ES_TMPDIR ( - set ES_TMPDIR=!TMP!\elasticsearch - if not exist "!ES_TMPDIR!" ( - mkdir "!ES_TMPDIR!" - ) + for /f "tokens=* usebackq" %%a in (`"%JAVA% -cp "!ES_CLASSPATH!" "org.elasticsearch.tools.launchers.TempDirectory""`) do set ES_TMPDIR=%%a ) diff --git a/distribution/tools/launchers/src/main/java/org/elasticsearch/tools/launchers/Launchers.java b/distribution/tools/launchers/src/main/java/org/elasticsearch/tools/launchers/Launchers.java index 6c9a1ef9473..bb82b1a09a8 100644 --- a/distribution/tools/launchers/src/main/java/org/elasticsearch/tools/launchers/Launchers.java +++ b/distribution/tools/launchers/src/main/java/org/elasticsearch/tools/launchers/Launchers.java @@ -21,6 +21,11 @@ package org.elasticsearch.tools.launchers; import org.elasticsearch.tools.java_version_checker.SuppressForbidden; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.attribute.FileAttribute; + /** * Utility methods for launchers. */ @@ -56,4 +61,9 @@ final class Launchers { System.exit(status); } + @SuppressForbidden(reason = "Files#createTempDirectory(String, FileAttribute...)") + static Path createTempDirectory(final String prefix, final FileAttribute... attrs) throws IOException { + return Files.createTempDirectory(prefix, attrs); + } + } diff --git a/distribution/tools/launchers/src/main/java/org/elasticsearch/tools/launchers/TempDirectory.java b/distribution/tools/launchers/src/main/java/org/elasticsearch/tools/launchers/TempDirectory.java new file mode 100644 index 00000000000..55f54a4e1da --- /dev/null +++ b/distribution/tools/launchers/src/main/java/org/elasticsearch/tools/launchers/TempDirectory.java @@ -0,0 +1,59 @@ +/* + * Licensed to Elasticsearch under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.elasticsearch.tools.launchers; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.Arrays; + +/** + * Provides a path for a temporary directory. On non-Windows OS, this will be created as a sub-directory of the default temporary directory. + * Note that this causes the created temporary directory to be a private temporary directory. + */ +final class TempDirectory { + + /** + * The main entry point. The exit code is 0 if we successfully created a temporary directory as a sub-directory of the default + * temporary directory and printed the resulting path to the console. + * + * @param args the args to the program which should be empty + * @throws IOException if an I/O exception occurred while creating the temporary directory + */ + public static void main(final String[] args) throws IOException { + if (args.length != 0) { + throw new IllegalArgumentException("expected zero arguments but was " + Arrays.toString(args)); + } + /* + * On Windows, we avoid creating a unique temporary directory per invocation lest we pollute the temporary directory. On other + * operating systems, temporary directories will be cleaned automatically via various mechanisms (e.g., systemd, or restarts). + */ + final Path path; + if (System.getProperty("os.name").startsWith("Windows")) { + path = Paths.get(System.getProperty("java.io.tmpdir"), "elasticsearch"); + Files.createDirectories(path); + } else { + path = Launchers.createTempDirectory("elasticsearch-"); + } + Launchers.outPrintln(path.toString()); + } + +}