xref: /5.5.2/couchdb/bin/couchdb.tpl.in (revision 93bc21db)
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 inherits the environment of this command.
65
66The exit status is 0 for success or 1 for failure.
67
68The \`-s' option will exit 0 for running and 1 for not running.
69
70Options:
71
72  -h          display a short help message and exit
73  -V          display version information and exit
74  -a FILE     add configuration FILE to chain
75  -A DIR      add configuration DIR to chain
76  -n          reset configuration file chain (including system default)
77  -c          print configuration file chain and exit
78  -i          use the interactive Erlang shell
79  -b          spawn as a background process
80  -p FILE     set the background PID FILE (overrides system default)
81  -r SECONDS  respawn background process after SECONDS (defaults to no respawn)
82  -o FILE     redirect background stdout to FILE (defaults to $STDOUT_FILE)
83  -e FILE     redirect background stderr to FILE (defaults to $STDERR_FILE)
84  -s          display the status of the background process
85  -k          kill the background process, will respawn if needed
86  -d          shutdown the background process
87
88Report bugs at <%bug_uri%>.
89EOF
90}
91
92display_error () {
93    if test -n "$1"; then
94        echo $1 >&2
95    fi
96    echo >&2
97    echo "Try \`"$basename" -h' for more information." >&2
98    false
99}
100
101_get_pid () {
102    if test -f $PID_FILE; then
103        PID=`cat $PID_FILE`
104    fi
105    echo $PID
106}
107
108_add_config_file () {
109    if test -z "$print_arguments"; then
110        print_arguments="$1"
111    else
112        print_arguments="`cat <<EOF
113$print_arguments
114$1
115EOF
116`"
117    fi
118    start_arguments="$start_arguments $1"
119    background_start_arguments="$background_start_arguments -a $1"
120}
121
122_add_config_dir () {
123    for file in `find "$1" -mindepth 1 -maxdepth 1 -type f -name '*.ini'`; do
124        _add_config_file $file
125    done
126}
127
128_load_config () {
129    _add_config_file "$DEFAULT_CONFIG_FILE"
130    _add_config_dir "$DEFAULT_CONFIG_DIR"
131    _add_config_file "$LOCAL_CONFIG_FILE"
132    _add_config_dir "$LOCAL_CONFIG_DIR"
133}
134
135_reset_config () {
136    print_arguments=""
137    start_arguments=""
138    background_start_arguments="-n"
139}
140
141_print_config () {
142    cat <<EOF
143$print_arguments
144EOF
145}
146
147check_status () {
148    PID=`_get_pid`
149    if test -n "$PID"; then
150        if kill -0 $PID 2> /dev/null; then
151            echo "Apache CouchDB is running as process $PID, time to relax."
152            return $SCRIPT_OK
153        else
154            echo >&2 << EOF
155Apache CouchDB is not running but a stale PID file exists: $PID_FILE"
156EOF
157        fi
158    else
159        echo "Apache CouchDB is not running." >&2
160    fi
161    return $SCRIPT_ERROR
162}
163
164check_environment () {
165    if test "$BACKGROUND" != "true"; then
166        return
167    fi
168    touch $PID_FILE 2> /dev/null || true
169    touch $STDOUT_FILE 2> /dev/null || true
170    touch $STDERR_FILE 2> /dev/null || true
171    message_prefix="Apache CouchDB needs write permission on the"
172    if test ! -w $PID_FILE; then
173        echo "$message_prefix PID file: $PID_FILE" >&2
174        false
175    fi
176    if test ! -w $STDOUT_FILE; then
177        echo "$message_prefix STDOUT file: $STDOUT_FILE" >&2
178        false
179    fi
180    if test ! -w $STDERR_FILE; then
181        echo "$message_prefix STDERR file: $STDERR_FILE" >&2
182        false
183    fi
184    message_prefix="Apache CouchDB needs a regular"
185    if test `echo 2> /dev/null >> $PID_FILE; echo $?` -gt 0; then
186        echo "$message_prefix PID file: $PID_FILE" >&2
187        false
188    fi
189    if test `echo 2> /dev/null >> $STDOUT_FILE; echo $?` -gt 0; then
190        echo "$message_prefix STDOUT file: $STDOUT_FILE" >&2
191        false
192    fi
193    if test `echo 2> /dev/null >> $STDERR_FILE; echo $?` -gt 0; then
194        echo "$message_prefix STDERR file: $STDERR_FILE" >&2
195        false
196    fi
197}
198
199start_couchdb () {
200    if test ! "$RECURSED" = "true"; then
201        if check_status 2> /dev/null; then
202            exit
203        fi
204        check_environment
205    fi
206    interactive_option="+Bd -noinput"
207    if test "$INTERACTIVE" = "true"; then
208        interactive_option=""
209    fi
210    if test "$BACKGROUND" = "true"; then
211        touch $PID_FILE
212        interactive_option="+Bd -noinput"
213    fi
214    command="%ERL% $interactive_option -sasl errlog_type error +K true \
215        -env ERL_LIBS %localerlanglibdir% -couch_ini $start_arguments -s couch"
216    if test "$BACKGROUND" = "true" -a "$RECURSED" = "false"; then
217        $0 $background_start_arguments -b -r $RESPAWN_TIMEOUT -p $PID_FILE \
218            -o $STDOUT_FILE -e $STDERR_FILE -R &
219        echo "Apache CouchDB has started, time to relax."
220    else
221        if test "$RECURSED" = "true"; then
222            while true; do
223                export HEART_COMMAND
224                export HEART_BEAT_TIMEOUT
225                `eval $command -pidfile $PID_FILE -heart \
226                    >> $STDOUT_FILE 2>> $STDERR_FILE` || true
227                PID=`_get_pid`
228                if test -n "$PID"; then
229                    if kill -0 $PID 2> /dev/null; then
230                        return $SCRIPT_ERROR
231                    fi
232                else
233                    return $SCRIPT_OK
234                fi
235                if test "$RESPAWN_TIMEOUT" = "0"; then
236                    return $SCRIPT_OK
237                fi
238                if test "$RESPAWN_TIMEOUT" != "1"; then
239                    plural_ending="s"
240                fi
241                cat << EOF
242Apache CouchDB crashed, restarting in $RESPAWN_TIMEOUT second$plural_ending.
243EOF
244                sleep $RESPAWN_TIMEOUT
245            done
246        else
247            eval exec $command
248        fi
249    fi
250}
251
252stop_couchdb () {
253    PID=`_get_pid`
254    if test -n "$PID"; then
255        if test "$1" = "false"; then
256            echo > $PID_FILE
257        fi
258        if kill -0 $PID 2> /dev/null; then
259            if kill -1 $PID 2> /dev/null; then
260                if test "$1" = "false"; then
261                    echo "Apache CouchDB has been shutdown."
262                else
263                    echo "Apache CouchDB has been killed."
264                fi
265                return $SCRIPT_OK
266            else
267                echo "Apache CouchDB could not be killed." >&2
268                return $SCRIPT_ERROR
269            fi
270            if test "$1" = "false"; then
271                echo "Stale PID file exists but Apache CouchDB is not running."
272            else
273                echo "Stale PID file existed but Apache CouchDB is not running."
274            fi
275        fi
276    else
277        echo "Apache CouchDB is not running."
278    fi
279}
280
281parse_script_option_list () {
282    _load_config
283    set +e
284    options=`getopt hVa:A:ncibp:r:Ro:e:skd $@`
285    if test ! $? -eq 0; then
286        display_error
287    fi
288    set -e
289    eval set -- $options
290    while [ $# -gt 0 ]; do
291        case "$1" in
292            -h) shift; display_help; exit;;
293            -V) shift; display_version; exit;;
294            -a) shift; _add_config_file "$1"; shift;;
295            -A) shift; _add_config_dir "$1"; shift;;
296            -n) shift; _reset_config;;
297            -c) shift; _print_config; exit;;
298            -i) shift; INTERACTIVE=true;;
299            -b) shift; BACKGROUND=true;;
300            -r) shift; RESPAWN_TIMEOUT=$1; shift;;
301            -R) shift; RECURSED=true;;
302            -p) shift; PID_FILE=$1; shift;;
303            -o) shift; STDOUT_FILE=$1; shift;;
304            -e) shift; STDERR_FILE=$1; shift;;
305            -s) shift; check_status; exit;;
306            -k) shift; KILL=true;;
307            -d) shift; SHUTDOWN=true;;
308            --) shift; break;;
309            *) display_error "Unknown option: $1" >&2;;
310        esac
311    done
312    if test "$KILL" = "true" -o "$SHUTDOWN" = "true"; then
313        stop_couchdb $KILL
314    else
315        start_couchdb
316    fi
317}
318
319parse_script_option_list $@
320