~ubuntu-branches/ubuntu/natty/couchdb/natty

1 by Noah Slater
Import upstream version 0.7.3~svn684
1
#! /bin/sh -e
2
3
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
1.2.2 by Ken VanDine
Import upstream version 0.10.0~svn806985
4
# use this file except in compliance with the License. You may obtain a copy of
1 by Noah Slater
Import upstream version 0.7.3~svn684
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
1.2.2 by Ken VanDine
Import upstream version 0.10.0~svn806985
11
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
1 by Noah Slater
Import upstream version 0.7.3~svn684
12
# License for the specific language governing permissions and limitations under
13
# the License.
14
1.2.2 by Ken VanDine
Import upstream version 0.10.0~svn806985
15
BACKGROUND=false
16
DEFAULT_CONFIG_DIR=%localconfdir%/default.d
17
DEFAULT_CONFIG_FILE=%localconfdir%/%defaultini%
1.2.9 by Elliot Murphy
Import upstream version 1.0.0
18
ERL_START_OPTIONS="-sasl errlog_type error +K true +A 4"
1.2.2 by Ken VanDine
Import upstream version 0.10.0~svn806985
19
HEART_BEAT_TIMEOUT=11
20
HEART_COMMAND="%bindir%/%couchdb_command_name% -k"
1.2.1 by Noah Slater
Import upstream version 0.9.0
21
INTERACTIVE=false
22
KILL=false
1.2.2 by Ken VanDine
Import upstream version 0.10.0~svn806985
23
LOCAL_CONFIG_DIR=%localconfdir%/local.d
24
LOCAL_CONFIG_FILE=%localconfdir%/%localini%
25
PID_FILE=%localstatedir%/run/couchdb/couchdb.pid
1.2.1 by Noah Slater
Import upstream version 0.9.0
26
RECURSED=false
27
RESET_CONFIG=true
1 by Noah Slater
Import upstream version 0.7.3~svn684
28
RESPAWN_TIMEOUT=0
1.2.2 by Ken VanDine
Import upstream version 0.10.0~svn806985
29
SCRIPT_ERROR=1
30
SCRIPT_OK=0
31
SHUTDOWN=false
32
STDERR_FILE=couchdb.stderr
1 by Noah Slater
Import upstream version 0.7.3~svn684
33
STDOUT_FILE=couchdb.stdout
34
1.2.2 by Ken VanDine
Import upstream version 0.10.0~svn806985
35
print_arguments=""
36
start_arguments=""
37
background_start_arguments=""
1 by Noah Slater
Import upstream version 0.7.3~svn684
38
1.1.2 by Noah Slater
Import upstream version 0.8.0
39
basename=`basename $0`
1 by Noah Slater
Import upstream version 0.7.3~svn684
40
41
display_version () {
42
    cat << EOF
43
$basename - %package_name% %version%
44
45
Licensed under the Apache License, Version 2.0 (the "License"); you may not use
1.2.2 by Ken VanDine
Import upstream version 0.10.0~svn806985
46
this file except in compliance with the License. You may obtain a copy of the
1 by Noah Slater
Import upstream version 0.7.3~svn684
47
License at
48
49
  http://www.apache.org/licenses/LICENSE-2.0
50
51
Unless required by applicable law or agreed to in writing, software distributed
52
under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
1.2.2 by Ken VanDine
Import upstream version 0.10.0~svn806985
53
CONDITIONS OF ANY KIND, either express or implied. See the License for the
1 by Noah Slater
Import upstream version 0.7.3~svn684
54
specific language governing permissions and limitations under the License.
1.2.2 by Ken VanDine
Import upstream version 0.10.0~svn806985
55
1 by Noah Slater
Import upstream version 0.7.3~svn684
56
EOF
1.2.2 by Ken VanDine
Import upstream version 0.10.0~svn806985
57
}
1 by Noah Slater
Import upstream version 0.7.3~svn684
58
59
display_help () {
60
    cat << EOF
61
Usage: $basename [OPTION]
62
63
The $basename command runs the %package_name% server.
64
1.2.9 by Elliot Murphy
Import upstream version 1.0.0
65
Erlang is called with:
66
67
    $ERL_START_OPTIONS
68
1.1.2 by Noah Slater
Import upstream version 0.8.0
69
Erlang inherits the environment of this command.
70
1.2.9 by Elliot Murphy
Import upstream version 1.0.0
71
You can override these options using the environment:
72
73
    ERL_AFLAGS, ERL_FLAGS, ERL_ZFLAGS
74
75
See erl(1) for more information about the environment variables.
76
1 by Noah Slater
Import upstream version 0.7.3~svn684
77
The exit status is 0 for success or 1 for failure.
78
79
Options:
80
81
  -h          display a short help message and exit
82
  -V          display version information and exit
1.2.2 by Ken VanDine
Import upstream version 0.10.0~svn806985
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
1 by Noah Slater
Import upstream version 0.7.3~svn684
87
  -i          use the interactive Erlang shell
88
  -b          spawn as a background process
1.2.1 by Noah Slater
Import upstream version 0.9.0
89
  -p FILE     set the background PID FILE (overrides system default)
1 by Noah Slater
Import upstream version 0.7.3~svn684
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
1.1.1 by Noah Slater
Import upstream version 0.7.3~svn650270
97
Report bugs at <%bug_uri%>.
1 by Noah Slater
Import upstream version 0.7.3~svn684
98
EOF
99
}
100
101
display_error () {
102
    if test -n "$1"; then
1.1.1 by Noah Slater
Import upstream version 0.7.3~svn650270
103
        echo $1 >&2
1 by Noah Slater
Import upstream version 0.7.3~svn684
104
    fi
105
    echo >&2
106
    echo "Try \`"$basename" -h' for more information." >&2
1.2.2 by Ken VanDine
Import upstream version 0.10.0~svn806985
107
    false
1 by Noah Slater
Import upstream version 0.7.3~svn684
108
}
109
110
_get_pid () {
111
    if test -f $PID_FILE; then
1.1.2 by Noah Slater
Import upstream version 0.8.0
112
        PID=`cat $PID_FILE`
1 by Noah Slater
Import upstream version 0.7.3~svn684
113
    fi
114
    echo $PID
115
}
116
1.2.2 by Ken VanDine
Import upstream version 0.10.0~svn806985
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
124
EOF
125
`"
126
    fi
36.1.1 by Clint Byrum
committing bug fix patch from comment https://bugs.launchpad.net/ubuntu/+source/couchdb/+bug/591444/comments/2 to bzr
127
    start_arguments="$start_arguments $1"
128
    background_start_arguments="$background_start_arguments -a $1"
1.2.2 by Ken VanDine
Import upstream version 0.10.0~svn806985
129
}
130
131
_add_config_dir () {
1.2.9 by Elliot Murphy
Import upstream version 1.0.0
132
    for file in "$1"/*.ini; do
133
        if [ -r "$file" ]; then
134
          _add_config_file "$file"
135
        fi
1.2.2 by Ken VanDine
Import upstream version 0.10.0~svn806985
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=""
1.2.4 by Elliot Murphy
Import upstream version 0.10.0~svn813472
149
    background_start_arguments="-n"
1.2.2 by Ken VanDine
Import upstream version 0.10.0~svn806985
150
}
151
152
_print_config () {
153
    cat <<EOF
154
$print_arguments
155
EOF
156
}
157
1 by Noah Slater
Import upstream version 0.7.3~svn684
158
check_status () {
1.1.2 by Noah Slater
Import upstream version 0.8.0
159
    PID=`_get_pid`
1 by Noah Slater
Import upstream version 0.7.3~svn684
160
    if test -n "$PID"; then
161
        if kill -0 $PID 2> /dev/null; then
1.2.1 by Noah Slater
Import upstream version 0.9.0
162
            echo "Apache CouchDB is running as process $PID, time to relax."
1 by Noah Slater
Import upstream version 0.7.3~svn684
163
            return $SCRIPT_OK
164
        else
165
            echo >&2 << EOF
1.1.1 by Noah Slater
Import upstream version 0.7.3~svn650270
166
Apache CouchDB is not running but a stale PID file exists: $PID_FILE"
1 by Noah Slater
Import upstream version 0.7.3~svn684
167
EOF
168
        fi
169
    else
1.1.1 by Noah Slater
Import upstream version 0.7.3~svn650270
170
        echo "Apache CouchDB is not running." >&2
1 by Noah Slater
Import upstream version 0.7.3~svn684
171
    fi
172
    return $SCRIPT_ERROR
173
}
174
175
check_environment () {
1.2.1 by Noah Slater
Import upstream version 0.9.0
176
    if test "$BACKGROUND" != "true"; then
1.1.3 by Noah Slater
Import upstream version 0.8.1
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
1.1.1 by Noah Slater
Import upstream version 0.7.3~svn650270
182
    message_prefix="Apache CouchDB needs write permission on the"
1.1.3 by Noah Slater
Import upstream version 0.8.1
183
    if test ! -w $PID_FILE; then
184
        echo "$message_prefix PID file: $PID_FILE" >&2
1.2.2 by Ken VanDine
Import upstream version 0.10.0~svn806985
185
        false
1.1.3 by Noah Slater
Import upstream version 0.8.1
186
    fi
187
    if test ! -w $STDOUT_FILE; then
188
        echo "$message_prefix STDOUT file: $STDOUT_FILE" >&2
1.2.2 by Ken VanDine
Import upstream version 0.10.0~svn806985
189
        false
1.1.3 by Noah Slater
Import upstream version 0.8.1
190
    fi
191
    if test ! -w $STDERR_FILE; then
192
        echo "$message_prefix STDERR file: $STDERR_FILE" >&2
1.2.2 by Ken VanDine
Import upstream version 0.10.0~svn806985
193
        false
1.1.3 by Noah Slater
Import upstream version 0.8.1
194
    fi
195
    message_prefix="Apache CouchDB needs a regular"
1.2.1 by Noah Slater
Import upstream version 0.9.0
196
    if test `echo 2> /dev/null >> $PID_FILE; echo $?` -gt 0; then
1.1.3 by Noah Slater
Import upstream version 0.8.1
197
        echo "$message_prefix PID file: $PID_FILE" >&2
1.2.2 by Ken VanDine
Import upstream version 0.10.0~svn806985
198
        false
1.1.3 by Noah Slater
Import upstream version 0.8.1
199
    fi
1.2.1 by Noah Slater
Import upstream version 0.9.0
200
    if test `echo 2> /dev/null >> $STDOUT_FILE; echo $?` -gt 0; then
1.1.3 by Noah Slater
Import upstream version 0.8.1
201
        echo "$message_prefix STDOUT file: $STDOUT_FILE" >&2
1.2.2 by Ken VanDine
Import upstream version 0.10.0~svn806985
202
        false
1.1.3 by Noah Slater
Import upstream version 0.8.1
203
    fi
1.2.1 by Noah Slater
Import upstream version 0.9.0
204
    if test `echo 2> /dev/null >> $STDERR_FILE; echo $?` -gt 0; then
1.1.3 by Noah Slater
Import upstream version 0.8.1
205
        echo "$message_prefix STDERR file: $STDERR_FILE" >&2
1.2.2 by Ken VanDine
Import upstream version 0.10.0~svn806985
206
        false
1 by Noah Slater
Import upstream version 0.7.3~svn684
207
    fi
208
}
209
210
start_couchdb () {
1.2.1 by Noah Slater
Import upstream version 0.9.0
211
    if test ! "$RECURSED" = "true"; then
1 by Noah Slater
Import upstream version 0.7.3~svn684
212
        if check_status 2> /dev/null; then
1.2.2 by Ken VanDine
Import upstream version 0.10.0~svn806985
213
            exit
1 by Noah Slater
Import upstream version 0.7.3~svn684
214
        fi
215
        check_environment
216
    fi
217
    interactive_option="+Bd -noinput"
1.2.1 by Noah Slater
Import upstream version 0.9.0
218
    if test "$INTERACTIVE" = "true"; then
1 by Noah Slater
Import upstream version 0.7.3~svn684
219
        interactive_option=""
220
    fi
1.2.1 by Noah Slater
Import upstream version 0.9.0
221
    if test "$BACKGROUND" = "true"; then
1 by Noah Slater
Import upstream version 0.7.3~svn684
222
        touch $PID_FILE
223
        interactive_option="+Bd -noinput"
224
    fi
1.2.9 by Elliot Murphy
Import upstream version 1.0.0
225
    command="%ERL% $interactive_option $ERL_START_OPTIONS \
36.1.1 by Clint Byrum
committing bug fix patch from comment https://bugs.launchpad.net/ubuntu/+source/couchdb/+bug/591444/comments/2 to bzr
226
        -env ERL_LIBS %localerlanglibdir% -couch_ini $start_arguments -s couch"
1.2.2 by Ken VanDine
Import upstream version 0.10.0~svn806985
227
    if test "$BACKGROUND" = "true" -a "$RECURSED" = "false"; then
1.2.1 by Noah Slater
Import upstream version 0.9.0
228
        $0 $background_start_arguments -b -r $RESPAWN_TIMEOUT -p $PID_FILE \
1 by Noah Slater
Import upstream version 0.7.3~svn684
229
            -o $STDOUT_FILE -e $STDERR_FILE -R &
1.2.1 by Noah Slater
Import upstream version 0.9.0
230
        echo "Apache CouchDB has started, time to relax."
1 by Noah Slater
Import upstream version 0.7.3~svn684
231
    else
1.2.1 by Noah Slater
Import upstream version 0.9.0
232
        if test "$RECURSED" = "true"; then
1 by Noah Slater
Import upstream version 0.7.3~svn684
233
            while true; do
234
                export HEART_COMMAND
235
                export HEART_BEAT_TIMEOUT
1.1.2 by Noah Slater
Import upstream version 0.8.0
236
                `eval $command -pidfile $PID_FILE -heart \
