-
Notifications
You must be signed in to change notification settings - Fork 26
/
couchdb.postinst
279 lines (242 loc) · 7.78 KB
/
couchdb.postinst
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
#!/bin/sh
# 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.
set -e
# without this, debconf doesn't run on initial install
. /usr/share/debconf/confmodule
db_get couchdb/mode && mode="$RET"
setadminpass() {
# don't try and set a password if debconf didn't get one
# this allows Admin Party if a blank password is entered
db_get couchdb/adminpass && adminpw="$RET"
if [ -z "${adminpw}" ]; then
return 0
fi
# don't overwrite an admin password already in an ini file
for i in /opt/couchdb/etc/default.ini \
/opt/couchdb/etc/default.d/*.ini \
/opt/couchdb/etc/local.ini \
/opt/couchdb/etc/local.d/*.ini; do
if grep -q '^admin \?= ' $i >/dev/null 2>&1; then
return 0
fi
done
# forget we ever saw the password.
# don't use reset, to keep the seen status
db_set couchdb/adminpass ""
db_set couchdb/adminpass_again ""
# this avoids us having to call "curl" with $adminpw
# CouchDB hashes the password for us on startup
adminsinifile='
# Package-introduced administrative user
[admins]
admin = '"${adminpw}"'
'
echo "${adminsinifile}" > /opt/couchdb/etc/local.d/10-admins.ini
}
setbindaddress() {
db_fget "couchdb/bindaddress" seen || true
bindaddress_set="$RET"
if [ "${bindaddress_set}" != "true" ]; then
return 0
fi
# don't overwrite a bind address already in an ini file
# other than default.ini
for i in /opt/couchdb/etc/default.d/*.ini \
/opt/couchdb/etc/local.ini \
/opt/couchdb/etc/local.d/*.ini; do
if grep -q '^bind_address \?= ' $i >/dev/null 2>&1; then
return 0
fi
done
db_get couchdb/bindaddress && bindaddr="$RET"
bindinifile='
# Package-introduced bind address
[chttpd]
bind_address = '"${bindaddr}"'
'
echo "${bindinifile}" > /opt/couchdb/etc/default.d/10-bind-address.ini
}
setsinglenode() {
# don't overwrite an n value already in an ini file
# other than default.ini
for i in /opt/couchdb/etc/default.d/*.ini \
/opt/couchdb/etc/local.ini \
/opt/couchdb/etc/local.d/*.ini; do
if grep -q '^n \?= ' $i >/dev/null 2>&1; then
return 0
fi
done
singlenodeinifile='
# Package-introduced single node file
[cluster]
n = 1
'
echo "${singlenodeinifile}" > /opt/couchdb/etc/default.d/5-single-node.ini
}
createdb() {
# see if db already exists; if not, create DB
db=$1
db_get couchdb/bindaddress && ip="$RET"
if [ "${ip}" = "0.0.0.0" ]; then
ip=127.0.0.1
fi
if [ ! -z "${adminpw}" ]; then
url="http://admin:${adminpw}@${ip}:5984"
else
url="http://${ip}:5984"
fi
if curl -s "${url}/${db}" | grep -q "${db}"; then
return 0
fi
curl -s -X PUT "${url}/${db}" >/dev/null 2>&1 || warnfail
return $?
}
warnfail() {
# print a warning and exit without error
cat <<EOF
WARNING: Unable to create standalone system databases.
CouchDB may not have started correctly (no init?)
Once CouchDB has started correctly, run the following:
curl -X PUT http://<admin-user>:<admin-pass>@127.0.0.1:5984/_users
curl -X PUT http://<admin-user>:<admin-pass>@127.0.0.1:5984/_replicator
EOF
exit 0
}
case $1 in
configure)
if dpkg --compare-versions "$2" lt-nl 1.2.0-2ubuntu1; then
# Hack to make the upgrade from the 1.0.1 Ubuntu package more correct:
chown root:root /etc/couchdb
chown root:root /etc/couchdb/default.ini
chown root:root /etc/couchdb/default.d
chown root:root /etc/couchdb/default.d/*
chmod 755 /etc/couchdb
chmod 644 /etc/couchdb/default.ini
chmod 755 /etc/couchdb/default.d
fi
if ! getent passwd couchdb > /dev/null; then
adduser --system --quiet \
--home /opt/couchdb --no-create-home \
--disabled-login --disabled-password \
--shell /bin/bash --group \
--gecos "CouchDB Administrator" \
couchdb
fi
if test "`id -u couchdb`" -eq 0; then
echo "The couchdb administrative user must not be root." >&2
false
fi
if test "`id -g couchdb`" -eq 0; then
echo "The couchdb administrative group must not be root." >&2
false
fi
# Set the cookie
db_get couchdb/cookie && cookie="$RET"
escaped_cookie=$(printf '%s' "$cookie" | sed 's:\\:\\\\:g')
# change existing setcookie line if present
sed -i "/^[# ]*-setcookie/c\-setcookie '${escaped_cookie}'" /opt/couchdb/etc/vm.args
# add setcookie line if not present
if ! grep -q '^-setcookie' /opt/couchdb/etc/vm.args; then
printf -- '-setcookie %s\n' "'${cookie}'" >> /opt/couchdb/etc/vm.args
fi
# set inet_dist_use_interface default if not present
if ! grep -q '^-kernel inet_dist_use_interface' /opt/couchdb/etc/vm.args; then
echo "-kernel inet_dist_use_interface '{127,0,0,1}'" >> /opt/couchdb/etc/vm.args
fi
case $mode in
none)
;;
standalone)
setbindaddress
setsinglenode
if ! setadminpass; then
password_error="yes"
fi
# Bind distribution port to loopback interface only
sed -i "s/^-kernel inet_dist_use_interface '*{0,0,0,0}'*$/-kernel inet_dist_use_interface '{127,0,0,1}'/" /opt/couchdb/etc/vm.args
# Bind EPMD to loopback interface
sed -i "s/^ERL_EPMD_ADDRESS=$/ERL_EPMD_ADDRESS=127.0.0.1/" /etc/default/couchdb
;;
clustered)
db_get couchdb/nodename && nodename="$RET"
sed -i "/^-name/c\-name ${nodename}" /opt/couchdb/etc/vm.args
setbindaddress
if ! setadminpass; then
password_error="yes"
fi
# Unbind distribution port from lookback interface only
sed -i "s/^-kernel inet_dist_use_interface '*{127,0,0,1}'*$/-kernel inet_dist_use_interface '{0,0,0,0}'/" /opt/couchdb/etc/vm.args
# Unbind EPMD from loopback interface
sed -i "s/^ERL_EPMD_ADDRESS=127.0.0.1$/ERL_EPMD_ADDRESS=/" /etc/default/couchdb
;;
*)
echo "incomprehensible couchdb mode '$mode'!" 1>&2
exit 1
;;
esac
# These should be owned by the couchdb user and group:
for i in "/opt/couchdb/etc
/opt/couchdb/etc/*
/opt/couchdb/etc/default.d/*
/opt/couchdb/etc/local.d/*
/var/lib/couchdb
/var/lib/couchdb/*
/var/lib/couchdb/shards/*
/var/lib/couchdb/shards/*/*
/var/lib/couchdb/.shards/*
/var/lib/couchdb/.shards/*/*
/var/log/couchdb
/var/log/couchdb/*"
do
chown couchdb:couchdb $i >/dev/null 2>&1 || true
done
# These should also not be world readable or writable:
find /opt/couchdb/etc -name '*.ini' -o -name vm.args -exec chmod 0640 {} \;
chmod 0750 /var/log/couchdb
chmod 0750 /var/lib/couchdb
if [ "${password_error}" = "yes" ]; then
db_input high couchdb/error_setting_password || true
db_go
fi
if ls /var/lib/couchdb/[A-Za-z0-9]*.couch >/dev/null 2>&1; then
db_input high couchdb/have_1x_databases || true
db_go
fi
;;
abort-upgrade|abort-remove|abort-configure)
;;
*)
echo "postinst called with unknown argument '$1'" 1>&2
exit 1
;;
esac
#DEBHELPER#
case $1 in
configure)
# just in case - CouchDB can be slow to startup
sleep 5
# if standalone, create _users and _replicator DBs
case $mode in
standalone)
createdb _users
createdb _replicator
;;
clustered)
;;
*)
;;
esac
;;
*)
;;
esac