xref: /5.5.2/couchdb/bin/couchdb.tpl.in (revision 6a6a8194)
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 "$1"/*.ini; do
124        if [ -r "$file" ]; then
125          _add_config_file "$file"
126        fi
127    done
128}
129
130_load_config () {
131    _add_config_file "$DEFAULT_CONFIG_FILE"
132    _add_config_dir "$DEFAULT_CONFIG_DIR"
133    _add_config_file "$LOCAL_CONFIG_FILE"
134    _add_config_dir "$LOCAL_CONFIG_DIR"
135}
136
137_reset_config () {
138    print_arguments=""
139    start_arguments=""
140    background_start_arguments="-n"
141}
142
143_print_config () {
144    cat <<EOF
145$print_arguments
146EOF
147}
148
149check_status () {
150    PID=`_get_pid`
151    if test -n "$PID"; then
152        if kill -0 $PID 2> /dev/null; then
153            echo "Apache CouchDB is running as process $PID, time to relax."
154            return $SCRIPT_OK
155        else
156            echo >&2 << EOF
157Apache CouchDB is not running but a stale PID file exists: $PID_FILE"
158EOF
159        fi
160    else
161        echo "Apache CouchDB is not running." >&2
162    fi
163    return $SCRIPT_ERROR
164}
165
166check_environment () {
167    if test "$BACKGROUND" != "true"; then
168        return
169    fi
170    touch $PID_FILE 2> /dev/null || true
171    touch $STDOUT_FILE 2> /dev/null || true
172    touch $STDERR_FILE 2> /dev/null || true
173    message_prefix="Apache CouchDB needs write permission on the"
174    if test ! -w $PID_FILE; then
175        echo "$message_prefix PID file: $PID_FILE" >&2
176        false
177    fi
178    if test ! -w $STDOUT_FILE; then
179        echo "$message_prefix STDOUT file: $STDOUT_FILE" >&2
180        false
181    fi
182    if test ! -w $STDERR_FILE; then
183        echo "$message_prefix STDERR file: $STDERR_FILE" >&2
184        false
185    fi
186    message_prefix="Apache CouchDB needs a regular"
187    if test `echo 2> /dev/null >> $PID_FILE; echo $?` -gt 0; then
188        echo "$message_prefix PID file: $PID_FILE" >&2
189        false
190    fi
191    if test `echo 2> /dev/null >> $STDOUT_FILE; echo $?` -gt 0; then
192        echo "$message_prefix STDOUT file: $STDOUT_FILE" >&2
193        false
194    fi
195    if test `echo 2> /dev/null >> $STDERR_FILE; echo $?` -gt 0; then
196        echo "$message_prefix STDERR file: $STDERR_FILE" >&2
197        false
198    fi
199}
200
201start_couchdb () {
202    if test ! "$RECURSED" = "true"; then
203        if check_status 2> /dev/null; then
204            exit
205        fi
206        check_environment
207    fi
208    interactive_option="+Bd -noinput"
209    if test "$INTERACTIVE" = "true"; then
210        interactive_option=""
211    fi
212    if test "$BACKGROUND" = "true"; then
213        touch $PID_FILE
214        interactive_option="+Bd -noinput"
215    fi
216    command="%ERL% $interactive_option -sasl errlog_type error +K true \
217        -env ERL_LIBS %localerlanglibdir% -couch_ini $start_arguments -s couch"
218    if test "$BACKGROUND" = "true" -a "$RECURSED" = "false"; then
219        $0 $background_start_arguments -b -r $RESPAWN_TIMEOUT -p $PID_FILE \
220            -o $STDOUT_FILE -e $STDERR_FILE -R &
221        echo "Apache CouchDB has started, time to relax."
222    else
223        if test "$RECURSED" = "true"; then
224            while true; do
225                export HEART_COMMAND
226                export HEART_BEAT_TIMEOUT
227                `eval $command -pidfile $PID_FILE -heart \
228                    >> $STDOUT_FILE 2>> $STDERR_FILE` || true
229                PID=`_get_pid`
230                if test -n "$PID"; then
231                    if kill -0 $PID 2> /dev/null; then
232                        return $SCRIPT_ERROR
233                    fi
234                else
235                    return $SCRIPT_OK
236                fi
237                if test "$RESPAWN_TIMEOUT" = "0"; then
238                    return $SCRIPT_OK
239                fi
240                if test "$RESPAWN_TIMEOUT" != "1"; then
241                    plural_ending="s"
242                fi
243                cat << EOF
244Apache CouchDB crashed, restarting in $RESPAWN_TIMEOUT second$plural_ending.
245EOF
246                sleep $RESPAWN_TIMEOUT
247            done
248        else
249            eval exec $command
250        fi
251    fi
252}
253
254stop_couchdb () {
255    PID=`_get_pid`
256    if test -n "$PID"; then
257        if test "$1" = "false"; then
258            echo > $PID_FILE
259        fi
260        if kill -0 $PID 2> /dev/null; then
261            if kill -1 $PID 2> /dev/null; then
262                if test "$1" = "false"; then
263                    echo "Apache CouchDB has been shutdown."
264                else
265                    echo "Apache CouchDB has been killed."
266                fi
267                return $SCRIPT_OK
268            else
269                echo "Apache CouchDB could not be killed." >&2
270                return $SCRIPT_ERROR
271            fi
272            if test "$1" = "false"; then
273                echo "Stale PID file exists but Apache CouchDB is not running."
274            else
275                echo "Stale PID file existed but Apache CouchDB is not running."
276            fi
277        fi
278    else
279        echo "Apache CouchDB is not running."
280    fi
281}
282
283parse_script_option_list () {
284    _load_config
285    set +e
286    options=`getopt hVa:A:ncibp:r:Ro:e:skd $@`
287    if test ! $? -eq 0; then
288        display_error
289    fi
290    set -e
291    eval set -- $options
292    while [ $# -gt 0 ]; do
293        case "$1" in
294            -h) shift; display_help; exit;;
295            -V) shift; display_version; exit;;
296            -a) shift; _add_config_file "$1"; shift;;
297            -A) shift; _add_config_dir "$1"; shift;;
298            -n) shift; _reset_config;;
299            -c) shift; _print_config; exit;;
300            -i) shift; INTERACTIVE=true;;
301            -b) shift; BACKGROUND=true;;
302            -r) shift; RESPAWN_TIMEOUT=$1; shift;;
303            -R) shift; RECURSED=true;;
304            -p) shift; PID_FILE=$1; shift;;
305            -o) shift; STDOUT_FILE=$1; shift;;
306            -e) shift; STDERR_FILE=$1; shift;;
307            -s) shift; check_status; exit;;
308            -k) shift; KILL=true;;
309            -d) shift; SHUTDOWN=true;;
310            --) shift; break;;
311            *) display_error "Unknown option: $1" >&2;;
312        esac
313    done
314    if test "$KILL" = "true" -o "$SHUTDOWN" = "true"; then
315        stop_couchdb $KILL
316    else
317        start_couchdb
318    fi
319}
320
321parse_script_option_list $@
322