forked from railsware/passenger-initscript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpassenger
265 lines (207 loc) · 6.1 KB
/
passenger
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
261
262
263
264
265
#!/bin/bash
### BEGIN INIT INFO
# Provides: passenger
# Required-Start: $remote_fs $network $syslog
# Required-Stop: $remote_fs $network $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: manage multiple passenger instances
### END INIT INFO
RVM_SHELL="rvm-shell"
[ -x /usr/local/bin/rvm-shell ] && RVM_SHELL="/usr/local/bin/rvm-shell"
[ -x /usr/local/rvm/bin/rvm-shell ] && RVM_SHELL="/usr/local/rvm/bin/rvm-shell"
RVM_PATH="/usr/local/rvm"
PASSENGER_SYSTEM_BIN="passenger"
PASSENGER_RVM_BIN="passenger"
CONFIG_DIR="/etc/passenger.d"
CONFIG_FILE_EXT=".yml"
CONFIG_SHELL_OPTIONS=(
rvm
cwd
)
CONFIG_PASSENGER_OPTIONS=(
address
port
socket
environment
max-pool-size
min-instances
spawn-method
ping-port
user
log-file
pid-file
nginx-bin
nginx-version
)
CONFIG_OPTIONS=( "${CONFIG_SHELL_OPTIONS[@]}" "${CONFIG_PASSENGER_OPTIONS[@]}")
##############################################################################
get_config_option() {
name="config_${1//-/_}"
_RETURN="${!name}"
}
set_config_option() {
eval config_${1//-/_}='${2}'
}
init_config_options() {
local config_file="$CONFIG_DIR/${1}"
[ ! -f "$config_file" ] && echo "error: config file does not exist: $config_file" && return 1
for option in ${CONFIG_OPTIONS[@]}; do set_config_option $option ''; done
while read LINE; do
LINE=${LINE// /}
name=${LINE%%:*}
value=${LINE##*:}
for option in ${CONFIG_OPTIONS[@]}; do
[ "$option" = "$name" ] && set_config_option $name $value && break
done
done < "$config_file"
return 0
}
init_config_files() {
[ ! -d $CONFIG_DIR ] && echo "error: directory does not exist: $CONFIG_DIR" && exit 1
if [ $# -eq 0 ]; then
CONFIG_FILES=`cd ${CONFIG_DIR} && ls *${CONFIG_FILE_EXT}`
else
for app_name in "$@"; do
CONFIG_FILES="$CONFIG_FILES ${app_name}${CONFIG_FILE_EXT}"
done
fi
}
validate_config_options() {
local command=$1
local errors=()
[ -z "$config_cwd" ] && errors=( "${errors[@]}" "cwd option is required" )
case "$command" in
start)
[ -z "$config_user" ] && errors=( "${errors[@]}" "user option is required" )
[ -z "$config_environment" ] && errors=( "${errors[@]}" "environment option is required'" )
;;
stop|status)
[ -z "$config_pid_file" ] && errors=( "${errors[@]}" "pid-file option is required" )
;;
restart)
[ -z "$config_user" ] && errors=( "${errors[@]}" "user option is required" )
[ -z "$config_environment" ] && errors=( "${errors[@]}" "environment option is required'" )
[ -z "$config_pid_file" ] && errors=( "${errors[@]}" "pid-file option is required" )
;;
esac
local size=${#errors[*]}
for (( i=0; i<$size; i++ )); do
echo "config error: ${errors[$i]}"
done
return $size
}
get_passenger_options() {
local command=$1
local option=""
case $command in
start)
options="$config_cwd --daemonize"
for option in ${CONFIG_PASSENGER_OPTIONS[@]}; do
get_config_option $option
[ -n "$_RETURN" ] && options="$options --${option} ${_RETURN}"
done
;;
stop|status)
options="--pid-file $config_pid_file"
;;
esac
_RETURN="$options"
}
passenger_ctl() {
local command=$1
get_passenger_options $command
local options="$_RETURN"
if [ -z "$config_rvm" ]; then
argv="$PASSENGER_SYSTEM_BIN $command $options"
else
###################################################################
##### MODIFICACION DEL SCRIPT PARA QUE FUNCIONE CON BUNDLE ########
########### se añade cd $config_cwd && bundle exec ################
###################################################################
argv="rvm_path=$RVM_PATH $RVM_SHELL $config_rvm -c 'cd $config_cwd && bundle exec $PASSENGER_RVM_BIN $command $options'"
if [ "$UID" -eq 0 ]; then
argv="runuser -l $config_user -c \"$argv\""
fi
fi
[ -n "$DEBUG" ] && echo $argv
eval $argv
}
passengers_ctl() {
local command=$1; shift
init_config_files "$@"
for config_file in ${CONFIG_FILES[@]}; do
echo "*** passenger $command ${config_file//$CONFIG_FILE_EXT}"
init_config_options "$config_file"
[ $? -eq 0 ] || continue
validate_config_options $command
[ $? -eq 0 ] || return 1
case $command in
reload|upgrade)
argv="touch $config_cwd/tmp/restart.txt"
[ -n "$DEBUG" ] && echo $argv
$argv
;;
restart)
passenger_ctl "stop"
passenger_ctl "start"
;;
start|stop|status)
passenger_ctl $command
;;
*)
echo "Unknown command ${command}"
;;
esac
done
return 0
}
enable_application() {
old="$1$CONFIG_FILE_EXT.disabled"
new="$1$CONFIG_FILE_EXT"
mv "$CONFIG_DIR/$old" "$CONFIG_DIR/$new"
}
disable_application() {
old="$1$CONFIG_FILE_EXT"
new="$1$CONFIG_FILE_EXT.disabled"
mv "$CONFIG_DIR/$old" "$CONFIG_DIR/$new"
}
setup() {
echo "Creating config directory: $CONFIG_DIR"
[ -d "$CONFIG_DIR" ] || mkdir "$CONFIG_DIR"
local config_file="${CONFIG_DIR}/example${CONFIG_FILE_EXT}.disabled"
echo "Creating config example: $config_file"
data=""
data+="rvm: ruby-1.9.2@example\n"
data+="cwd: /var/apps/blog/current\n"
data+="user: deploy\n"
data+="port: 8080\n"
data+="environment: production\n"
data+="max-pool-size: 4\n"
data+="min-instances: 1\n"
data+="pid-file: /var/apps/blog/current/tmp/pids/passenger.pid\n"
data+="log-file: /var/apps/blog/current/log/passenger.log"
echo -e "$data" > "$config_file"
}
##############################################################################
###################################################################
##### MODIFICACION DEL SCRIPT PARA QUE FUNCIONE SIN ROOT ########
########### se comenta la linea, con root no va bien ##############
###################################################################
#[ "$UID" -ne 0 ] && echo "You should run this script as a root " && exit 1
case "$1" in
start|stop|status|restart|reload|upgrade)
passengers_ctl "$@"
;;
enable|disable)
$1_application "$2"
;;
setup)
setup
;;
*)
echo "Usage: $0 {start|stop|status|restart|reload|enable|disable|setup} [app_name]"
exit 1
;;
esac
exit 0