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