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