Skip to content

Commit 6ef0017

Browse files
committed
Merge pull request #49 from basho/jem-pidfile
Create .pid files for package builds
2 parents 54bbda3 + 8989999 commit 6ef0017

File tree

2 files changed

+143
-7
lines changed

2 files changed

+143
-7
lines changed

priv/base/env.sh

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@ PIPE_DIR={{pipe_dir}}
2727
RUNNER_USER={{runner_user}}
2828
APP_VERSION={{app_version}}
2929

30+
# Variables needed to support creation of .pid files
31+
# PID directory and pid file name of this app
32+
# ex: /var/run/riak & /var/run/riak/riak.pid
33+
RUN_DIR="/var/run" # for now hard coded unless we find a platform that differs
34+
PID_DIR=$RUN_DIR/$RUNNER_SCRIPT
35+
PID_FILE=$PID_DIR/$RUNNER_SCRIPT.pid
36+
3037
# Threshold where users will be warned of low ulimit file settings
3138
# default it if it is not set
3239
ULIMIT_WARN={{runner_ulimit_warn}}
@@ -88,11 +95,71 @@ ping_node() {
8895
$NODETOOL ping < /dev/null
8996
}
9097

98+
# Attempts to create a pid directory like /var/run/APPNAME and then
99+
# changes the permissions on that directory so the $RUNNER_USER can
100+
# read/write/delete .pid files during startup/shutdown
101+
create_pid_dir() {
102+
# Validate RUNNER_USER is set and they have permissions to write to /var/run
103+
# Don't continue if we've already sudo'd to RUNNER_USER
104+
if ([ "$RUNNER_USER" ] && [ "x$WHOAMI" != "x$RUNNER_USER" ]); then
105+
if [ -w $RUN_DIR ]; then
106+
mkdir -p $PID_DIR
107+
ES=$?
108+
if [ "$ES" -ne 0 ]; then
109+
return 1
110+
else
111+
# Change permissions on $PID_DIR
112+
chown $RUNNER_USER $PID_DIR
113+
ES=$?
114+
if [ "$ES" -ne 0 ]; then
115+
return 1
116+
else
117+
return 0
118+
fi
119+
fi
120+
else
121+
# If we don't have permissions, fail
122+
return 1
123+
fi
124+
fi
125+
126+
# If RUNNER_USER is not set this is probably a test setup (devrel) and does
127+
# not need a .pid file, so do not return error
128+
return 0
129+
}
130+
131+
# Attempt to create a pid file for the process
132+
# This function assumes the process is already up and running and can
133+
# respond to a getpid call. It also assumes that two processes
134+
# with the same name will not be run on the machine
135+
# Do not print any error messages as failure to create a pid file because
136+
# pid files are strictly optional
137+
# This function should really only be called in a "start" function
138+
# you have been warned
139+
create_pid_file() {
140+
# Validate a pid directory even exists
141+
if [ -w $PID_DIR ]; then
142+
# Grab the proper pid from getpid
143+
get_pid
144+
ES=$?
145+
if [ "$ES" -ne 0 ]; then
146+
return $ES
147+
else
148+
# Remove pid file if it already exists since we do not
149+
# plan for multiple identical runners on a single machine
150+
rm -f $PID_FILE
151+
echo $PID > $PID_FILE
152+
return 0
153+
fi
154+
else
155+
return 1
156+
fi
157+
}
158+
91159
# Function to su into correct user
92160
check_user() {
93161
# Validate that the user running the script is the owner of the
94162
# RUN_DIR.
95-
96163
if ([ "$RUNNER_USER" ] && [ "x$WHOAMI" != "x$RUNNER_USER" ]); then
97164
type sudo > /dev/null 2>&1
98165
if [ "$?" -ne 0 ]; then

priv/base/runner

Lines changed: 75 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,6 @@
77
# Pull environment for this install
88
. "{{runner_base_dir}}/lib/env.sh"
99

10-
# Make sure the user running this script is the owner and/or su to that user
11-
check_user $@
12-
ES=$?
13-
if [ "$ES" -ne 0 ]; then
14-
exit $ES
15-
fi
1610

1711
# Keep track of where script was invoked
1812
ORIGINAL_DIR=$(pwd)
@@ -29,10 +23,38 @@ usage() {
2923
echo " getpid | top [-interval N] [-sort reductions|memory|msg_q] [-lines N] }"
3024
}
3125

26+
# All commands must either call bootstrap or bootstrapd
27+
# Call bootstrap for non-daemon commands like ping or chkconfig
28+
# Call bootstrapd for daemon commands like start/stop/console
29+
bootstrap() {
30+
# Make sure the user running this script is the owner and/or su to that user
31+
check_user $@
32+
ES=$?
33+
if [ "$ES" -ne 0 ]; then
34+
exit $ES
35+
fi
36+
}
37+
38+
bootstrapd() {
39+
# Create PID directory if it does not exist before dropping permissiongs
40+
# to the runner user
41+
create_pid_dir
42+
ES=$?
43+
if [ "$ES" -ne 0 ]; then
44+
echoerr "Unable to access $PID_DIR, permission denied, run script as root"
45+
exit 1
46+
fi
47+
48+
# Now call bootstrap to drop to $RUNNER_USER
49+
bootstrap $@
50+
}
3251

3352
# Check the first argument for instructions
3453
case "$1" in
3554
start)
55+
# Bootstrap daemon command (check perms & drop to $RUNNER_USER)
56+
bootstrapd $@
57+
3658
# Make sure there is not already a node running
3759
node_down_check
3860

