xref: /5.5.2/couchdb/bin/couchdb.tpl.in (revision bbd0703f)
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 +A 4"
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=`basename $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    if test "$BACKGROUND" = "true" -a "$RECURSED" = "false"; then
228        $0 $background_start_arguments -b -r $RESPAWN_TIMEOUT -p $PID_FILE \
229            -o $STDOUT_FILE -e $STDERR_FILE -R &
230        echo "Apache CouchDB has started, time to relax."
231    else
232        if test "$RECURSED" = "true"; then
233            while true; do
234                export HEART_COMMAND
235                export HEART_BEAT_TIMEOUT
236                `eval $command -pidfile $PID_FILE -heart \
237                    >> $STDOUT_FILE 2>> $STDERR_FILE` || true
238                PID=`_get_pid`
239                if test -n "$PID"; then
240                    if kill -0 $PID 2> /dev/null; then
241                        return $SCRIPT_ERROR
242                    fi
243                else
244                    return $SCRIPT_OK
245                fi
246                if test "$RESPAWN_TIMEOUT" = "0"; then
247                    return $SCRIPT_OK
248                fi
249                if test "$RESPAWN_TIMEOUT" != "1"; then
250                    plural_ending="s"
251                fi
252                cat << EOF
253Apache CouchDB crashed, restarting in $RESPAWN_TIMEOUT second$plural_ending.
254EOF
255                sleep $RESPAWN_TIMEOUT
256            done
257        else
258            eval exec $command
259        fi
260    fi
261}
262
263stop_couchdb () {
264    PID=`_get_pid`
265    if test -n "$PID"; then
266        if test "$1" = "false"; then
267            echo > $PID_FILE
268        fi
269        if kill -0 $PID 2> /dev/null; then
270            if kill -1 $PID 2> /dev/null; then
271                if test "$1" = "false"; then
272                    echo "Apache CouchDB has been shutdown."
273                else
274                    echo "Apache CouchDB has been killed."
275                fi
276                return $SCRIPT_OK
277            else
278                echo "Apache CouchDB could not be killed." >&2
279                return $SCRIPT_ERROR
280            fi
281            if test "$1" = "false"; then
282                echo "Stale PID file exists but Apache CouchDB is not running."
283            else
284                echo "Stale PID file existed but Apache CouchDB is not running."
285            fi
286        fi
287    else
288        echo "Apache CouchDB is not running."
289    fi
290}
291
292parse_script_option_list () {
293    _load_config
294    set +e
295    options=`getopt hVa:A:ncibp:r:Ro:e:skd $@`
296    if test ! $? -eq 0; then
297        display_error
298    fi
299    set -e
300    eval set -- $options
301    while [ $# -gt 0 ]; do
302        case "$1" in
303            -h) shift; display_help; exit;;
304            -V) shift; display_version; exit;;
305            -a) shift; _add_config_file "$1"; shift;;
306            -A) shift; _add_config_dir "$1"; shift;;
307            -n) shift; _reset_config;;
308            -c) shift; _print_config; exit;;
309            -i) shift; INTERACTIVE=true;;
310            -b) shift; BACKGROUND=true;;
311            -r) shift; RESPAWN_TIMEOUT=$1; shift;;
312            -R) shift; RECURSED=true;;
313            -p) shift; PID_FILE=$1; shift;;
314            -o) shift; STDOUT_FILE=$1; shift;;
315            -e) shift; STDERR_FILE=$1; shift;;
316            -s) shift; check_status; exit;;
317            -k) shift; KILL=true;;
318            -d) shift; SHUTDOWN=true;;
319            --) shift; break;;
320            *) display_error "Unknown option: $1" >&2;;
321        esac
322    done
323    if test "$KILL" = "true" -o "$SHUTDOWN" = "true"; then
324        stop_couchdb $KILL
325    else
326        start_couchdb
327    fi
328}
329
330parse_script_option_list $@
331