xref: /5.5.2/couchdb/bin/couchdb.tpl.in (revision 5e803151)
1#! /bin/sh -e
2
3# Licensed under the Apache License, Version 2.0 (the "License"); you may not
4# use this file except in compliance with the License. You may obtain a copy of
5# the License at
6#
7#   http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12# License for the specific language governing permissions and limitations under
13# the License.
14
15BACKGROUND=false
16DEFAULT_CONFIG_DIR=%localconfdir%/default.d
17DEFAULT_CONFIG_FILE=%localconfdir%/%defaultini%
18HEART_BEAT_TIMEOUT=11
19HEART_COMMAND="%bindir%/%couchdb_command_name% -k"
20INTERACTIVE=false
21KILL=false
22LOCAL_CONFIG_DIR=%localconfdir%/local.d
23LOCAL_CONFIG_FILE=%localconfdir%/%localini%
24PID_FILE=%localstatedir%/run/couchdb/couchdb.pid
25RECURSED=false
26RESET_CONFIG=true
27RESPAWN_TIMEOUT=0
28SCRIPT_ERROR=1
29SCRIPT_OK=0
30SHUTDOWN=false
31STDERR_FILE=couchdb.stderr
32STDOUT_FILE=couchdb.stdout
33
34print_arguments=""
35start_arguments=""
36background_start_arguments=""
37
38basename=`basename $0`
39
40display_version () {
41    cat << EOF
42$basename - %package_name% %version%
43
44Licensed under the Apache License, Version 2.0 (the "License"); you may not use
45this file except in compliance with the License. You may obtain a copy of the
46License at
47
48  http://www.apache.org/licenses/LICENSE-2.0
49
50Unless required by applicable law or agreed to in writing, software distributed
51under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
52CONDITIONS OF ANY KIND, either express or implied. See the License for the
53specific language governing permissions and limitations under the License.
54
55EOF
56}
57
58display_help () {
59    cat << EOF
60Usage: $basename [OPTION]
61
62The $basename command runs the %package_name% server.
63
64Erlang is called with:
65
66    -sasl errlog_type error +K true
67
68Erlang inherits the environment of this command.
69
70You can override any of the flags using the environment:
71
72    ERL_AFLAGS, ERL_FLAGS, ERL_ZFLAGS
73
74See erl(1) for more information about the environment variables.
75
76The exit status is 0 for success or 1 for failure.
77
78The \`-s' option will exit 0 for running and 1 for not running.
79
80Options:
81
82  -h          display a short help message and exit
83  -V          display version information and exit
84  -a FILE     add configuration FILE to chain
85  -A DIR      add configuration DIR to chain
86  -n          reset configuration file chain (including system default)
87  -c          print configuration file chain and exit
88  -i          use the interactive Erlang shell
89  -b          spawn as a background process
90  -p FILE     set the background PID FILE (overrides system default)
91  -r SECONDS  respawn background process after SECONDS (defaults to no respawn)
92  -o FILE     redirect background stdout to FILE (defaults to $STDOUT_FILE)
93  -e FILE     redirect background stderr to FILE (defaults to $STDERR_FILE)
94  -s          display the status of the background process
95  -k          kill the background process, will respawn if needed
96  -d          shutdown the background process
97
98Report bugs at <%bug_uri%>.
99EOF
100}
101
102display_error () {
103    if test -n "$1"; then
104        echo $1 >&2
105    fi
106    echo >&2
107    echo "Try \`"$basename" -h' for more information." >&2
108    false
109}
110
111_get_pid () {
112    if test -f $PID_FILE; then
113        PID=`cat $PID_FILE`
114    fi
115    echo $PID
116}
117
118_add_config_file () {
119    if test -z "$print_arguments"; then
120        print_arguments="$1"
121    else
122        print_arguments="`cat <<EOF
123$print_arguments
124$1
125EOF
126`"
127    fi
128    start_arguments="$start_arguments $1"
129    background_start_arguments="$background_start_arguments -a $1"
130}
131
132_add_config_dir () {
133    for file in "$1"/*.ini; do
134        if [ -r "$file" ]; then
135          _add_config_file "$file"
136        fi
137    done
138}
139
140_load_config () {
141    _add_config_file "$DEFAULT_CONFIG_FILE"
142    _add_config_dir "$DEFAULT_CONFIG_DIR"
143    _add_config_file "$LOCAL_CONFIG_FILE"
144    _add_config_dir "$LOCAL_CONFIG_DIR"
145}
146
147_reset_config () {
148    print_arguments=""
149    start_arguments=""
150    background_start_arguments="-n"
151}
152
153_print_config () {
154    cat <<EOF
155$print_arguments
156EOF
157}
158
159check_status () {
160    PID=`_get_pid`
161    if test -n "$PID"; then
162        if kill -0 $PID 2> /dev/null; then
163            echo "Apache CouchDB is running as process $PID, time to relax."
164            return $SCRIPT_OK
165        else
166            echo >&2 << EOF
167Apache CouchDB is not running but a stale PID file exists: $PID_FILE"
168EOF
169        fi
170    else
171        echo "Apache CouchDB is not running." >&2
172    fi
173    return $SCRIPT_ERROR
174}
175
176check_environment () {
177    if test "$BACKGROUND" != "true"; then
178        return
179    fi
180    touch $PID_FILE 2> /dev/null || true
181    touch $STDOUT_FILE 2> /dev/null || true
182    touch $STDERR_FILE 2> /dev/null || true
183    message_prefix="Apache CouchDB needs write permission on the"
184    if test ! -w $PID_FILE; then
185        echo "$message_prefix PID file: $PID_FILE" >&2
186        false
187    fi
188    if test ! -w $STDOUT_FILE; then
189        echo "$message_prefix STDOUT file: $STDOUT_FILE" >&2
190        false
191    fi
192    if test ! -w $STDERR_FILE; then
193        echo "$message_prefix STDERR file: $STDERR_FILE" >&2
194        false
195    fi
196    message_prefix="Apache CouchDB needs a regular"
197    if test `echo 2> /dev/null >> $PID_FILE; echo $?` -gt 0; then
198        echo "$message_prefix PID file: $PID_FILE" >&2
199        false
200    fi
201    if test `echo 2> /dev/null >> $STDOUT_FILE; echo $?` -gt 0; then
202        echo "$message_prefix STDOUT file: $STDOUT_FILE" >&2
203        false
204    fi
205    if test `echo 2> /dev/null >> $STDERR_FILE; echo $?` -gt 0; then
206        echo "$message_prefix STDERR file: $STDERR_FILE" >&2
207        false
208    fi
209}
210
211start_couchdb () {
212    if test ! "$RECURSED" = "true"; then
213        if check_status 2> /dev/null; then
214            exit
215        fi
216        check_environment
217    fi
218    interactive_option="+Bd -noinput"
219    if test "$INTERACTIVE" = "true"; then
220        interactive_option=""
221    fi
222    if test "$BACKGROUND" = "true"; then
223        touch $PID_FILE
224        interactive_option="+Bd -noinput"
225    fi
226    command="%ERL% $interactive_option -sasl errlog_type error +K true \
227        -env ERL_LIBS %localerlanglibdir% -couch_ini $start_arguments -s couch"
228    if test "$BACKGROUND" = "true" -a "$RECURSED" = "false"; then
229        $0 $background_start_arguments -b -r $RESPAWN_TIMEOUT -p $PID_FILE \
230            -o $STDOUT_FILE -e $STDERR_FILE -R &
231        echo "Apache CouchDB has started, time to relax."
232    else
233        if test "$RECURSED" = "true"; then
234            while true; do
235                export HEART_COMMAND
236                export HEART_BEAT_TIMEOUT
237                `eval $command -pidfile $PID_FILE -heart \
238                    >> $STDOUT_FILE 2>> $STDERR_FILE` || true
239                PID=`_get_pid`
240                if test -n "$PID"; then
241                    if kill -0 $PID 2> /dev/null; then
242                        return $SCRIPT_ERROR
243                    fi
244                else
245                    return $SCRIPT_OK
246                fi
247                if test "$RESPAWN_TIMEOUT" = "0"; then
248                    return $SCRIPT_OK
249                fi
250                if test "$RESPAWN_TIMEOUT" != "1"; then
251                    plural_ending="s"
252                fi
253                cat << EOF
254Apache CouchDB crashed, restarting in $RESPAWN_TIMEOUT second$plural_ending.
255EOF
256                sleep $RESPAWN_TIMEOUT
257            done
258        else
259            eval exec $command
260        fi
261    fi
262}
263
264stop_couchdb () {
265    PID=`_get_pid`
266    if test -n "$PID"; then
267        if test "$1" = "false"; then
268            echo > $PID_FILE
269        fi
270        if kill -0 $PID 2> /dev/null; then
271            if kill -1 $PID 2> /dev/null; then
272                if test "$1" = "false"; then
273                    echo "Apache CouchDB has been shutdown."
274                else
275                    echo "Apache CouchDB has been killed."
276                fi
277                return $SCRIPT_OK
278            else
279                echo "Apache CouchDB could not be killed." >&2
280                return $SCRIPT_ERROR
281            fi
282            if test "$1" = "false"; then
283                echo "Stale PID file exists but Apache CouchDB is not running."
284            else
285                echo "Stale PID file existed but Apache CouchDB is not running."
286            fi
287        fi
288    else
289        echo "Apache CouchDB is not running."
290    fi
291}
292
293parse_script_option_list () {
294    _load_config
295    set +e
296    options=`getopt hVa:A:ncibp:r:Ro:e:skd $@`
297    if test ! $? -eq 0; then
298        display_error
299    fi
300    set -e
301    eval set -- $options
302    while [ $# -gt 0 ]; do
303        case "$1" in
304            -h) shift; display_help; exit;;
305            -V) shift; display_version; exit;;
306            -a) shift; _add_config_file "$1"; shift;;
307            -A) shift; _add_config_dir "$1"; shift;;
308            -n) shift; _reset_config;;
309            -c) shift; _print_config; exit;;
310            -i) shift; INTERACTIVE=true;;
311            -b) shift; BACKGROUND=true;;
312            -r) shift; RESPAWN_TIMEOUT=$1; shift;;
313            -R) shift; RECURSED=true;;
314            -p) shift; PID_FILE=$1; shift;;
315            -o) shift; STDOUT_FILE=$1; shift;;
316            -e) shift; STDERR_FILE=$1; shift;;
317            -s) shift; check_status; exit;;
318            -k) shift; KILL=true;;
319            -d) shift; SHUTDOWN=true;;
320            --) shift; break;;
321            *) display_error "Unknown option: $1" >&2;;
322        esac
323    done
324    if test "$KILL" = "true" -o "$SHUTDOWN" = "true"; then
325        stop_couchdb $KILL
326    else
327        start_couchdb
328    fi
329}
330
331parse_script_option_list $@
332