Skip to content

Commit c6b2262

Browse files
DSP-5892-2: General Shell Script Safety Improvements
Doublequoting most variables to ensure we aren't bitten by variable expansion.
1 parent eed5140 commit c6b2262

File tree

6 files changed

+87
-22
lines changed

6 files changed

+87
-22
lines changed

bin/kill-process-tree.sh

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#!/bin/bash
2+
3+
if [ "$(which pgrep)" == "" ]; then
4+
echo "pgrep is not available" >&2
5+
exit 1
6+
fi
7+
8+
if [ "$#" != "2" ]; then
9+
echo "Syntax error, the expected usage is: kill-process-tree <signal> <process_id>" >&2
10+
exit 1
11+
fi
12+
13+
# This simple script allows to kill the whole process tree. The first argument is a signal and the second
14+
# is the root process
15+
parent=$2
16+
signal=$1
17+
18+
# A string which will be populated with descendant processes ids
19+
pids=""
20+
21+
# Kills the given process if it is running. It also checks whether the given parameter contains process ID
22+
function kill_if_running() {
23+
if [[ "$1" =~ [0-9]+ ]]; then
24+
ps -e -o pid= | grep $1 > /dev/null
25+
if [ $? -eq 0 ]; then
26+
kill "-$signal" "$1"
27+
return 0
28+
else
29+
return 1
30+
fi
31+
fi
32+
}
33+
34+
# Populates pids with descendant processes ids
35+
function get_descendants_pids() {
36+
for cpid in $(pgrep -P $1);
37+
do
38+
pids="$cpid $pids"
39+
get_descendants_pids $cpid
40+
done
41+
}
42+
43+
get_descendants_pids $parent
44+
45+
for pid in $pids
46+
do
47+
if [ "$signal" == "" ]; then
48+
echo "$pid"
49+
else
50+
kill_if_running $pid
51+
if [ $? -eq 0 ]; then
52+
# If the process has been really killed, then wait for 3 seconds before going to the next
53+
# process. In most cases, where there is a scripts execution hierarchy with Java process
54+
# at the bottom, it is sufficient to kill that Java process and the scripts will just exit
55+
# in a natural way
56+
echo "Sent $signal to $pid, sleeping for 3 seconds"
57+
sleep 3s
58+
fi
59+
fi
60+
done

bin/server_package.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,9 @@ fi
4444
FILES="job-server-extras/target/scala-$majorVersion/spark-job-server.jar
4545
bin/server_start.sh
4646
bin/server_stop.sh
47+
bin/kill-process-tree.sh
4748
$CONFIG_DIR/$ENV.conf
48-
config/log4j-server.properties"
49+
config/logback-server.xml"
4950

5051
rm -rf $WORK_DIR
5152
mkdir -p $WORK_DIR

bin/server_start.sh

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ set -e
66

77
get_abs_script_path() {
88
pushd . >/dev/null
9-
cd $(dirname $0)
9+
cd "$(dirname "$0")"
1010
appdir=$(pwd)
1111
popd >/dev/null
1212
}
@@ -26,14 +26,14 @@ JAVA_OPTS="-XX:MaxDirectMemorySize=512M
2626

2727
MAIN="spark.jobserver.JobServer"
2828

