summaryrefslogtreecommitdiff
path: root/bin/couchdb.tpl.in
blob: 502d6b3f081312cea456a378da5a04990acfd515 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
#! /bin/sh -e

# Licensed under the Apache License, Version 2.0 (the "License"); you may not
# use this file except in compliance with the License.  You may obtain a copy of
# the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
# License for the specific language governing permissions and limitations under
# the License.

SCRIPT_OK=0
SCRIPT_ERROR=1

INTERACTIVE=false
BACKGROUND=false
KILL=false
SHUTDOWN=false
RECURSED=false
RESET_CONFIG=true

RESPAWN_TIMEOUT=0

DEFAULT_INI_FILE=%localconfdir%/%defaultini%
LOCAL_INI_FILE=%localconfdir%/%localini%

PID_FILE=%localstatedir%/run/couchdb/couchdb.pid

STDOUT_FILE=couchdb.stdout
STDERR_FILE=couchdb.stderr

HEART_COMMAND="%bindir%/%couchdb_command_name% -k"
HEART_BEAT_TIMEOUT=11

basename=`basename $0`

display_version () {
    cat << EOF
$basename - %package_name% %version%

Licensed under the Apache License, Version 2.0 (the "License"); you may not use
this file except in compliance with the License.  You may obtain a copy of the
License at

  http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed
under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
CONDITIONS OF ANY KIND, either express or implied.  See the License for the
specific language governing permissions and limitations under the License.
EOF
 }

