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
This commit is contained in:
Dimitrios Liappis 2018-12-03 10:43:36 +02:00 committed by GitHub
parent 43773a32a4
commit 6a773d7d51
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 14 additions and 7 deletions

View File

@ -1,3 +1,4 @@
#!/bin/bash
# #
# This script is executed in the pre-installation phase # This script is executed in the pre-installation phase
# #
@ -9,16 +10,22 @@
# $1=1 : indicates an new install # $1=1 : indicates an new install
# $1=2 : indicates an upgrade # $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 # Check for these at preinst time due to failures in postinst if they do not exist
if [ -x "$JAVA_HOME/bin/java" ]; then if [ -x "$JAVA_HOME/bin/java" ]; then
JAVA="$JAVA_HOME/bin/java" JAVA="$JAVA_HOME/bin/java"
elif command -v java; then
JAVA=`command -v java`
else else
JAVA=`which java` JAVA=""
fi fi
if [ -z "$JAVA" ]; then if [ -z "$JAVA" ]; then
echo "could not find java; set JAVA_HOME or ensure java is in PATH" err_exit "could not find java; set JAVA_HOME or ensure java is in PATH"
exit 1
fi fi
case "$1" in case "$1" in
@ -75,8 +82,7 @@ case "$1" in
;; ;;
*) *)
echo "pre install script called with unknown argument \`$1'" >&2 err_exit "pre install script called with unknown argument \`$1'"
exit 1
;; ;;
esac esac

View File

@ -45,7 +45,7 @@ else
fi fi
if [ ! -x "$JAVA" ]; then 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 exit 1
fi fi

View File

@ -111,7 +111,7 @@ public abstract class ArchiveTestCase extends PackagingTestCase {
sh.run("chmod -x '" + javaPath + "'"); sh.run("chmod -x '" + javaPath + "'");
final Result runResult = sh.runIgnoreExitCode(bin.elasticsearch.toString()); final Result runResult = sh.runIgnoreExitCode(bin.elasticsearch.toString());
assertThat(runResult.exitCode, is(1)); 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 { } finally {
sh.run("chmod +x '" + javaPath + "'"); sh.run("chmod +x '" + javaPath + "'");
} }

View File

@ -70,6 +70,7 @@ public abstract class PackageTestCase extends PackagingTestCase {
mv(originalJavaPath, relocatedJavaPath); mv(originalJavaPath, relocatedJavaPath);
final Result installResult = runInstallCommand(distribution()); final Result installResult = runInstallCommand(distribution());
assertThat(installResult.exitCode, is(1)); assertThat(installResult.exitCode, is(1));
assertThat(installResult.stderr, containsString("could not find java; set JAVA_HOME or ensure java is in PATH"));
} finally { } finally {
mv(relocatedJavaPath, originalJavaPath); mv(relocatedJavaPath, originalJavaPath);
} }