forked from pulcy/arangodb-cluster
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.sh
executable file
·260 lines (235 loc) · 6.3 KB
/
run.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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
#!/bin/bash
ETCDCTL=$(which etcdctl)
ETCD_PREFIX=/pulcy/arangodb3
ROLE=primary
LOGLEVEL=info
DATADIR=/var/lib/arangodb3
STATISTICS=false
export ARANGO_NO_AUTH=1
need_etcd() {
if [ -z ${ETCDCTL} ]; then
echo etcdctl is not found
exit 1
fi
if [ -z ${ETCD_URL} ]; then
echo ETCD_URL is empty
exit 1
fi
}
eget() {
local key=$1
${ETCDCTL} --endpoints=${ETCD_URL} get $key
}
eset() {
local key=$1
local value=$2
${ETCDCTL} --endpoints=${ETCD_URL} set $key $value
}
els() {
local key=$1
${ETCDCTL} --endpoints=${ETCD_URL} ls $key 2> /dev/null
}
need_instance() {
if [ -z ${INSTANCE} ]; then
echo INSTANCE is empty
exit 1
fi
INSTANCE_ID=${INSTANCE}
echo "Using instance id: ${INSTANCE_ID}"
}
need_host() {
if [ -z ${HOST} ]; then
echo HOST is empty
exit 1
fi
if [ -z ${PORT} ]; then
echo PORT is empty
exit 1
fi
}
get_agency_endpoints() {
local key=$1
agents=$(els ${ETCD_PREFIX}/agents/)
ENDPOINTS=""
for agentid in ${agents}; do
addr=$(eget $agentid)
ENDPOINTS="$ENDPOINTS $key tcp://$addr"
done
echo "Using endpoints: $ENDPOINTS"
}
init_database() {
chown -R arangodb /var/lib/arangodb3
chown -R arangodb /var/lib/arangodb3-apps
if [ ! -f $DATADIR/SERVER ]; then
echo "Initializing database...Hang on..."
arangod \
--server.statistics false \
--frontend.version-check false \
--server.authentication false \
--server.endpoint=tcp://127.0.0.1:3333 \
--database.directory $DATADIR \
--log.file /tmp/init-log \
--log.foreground-tty false &
pid="$!"
echo "arangod pid: $pid"
counter=0
ARANGO_UP=""
while [ -z "$ARANGO_UP" ];do
sleep 1
if [ "$counter" -gt 100 ];then
echo "ArangoDB didn't start correctly during init"
cat /tmp/init-log
exit 1
fi
let counter=counter+1
version=$(curl --noproxy localhost -s http://localhost:3333/_api/version 2>/dev/null)
if [ ! -z "$version" ]; then
ARANGO_UP=1
echo "Arango is up: $version"
fi
done
for f in /docker-entrypoint-initdb.d/*; do
case "$f" in
*.sh) echo "$0: running $f"; . "$f" ;;
*.js) echo "$0: running $f"; arangosh --javascript.execute "$f" ;;
*/dumps) echo "$0: restoring databases"; for d in $f/*; do echo "restoring $d";arangorestore --server.endpoint=tcp://127.0.0.1:8529 --create-database true --include-system-collections true --input-directory $d; done; echo ;;
esac
done
echo "Stopping init arangod with pid $pid"
if ! kill -s TERM "$pid" || ! wait "$pid"; then
echo >&2 'ArangoDB Init failed.'
exit 1
fi
echo "Database initialized...Starting System..."
fi
}
run_agency() {
init_database
eset "${ETCD_PREFIX}/agents/agency${INSTANCE_ID}" "$HOST:$PORT"
ENDPOINTS=""
NOTIFY=""
#if [ ${INSTANCE} -eq 3 ]; then
get_agency_endpoints "--agency.endpoint"
NOTIFY="--agency.notify true"
#fi
exec arangod \
--frontend.version-check false \
--log.level "${LOGLEVEL}" \
--database.directory $DATADIR \
--server.endpoint "http+tcp://0.0.0.0:8529" \
--server.authentication false \
--server.statistics="${STATISTICS}" \
--cluster.my-address "tcp://$HOST:$PORT" \
--agency.id "${INSTANCE_ID}" \
--agency.size 3 \
--agency.supervision true \
--agency.wait-for-sync true \
$NOTIFY $ENDPOINTS
}
wait_for_agency() {
echo "Waiting for agency..."
while true ; do
has_ready_agents=""
agents=$(els ${ETCD_PREFIX}/agents/)
for agentid in ${agents}; do
addr=$(eget $agentid)
curl -s -f -X GET "http://$addr/_api/version" > /dev/null 2>&1
if [ "$?" != "0" ] ; then
echo Server on address $addr does not answer yet.
else
echo Server on address $addr is ready for business.
has_ready_agents="1"
break
fi
done
if [ ! -z "$has_ready_agents" ]; then
break
fi
sleep 1
done
}
run_primary() {
init_database
wait_for_agency
get_agency_endpoints "--cluster.agency-endpoint"
exec arangod \
--frontend.version-check false \
--log.level "${LOGLEVEL}" \
--database.directory $DATADIR \
--server.authentication=false \
--server.endpoint "http+tcp://0.0.0.0:8529" \
--server.statistics="${STATISTICS}" \
--cluster.my-address "tcp://$HOST:$PORT" \
--cluster.my-local-info "primary${INSTANCE_ID}" \
--cluster.my-role "PRIMARY" \
$ENDPOINTS
}
run_coordinator() {
init_database
wait_for_agency
get_agency_endpoints "--cluster.agency-endpoint"
exec arangod \
--frontend.version-check false \
--log.level="${LOGLEVEL}" \
--database.directory $DATADIR \
--server.authentication false \
--server.endpoint="http+tcp://0.0.0.0:8529" \
--server.statistics="${STATISTICS}" \
--cluster.my-address="tcp://$HOST:$PORT" \
--cluster.my-local-info "coordinator${INSTANCE_ID}" \
--cluster.my-role "COORDINATOR" \
$ENDPOINTS
}
for i in "$@"
do
case $i in
--host=*)
HOST="${i#*=}"
;;
--port=*)
PORT="${i#*=}"
;;
--instance=*)
INSTANCE="${i#*=}"
;;
--role=*)
ROLE="${i#*=}"
;;
--etcd-prefix=*)
ETCD_PREFIX="${i#*=}"
;;
--etcd-url=*)
ETCD_URL="${i#*=}"
;;
--log-level=*)
LOGLEVEL="${i#*=}"
;;
--statistics=*)
STATISTICS="${i#*=}"
;;
*)
echo "unknown option '${i}'"
;;
esac
done
need_etcd
need_host
need_instance
mkdir -p $DATADIR
mkdir -p /var/lib/arangodb3-apps
chown -R arangodb $DATADIR
chown -R arangodb /var/lib/arangodb3-apps
case ${ROLE} in
agency)
run_agency
;;
primary)
run_primary
;;
coordinator)
run_coordinator
;;
*)
echo "unknown role '${ROLE}'"
;;
esac