-
Notifications
You must be signed in to change notification settings - Fork 11
/
trinidad.erb
260 lines (231 loc) · 7.17 KB
/
trinidad.erb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
#!/usr/bin/env bash
#
# trinidad init.d script using `jsvc` customized from the jsvc-wrapper.sh
# http://github.com/nicobrevin/jruby-jsvc/blob/master/bin/jsvc-wrapper.sh
#
# (c) generated by trinidad_init_services gem on <%= Time.now %>
#
### BEGIN INIT INFO
# Provides: <%= @service_id || 'trinidad' %>
# Required-Start: $network $syslog
# Required-Stop: $network $syslog
# Default-Start:
# Default-Stop:
# Description: <%= @service_desc %>
# Short-Description: start and stop trinidad
### END INIT INFO
NAME="<%= @service_name %>"
DESC="<%= @service_desc %>"
# https://github.com/trinidad/trinidad_init_services/wiki/Installing-JSVC
JSVC=<%= @jsvc %>
JAVA_HOME="<%= @java_home %>"
JRUBY_HOME="<%= @jruby_home %>"
BASE_PATH="<%= @app_path || @base_path %>"
RUBY_SCRIPT="<%= @trinidad_daemon_path %>"
# The user rights for the running daemon, if you don't bind directly to 80 than
# it's always a good idea to run trinidad with non-root user rights.
# Make sure this user has rights on PID_FILE and OUT_FILE paths.
# Leaving this empty will cause the daemon to always run as whichever user calls
# the script (which during init is equivalent to running it as root).
RUN_USER="<%= @run_user %>"
# Set this to "1" to echo commands before running them (for troubleshooting) :
ECHO_COMMAND=""
# options that Trinidad needs to run your application, e.g. "-e production"
TRINIDAD_OPTS="<%= @trinidad_opts.is_a?(String) ? @trinidad_opts : @trinidad_opts.join(' ') %>"
# a custom (daemon) script will be started (not jruby -S) thus JRUBY_OPTS
# won't be picked up, however one can hand tune JAVA_OPTS here instead ...
<% if @java_opts.empty? -%>
JAVA_OPTS="$JAVA_OPTS"
<% else -%>
JAVA_OPTS="<%= @java_opts.is_a?(String) ? @java_opts : @java_opts.join(" \\\n") %>"
<% end -%>
PID_FILE="<%= @pid_file %>"
OUT_FILE="<%= @out_file %>"
# Create pidfile directory if missing
# NOTE: Remove PID_FILE_DIR creation once jsvc can be started
# as root using the -user option without any issues.
PID_FILE_DIR=$(dirname $PID_FILE)
if [ ! -d "$PID_FILE_DIR" ] ; then
mkdir -p $PID_FILE_DIR
if [ ! -z "$RUN_USER" ] ; then
chown -R $RUN_USER $PID_FILE_DIR
fi
fi
# Implements the jsvc Daemon interface.
MAIN_CLASS=com.msp.jsvc.JRubyDaemon
CLASSPATH=<%= @classpath.join(":") %>
if [ ! -z "$BASE_PATH" ] ; then
if [ ! -d "$BASE_PATH" ]; then
echo "'$BASE_PATH' is not a directory"
exit 1
fi
fi
if [ -z "$JRUBY_SHELL" ] ; then
JRUBY_SHELL=/bin/sh
fi
JRUBY_NATIVE_PATH=""
if [ -d "$JRUBY_HOME/lib/native/" ]; then
for d in $JRUBY_HOME/lib/native/*`uname -s`; do
if [ -z "$JRUBY_NATIVE_PATH" ]; then
JRUBY_NATIVE_PATH="$d"
else
JRUBY_NATIVE_PATH="$JRUBY_NATIVE_PATH:$d"
fi
done
fi
# JAVA_MEM, JAVA_MEM_MIN & JAVA_STACK compatible with JRuby's binary :
if [ -z "$JAVA_MEM" ] ; then
JAVA_MEM="-Xmx500m"
fi
# backwards compatibility we accept the non-full option (e.g. JAVA_MEM=500m)
if [ "${JAVA_MEM:0:4}" != "-Xmx" ] ; then
JAVA_MEM="-Xmx$JAVA_MEM"
fi
# we accept the non-full option (e.g. JAVA_MEM_MIN=500m)
if [ -n "$JAVA_MEM_MIN" ] ; then
if [ "${JAVA_MEM_MIN:0:4}" != "-Xms" ] ; then
JAVA_MEM_MIN="-Xms$JAVA_MEM_MIN"
fi
else
if [ -z "$JAVA_MEM_MIN" ] ; then
JAVA_MEM_MIN=""
fi
fi
if [ -z "$JAVA_STACK" ] ; then
JAVA_STACK="-Xss1536k"
fi
# backwards compatibility we accept the non-full option (e.g. JAVA_STACK=2048k)
if [ "${JAVA_STACK:0:4}" != "-Xss" ] ; then
JAVA_STACK="-Xss$JAVA_STACK"
fi
if [ -z "$JAVA_OPTS" ] ; then
JAVA_OPTS="$JAVA_MEM $JAVA_MEM_MIN $JAVA_STACK"
else
for opt in Xmx Xss; do
if case ${JAVA_OPTS} in *"-${opt}"*) true;; *) false;; esac ; then
: # JAVA_OPTS contains option - do no-thing
else
if [ "${opt}" == "Xmx" ] ; then
JAVA_OPTS="$JAVA_OPTS $JAVA_MEM $JAVA_MEM_MIN"
fi
if [ "${opt}" == "Xss" ] ; then
JAVA_OPTS="$JAVA_OPTS $JAVA_STACK"
fi
fi
done
fi
JAVA_OPTS="$JAVA_OPTS -Xbootclasspath/a:$JRUBY_HOME/lib/jruby.jar"
# force file.encoding to UTF-8 (if JAVA_ENCODING specified) :
if [ -z "$JAVA_ENCODING" ]; then
JAVA_OPTS="$JAVA_OPTS -Dfile.encoding=UTF-8"
fi
JAVA_PROPS="$JAVA_PROPS \
-Djruby.shell=$JRUBY_SHELL \
-Djffi.boot.library.path=$JRUBY_NATIVE_PATH \
<%= @jruby_opts.join(' ') %> "
PROC_NAME=${SCRIPT_NAME:-${APP_NAME:-"trinidad"}}
# should be multiple of 10 (min=10) :
if [ -z "$JSVC_WAIT" ] ; then
JSVC_WAIT="20"
fi
JSVC_ARGS="-home $JAVA_HOME \
$JSVC_ARGS_EXTRA \
-wait $JSVC_WAIT \
-pidfile $PID_FILE \
-procname jsvc-$PROC_NAME \
-server"
if [ -z "$JSVC_CWD" ] ; then
JSVC_CWD="$BASE_PATH"
fi
# NOTE: jsvc -cwd does not handle quoted (' or ") paths
if [ "$JSVC_CWD" != "false" ] ; then
JSVC_ARGS="$JSVC_ARGS -cwd $JSVC_CWD"
else
if [ ! -z "$BASE_PATH" ] ; then
cd "$BASE_PATH" || exit 1
fi
fi
if [ ! -z "$JSVC_STDIN" ] ; then
if [ "$JSVC_STDIN" != "false" ] ; then
JSVC_ARGS="$JSVC_ARGS -keepstdin"
fi
fi
JSVC_ARGS="$JSVC_ARGS -outfile $OUT_FILE -errfile &1"
#
# Stop/Start
#
if [ -n "$RUN_USER" ]; then
if [ $EUID -eq 0 ]; then
JSVC="sudo -u $RUN_USER $JSVC"
fi
fi
STOP_COMMAND="$JSVC $JSVC_ARGS -stop $MAIN_CLASS"
START_COMMAND="$JSVC $JSVC_ARGS -cp $CLASSPATH $JAVA_PROPS $JAVA_OPTS $MAIN_CLASS $RUBY_SCRIPT $TRINIDAD_OPTS"
case "$1" in
start)
if [ -e "$PID_FILE" ]; then
CUR_PID=$(<"$PID_FILE")
COM_NAME=$(ps -p $CUR_PID -o command= | cut -d ' ' -f 1)
PROCESS_NAME="jsvc-$PROC_NAME"
if [ "$COM_NAME" != "$PROCESS_NAME" ]; then
rm $PID_FILE
$START_COMMAND
else
echo "Pidfile already exists for pid $CUR_PID, not starting."
exit 1
fi
else
echo "Starting $PROC_NAME daemon..."
if [ -n "$ECHO_COMMAND" ]; then echo $START_COMMAND; fi
$START_COMMAND
EXIT_CODE=$?
if [ "$EXIT_CODE" != 0 ]; then
echo "Daemon exited with status: $EXIT_CODE. Check pidfile and log"
fi
fi
;;
stop)
if [ -e "$PID_FILE" ]; then
echo "Stopping $PROC_NAME daemon..."
if [ -n "$ECHO_COMMAND" ]; then echo $STOP_COMMAND; fi
$STOP_COMMAND
else
echo "No pid file, not stopping."
exit 1
fi
;;
restart)
if [ -e "$PID_FILE" ]; then
echo "Stopping $PROC_NAME daemon..."
if [ -n "$ECHO_COMMAND" ]; then echo $STOP_COMMAND; fi
$STOP_COMMAND
fi
if [ -e "$PID_FILE" ]; then
echo "Pidfile still present, $PROC_NAME hasn't stopped"
exit 1
else
if [ -n "$ECHO_COMMAND" ]; then echo $START_COMMAND; fi
$START_COMMAND
EXIT_CODE=$?
if [ "$EXIT_CODE" != 0 ]; then
echo "Daemon exited with status: $EXIT_CODE. Check pidfile and log"
fi
fi
;;
status)
if [ "$PID_FILE" ]; then
PID=`cat $PID_FILE`
OUTPUT=`ps -o pid $PID | grep "$PID"`
if [ ${#OUTPUT} -gt 0 ]; then
echo "Service running with pid: $PID"
else
echo "Pidfile present, but process not running"
fi
else
echo "No pidfile present"
fi
;;
*)
echo "Unrecognised command. Usage trinidad [ start | stop | restart ]"
;;
esac