Skip to content

Commit ccbc6e5

Browse files
committed
Support executing Spark from symlinks
The current scripts (e.g. pyspark) fail to run when they are executed via symlinks. A common Linux scenario would be to have Spark installed somewhere (e.g. /opt) and have a symlink to it in /usr/bin. Fixed the scripts to traverse symlinks until reaching the actual binary.
1 parent 6f671d0 commit ccbc6e5

24 files changed

+231
-112
lines changed

bin/beeline

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,13 @@
2424
set -o posix
2525

2626
# Figure out where Spark is installed
27-
FWDIR="$(cd `dirname $0`/..; pwd)"
27+
SOURCE=$0
28+
while [ -h "$SOURCE" ]
29+
do
30+
SOURCE="$(readlink "$SOURCE")"
31+
[[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE"
32+
done
33+
FWDIR="$(cd `dirname $SOURCE`/..; pwd)"
2834

2935
CLASS="org.apache.hive.beeline.BeeLine"
3036
exec "$FWDIR/bin/spark-class" $CLASS "$@"

bin/compute-classpath.sh

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,13 @@
2323
SCALA_VERSION=2.10
2424

2525
# Figure out where Spark is installed
26-
FWDIR="$(cd `dirname $0`/..; pwd)"
26+
SOURCE=$0
27+
while [ -h "$SOURCE" ]
28+
do
29+
SOURCE="$(readlink "$SOURCE")"
30+
[[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE"
31+
done
32+
FWDIR="$(cd `dirname $SOURCE`/..; pwd)"
2733

2834
. $FWDIR/bin/load-spark-env.sh
2935

bin/load-spark-env.sh

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,13 @@
1818
#
1919

2020
# This script loads spark-env.sh if it exists, and ensures it is only loaded once.
21-
# spark-env.sh is loaded from SPARK_CONF_DIR if set, or within the current directory's
21+
# spark-env.sh is loaded from SPARK_CONF_DIR if set, or within SPARK_HOME's
2222
# conf/ subdirectory.
2323

2424
if [ -z "$SPARK_ENV_LOADED" ]; then
2525
export SPARK_ENV_LOADED=1
2626

27-
# Returns the parent of the directory this script lives in.
28-
parent_dir="$(cd `dirname $0`/..; pwd)"
29-
30-
user_conf_dir=${SPARK_CONF_DIR:-"$parent_dir/conf"}
27+
use_conf_dir=${SPARK_CONF_DIR:-"$SPARK_HOME/conf"}
3128

3229
if [ -f "${user_conf_dir}/spark-env.sh" ]; then
3330
# Promote all variable declarations to environment (exported) variables

bin/pyspark

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,13 @@
1818
#
1919

2020
# Figure out where Spark is installed
21-
FWDIR="$(cd `dirname $0`/..; pwd)"
21+
SOURCE=$0
22+
while [ -h "$SOURCE" ]
23+
do
24+
SOURCE="$(readlink "$SOURCE")"
25+
[[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE"
26+
done
27+
FWDIR="$(cd `dirname $SOURCE`/..; pwd)"
2228

2329
# Export this as SPARK_HOME
2430
export SPARK_HOME="$FWDIR"

bin/run-example

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,15 @@
1919

2020
SCALA_VERSION=2.10
2121

22-
FWDIR="$(cd `dirname $0`/..; pwd)"
22+
# Figure out where Spark is installed
23+
SOURCE=$0
24+
while [ -h "$SOURCE" ]
25+
do
26+
SOURCE="$(readlink "$SOURCE")"
27+
[[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE"
28+
done
29+
FWDIR="$(cd `dirname $SOURCE`/..; pwd)"
30+
2331
export SPARK_HOME="$FWDIR"
2432
EXAMPLES_DIR="$FWDIR"/examples
2533

bin/spark-class

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,13 @@ esac
2727
SCALA_VERSION=2.10
2828

2929
# Figure out where Spark is installed
30-
FWDIR="$(cd `dirname $0`/..; pwd)"
30+
SOURCE=$0
31+
while [ -h "$SOURCE" ]
32+
do
33+
SOURCE="$(readlink "$SOURCE")"
34+
[[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE"
35+
done
36+
FWDIR="$(cd `dirname $SOURCE`/..; pwd)"
3137

3238
# Export this as SPARK_HOME
3339
export SPARK_HOME="$FWDIR"

bin/spark-shell

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,14 @@ esac
2828
# Enter posix mode for bash
2929
set -o posix
3030

31-
## Global script variables
32-
FWDIR="$(cd `dirname $0`/..; pwd)"
31+
# Figure out where Spark is installed
32+
SOURCE=$0
33+
while [ -h "$SOURCE" ]
34+
do
35+
SOURCE="$(readlink "$SOURCE")"
36+
[[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE"
37+
done
38+
FWDIR="$(cd `dirname $SOURCE`/..; pwd)"
3339

3440
function usage() {
3541
echo "Usage: ./bin/spark-shell [options]"

bin/spark-sql

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,13 @@ CLASS="org.apache.spark.sql.hive.thriftserver.SparkSQLCLIDriver"
2727
CLASS_NOT_FOUND_EXIT_STATUS=1
2828

2929
# Figure out where Spark is installed
30-
FWDIR="$(cd `dirname $0`/..; pwd)"
30+
SOURCE=$0
31+
while [ -h "$SOURCE" ]
32+
do
33+
SOURCE="$(readlink "$SOURCE")"
34+
[[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE"
35+
done
36+
FWDIR="$(cd `dirname $SOURCE`/..; pwd)"
3137

3238
function usage {
3339
echo "Usage: ./bin/spark-sql [options] [cli option]"

bin/spark-submit

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,18 @@
1919

2020
# NOTE: Any changes in this file must be reflected in SparkSubmitDriverBootstrapper.scala!
2121

22-
export SPARK_HOME="$(cd `dirname $0`/..; pwd)"
22+
# Figure out where Spark is installed
23+
SOURCE=$0
24+
while [ -h "$SOURCE" ]
25+
do
26+
SOURCE="$(readlink "$SOURCE")"
27+
[[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE"
28+
done
29+
FWDIR="$(cd `dirname $SOURCE`/..; pwd)"
30+
31+
# Export this as SPARK_HOME
32+
export SPARK_HOME="$FWDIR"
33+
2334
ORIG_ARGS=("$@")
2435

2536
while (($#)); do

sbin/slaves.sh

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,16 @@ if [ $# -le 0 ]; then
3636
exit 1
3737
fi
3838

39-
sbin=`dirname "$0"`
40-
sbin=`cd "$sbin"; pwd`
39+
# Figure out where Spark is installed
40+
SOURCE=$0
41+
while [ -h "$SOURCE" ]
42+
do
43+
SOURCE="$(readlink "$SOURCE")"
44+
[[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE"
45+
done
46+
export SPARK_HOME="$(cd `dirname $SOURCE`/..; pwd)"
4147

42-
. "$sbin/spark-config.sh"
48+
. "$SPARK_HOME/sbin/spark-config.sh"
4349

4450
# If the slaves file is specified in the command line,
4551
# then it takes precedence over the definition in
@@ -63,7 +69,7 @@ then
6369
shift
6470
fi
6571

66-
. "$SPARK_PREFIX/bin/load-spark-env.sh"
72+
. "$SPARK_HOME/bin/load-spark-env.sh"
6773

6874
if [ "$HOSTLIST" = "" ]; then
6975
if [ "$SPARK_SLAVES" = "" ]; then

0 commit comments

Comments
 (0)