1.2.1 by Noah Slater
Import upstream version 0.9.0
237
                    >> $STDOUT_FILE 2>> $STDERR_FILE` || true
1.1.2 by Noah Slater
Import upstream version 0.8.0
238
                PID=`_get_pid`
1 by Noah Slater
Import upstream version 0.7.3~svn684
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
1.1.1 by Noah Slater
Import upstream version 0.7.3~svn650270
253
Apache CouchDB crashed, restarting in $RESPAWN_TIMEOUT second$plural_ending.
1 by Noah Slater
Import upstream version 0.7.3~svn684
254
EOF
255
                sleep $RESPAWN_TIMEOUT
256
            done
257
        else
1.1.1 by Noah Slater
Import upstream version 0.7.3~svn650270
258
            eval exec $command
1 by Noah Slater
Import upstream version 0.7.3~svn684
259
        fi
260
    fi
261
}
262
263
stop_couchdb () {
1.1.2 by Noah Slater
Import upstream version 0.8.0
264
    PID=`_get_pid`
1 by Noah Slater
Import upstream version 0.7.3~svn684
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
1.1.1 by Noah Slater
Import upstream version 0.7.3~svn650270
272
                    echo "Apache CouchDB has been shutdown."
1 by Noah Slater
Import upstream version 0.7.3~svn684
273
                else
1.1.1 by Noah Slater
Import upstream version 0.7.3~svn650270
274
                    echo "Apache CouchDB has been killed."
1 by Noah Slater
Import upstream version 0.7.3~svn684
275
                fi
276
                return $SCRIPT_OK
277
            else
1.1.1 by Noah Slater
Import upstream version 0.7.3~svn650270
278
                echo "Apache CouchDB could not be killed." >&2
1 by Noah Slater
Import upstream version 0.7.3~svn684
279
                return $SCRIPT_ERROR
280
            fi
281
            if test "$1" = "false"; then
1.1.1 by Noah Slater
Import upstream version 0.7.3~svn650270
282
                echo "Stale PID file exists but Apache CouchDB is not running."
1 by Noah Slater
Import upstream version 0.7.3~svn684
283
            else
1.1.1 by Noah Slater
Import upstream version 0.7.3~svn650270
284
                echo "Stale PID file existed but Apache CouchDB is not running."
1 by Noah Slater
Import upstream version 0.7.3~svn684
285
            fi
286
        fi
287
    else
1.1.1 by Noah Slater
Import upstream version 0.7.3~svn650270
288
        echo "Apache CouchDB is not running."
1 by Noah Slater
Import upstream version 0.7.3~svn684
289
    fi
290
}
291
292
parse_script_option_list () {
1.2.2 by Ken VanDine
Import upstream version 0.10.0~svn806985
293
    _load_config
1.1.2 by Noah Slater
Import upstream version 0.8.0
294
    set +e
1.2.2 by Ken VanDine
Import upstream version 0.10.0~svn806985
295
    options=`getopt hVa:A:ncibp:r:Ro:e:skd $@`
1.1.2 by Noah Slater
Import upstream version 0.8.0
296
    if test ! $? -eq 0; then
1 by Noah Slater
Import upstream version 0.7.3~svn684
297
        display_error
298
    fi
1.1.2 by Noah Slater
Import upstream version 0.8.0
299
    set -e
300
    eval set -- $options
1 by Noah Slater
Import upstream version 0.7.3~svn684
301
    while [ $# -gt 0 ]; do
302
        case "$1" in
1.2.2 by Ken VanDine
Import upstream version 0.10.0~svn806985
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;;
1.2.1 by Noah Slater
Import upstream version 0.9.0
309
            -i) shift; INTERACTIVE=true;;
310
            -b) shift; BACKGROUND=true;;
1 by Noah Slater
Import upstream version 0.7.3~svn684
311
            -r) shift; RESPAWN_TIMEOUT=$1; shift;;
1.2.1 by Noah Slater
Import upstream version 0.9.0
312
            -R) shift; RECURSED=true;;
1 by Noah Slater
Import upstream version 0.7.3~svn684
313
            -p) shift; PID_FILE=$1; shift;;
314
            -o) shift; STDOUT_FILE=$1; shift;;
315
            -e) shift; STDERR_FILE=$1; shift;;
1.2.2 by Ken VanDine
Import upstream version 0.10.0~svn806985
316
            -s) shift; check_status; exit;;
1.2.1 by Noah Slater
Import upstream version 0.9.0
317
            -k) shift; KILL=true;;
318
            -d) shift; SHUTDOWN=true;;
1 by Noah Slater
Import upstream version 0.7.3~svn684
319
            --) shift; break;;
320
            *) display_error "Unknown option: $1" >&2;;
321
        esac
322
    done
1.2.1 by Noah Slater
Import upstream version 0.9.0
323
    if test "$KILL" = "true" -o "$SHUTDOWN" = "true"; then
324
        stop_couchdb $KILL
1 by Noah Slater
Import upstream version 0.7.3~svn684
325
    else
326
        start_couchdb
327
    fi
328
}
329
330
parse_script_option_list $@