-
Notifications
You must be signed in to change notification settings - Fork 13
/
start
392 lines (312 loc) · 9.08 KB
/
start
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
#! /usr/bin/env bash
set -e
export STELLAR_HOME="/opt/stellar"
export PGHOME="$STELLAR_HOME/postgresql"
export SUPHOME="$STELLAR_HOME/supervisor"
export COREHOME="$STELLAR_HOME/core"
export HZHOME="$STELLAR_HOME/horizon"
export BRIDGEHOME="$STELLAR_HOME/bridge"
export FRIENDBOTHOME="$STELLAR_HOME/friendbot"
export PGBIN="/usr/lib/postgresql/9.6/bin"
export PGDATA="$PGHOME/data"
export PGUSER="stellar"
export PGPORT=5432
QUICKSTART_INITIALIZED=false
CURRENT_POSTGRES_PID=""
# NOTE: not configurable, generated by network and updated in init_stellar_core
ROOT_ACCOUNT_SEED=""
function main() {
echo ""
echo "Starting Stellar Quickstart"
echo ""
process_args $*
echo "mode: $STELLAR_MODE"
echo "network: $NETWORK ($NETWORK_PASSPHRASE)"
# custom supervisord scripts
init_supervisor_custom
copy_defaults
init_db
init_stellar_core
init_bridge_server # must come after init_stellar_core!
init_horizon
init_friendbot
copy_pgpass
stop_postgres # this gets started in init_db
echo "**************************************************************"
echo "Starting Stellar services..."
echo ""
echo "Stellar Core v10.0.0-685-1fc018b4"
echo "Horizon v0.15.1"
echo "Bridge v0.0.31"
echo "Friendbot v0.0.1"
echo "**************************************************************"
# launch services
exec_supervisor
}
function process_args() {
while [[ -n "$1" ]]; do
ARG="$1"
shift
case "${ARG}" in
--testnet)
NETWORK="testnet"
;;
--pubnet)
NETWORK="pubnet"
;;
--integrationnet)
NETWORK="integrationnet"
;;
*)
echo "Unknown container arg $ARG" >&2
exit 1
esac
done
# TODO: ask for what network to use
if [ -z "$NETWORK" ]; then
NETWORK="testnet"
fi
case "$NETWORK" in
testnet)
export NETWORK_PASSPHRASE="Test SDF Network ; September 2015"
;;
pubnet)
export NETWORK_PASSPHRASE="Public Global Stellar Network ; September 2015"
;;
integrationnet)
export NETWORK_PASSPHRASE="Integration Test Network ; zulucrypto"
;;
*)
echo "Unknown network: '$NETWORK'" >&2
exit 1
esac
# Are we ephemeral or persistent?
if [ -z "$STELLAR_MODE" ]; then
if [ -f "/opt/stellar/.docker-ephemeral" ]; then
STELLAR_MODE="ephemeral"
else
STELLAR_MODE="persistent"
fi
fi
}
function set_pg_password() {
# use a random password when ephemeral (or some other unknown mode)
if [ "$STELLAR_MODE" != "persistent" ]; then
PGPASS=$(head /dev/urandom | tr -dc 'a-zA-Z0-9' | head -c 16)
echo "postgres password: $PGPASS"
return 0
fi
if [ -n "$PGPASS" ]; then
echo "postgres password: $PGPASS"
return 0
fi
# ask for a password when persistent
read -s -p "Enter New Postgresql Password: " PGPASS
echo ""
read -s -p "Confirm: " PGPASS_CONFIRMATION
echo ""
if [ -z "$PGPASS" ]; then
echo "Password empty" >&2
exit 1
fi
if [ "$PGPASS" != "$PGPASS_CONFIRMATION" ]; then
echo "Password mistmach" >&2
exit 1
fi
}
function copy_defaults() {
local CP="rsync -a"
if [ -d $PGHOME/etc ]; then
echo "postgres: config directory exists, skipping copy"
else
$CP /opt/stellar-default/common/postgresql/ $PGHOME
fi
if [ -d $SUPHOME/etc ]; then
echo "supervisor: config directory exists, skipping copy"
else
$CP /opt/stellar-default/common/supervisor/ $SUPHOME
fi
if [ -d $COREHOME/etc ]; then
echo "stellar-core: config directory exists, skipping copy"
else
$CP /opt/stellar-default/common/core/ $COREHOME
$CP /opt/stellar-default/$NETWORK/core/ $COREHOME
fi
if [ -d $HZHOME/etc ]; then
echo "horizon: config directory exists, skipping copy"
else
$CP /opt/stellar-default/common/horizon/ $HZHOME
fi
if [ -d $BRIDGEHOME/etc ]; then
echo "bridge: config directory exists, skipping copy"
else
$CP /opt/stellar-default/common/bridge/ $BRIDGEHOME
$CP /opt/stellar-default/$NETWORK/bridge/ $BRIDGEHOME
fi
}
function copy_pgpass() {
local CP="rsync -a"
$CP /opt/stellar/postgresql/.pgpass /root/
chmod 0600 /root/.pgpass
$CP /opt/stellar/postgresql/.pgpass /home/stellar
chmod 0600 /home/stellar/.pgpass
chown stellar:stellar /home/stellar/.pgpass
}
function init_supervisor_custom() {
local CP="rsync -a"
if [ "$NETWORK" == "integrationnet" ]; then
echo "init-supervisor-custom: copying integrationnet scripts"
$CP /opt/stellar-default/$NETWORK/supervisor.d/ /etc/supervisor.d/
$CP /opt/stellar-default/$NETWORK/home/fixtures/ /home/stellar/fixtures/
fi
}
function init_db() {
if [ -f $PGHOME/.quickstart-initialized ]; then
echo "postgres: already initialized"
return 0
fi
pushd $PGHOME
# workaround!!!! from: https://github.com/nimiq/docker-postgresql93/issues/2
mkdir /etc/ssl/private-copy; mv /etc/ssl/private/* /etc/ssl/private-copy/; rm -r /etc/ssl/private; mv /etc/ssl/private-copy /etc/ssl/private; chmod -R 0700 /etc/ssl/private; chown -R postgres /etc/ssl/private
# end workaround
echo "postgres user: $PGUSER"
set_pg_password
run_silent "finalize-pgpass" sed -ri "s/__PGPASS__/$PGPASS/g" /opt/stellar/postgresql/.pgpass
mkdir -p $PGDATA
chown postgres:postgres $PGDATA
chmod 0700 $PGDATA
run_silent "init-postgres" sudo -u postgres $PGBIN/initdb -D $PGDATA
start_postgres
run_silent "create-horizon-db" sudo -u postgres createdb horizon
run_silent "create-core-db" sudo -u postgres createdb core
run_silent "create-bridge-db" sudo -u postgres createdb bridge
run_silent "stellar-postgres-user" sudo -u postgres psql <<-SQL
CREATE USER $PGUSER WITH PASSWORD '$PGPASS';
GRANT ALL PRIVILEGES ON DATABASE horizon to $PGUSER;
GRANT ALL PRIVILEGES ON DATABASE core to $PGUSER;
GRANT ALL PRIVILEGES ON DATABASE bridge to $PGUSER;
SQL
touch .quickstart-initialized
popd
}
function init_stellar_core() {
if [ -f $COREHOME/.quickstart-initialized ]; then
echo "core: already initialized"
return 0
fi
pushd $COREHOME
run_silent "chown-core" chown stellar:stellar .
run_silent "finalize-core-config" sed -ri "s/__PGPASS__/$PGPASS/g" etc/stellar-core.cfg
start_postgres
run_silent "init-core-db" sudo -u stellar stellar-core --newdb --conf etc/stellar-core.cfg
# get master seed from the file that run_silent generates
ROOT_ACCOUNT_SEED=$(grep "Root account seed" /tmp/run_silent.out | cut -d' ' -f 8)
echo "Root Seed: ${ROOT_ACCOUNT_SEED}"
# integration test network is standalone and needs additional setup
if [ "$NETWORK" == "integrationnet" ]; then
run_silent "init-core-scp" sudo -u stellar stellar-core --forcescp --conf etc/stellar-core.cfg
fi
touch .quickstart-initialized
popd
}
# NOTE: must be run after init_stellar_core since it requires $ROOT_ACCOUNT_SEED
function init_bridge_server() {
if [ -f $BRIDGEHOME/.quickstart-initialized ]; then
echo "bridge: already initialized"
return 0
fi
pushd $BRIDGEHOME
run_silent "chown-bridge" chown stellar:stellar .
sed -ri \
-e "s/__PGPASS__/$PGPASS/g" \
-e "s/__NETWORK__/$NETWORK_PASSPHRASE/g" \
-e "s/__ROOT_ACCOUNT_SEED__/$ROOT_ACCOUNT_SEED/g" \
etc/bridge.cfg
touch .quickstart-initialized
popd
}
function init_horizon() {
if [ -f $HZHOME/.quickstart-initialized ]; then
echo "horizon: already initialized"
return 0
fi
pushd $HZHOME
run_silent "chown-horizon" chown stellar:stellar .
sed -ri \
-e "s/__PGPASS__/$PGPASS/g" \
-e "s/__NETWORK__/$NETWORK_PASSPHRASE/g" \
etc/horizon.env
start_postgres
run_silent "init-horizon-db" sudo -u stellar ./bin/horizon db init
touch .quickstart-initialized
popd
}
function init_friendbot() {
if [ -f $FRIENDBOTHOME/.quickstart-initialized ]; then
echo "friendbot: already initialized"
return 0
fi
cp -R /opt/stellar-default/common/friendbot/ $FRIENDBOTHOME
pushd $FRIENDBOTHOME
run_silent "chown-friendbot" chown stellar:stellar .
touch .quickstart-initialized
popd
}
function exec_supervisor() {
echo "starting suervisor"
exec supervisord -n -c $SUPHOME/etc/supervisord.conf
}
# run_silent is a utility function that runs a command with an abbreviated
# output provided it succeeds.
function run_silent() {
local LABEL=$1
shift
local COMMAND=$1
shift
local ARGS=$@
local OUTFILE="/tmp/run_silent.out"
echo -n "$LABEL: "
set +e
$COMMAND $ARGS &> $OUTFILE
if [ $? -eq 0 ]; then
echo "ok"
else
echo "failed!"
echo ""
cat $OUTFILE
exit 1
fi
set -e
}
function start_postgres() {
if [ ! -z "$CURRENT_POSTGRES_PID" ]; then
return 0
fi
sudo -u postgres $PGBIN/postgres -D $PGDATA -c config_file=$PGHOME/etc/postgresql.conf &> /dev/null &
CURRENT_POSTGRES_PID=$!
while ! sudo -u postgres psql -c 'select 1' &> /dev/null ; do
echo "Waiting for postgres to be available..."
sleep 1
done
echo "postgres: up"
}
function stop_postgres() {
echo "Stopping: '$CURRENT_POSTGRES_PID'"
if [ -z "$CURRENT_POSTGRES_PID" ]; then
return 0
fi
# wait for postgres to die
while kill -0 "$CURRENT_POSTGRES_PID" &> /dev/null; do
# If kill doesn't work, use "killall" on subsequent runs since the PID may be invalid
killall postgres
sleep 5
done
echo "postgres: down"
}
pushd () {
command pushd "$@" > /dev/null
}
popd () {
command popd "$@" > /dev/null
}
main $@