29-
conffile=$(ls -1 $appdir/*.conf | head -1)
29+
conffile="$(ls -1 "$appdir"/*.conf | head -1)"
3030
if [ -z "$conffile" ]; then
3131
echo "No configuration file found"
3232
exit 1
3333
fi
3434

3535
if [ -f "$appdir/settings.sh" ]; then
36-
. $appdir/settings.sh
36+
. "$appdir/settings.sh"
3737
else
3838
echo "Missing $appdir/settings.sh, exiting"
3939
exit 1
@@ -46,7 +46,7 @@ fi
4646

4747
pidFilePath=$appdir/$PIDFILE
4848

49-
if [ -f "$pidFilePath" ] && kill -0 $(cat "$pidFilePath"); then
49+
if [ -f "$pidFilePath" ] && kill -0 "$(cat "$pidFilePath")"; then
5050
echo 'Job server is already running'
5151
exit 1
5252
fi
@@ -57,8 +57,9 @@ if [ -z "$LOG_DIR" ]; then
5757
fi
5858
mkdir -p $LOG_DIR
5959

60-
LOGGING_OPTS="-Dlog4j.configuration=file:$appdir/log4j-server.properties
61-
-DLOG_DIR=$LOG_DIR"
60+
LOGGING_OPTS="-DLOG_DIR=$LOG_DIR"
61+
62+
export SPARK_SUBMIT_LOGBACK_CONF_FILE="$appdir/logback-server.xml"
6263

6364
# For Mesos
6465
CONFIG_OVERRIDES=""
@@ -78,8 +79,8 @@ fi
7879
export SPARK_HOME
7980

8081
# DSE_BIN is set in settings.sh
81-
$DSE_HOME/bin/dse spark-submit --class $MAIN --driver-memory 5G \
82+
"$DSE_HOME/bin/dse" spark-submit --class "$MAIN" --driver-memory 5G \
8283
--conf "spark.executor.extraJavaOptions=$LOGGING_OPTS" \
8384
--driver-java-options "$GC_OPTS $JAVA_OPTS $LOGGING_OPTS $CONFIG_OVERRIDES" \
84-
$@ $appdir/spark-job-server.jar $conffile 2>&1 &
85-
echo $! > $pidFilePath
85+
"$@" "$appdir/spark-job-server.jar" "$conffile" 2>&1 &
86+
echo "$!" > "$pidFilePath"

bin/server_stop.sh

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,30 @@
33

44
get_abs_script_path() {
55
pushd . >/dev/null
6-
cd $(dirname $0)
6+
cd "$(dirname "$0")"
77
appdir=$(pwd)
88
popd >/dev/null
99
}
1010

1111
get_abs_script_path
1212

1313
if [ -f "$appdir/settings.sh" ]; then
14-
. $appdir/settings.sh
14+
. "$appdir/settings.sh"
1515
else
1616
echo "Missing $appdir/settings.sh, exiting"
1717
exit 1
1818
fi
1919

2020
pidFilePath=$appdir/$PIDFILE
2121

22-
if [ ! -f "$pidFilePath" ] || ! kill -0 $(cat "$pidFilePath"); then
22+
if [ ! -f "$pidFilePath" ] || ! kill -0 "$(cat "$pidFilePath")"; then
2323
echo 'Job server not running'
2424
else
2525
echo 'Stopping job server...'
26-
PID=$(cat "$pidFilePath")
27-
kill -15 -- -$(ps -o pgid= $PID | grep -o [0-9]*) && rm -f "$pidFilePath"
26+
PID="$(cat "$pidFilePath")"
27+
"$(dirname "$0")"/kill-process-tree.sh -2 $PID
2828
echo '...job server stopped'
2929
fi
30+
31+
32+

job-server/config/dse.sh

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ APP_GROUP=cassandra
1010
#Package location for dse-env.sh to get environment variables
1111
if [ -z "$DSE_ENV" ]; then
1212
for include in "$HOME/.dse-env.sh" \
13-
"`dirname "$0"`/../../bin/dse-env.sh" \
13+
"`dirname "$0"`/../../../bin/dse-env.sh" \
1414
"/etc/dse/dse-env.sh"; do
1515
if [ -r "$include" ]; then
1616
DSE_ENV="$include"
@@ -31,15 +31,15 @@ elif [ -z "$ENV" ]; then
3131
exit 1
3232
fi
3333

34-
SPARK_VERSION=1.3.1
34+
SPARK_VERSION=1.3.1.0 #Last digit is DSE Specific
3535

3636
DEPLOY_HOSTS="localhost"
3737

38-
INSTALL_DIR=$DSE_COMPONENTS_ROOT/spark-jobserver
39-
LOG_DIR=/var/log/job-server
38+
INSTALL_DIR="$DSE_COMPONENTS_ROOT/spark/spark-jobserver"
39+
LOG_DIR=/var/log/spark/job-server
4040

4141
PIDFILE=spark-jobserver.pid
4242

43-
SPARK_CONF_DIR=$SPARK_HOME/conf
43+
SPARK_CONF_DIR=${SPARK_CONF_DIR:-"$SPARK_HOME/conf"}
4444

45-
SCALA_VERSION=2.10.4 # or 2.11.6
45+
SCALA_VERSION=2.10.5 # or 2.11.6

version.sbt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
version in ThisBuild := "0.5.2-DSP-5892-SNAPSHOT"
1+
version in ThisBuild := "0.5.2.DSP-5892-2-SNAPSHOT"

0 commit comments

Comments
 (0)