xref: /6.0.3/couchdb/bin/couchdb.tpl.in (revision 161181c8)
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    if [ "$COUCHDB_ADDITIONAL_CONFIG_FILE" != '' ]
145    then
146        _add_config_file "$COUCHDB_ADDITIONAL_CONFIG_FILE"
147    fi
148}
149
150_reset_config () {
151    print_arguments=""
152    start_arguments=""
153    background_start_arguments="-n"
154}
155
156_print_config () {
157    cat <<EOF
158$print_arguments
159EOF
160}
161
162check_status () {
163    PID=`_get_pid`
164    if test -n "$PID"; then
165        if kill -0 $PID 2> /dev/null; then
166            echo "Apache CouchDB is running as process $PID, time to relax."
167            return $SCRIPT_OK
168        else
169            echo >&2 << EOF
170Apache CouchDB is not running but a stale PID file exists: $PID_FILE"
171EOF
172        fi
173    else
174        echo "Apache CouchDB is not running." >&2
175    fi
176    return $SCRIPT_ERROR
177}
178
179check_environment () {
180    if test "$BACKGROUND" != "true"; then
181        return
182    fi
183    touch $PID_FILE 2> /dev/null || true
184    touch $STDOUT_FILE 2> /dev/null || true
185    touch $STDERR_FILE 2> /dev/null || true
186    message_prefix="Apache CouchDB needs write permission on the"
187    if test ! -w $PID_FILE; then
188        echo "$message_prefix PID file: $PID_FILE" >&2
189        false
190    fi
191    if test ! -w $STDOUT_FILE; then
192        echo "$message_prefix STDOUT file: $STDOUT_FILE" >&2
193        false
194    fi
195    if test ! -w $STDERR_FILE; then
196        echo "$message_prefix STDERR file: $STDERR_FILE" >&2
197        false
198    fi
199    message_prefix="Apache CouchDB needs a regular"
200    if test `echo 2> /dev/null >> $PID_FILE; echo $?` -gt 0; then
201        echo "$message_prefix PID file: $PID_FILE" >&2
202        false
203    fi
204    if test `echo 2> /dev/null >> $STDOUT_FILE; echo $?` -gt 0; then
205        echo "$message_prefix STDOUT file: $STDOUT_FILE" >&2
206        false
207    fi
208    if test `echo 2> /dev/null >> $STDERR_FILE; echo $?` -gt 0; then
209        echo "$message_prefix STDERR file: $STDERR_FILE" >&2
210        false
211    fi
212}
213
214start_couchdb () {
215    if test ! "$RECURSED" = "true"; then
216        if check_status 2> /dev/null; then
217            exit
218        fi
219        check_environment
220    fi
221    interactive_option="+Bd -noinput"
222    if test "$INTERACTIVE" = "true"; then
223        interactive_option=""
224    fi
225    if test "$BACKGROUND" = "true"; then
226        touch $PID_FILE
227        interactive_option="+Bd -noinput"
228    fi
229    command="'%ERL%' $interactive_option $ERL_START_OPTIONS \
230        -env ERL_LIBS '%localerlanglibdir%' -couch_ini $start_arguments -s couch"
231
232    # Find plugins and add them to the Erlang path.
233    if test -d "%localerlanglibdir%/../../plugins"; then
234        for plugin in "%localerlanglibdir%/../../plugins"/*; do
235            if echo "$ERL_ZFLAGS" | grep "$plugin/ebin" > /dev/null 2> /dev/null; then
236                : # It's already loaded.
237            else
238                if echo "$COUCH_PLUGIN_BLACKLIST" | grep "$plugin" > /dev/null 2> /dev/null; then
239                    : # Do not use this plugin.
240                else
241                    ERL_ZFLAGS="$ERL_ZFLAGS -pz '$plugin/ebin'"
242                fi
243            fi
244        done
245        export ERL_ZFLAGS
246    fi
247
248    if test "$BACKGROUND" = "true" -a "$RECURSED" = "false"; then
249        $0 $background_start_arguments -b -r $RESPAWN_TIMEOUT -p $PID_FILE \
250            -o $STDOUT_FILE -e $STDERR_FILE -R &
251        echo "Apache CouchDB has started, time to relax."
252    else
253        if test "$RECURSED" = "true"; then
254            while true; do
255                export HEART_COMMAND
256                export HEART_BEAT_TIMEOUT
257                `eval $command -pidfile $PID_FILE -heart \
258                    >> $STDOUT_FILE 2>> $STDERR_FILE` || true
259                PID=`_get_pid`
260                if test -n "$PID"; then
261                    if kill -0 $PID 2> /dev/null; then
262                        return $SCRIPT_ERROR
263                    fi
264                else
265                    return $SCRIPT_OK
266                fi
267                if test "$RESPAWN_TIMEOUT" = "0"; then
268                    return $SCRIPT_OK
269                fi
270                if test "$RESPAWN_TIMEOUT" != "1"; then
271                    plural_ending="s"
272                fi
273                cat << EOF
274Apache CouchDB crashed, restarting in $RESPAWN_TIMEOUT second$plural_ending.
275EOF
276                sleep $RESPAWN_TIMEOUT
277            done
278        else
279            eval exec "$command"
280        fi
281    fi
282}
283
284stop_couchdb () {
285    PID=`_get_pid`
286    if test -n "$PID"; then
287        if test "$1" = "false"; then
288            echo > $PID_FILE
289        fi
290        if kill -0 $PID 2> /dev/null; then
291            if kill -1 $PID 2> /dev/null; then
292                if test "$1" = "false"; then
293                    echo "Apache CouchDB has been shutdown."
294                else
295                    echo "Apache CouchDB has been killed."
296                fi
297                return $SCRIPT_OK
298            else
299                echo "Apache CouchDB could not be killed." >&2
300                return $SCRIPT_ERROR
301            fi
302            if test "$1" = "false"; then
303                echo "Stale PID file exists but Apache CouchDB is not running."
304            else
305                echo "Stale PID file existed but Apache CouchDB is not running."
306            fi
307        fi
308    else
309        echo "Apache CouchDB is not running."
310    fi
311}
312
313parse_script_option_list () {
314    _load_config
315    set +e
316    options=`getopt hVa:A:ncibp:r:Ro:e:skd $@`
317    if test ! $? -eq 0; then
318        display_error
319    fi
320    set -e
321    eval set -- $options
322    while [ $# -gt 0 ]; do
323        case "$1" in
324            -h) shift; display_help; exit;;
325            -V) shift; display_version; exit;;
326            -a) shift; _add_config_file "$1"; shift;;
327            -A) shift; _add_config_dir "$1"; shift;;
328            -n) shift; _reset_config;;
329            -c) shift; _print_config; exit;;
330            -i) shift; INTERACTIVE=true;;
331            -b) shift; BACKGROUND=true;;
332            -r) shift; RESPAWN_TIMEOUT=$1; shift;;
333            -R) shift; RECURSED=true;;
334            -p) shift; PID_FILE=$1; shift;;
335            -o) shift; STDOUT_FILE=$1; shift;;
336            -e) shift; STDERR_FILE=$1; shift;;
337            -s) shift; check_status; exit;;
338            -k) shift; KILL=true;;
339            -d) shift; SHUTDOWN=true;;
340            --) shift; break;;
341            *) display_error "Unknown option: $1" >&2;;
342        esac
343    done
344    if test "$KILL" = "true" -o "$SHUTDOWN" = "true"; then
345        stop_couchdb $KILL
346    else
347        start_couchdb
348    fi
349}
350
351parse_script_option_list $@
352