forked from stellar/quickstart
-
Notifications
You must be signed in to change notification settings - Fork 0
/
start
351 lines (281 loc) · 7.61 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
#! /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 PGBIN="/usr/lib/postgresql/9.6/bin"
export PGDATA="$PGHOME/data"
export PGUSER="stellar"
export PGPORT=5432
QUICKSTART_INITIALIZED=false
CURRENT_POSTGRES_PID=""
function main() {
echo ""
echo "Starting Stellar Quickstart"
echo ""
process_args $*
echo "mode: $STELLAR_MODE"
echo "network: $NETWORK ($NETWORK_PASSPHRASE)"
copy_defaults
init_db
init_stellar_core
init_horizon
copy_pgpass
stop_postgres # this gets started in init_db
# launch services
exec_supervisor
}
function process_args() {
while [[ -n "$1" ]]; do
ARG="$1"
shift
case "${ARG}" in
--testnet)
NETWORK="testnet"
;;
--pubnet)
NETWORK="pubnet"
;;
--standalone)
NETWORK="standalone"
;;
--protocol-version)
export PROTOCOL_VERSION="$1"
shift
;;
--enable-asset-stats)
export ENABLE_ASSET_STATS="$1"
shift
;;
*)
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"
;;
standalone)
NETWORK_PASSPHRASE="Standalone Network ; February 2017"
;;
*)
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() {
if [ -n "$POSTGRES_PASSWORD" ]; then
PGPASS=$POSTGRES_PASSWORD
echo "using POSTGRES_PASSWORD"
return 0
fi
# 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
}
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_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 "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;
SQL
touch .quickstart-initialized
popd
}
function init_stellar_core() {
if [ -f $COREHOME/.quickstart-initialized ]; then
echo "core: already initialized"
if [ "$NETWORK" == "standalone" ]; then
start_postgres
run_silent "init-core-scp" sudo -u stellar stellar-core --forcescp --conf $COREHOME/etc/stellar-core.cfg
fi
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
if [ "$NETWORK" == "standalone" ]; then
run_silent "init-core-scp" sudo -u stellar stellar-core --forcescp --conf etc/stellar-core.cfg
fi
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 upgrade_standalone() {
# Upgrade standalone network's protocol version
if [ "$NETWORK" = "standalone" ]; then
# Wait for server
while ! echo "Stellar-core http server listening!" | nc localhost 11626 &> /dev/null; do sleep 1; done
if [ -z "$PROTOCOL_VERSION" ]; then
# default to latest version supported by core
export PROTOCOL_VERSION=`curl -s http://localhost:11626/info | jq -r '.info.protocol_version'`
fi
if [ ".$PROTOCOL_VERSION" != ".none" ] ; then
if [ $PROTOCOL_VERSION -gt 0 ]; then
curl "http://localhost:11626/upgrades?mode=set&upgradetime=1970-01-01T00:00:00Z&protocolversion=$PROTOCOL_VERSION"
fi
fi
fi
}
function exec_supervisor() {
echo "starting supervisor"
upgrade_standalone &
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() {
if [ -z "$CURRENT_POSTGRES_PID" ]; then
return 0
fi
killall postgres
# wait for postgres to die
while kill -0 "$CURRENT_POSTGRES_PID" &> /dev/null; do
sleep 0.5
done
echo "postgres: down"
}
pushd () {
command pushd "$@" > /dev/null
}
popd () {
command popd "$@" > /dev/null
}
main $@