display_help () {
    cat << EOF
Usage: $basename [OPTION]

The $basename command runs the %package_name% server.

Erlang inherits the environment of this command.

The exit status is 0 for success or 1 for failure.

The \`-s' option will exit 0 for running and 1 for not running.

Options:

  -h          display a short help message and exit
  -V          display version information and exit
  -c FILE     use configuration FILE (chainable, resets system default)
  -C FILE     use configuration FILE (chainable, does not reset system default)
  -i          use the interactive Erlang shell
  -b          spawn as a background process
  -p FILE     set the background PID FILE (overrides system default)
  -r SECONDS  respawn background process after SECONDS (defaults to no respawn)
  -o FILE     redirect background stdout to FILE (defaults to $STDOUT_FILE)
  -e FILE     redirect background stderr to FILE (defaults to $STDERR_FILE)
  -s          display the status of the background process
  -k          kill the background process, will respawn if needed
  -d          shutdown the background process

Report bugs at <%bug_uri%>.
EOF
}

display_error () {
    if test -n "$1"; then
        echo $1 >&2
    fi
    echo >&2
    echo "Try \`"$basename" -h' for more information." >&2
    exit $SCRIPT_ERROR
}

_get_pid () {
    if test -f $PID_FILE; then
        PID=`cat $PID_FILE`
    fi
    echo $PID
}

check_status () {
    PID=`_get_pid`
    if test -n "$PID"; then
        if kill -0 $PID 2> /dev/null; then
            echo "Apache CouchDB is running as process $PID, time to relax."
            return $SCRIPT_OK
        else
            echo >&2 << EOF
Apache CouchDB is not running but a stale PID file exists: $PID_FILE"
EOF
        fi
    else
        echo "Apache CouchDB is not running." >&2
    fi
    return $SCRIPT_ERROR
}

check_environment () {
    if test "$BACKGROUND" != "true"; then
        return
    fi
    touch $PID_FILE 2> /dev/null || true
    touch $STDOUT_FILE 2> /dev/null || true
    touch $STDERR_FILE 2> /dev/null || true
    message_prefix="Apache CouchDB needs write permission on the"
    if test ! -w $PID_FILE; then
        echo "$message_prefix PID file: $PID_FILE" >&2
        exit $SCRIPT_ERROR
    fi
    if test ! -w $STDOUT_FILE; then
        echo "$message_prefix STDOUT file: $STDOUT_FILE" >&2
        exit $SCRIPT_ERROR
    fi
    if test ! -w $STDERR_FILE; then
        echo "$message_prefix STDERR file: $STDERR_FILE" >&2
        exit $SCRIPT_ERROR
    fi
    message_prefix="Apache CouchDB needs a regular"
    if test `echo 2> /dev/null >> $PID_FILE; echo $?` -gt 0; then
        echo "$message_prefix PID file: $PID_FILE" >&2
        exit $SCRIPT_ERROR
    fi
    if test `echo 2> /dev/null >> $STDOUT_FILE; echo $?` -gt 0; then
        echo "$message_prefix STDOUT file: $STDOUT_FILE" >&2
        exit $SCRIPT_ERROR
    fi
    if test `echo 2> /dev/null >> $STDERR_FILE; echo $?` -gt 0; then
        echo "$message_prefix STDERR file: $STDERR_FILE" >&2
        exit $SCRIPT_ERROR
    fi
}

start_couchdb () {
    if test ! "$RECURSED" = "true"; then
        if check_status 2> /dev/null; then
            exit $SCRIPT_OK
        fi
        check_environment
    fi
    interactive_option="+Bd -noinput"
    if test "$INTERACTIVE" = "true"; then
        interactive_option=""
    fi
    if test "$BACKGROUND" = "true"; then
        touch $PID_FILE
        interactive_option="+Bd -noinput"
    fi
    if test -n "$INI_FILES"; then
        if test "$RESET_CONFIG" = "true"; then
            ini_files="$INI_FILES"
        else
            ini_files="$DEFAULT_INI_FILE $INI_FILES"
        fi
    else
        ini_files="$DEFAULT_INI_FILE $LOCAL_INI_FILE"
    fi
    for file in $ini_files; do
        if test -n "$start_arguments"; then
            start_arguments="$start_arguments, ";
        fi
        start_arguments="$start_arguments \\\"$file\\\""
        background_start_arguments="$background_start_arguments -c \\\"$file\\\""
    done
    command="`%ICU_CONFIG% --invoke` \
        %ERL% $interactive_option -smp auto -sasl errlog_type error +K true \
        -pa %localerlanglibdir%/%couchdbebindir% \
            %localerlanglibdir%/%mochiwebebindir% \
            %localerlanglibdir%/%ibrowseebindir% \
        -eval \"application:load(ibrowse)\" \
        -eval \"application:load(crypto)\" \
        -eval \"application:load(couch)\" \
        -eval \"crypto:start()\" \
        -eval \"ibrowse:start()\" \
        -eval \"couch_server:start([$start_arguments]), receive done -> done end.\" "
    if test "$BACKGROUND" = "true" \
        -a "$RECURSED" = "false"; then
        $0 $background_start_arguments -b -r $RESPAWN_TIMEOUT -p $PID_FILE \
            -o $STDOUT_FILE -e $STDERR_FILE -R &
        echo "Apache CouchDB has started, time to relax."
    else
        if test "$RECURSED" = "true"; then
            while true; do
                export HEART_COMMAND
                export HEART_BEAT_TIMEOUT
                `eval $command -pidfile $PID_FILE -heart \
                    >> $STDOUT_FILE 2>> $STDERR_FILE` || true
                PID=`_get_pid`
                if test -n "$PID"; then
                    if kill -0 $PID 2> /dev/null; then
                        return $SCRIPT_ERROR
                    fi
                else
                    return $SCRIPT_OK
                fi
                if test "$RESPAWN_TIMEOUT" = "0"; then
                    return $SCRIPT_OK
                fi
                if test "$RESPAWN_TIMEOUT" != "1"; then
                    plural_ending="s"
                fi
                cat << EOF
Apache CouchDB crashed, restarting in $RESPAWN_TIMEOUT second$plural_ending.
EOF
                sleep $RESPAWN_TIMEOUT
            done
        else
            eval exec $command
        fi
    fi
}

stop_couchdb () {
    PID=`_get_pid`
    if test -n "$PID"; then
        if test "$1" = "false"; then
            echo > $PID_FILE
        fi
        if kill -0 $PID 2> /dev/null; then
            if kill -1 $PID 2> /dev/null; then
                if test "$1" = "false"; then
                    echo "Apache CouchDB has been shutdown."
                else
                    echo "Apache CouchDB has been killed."
                fi
                return $SCRIPT_OK
            else
                echo "Apache CouchDB could not be killed." >&2
                return $SCRIPT_ERROR
            fi
            if test "$1" = "false"; then
                echo "Stale PID file exists but Apache CouchDB is not running."
            else
                echo "Stale PID file existed but Apache CouchDB is not running."
            fi
        fi
    else
        echo "Apache CouchDB is not running."
    fi
}

parse_script_option_list () {
    set +e
    options=`getopt hVc:C:ibp:r:Ro:e:skd $@`
    if test ! $? -eq 0; then
        display_error
    fi
    set -e
    eval set -- $options
    while [ $# -gt 0 ]; do
        case "$1" in
            -h) shift; display_help; exit $SCRIPT_OK;;
            -V) shift; display_version; exit $SCRIPT_OK;;
            -c) shift; INI_FILES="$INI_FILES $1"; shift;;
            -C) shift; RESET_CONFIG=false; INI_FILES="$INI_FILES $1"; shift;;
            -i) shift; INTERACTIVE=true;;
            -b) shift; BACKGROUND=true;;
            -r) shift; RESPAWN_TIMEOUT=$1; shift;;
            -R) shift; RECURSED=true;;
            -p) shift; PID_FILE=$1; shift;;
            -o) shift; STDOUT_FILE=$1; shift;;
            -e) shift; STDERR_FILE=$1; shift;;
            -s) shift; check_status; exit $SCRIPT_OK;;
            -k) shift; KILL=true;;
            -d) shift; SHUTDOWN=true;;
            --) shift; break;;
            *) display_error "Unknown option: $1" >&2;;
        esac
    done
    if test "$KILL" = "true" -o "$SHUTDOWN" = "true"; then
        stop_couchdb $KILL
    else
        start_couchdb
    fi
}

parse_script_option_list $@