-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathentrypoint.sh
executable file
·246 lines (225 loc) · 6.92 KB
/
entrypoint.sh
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
#!/bin/bash
#######################################
# function retry
# Runs a command for several times and gives up with
# an error message after max retries.
#
# Arguments:
# maxretries number of retries before giving up
# errormessage error message for each failed try
# command command to be executed
#
# Returns:
# 1 on failure. 0 on success
#
# Author: Christopher Hauser <[email protected]>
#######################################
function retry(){
maxretries=$1; shift
errormessage=$1; shift
command="$@"
tries=0
while (( $tries >= 0 )); do
$command
if [[ $? != 0 ]]; then
let tries+=1
if (( $tries < $maxretries )); then
echo "$errormessage"
echo "$tries retries. retry in 5s." >&2
sleep 5
else
echo "$errormessage"
echo "$tries retries. Will give up." >&2
return 1
fi
else
tries=-1 # try success
return 0
fi
done
}
#######################################
# function initializeAddHost
# Inizialize a couchbase server: join a cluster
#
# Arguments:
# none
#
# Returns:
# 1 on failure. 0 on success
#
# Author: Christopher Hauser <[email protected]>
#######################################
function initializeAddHost(){
# validate if host is part of cluster, then skip
if [[ $(couchbase-cli server-list -c $CLUSTER -u $USER -p $PASS | grep $(hostname --ip-address):$PORT) ]]; then
echo "server is already part of the cluster. skipping."
return 0;
fi
# add host to cluster
couchbase-cli server-add -c $CLUSTER -u $USER -p $PASS \
--server-add=$(hostname --ip-address):$PORT \
--server-add-username=$USER \
--server-add-password=$PASS
if [[ $? != 0 ]]; then
echo "failed to add server to cluster." >&2
return 1
fi
# if auto rebalance is activated, start rebalancing cluster
if [[ "$AUTOREBALANCE" == "true" ]]; then
couchbase-cli rebalance -c $CLUSTER -u $USER -p $PASS
if [[ $? != 0 ]]; then
echo "failed to rebalance cluster. please trigger again manually." >&2
fi
fi
}
#######################################
# function initializeAddBuckets
# Inizialize a couchbase server: add buckets to cluster
#
# Arguments:
# none
#
# Returns:
# 1 on failure. 0 on success
#
# Authors: Christopher Hauser <[email protected]>, Jérôme Bertaux <[email protected]>
#######################################
function initializeAddBuckets(){
# validate if all env vars are present
params=("BUCKETSIZES BUCKETS")
for var in $params ; do
if [ -z ${!var+x} ]; then
echo "$var is unset. skipping initialization of buckets."
return 0
fi
done
i=0
sizes=($BUCKETSIZES)
for bucket in $BUCKETS; do
if [[ $(couchbase-cli bucket-list -c $CLUSTER -u $USER -p $PASS | grep -Fx $bucket) ]]; then
echo "bucket $bucket exists. skipping."
continue
fi
size=${sizes[$i]}
echo "create bucket $bucket with size $size"
couchbase-cli bucket-create -c $CLUSTER -u $USER -p $PASS \
--bucket=$bucket \
--bucket-type=couchbase \
--bucket-ramsize=$size \
--bucket-replica=1 \
--wait
if [[ $? != 0 ]]; then
echo "failed to create buckets." >&2
return 1
fi
echo "create user for bucket $bucket"
couchbase-cli user-manage -c $CLUSTER -u $USER -p $PASS \
--set --rbac-username $bucket --rbac-password $PASS \
--rbac-name $bucket --roles bucket_admin[$bucket],bucket_full_access[$bucket] \
--auth-domain local
if [[ $? != 0 ]]; then
echo "failed to create user bucket." >&2
return 1
fi
let i+=1
done
}
#######################################
# function initializeCluster
# Inizialize a couchbase server: create or join a cluster,
# create buckets if specified.
#
# Arguments:
# none
#
# Returns:
# 1 on failure. 0 on success
#
# Author: Christopher Hauser <[email protected]>
#######################################
function initializeCluster(){
# validate if all env vars are present
params=("CLUSTER USER PASS PORT RAMSIZEMB RAMSIZEINDEXMB RAMSIZEFTSMB")
for var in $params ; do
if [ -z ${!var+x} ]; then
echo "$var is unset. skipping initialization of cluster."
return 0
fi
done
# initialize cluster
echo "try initialization"
initOutput=$(couchbase-cli cluster-init -c $CLUSTER -u $USER -p $PASS \
--cluster-username=$USER \
--cluster-password=$PASS \
--cluster-port=$PORT \
--services=data,index,query,fts \
--cluster-ramsize=$RAMSIZEMB \
--cluster-index-ramsize=$RAMSIZEINDEXMB \
--cluster-fts-ramsize=$RAMSIZEFTSMB \
--index-storage-setting=default)
if [[ $? != 0 ]]; then
echo $initOutput
echo "failed to initialize cluster." >&2
return 1
fi
echo $initOutput
# add host to cluster
echo "try to add host (if not already added) to cluster"
retry 5 "failed to add host to cluster" initializeAddHost
if [[ $? != 0 ]]; then
echo "failed to add host to cluster." >&2
return 1
fi
}
#######################################
# function main
# Main function to bootstrap Couchbase in a
# Docker container
#
# Arguments:
# none
#
# Returns:
# none
#
# Author: Christopher Hauser <[email protected]>
#######################################
function main(){
echo "Starting Couchbase Server -- Web UI available at http://<ip>:8091 and logs available in /opt/couchbase/var/lib/couchbase/logs"
exec /usr/sbin/runsvdir-start &
if [[ $? != 0 ]]; then
echo "failed to start couchbase. exiting now." >&2
exit 1
fi
# validate if all env vars are present
params=("RETRIES_INIT RETRIES_ADDBUCKETS")
for var in $params ; do
if [ -z ${!var+x} ]; then
echo "$var is unset. skipping initialization of cluster."
return 0
fi
done
echo "now initializing couchbase ..."
retry $RETRIES_INIT "cannot initialize couchbase" initializeCluster
if [[ $? != 0 ]]; then
echo "init failed. exiting now." >&2
exit 1
fi
echo "try to create buckets"
retry $RETRIES_ADDBUCKETS "failed to add buckets" initializeAddBuckets
if [[ $? != 0 ]]; then
echo "failed to add buckets to cluster. please trigger again manually." >&2
fi
# wait for couchbase server
echo "now wait for couchbase forever"
wait
}
#######################################
# Start the magic ...
#######################################
if [[ "$1" == "couchbase-server" ]]; then
main
else
exec "$@"
fi