-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsetup.sh
executable file
·429 lines (354 loc) · 13.2 KB
/
setup.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
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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
#!/bin/bash
# This script is used to setup all servers in one host.
# Usage:
# ./setup.sh - Compile using the trial of each service image.
# And start them up. The configuration
# file for the service comes from the trial directory
# of this project.
# ./setup.sh -c - Clean the environmen
# ./setup.sh -q - quick setup, skip the pull stage.
# Colour codes
YELLOW='\033[0;33m'
RED='\033[0;31m'
NC='\033[0m' # No Color
WHITE_LINE="echo"
BRANCH="trial"
IOTEX_MODE="one-node"
SKIP_PULL_IMAGE=0
WANT_CLEAN=0
function usage () {
echo ' Usage:
./setup.sh - Compile using the trial of each service image.
And start them up. The configuration
file for the service comes from the trial directory
of this project.
./setup.sh -c - Clean the environmen
./setup.sh -q - quick setup, skip the pull stage.
'
exit 2
}
pushd () {
command pushd "$@" > /dev/null
}
popd () {
command popd "$@" > /dev/null
}
function checkDockerPermissions() {
docker ps > /dev/null
if [ $? = 1 ];then
echo -e "your $RED [$USER] $NC not privilege docker"
echo -e "please run $RED [sudo bash] $NC first"
echo -e "Or docker not install "
exit 1
fi
}
function checkDockerCompose() {
docker-compose --version > /dev/null 2>&1
if [ $? -eq 127 ];then
echo -e "$RED docker-compose command not found $NC"
echo -e "Please install it first"
exit 1
fi
}
function setVar4Trial() {
IOTEX_IMAGE=363205959602.dkr.ecr.us-east-1.amazonaws.com/iotex-core:latest
IOTEX_ELECTION_IMAGE=363205959602.dkr.ecr.us-east-1.amazonaws.com/iotex-election:latest
IOTEX_ANALYTICS_IMAGE=363205959602.dkr.ecr.us-east-1.amazonaws.com/iotex-analytics:latest
IOTEX_XUN_EXPLORER_IMAGE=363205959602.dkr.ecr.us-east-1.amazonaws.com/xun-explorer:latest
IOTEX_HIGH_TABLE_IMAGE=363205959602.dkr.ecr.us-east-1.amazonaws.com/high-table:latest
IOTEX_GOLDEN_GATE_IMAGE=363205959602.dkr.ecr.us-east-1.amazonaws.com/golden-gate:latest
}
function setVar() {
producerPrivKey=""
externalHost=""
defaultdatadir="$HOME/iotex-var"
COPY="cp -vf"
DOCKER_PULL_CMD="docker pull"
RM="sudo rm -rf"
PROJECT_ABS_DIR=$(cd "$(dirname "$0")";pwd)
IOTEX_DOCKER_COMPOSE_DIR=docker-compose
IOTEX_MONITOR_IMAGE=iotex/iotex-monitor:v1
IOTEX_VAULT_IMAGE=vault:1.3.2
IOTEX_DB_IMAGE=mysql:8.0.19
IOTEX_ENVOY_IMAGE=envoyproxy/envoy-alpine:d920944aed67425f91fc203774aebce9609e5d9a
setVar4Trial
}
function determinIotexHome() {
##Input Data Dir
echo "The current user of the input directory must have write permission!!!"
echo -e "${RED} Input your directory \$IOTEX_HOME !!! ${NC}"
#while True: do
read -p "Input your \$IOTEX_HOME [e.g., $defaultdatadir]: " inputdir
IOTEX_HOME=${inputdir:-"$defaultdatadir"}
}
function confirmEnvironmentVariable() {
echo -e "Confirm IOTEX_HOME directory: ${RED} ${IOTEX_HOME} ${NC}"
read -p "Press any key to continue ... [Ctrl + c exit!] " key1
}
function determinIotexAnalyticsDatabaseRootPass() {
##Input Database root password
echo "The database of the input password is used to the Analytics service."
echo -e "${RED} Set the password: [ Default is 'rootuser' ]${NC}"
read -s userpass
DB_ROOT_PASSWORD=${userpass:-"rootuser"}
}
function makeDbWorkspace() {
mkdir -p data/mysql
}
function makeCoreWorkspace() {
mkdir -p data/core log/core etc/core
}
function makeVaultWorkspace() {
mkdir -p etc/vault data/vault log/vault
}
function makeHighTableWorkspace() {
mkdir -p data/high-table etc/high-table key/high-table
}
function makeMonitorWorkspace() {
mkdir -p etc/monitor
}
function makeGoldenGateWorkspace() {
mkdir -p etc/golden-gate
}
function makeEnvoyWorkspace() {
mkdir -p etc/envoy
}
function makeAnalyticsWorkspace() {
mkdir -p etc/analytics
}
function makeElectionWorkspace() {
mkdir -p etc/election
}
function makeWorkspace() {
mkdir -p ${IOTEX_HOME}
pushd ${IOTEX_HOME}
makeDbWorkspace
makeCoreWorkspace
makeVaultWorkspace
makeHighTableWorkspace
makeMonitorWorkspace
makeGoldenGateWorkspace
makeEnvoyWorkspace
makeAnalyticsWorkspace
makeElectionWorkspace
mkdir -p docker-compose
popd
}
function copyConfig() {
if [ ! -d $PROJECT_ABS_DIR/$1 ];then
echo "branch $1 is not supported now."
exit 2
fi
echo -e "$YELLOW Copy the configure files... $NC"
$COPY $PROJECT_ABS_DIR/$1/core-config.yaml $IOTEX_HOME/etc/core/config.yaml
$COPY $PROJECT_ABS_DIR/$1/genesis.yaml $IOTEX_HOME/etc/core/genesis.yaml
$COPY $PROJECT_ABS_DIR/$1/prometheus-one-node.yml $IOTEX_HOME/etc/monitor/prometheus.yml
$COPY $PROJECT_ABS_DIR/$1/election-config.yaml $IOTEX_HOME/etc/election/config.yaml
$COPY $PROJECT_ABS_DIR/$1/analytics-config.yaml $IOTEX_HOME/etc/analytics/config.yaml
$COPY $PROJECT_ABS_DIR/$1/high-table-ec256-private.test.pem $IOTEX_HOME/key/high-table/ec256-private.test.pem
$COPY $PROJECT_ABS_DIR/$1/high-table.json $IOTEX_HOME/etc/high-table/high-table.json
$COPY $PROJECT_ABS_DIR/$1/envoy.yaml $IOTEX_HOME/etc/envoy/envoy.yaml
$COPY $PROJECT_ABS_DIR/$1/golden-gate-config.yaml $IOTEX_HOME/etc/golden-gate/config.yaml
$COPY $PROJECT_ABS_DIR/$1/admin-policy.hcl $IOTEX_HOME/etc/vault/admin-policy.hcl
$COPY $PROJECT_ABS_DIR/$1/regular-policy.hcl $IOTEX_HOME/etc/vault/regular-policy.hcl
$COPY $PROJECT_ABS_DIR/docker-compose/docker-compose-one-node.yml $IOTEX_HOME/$IOTEX_DOCKER_COMPOSE_DIR/docker-compose.yml
$COPY $PROJECT_ABS_DIR/docker-compose/.env $IOTEX_HOME/$IOTEX_DOCKER_COMPOSE_DIR/.env
echo -e "$YELLOW Copy done. $NC"
$WHITE_LINE
}
function pullImage(){
echo -e "$YELLOW docker pull $1... $NC"
$DOCKER_PULL_CMD $1
echo -e "$YELLOW Pull $1 done. $NC"
$WHITE_LINE
}
function pullImages() {
pullImage $IOTEX_MONITOR_IMAGE
pullImage $IOTEX_DB_IMAGE
pullImage $IOTEX_VAULT_IMAGE
pullImage $IOTEX_ENVOY_IMAGE
}
function pullTrialImages() {
pullImage $IOTEX_IMAGE
pullImage $IOTEX_ELECTION_IMAGE
pullImage $IOTEX_ANALYTICS_IMAGE
pullImage $IOTEX_XUN_EXPLORER_IMAGE
pullImage $IOTEX_HIGH_TABLE_IMAGE
pullImage $IOTEX_GOLDEN_GATE_IMAGE
}
function exportAll() {
export IOTEX_HOME DB_ROOT_PASSWORD IOTEX_IMAGE IOTEX_ELECTION_IMAGE IOTEX_ANALYTICS_IMAGE IOTEX_XUN_EXPLORER_IMAGE IOTEX_HIGH_TABLE_IMAGE IOTEX_GOLDEN_GATE_IMAGE IOTEX_VAULT_IMAGE IOTEX_ENVOY_IMAGE IOTEX_DB_IMAGE IOTEX_MONITOR_IMAGE
}
function startup() {
echo -e "$YELLOW Start iotex-server, iotex-election, iotex-analytics and it's database, iotex-explorer and iotex-server monitor. $NC"
pushd $IOTEX_HOME/$IOTEX_DOCKER_COMPOSE_DIR
docker-compose up -d --no-recreate
if [ $? -eq 0 ];then
echo -e "${YELLOW} To view blockchain explorer, visit: http://localhost:4004. For first time usage, you will need to create a user ${NC}"
echo ""
echo -e "${YELLOW} To view blockchain status dashboard, visit: localhost:3000. The default User/Pass: admin/admin. ${NC}"
fi
popd
}
function grantPrivileges() {
echo -e "$YELLOW Starting database...$NC"
# maxRetryTime * sleeptime = timeout
retryTimes=0
maxRetryTime=10
pushd $IOTEX_HOME/$IOTEX_DOCKER_COMPOSE_DIR
docker-compose up -d database
echo -e "$YELLOW Waiting for the mysqld daemon in the iotex-db container to successful... $NC"
while true;do
if [ $retryTimes -gt $maxRetryTime ];then
echo -e "$RED Start mysql server container faild. $NC"
echo -e "$RED Please check its logs by command \"docker logs iotex-db\" $NC"
exit 1
fi
docker exec iotex-db mysql -uroot -p${DB_ROOT_PASSWORD} -e "\q" > /dev/null 2>&1
if [ $? -eq 0 ];then
break
fi
retryTimes=$((retryTimes+1))
sleep 4
done
popd
echo -e "$YELLOW Success! $NC"
docker exec iotex-db mysql -uroot -p${DB_ROOT_PASSWORD} -e "GRANT ALL PRIVILEGES ON *.* TO 'root'@'%'" > /dev/null 2>&1
$WHITE_LINE
}
function initVault() {
echo -e "$YELLOW Start vault server... $NC"
pushd $IOTEX_HOME/$IOTEX_DOCKER_COMPOSE_DIR
docker-compose up -d vault
popd
# maxRetryTime * sleeptime = timeout
retryTimes=0
maxRetryTime=10
echo -e "$YELLOW Waiting for the vault daemon in the iotex-vault container to successful... $NC"
while true;do
if [ $retryTimes -gt $maxRetryTime ];then
echo -e "RED Start vault server container faild."
echo -e "$RED Please check its logs by command \"docker logs iotex-vault\" $NC"
exit 1
fi
docker exec -e "VAULT_ADDR=http://127.0.0.1:8200" iotex-vault vault status > /dev/null 2>&1
if [ $? -eq 2 ];then
break
fi
retryTimes=$((retryTimes+1))
sleep 4
done
echo -e "$YELLOW Success! $NC"
$WHITE_LINE
if [ ! -f $IOTEX_HOME/data/vault/initdata ];then
docker exec -e "VAULT_ADDR=http://127.0.0.1:8200" iotex-vault vault operator init > $IOTEX_HOME/data/vault/initdata
else
echo -e "$YELLOW vault is already initialized. Unsealing the data...$NC"
fi
index=0
while read -r line; do
echo $line | grep 'Initial Root Token' > /dev/null 2>&1
if [ $? -eq 0 ];then
rootToken=$(echo $line | awk -F':' '{print $2}' | tr -d '[:space:]')
continue
fi
echo $line | grep 'Unseal Key' > /dev/null 2>&1
if [ $? -eq 0 ];then
index=$(($index+1))
echo -e "$YELLOW Unseal the vault data $index time: $NC"
key=$(echo $line | awk -F':' '{print $2}' | tr -d '[:space:]')
docker exec -e "VAULT_ADDR=http://127.0.0.1:8200" -e "VAULT_TOKEN=$rootToken" iotex-vault vault operator unseal $key
keys="$keys $key"
$WHITE_LINE
fi
done < $IOTEX_HOME/data/vault/initdata
echo -e "$YELLOW Initial Root Token: $rootToken $NC"
echo -e "$YELLOW Unseal Keys: $NC"
for k in `echo $keys`; do
echo $k
done
$WHITE_LINE
if [ ! -f $IOTEX_HOME/data/vault/usertoken ];then
echo -e "$YELLOW Create token: $NC"
docker exec -e "VAULT_ADDR=http://127.0.0.1:8200" -e "VAULT_TOKEN=$rootToken" iotex-vault vault token create | tee $IOTEX_HOME/data/vault/usertoken
echo -e "$YELLOW Please save these information to login vault. $NC"
else
echo -e "$YELLOW user is already created: $NC"
cat $IOTEX_HOME/data/vault/usertoken
fi
IOTEX_USER_KEY=`cat $IOTEX_HOME/data/vault/usertoken|grep token_accessor | awk '{print $2}'`
IOTEX_USER_TOKEN=`cat $IOTEX_HOME/data/vault/usertoken|grep -E "token\b" | awk '{print $2}'`
export IOTEX_USER_KEY IOTEX_USER_TOKEN
$WHITE_LINE
# Write policy
docker exec -e "VAULT_ADDR=http://127.0.0.1:8200" -e "VAULT_TOKEN=$rootToken" iotex-vault vault policy read admin > /dev/null 2>&1
if [ $? -ne 0 ];then
echo -e "${YELLOW} Setup admin policy for vault. ${NC}"
docker exec -e "VAULT_ADDR=http://127.0.0.1:8200" -e "VAULT_TOKEN=$rootToken" iotex-vault vault policy write admin /vault/iotex-policy/admin-policy.hcl
fi
docker exec -e "VAULT_ADDR=http://127.0.0.1:8200" -e "VAULT_TOKEN=$rootToken" iotex-vault vault policy read regular > /dev/null 2>&1
if [ $? -ne 0 ];then
echo -e "${YELLOW} Setup regular policy for vault. ${NC}"
docker exec -e "VAULT_ADDR=http://127.0.0.1:8200" -e "VAULT_TOKEN=$rootToken" iotex-vault vault policy write regular /vault/iotex-policy/regular-policy.hcl
fi
$WHITE_LINE
}
function cleanAll() {
echo -e "$YELLOW Starting clean all containers... $NC"
pushd $IOTEX_HOME/$IOTEX_DOCKER_COMPOSE_DIR
docker-compose rm -s -f -v
popd
echo -e "${YELLOW} Done. ${NC}"
echo -e "${YELLOW} Starting delete all files... ${NC}"
if [ "${IOTEX_HOME}X" = "X" ] || [ "${IOTEX_HOME}X" = "/X" ];then
echo -e "${RED} \$IOTEX_HOME: ${IOTEX_HOME} is wrong. ${NC}"
## For safe.
return
fi
$RM $IOTEX_HOME
echo -e "${YELLOW} Done. ${NC}"
}
function main() {
while getopts 'cqh' c
do
case $c in
c)
WANT_CLEAN=1 ;;
q)
SKIP_PULL_IMAGE=1 ;;
h|*)
usage ;;
esac
done
checkDockerPermissions
checkDockerCompose
setVar
determinIotexHome
confirmEnvironmentVariable
if [ $WANT_CLEAN -eq 1 ];then
if [ -d $IOTEX_HOME ];then
cleanAll
else
echo -e "${RED} There is no dir $IOTEX_HOME, ignore deletion and exit"
fi
exit 0
fi
determinIotexAnalyticsDatabaseRootPass
makeWorkspace
copyConfig $BRANCH
if [ $SKIP_PULL_IMAGE -eq 0 ];then
pullImages
pullTrialImages
fi
exportAll
grantPrivileges
initVault
startup
. $PROJECT_ABS_DIR/scripts/setup_cli.sh
. $PROJECT_ABS_DIR/scripts/setup_userpass.sh
# Set env for command userpass.
VAULT_ADDR=localhost:8200
HIGH_TABLE_ADDR=localhost:8090
export VAULT_ADDR HIGH_TABLE_ADDR
}
main $@