@@ -75,6 +97,8 @@ case "$1" in
7597
fi
7698
PROCESS=`$NODETOOL rpcterms erlang whereis "'${WAIT_FOR_PROCESS}'."`
7799
if [ "$PROCESS" != "undefined" ]; then
100+
# Attempt to create a .pid file for the process
101+
create_pid_file
78102
exit 0
79103
fi
80104
done
@@ -84,9 +108,15 @@ case "$1" in
84108
echo "WAIT_FOR_ERLANG to the number of seconds to wait."
85109
exit 1
86110
fi
111+
112+
# Attempt to create .pid file
113+
create_pid_file
87114
;;
88115

89116
stop)
117+
# Bootstrap daemon command (check perms & drop to $RUNNER_USER)
118+
bootstrapd $@
119+
90120
get_pid
91121
ES=$?
92122
if [ "$ES" -ne 0 ] || [ -z $PID ]; then
@@ -103,9 +133,15 @@ case "$1" in
103133
do
104134
sleep 1
105135
done
136+
137+
# remove pid file
138+
rm -f $PID_FILE
106139
;;
107140

108141
restart)
142+
# Bootstrap daemon command (check perms & drop to $RUNNER_USER)
143+
bootstrapd $@
144+
109145
## Restart the VM without exiting the process
110146
$NODETOOL restart
111147
ES=$?
@@ -115,6 +151,9 @@ case "$1" in
115151
;;
116152

117153
reboot)
154+
# Bootstrap daemon command (check perms & drop to $RUNNER_USER)
155+
bootstrapd $@
156+
118157
## Restart the VM completely (uses heart to restart it)
119158
$NODETOOL reboot
120159
ES=$?
@@ -124,6 +163,9 @@ case "$1" in
124163
;;
125164

126165
ping)
166+
# Bootstrap command (simply drop to $RUNNER_USER)
167+
bootstrap $@
168+
127169
## See if the VM is alive
128170
ping_node
129171
ES=$?
@@ -133,6 +175,9 @@ case "$1" in
133175
;;
134176

135177
attach-direct)
178+
# Bootstrap daemon command (check perms & drop to $RUNNER_USER)
179+
bootstrapd $@
180+
136181
# Allow attaching to a node without pinging it
137182
if [ "$2" = "-f" ]; then
138183
echo "Forcing connection..."
@@ -146,6 +191,9 @@ case "$1" in
146191
;;
147192

148193
attach)
194+
# Bootstrap daemon command (check perms & drop to $RUNNER_USER)
195+
bootstrapd $@
196+
149197
# Make sure a node is running
150198
node_up_check
151199

@@ -155,6 +203,9 @@ case "$1" in
155203
;;
156204

157205
console)
206+
# Bootstrap daemon command (check perms & drop to $RUNNER_USER)
207+
bootstrapd $@
208+
158209
RES=`ping_node`
159210
if [ "$?" -eq 0 ]; then
160211
echo "Node is already running - use '$SCRIPT attach' instead"
@@ -198,7 +249,11 @@ case "$1" in
198249
# Start the VM
199250
exec $CMD
200251
;;
252+
201253
top)
254+
# Bootstrap command (simply drop to $RUNNER_USER)
255+
bootstrap $@
256+
202257
# Make sure the local node IS running
203258
node_up_check
204259

@@ -212,20 +267,34 @@ case "$1" in
212267
-node $NODE_NAME \
213268
$* -tracing off
214269
;;
270+
215271
ertspath)
216272
echo $ERTS_PATH
217273
;;
274+
218275
chkconfig)
276+
# Bootstrap command (simply drop to $RUNNER_USER)
277+
bootstrap $@
278+
219279
check_config
220280
;;
281+
221282
escript)
283+
# Bootstrap command (simply drop to $RUNNER_USER)
284+
bootstrap $@
285+
222286
shift
223287
$ERTS_PATH/escript "$@"
224288
;;
289+
225290
version)
226291
echo $APP_VERSION
227292
;;
293+
228294
getpid)
295+
# Bootstrap command (simply drop to $RUNNER_USER)
296+
bootstrap $@
297+
229298
# Get the PID from nodetool
230299
get_pid
231300
ES=$?

0 commit comments

Comments
 (0)