From 6a773d7d511546661e0bc6cf3e84cfa1152cdb85 Mon Sep 17 00:00:00 2001 From: Dimitrios Liappis Date: Mon, 3 Dec 2018 10:43:36 +0200 Subject: [PATCH] Fix error message when package install fails due to missing Java (#36077) Currently is `java` is not in $PATH the preinst script fails prematurely and prevents an appropriate message from getting displayed to the user. Make package installation more user friendly when java is not in $PATH and add a test for it. Also use a she-bang in the preinst script, as, at least in Debian, maintainer scripts must start with the #! convention [1]. Relates #31845 [1] https://www.debian.org/doc/debian-policy/ch-maintainerscripts.html --- distribution/packages/src/common/scripts/preinst | 16 +++++++++++----- distribution/src/bin/elasticsearch-env | 2 +- .../packaging/test/ArchiveTestCase.java | 2 +- .../packaging/test/PackageTestCase.java | 1 + 4 files changed, 14 insertions(+), 7 deletions(-) diff --git a/distribution/packages/src/common/scripts/preinst b/distribution/packages/src/common/scripts/preinst index 22f2405af3c..0718e31b05e 100644 --- a/distribution/packages/src/common/scripts/preinst +++ b/distribution/packages/src/common/scripts/preinst @@ -1,3 +1,4 @@ +#!/bin/bash # # This script is executed in the pre-installation phase # @@ -9,16 +10,22 @@ # $1=1 : indicates an new install # $1=2 : indicates an upgrade +err_exit() { + echo "$@" >&2 + exit 1 +} + # Check for these at preinst time due to failures in postinst if they do not exist if [ -x "$JAVA_HOME/bin/java" ]; then JAVA="$JAVA_HOME/bin/java" +elif command -v java; then + JAVA=`command -v java` else - JAVA=`which java` + JAVA="" fi if [ -z "$JAVA" ]; then - echo "could not find java; set JAVA_HOME or ensure java is in PATH" - exit 1 + err_exit "could not find java; set JAVA_HOME or ensure java is in PATH" fi case "$1" in @@ -75,8 +82,7 @@ case "$1" in ;; *) - echo "pre install script called with unknown argument \`$1'" >&2 - exit 1 + err_exit "pre install script called with unknown argument \`$1'" ;; esac diff --git a/distribution/src/bin/elasticsearch-env b/distribution/src/bin/elasticsearch-env index fc8b4a809fe..d1dec54f93d 100644 --- a/distribution/src/bin/elasticsearch-env +++ b/distribution/src/bin/elasticsearch-env @@ -45,7 +45,7 @@ else fi if [ ! -x "$JAVA" ]; then - echo "could not find java; set JAVA_HOME or ensure java is in PATH" + echo "could not find java; set JAVA_HOME or ensure java is in PATH" >&2 exit 1 fi diff --git a/qa/vagrant/src/main/java/org/elasticsearch/packaging/test/ArchiveTestCase.java b/qa/vagrant/src/main/java/org/elasticsearch/packaging/test/ArchiveTestCase.java index 341ecc78bc9..f298e7681cc 100644 --- a/qa/vagrant/src/main/java/org/elasticsearch/packaging/test/ArchiveTestCase.java +++ b/qa/vagrant/src/main/java/org/elasticsearch/packaging/test/ArchiveTestCase.java @@ -111,7 +111,7 @@ public abstract class ArchiveTestCase extends PackagingTestCase { sh.run("chmod -x '" + javaPath + "'"); final Result runResult = sh.runIgnoreExitCode(bin.elasticsearch.toString()); assertThat(runResult.exitCode, is(1)); - assertThat(runResult.stdout, containsString("could not find java; set JAVA_HOME or ensure java is in PATH")); + assertThat(runResult.stderr, containsString("could not find java; set JAVA_HOME or ensure java is in PATH")); } finally { sh.run("chmod +x '" + javaPath + "'"); } diff --git a/qa/vagrant/src/main/java/org/elasticsearch/packaging/test/PackageTestCase.java b/qa/vagrant/src/main/java/org/elasticsearch/packaging/test/PackageTestCase.java index db31babca57..cc2ae29653c 100644 --- a/qa/vagrant/src/main/java/org/elasticsearch/packaging/test/PackageTestCase.java +++ b/qa/vagrant/src/main/java/org/elasticsearch/packaging/test/PackageTestCase.java @@ -70,6 +70,7 @@ public abstract class PackageTestCase extends PackagingTestCase { mv(originalJavaPath, relocatedJavaPath); final Result installResult = runInstallCommand(distribution()); assertThat(installResult.exitCode, is(1)); + assertThat(installResult.stderr, containsString("could not find java; set JAVA_HOME or ensure java is in PATH")); } finally { mv(relocatedJavaPath, originalJavaPath); }