Skip to content

Commit

Permalink
Fix error message when package install fails due to missing Java
Browse files Browse the repository at this point in the history
Currently is `java` is not in $PATH the preinst script fails
prematurely and prevents an appropriate message from getting displayed
for the user.

Make package installation more user friendly when java is not in
$PATH.

Relates: elastic#31845
  • Loading branch information
dliappis committed Nov 27, 2018
1 parent ad1f0dc commit e000c15
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 7 deletions.
16 changes: 11 additions & 5 deletions distribution/packages/src/common/scripts/preinst
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#!/bin/sh
#
# This script is executed in the pre-installation phase
#
Expand All @@ -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
Expand Down Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion distribution/src/bin/elasticsearch-env
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ public void test30AbortWhenJavaMissing() {
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 + "'");
}
Expand Down

0 comments on commit e000c15

Please sign in to comment.